Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/start_and_end_with_spec.rb

raw symbols SCIP
1 RSpec.describe "expect(...).to start_with" do
2 it_behaves_like "an RSpec value matcher", :valid_value => "ab", :invalid_value => "bc" do
3 let(:matcher) { start_with("a") }
4 end
5
6 context "with a string" do
7 it "passes if it matches the start of the actual string" do
8 expect("this string").to start_with "this str"
9 end
10
11 it "fails if it does not match the start of the actual string" do
12 expect {
13 expect("this string").to start_with "that str"
14 }.to fail_with("expected \"this string\" to start with \"that str\"")
15 end
16 end
17
18 context "with an array" do
19 it "passes if it is the first element of the array" do
20 expect([0, 1, 2]).to start_with 0
21 end
22
23 it "passes if the first elements of the array match" do
24 expect([0, 1, 2]).to start_with 0, 1
25 end
26
27 it "fails if it does not match the first element of the array" do
28 expect {
29 expect([0, 1, 2]).to start_with 2
30 }.to fail_with("expected [0, 1, 2] to start with 2")
31 end
32
33 it "fails if it the first elements of the array do not match" do
34 expect {
35 expect([0, 1, 2]).to start_with 1, 2
36 }.to fail_with("expected [0, 1, 2] to start with 1 and 2")
37 end
38 end
39
40 context "with an array of strings" do
41 it "passes if given the first element of the array" do
42 expect(%w[ a b c ]).to start_with 'a'
43 end
44
45 it "passes if given the first n of the array" do
46 expect(%w[ a b c ]).to start_with('a', 'b')
47 end
48
49 it 'fails if given the wrong first element of the array' do
50 expect {
51 expect(%w[ a b c ]).to start_with 'z'
52 }.to fail_with('expected ["a", "b", "c"] to start with "z"')
53 end
54 end
55
56 context "with an array of uncustomized structs" do
57 struct = Struct.new(:foo)
58
59 it 'passes if the array ends with a struct equal to the provided struct' do
60 s1 = struct.new(5)
61 s2 = struct.new(5)
62 expect(s1).to eq(s2)
63
64 expect([s1, 10]).to start_with(s2)
65 end
66
67 it 'fails if the array ends with a struct not equal to the provided struct' do
68 s1 = struct.new(5)
69 s2 = struct.new(6)
70 expect(s1).not_to eq(s2)
71
72 expect {
73 expect([s1, 10]).to start_with(s2)
74 }.to fail_including("expected [#{s1.inspect}, 10] to start with #{s2.inspect}")
75 end
76 end
77
78 context "with an array of structs that have a custom `==` definition" do
79 my_struct = Struct.new(:id, :fluff) do
80 def ==(other)
81 other.is_a?(self.class) && other.id == id
82 end
83 end
84
85 it 'passes if the array ends with a struct equal to the provided struct' do
86 s1 = my_struct.new(1, "foo")
87 s2 = my_struct.new(1, "bar")
88 expect(s1).to eq(s2)
89
90 expect([s1, 10]).to start_with(s2)
91 end
92
93 it 'fails if the array ends with a struct not equal to the provided struct' do
94 s1 = my_struct.new(1, "foo")
95 s2 = my_struct.new(2, "bar")
96 expect(s1).not_to eq(s2)
97
98 expect {
99 expect([s1, 10]).to start_with(s2)
100 }.to fail_including("expected [#{s1.inspect}, 10] to start with #{s2.inspect}")
101 end
102 end
103
104 context "with an object that does not respond to :[]" do
105 it "fails with a useful message" do
106 actual = Object.new
107 expect {
108 expect(actual).to start_with 0
109 }.to fail_with("expected #{actual.inspect} to start with 0, but it cannot be indexed using #[]")
110 end
111 end
112
113 context "with a hash" do
114 it "fails with a useful error if trying to match more than one element" do
115 actual = { :a => 'b', :b => 'b', :c => 'c' }
116 expected = { :a => 'b', :b => 'b' }
117 expect {
118 expect(actual).to start_with(expected)
119 }.to fail_with(
120 /\Aexpected #{hash_inspect(actual).gsub(" => ", "=>")} to start with #{hash_inspect(expected).gsub(" => ", "=>")}, but it does not have ordered elements\z/
121 )
122 end
123 end
124
125 describe "composing with other matchers" do
126 it 'passes if the start of an array matches two given matchers' do
127 expect([1.01, "food", 3]).to start_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
128 end
129
130 it 'passes if the start of an array matches one given matcher' do
131 expect([1.01, "food", 3]).to start_with(a_value_within(0.2).of(1))
132 end
133
134 it 'provides a description' do
135 description = start_with(a_value_within(0.1).of(1), a_string_matching(/abc/)).description
136 expect(description).to eq("start with a value within 0.1 of 1 and a string matching /abc/")
137 end
138
139 it 'fails with a clear error message when the matchers do not match' do
140 expect {
141 expect([2.01, "food", 3]).to start_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
142 }.to fail_with('expected [2.01, "food", 3] to start with a value within 0.2 of 1 and a string matching /foo/')
143 end
144 end
145 end
146
147 RSpec.describe "expect(...).not_to start_with" do
148 context "with a string" do
149 it "passes if it does not match the start of the actual string" do
150 expect("this string").not_to start_with "that str"
151 end
152
153 it "fails if it does match the start of the actual string" do
154 expect {
155 expect("this string").not_to start_with "this str"
156 }.to fail_with("expected \"this string\" not to start with \"this str\"")
157 end
158 end
159
160 context "with an array" do
161 it "passes if it is not the first element of the array" do
162 expect([0, 1, 2]).not_to start_with 2
163 end
164
165 it "passes if the first elements of the array do not match" do
166 expect([0, 1, 2]).not_to start_with 1, 2
167 end
168
169 it "fails if it matches the first element of the array" do
170 expect {
171 expect([0, 1, 2]).not_to start_with 0
172 }.to fail_with("expected [0, 1, 2] not to start with 0")
173 end
174
175 it "fails if it the first elements of the array match" do
176 expect {
177 expect([0, 1, 2]).not_to start_with 0, 1
178 }.to fail_with("expected [0, 1, 2] not to start with 0 and 1")
179 end
180 end
181
182 context "with an array of strings" do
183 it "fails if given the first element of the array" do
184 expect {
185 expect(%w[ a b c ]).not_to start_with 'a'
186 }.to fail_with('expected ["a", "b", "c"] not to start with "a"')
187 end
188
189 it "fails if given the first n of the array" do
190 expect {
191 expect(%w[ a b c ]).not_to start_with('a', 'b')
192 }.to fail_with('expected ["a", "b", "c"] not to start with "a" and "b"')
193 end
194
195 it 'passes if given the wrong first element of the array' do
196 expect(%w[ a b c ]).not_to start_with 'z'
197 end
198 end
199
200 it 'can pass when composed with another matcher' do
201 expect(["a"]).not_to start_with(a_string_matching(/bar/))
202 end
203
204 it 'can fail when composed with another matcher' do
205 expect {
206 expect(["a"]).not_to start_with(a_string_matching(/a/))
207 }.to fail_with('expected ["a"] not to start with a string matching /a/')
208 end
209 end
210
211 RSpec.describe "expect(...).to end_with" do
212 it_behaves_like "an RSpec value matcher", :valid_value => "ab", :invalid_value => "bc" do
213 let(:matcher) { end_with("b") }
214 end
215
216 context "with a string" do
217 it "passes if it matches the end of the actual string" do
218 expect("this string").to end_with "is string"
219 end
220
221 it "fails if it does not match the end of the actual string" do
222 expect {
223 expect("this string").to end_with "is stringy"
224 }.to fail_with("expected \"this string\" to end with \"is stringy\"")
225 end
226 end
227
228 context "with an array" do
229 it "passes if it is the last element of the array" do
230 expect([0, 1, 2]).to end_with 2
231 end
232
233 it "passes if the last elements of the array match" do
234 expect([0, 1, 2]).to end_with [1, 2]
235 end
236
237 it "fails if it does not match the last element of the array" do
238 expect {
239 expect([0, 1, 2]).to end_with 1
240 }.to fail_with("expected [0, 1, 2] to end with 1")
241 end
242
243 it "fails if it the last elements of the array do not match" do
244 expect {
245 expect([0, 1, 2]).to end_with [0, 1]
246 }.to fail_with("expected [0, 1, 2] to end with 0 and 1")
247 end
248 end
249
250 context "with an array of strings" do
251 it "passes if given the last element of the array" do
252 expect(%w[ a b c ]).to end_with 'c'
253 end
254
255 it "passes if given the last n of the array" do
256 expect(%w[ a b c ]).to end_with('b', 'c')
257 end
258
259 it 'fails if given the wrong last element of the array' do
260 expect {
261 expect(%w[ a b c ]).to end_with 'z'
262 }.to fail_with('expected ["a", "b", "c"] to end with "z"')
263 end
264 end
265
266 context "with an array of uncustomized structs" do
267 struct = Struct.new(:foo)
268
269 it 'passes if the array ends with a struct equal to the provided struct' do
270 s1 = struct.new(5)
271 s2 = struct.new(5)
272 expect(s1).to eq(s2)
273
274 expect([10, s1]).to end_with(s2)
275 end
276
277 it 'fails if the array ends with a struct not equal to the provided struct' do
278 s1 = struct.new(5)
279 s2 = struct.new(6)
280 expect(s1).not_to eq(s2)
281
282 expect {
283 expect([10, s1]).to end_with(s2)
284 }.to fail_including("expected [10, #{s1.inspect}] to end with #{s2.inspect}")
285 end
286 end
287
288 context "with an array of structs that have a custom `==` definition" do
289 my_struct = Struct.new(:id, :fluff) do
290 def ==(other)
291 other.is_a?(self.class) && other.id == id
292 end
293 end
294
295 it 'passes if the array ends with a struct equal to the provided struct' do
296 s1 = my_struct.new(1, "foo")
297 s2 = my_struct.new(1, "bar")
298 expect(s1).to eq(s2)
299
300 expect([10, s1]).to end_with(s2)
301 end
302
303 it 'fails if the array ends with a struct not equal to the provided struct' do
304 s1 = my_struct.new(1, "foo")
305 s2 = my_struct.new(2, "bar")
306 expect(s1).not_to eq(s2)
307
308 expect {
309 expect([10, s1]).to end_with(s2)
310 }.to fail_including("expected [10, #{s1.inspect}] to end with #{s2.inspect}")
311 end
312 end
313
314 context "with an object that does not respond to :[]" do
315 it "fails with a useful message" do
316 actual = Object.new
317 expect {
318 expect(actual).to end_with 0
319 }.to fail_with("expected #{actual.inspect} to end with 0, but it cannot be indexed using #[]")
320 end
321 end
322
323 context "with a hash" do
324 it "raises an ArgumentError if trying to match more than one element" do
325 actual = { :a => 'b', :b => 'b', :c => 'c' }
326 expected = { :a => 'b', :b => 'b' }
327 expect {
328 expect(actual).to end_with(expected)
329 }.to fail_with(
330 /\Aexpected #{hash_inspect(actual).gsub(" => ", "=>")} to end with #{hash_inspect(expected).gsub(" => ", "=>")}, but it does not have ordered elements\z/
331 )
332 end
333 end
334
335 describe "composing with other matchers" do
336 it 'passes if the end of an array matches two given matchers' do
337 expect([3, "food", 1.1]).to end_with(a_string_matching(/foo/), a_value_within(0.2).of(1))
338 end
339
340 it 'passes if the end of an array matches one given matcher' do
341 expect([3, "food", 1.1]).to end_with(a_value_within(0.2).of(1))
342 end
343
344 it 'provides a description' do
345 description = end_with(a_value_within(0.1).of(1), a_string_matching(/abc/)).description
346 expect(description).to eq("end with a value within 0.1 of 1 and a string matching /abc/")
347 end
348
349 it 'fails with a clear error message when the matchers do not match' do
350 expect {
351 expect([2.01, 3, "food"]).to end_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
352 }.to fail_with('expected [2.01, 3, "food"] to end with a value within 0.2 of 1 and a string matching /foo/')
353 end
354 end
355 end
356
357 RSpec.describe "expect(...).not_to end_with" do
358 context "with a sting" do
359 it "passes if it does not match the end of the actual string" do
360 expect("this string").not_to end_with "stringy"
361 end
362
363 it "fails if it matches the end of the actual string" do
364 expect {
365 expect("this string").not_to end_with "string"
366 }.to fail_with("expected \"this string\" not to end with \"string\"")
367 end
368 end
369
370 context "an array" do
371 it "passes if it is not the last element of the array" do
372 expect([0, 1, 2]).not_to end_with 1
373 end
374
375 it "passes if the last elements of the array do not match" do
376 expect([0, 1, 2]).not_to end_with [0, 1]
377 end
378
379 it "fails if it matches the last element of the array" do
380 expect {
381 expect([0, 1, 2]).not_to end_with 2
382 }.to fail_with("expected [0, 1, 2] not to end with 2")
383 end
384
385 it "fails if it the last elements of the array match" do
386 expect {
387 expect([0, 1, 2]).not_to end_with [1, 2]
388 }.to fail_with("expected [0, 1, 2] not to end with 1 and 2")
389 end
390 end
391
392 context "with an array of strings" do
393 it "fails if given the last element of the array" do
394 expect {
395 expect(%w[ a b c ]).not_to end_with 'c'
396 }.to fail_with('expected ["a", "b", "c"] not to end with "c"')
397 end
398
399 it "fails if given the last n of the array" do
400 expect {
401 expect(%w[ a b c ]).not_to end_with('b', 'c')
402 }.to fail_with('expected ["a", "b", "c"] not to end with "b" and "c"')
403 end
404
405 it 'passes if given the wrong last element of the array' do
406 expect(%w[ a b c ]).not_to end_with 'z'
407 end
408 end
409
410 it 'can pass when composed with another matcher' do
411 expect(["a"]).not_to end_with(a_string_matching(/bar/))
412 end
413
414 it 'can fail when composed with another matcher' do
415 expect {
416 expect(["a"]).not_to end_with(a_string_matching(/a/))
417 }.to fail_with('expected ["a"] not to end with a string matching /a/')
418 end
419 end