Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit fb82b23

Browse files
committed
Fix singular regexp include
1 parent 195343e commit fb82b23

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

features/built_in_matchers/include.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Feature: `include` matcher
44

55
```ruby
66
expect("a string").to include("a")
7+
expect("a string").to include(/str..g/)
78
expect("a string").to include(/a|str/).twice
89
expect("a string").to include("str", "g")
910
expect("a string").not_to include("foo")
@@ -80,22 +81,25 @@ Feature: `include` matcher
8081
RSpec.describe "a string" do
8182
it { is_expected.to include("str") }
8283
it { is_expected.to include("a", "str", "ng") }
84+
it { is_expected.to include(/str..g/) }
8385
it { is_expected.to include(/a|str/).twice }
8486
it { is_expected.not_to include("foo") }
8587
it { is_expected.not_to include("foo", "bar") }
8688
8789
# deliberate failures
8890
it { is_expected.to include("foo") }
8991
it { is_expected.not_to include("str") }
92+
it { is_expected.not_to include(/str..g/) }
9093
it { is_expected.to include("str").at_least(:twice) }
9194
it { is_expected.to include("str", "foo") }
9295
it { is_expected.not_to include("str", "foo") }
9396
end
9497
"""
9598
When I run `rspec string_include_matcher_spec.rb`
9699
Then the output should contain all of these:
97-
| 10 examples, 5 failures |
100+
| 12 examples, 6 failures |
98101
| expected "a string" to include "foo" |
102+
| expected "a string" not to include /str..g/ |
99103
| expected "a string" not to include "str" |
100104
| expected "a string" to include "str" at least twice but it is included once |
101105
| expected "a string" to include "foo" |

lib/rspec/matchers/built_in/include.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def actual_hash_has_key?(expected_key)
166166
end
167167

168168
def actual_collection_includes?(expected_item)
169+
return actual.scan(expected_item).size > 0 if Regexp === expected_item && String === actual
169170
return true if actual.include?(expected_item)
170171

171172
# String lacks an `any?` method...
@@ -200,6 +201,7 @@ def count_inclusions
200201

201202
def diff_would_wrongly_highlight_matched_item?
202203
return false unless actual.is_a?(String) && expected.is_a?(Array)
204+
return false if Regexp === expecteds.first
203205

204206
lines = actual.split("\n")
205207
expected.any? do |str|

spec/rspec/matchers/built_in/include_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ def hash.send; :sent; end
215215
expect("abc").to include("a")
216216
end
217217

218+
it "passes if target matches a regexp" do
219+
expect("abc").to include(/[cba]{3}/)
220+
end
221+
218222
it "fails if target does not include expected" do
219223
expect {
220224
expect("abc").to include("d")
@@ -240,16 +244,20 @@ def hash.send; :sent; end
240244
end
241245

242246
context "with exact count" do
243-
it 'fails if the block yields wrong number of times' do
247+
it 'fails if the string contains the wrong number of occurences' do
244248
expect {
245249
expect('foo bar foo').to include('foo').once
246250
}.to fail_with(/expected "foo bar foo" to include "foo" once but it is included twice/)
251+
expect {
252+
expect('foo bar foo').to include(/[of]{3}/).once
253+
}.to fail_with(/expected "foo bar foo" to include \/\[of\]\{3\}\/ once but it is included twice/)
247254
end
248255

249-
it 'passes if the block yields the specified number of times' do
256+
it 'passes if the string contains the correct number of occurences' do
250257
expect('fooo bar').to include('oo').once
251258
expect('fooo bar').to include('o').thrice
252259
expect('fooo ooo oo bar foo').to include('oo').exactly(4).times
260+
expect('fooo ooo oo bar foo').to include(/fo{2}/).exactly(2).times
253261
end
254262
end
255263

0 commit comments

Comments
 (0)