Skip to content

Commit 7b73996

Browse files
nobueregon
authored andcommitted
Split examples to each sub sections
1 parent f03892a commit 7b73996

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

CONTRIBUTING.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ which indicates the file was generated but the method unspecified.
5353
Here is a list of frequently-used matchers, which should be enough for most specs.
5454
There are a few extra specific matchers used in the couple specs that need it.
5555

56+
#### Comparison matchers
57+
5658
```ruby
5759
(1 + 2).should == 3 # Calls #==
5860
(1 + 2).should_not == 5
@@ -66,7 +68,11 @@ File.should equal(File) # Calls #equal? (tests identity)
6668
4.should > 3
6769

6870
"Hello".should =~ /l{2}/ # Calls #=~ (Regexp match)
71+
```
6972

73+
#### Predicate matchers
74+
75+
```ruby
7076
[].should be_empty # Calls #empty?
7177
[1,2,3].should include(2) # Calls #include?
7278

@@ -82,8 +88,13 @@ Numeric.should be_ancestor_of(Float) # Float.ancestors.include?(Numeric)
8288
3.14.should respond_to(:to_i) # Calls #respond_to?
8389
Fixnum.should have_instance_method(:+)
8490
Array.should have_method(:new)
85-
# Also have_constant, have_private_instance_method, have_singleton_method, etc
91+
```
92+
93+
Also `have_constant`, `have_private_instance_method`, `have_singleton_method`, etc.
94+
95+
#### Exception matchers
8696

97+
```ruby
8798
-> {
8899
raise "oops"
89100
}.should raise_error(RuntimeError, /oops/)
@@ -95,11 +106,18 @@ Array.should have_method(:new)
95106
e.message.should include("oops")
96107
e.cause.should == nil
97108
}
109+
```
98110

99-
# To avoid! Instead, use an expectation testing what the code in the lambda does.
100-
# If an exception is raised, it will fail the example anyway.
111+
**To avoid!** Instead, use an expectation testing what the code in the lambda does.
112+
If an exception is raised, it will fail the example anyway.
113+
114+
```ruby
101115
-> { ... }.should_not raise_error
116+
```
117+
118+
#### Warning matcher
102119

120+
```ruby
103121
-> {
104122
Fixnum
105123
}.should complain(/constant ::Fixnum is deprecated/) # Expect a warning
@@ -110,6 +128,8 @@ Array.should have_method(:new)
110128
Different guards are available as defined by mspec.
111129
Here is a list of the most commonly-used guards:
112130

131+
#### Version guards
132+
113133
```ruby
114134
ruby_version_is ""..."2.4" do
115135
# Specs for RUBY_VERSION < 2.4
@@ -118,7 +138,11 @@ end
118138
ruby_version_is "2.4" do
119139
# Specs for RUBY_VERSION >= 2.4
120140
end
141+
```
121142

143+
#### Platform guards
144+
145+
```ruby
122146
platform_is :windows do
123147
# Specs only valid on Windows
124148
end
@@ -140,34 +164,46 @@ end
140164
big_endian do
141165
# Big-endian platform
142166
end
167+
```
168+
169+
#### Guard for bug
170+
171+
In case there is a bug in MRI but the expected behavior is obvious
172+
First file a bug at https://bugs.ruby-lang.org/
173+
It is better to use a `ruby_version_is` guard if there was a release with the fix
143174

144-
# In case there is a bug in MRI but the expected behavior is obvious
145-
# First file a bug at https://bugs.ruby-lang.org/
146-
# It is better to use a ruby_version_is guard if there was a release with the fix
175+
```ruby
147176
ruby_bug '#13669', ''...'2.5' do
148177
it "works like this" do
149178
# Specify the expected behavior here, not the bug
150179
end
151180
end
181+
```
152182

183+
#### Combining guards
153184

154-
# Combining guards
185+
```ruby
155186
guard -> { platform_is :windows and ruby_version_is ""..."2.5" } do
156187
# Windows and RUBY_VERSION < 2.5
157188
end
158189

159190
guard_not -> { platform_is :windows and ruby_version_is ""..."2.5" } do
160191
# The opposite
161192
end
193+
```
162194

163-
# Custom guard
195+
#### Custom guard
196+
197+
```ruby
164198
max_uint = (1 << 32) - 1
165199
guard -> { max_uint <= fixnum_max } do
166200
end
167201
```
168202

169203
Custom guards are better than a simple `if` as they allow [mspec commands](https://github.com/ruby/mspec/issues/30#issuecomment-312487779) to work properly.
170204

205+
#### Implementation-specific behaviors
206+
171207
In general, the usage of guards should be minimized as possible.
172208

173209
There are no guards to define implementation-specific behavior because

0 commit comments

Comments
 (0)