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

Commit 0b23e89

Browse files
committed
Merge pull request #176 from bf4/factor_out_string_matcher
Unify be_identical_string/be_diffed_as in spec/string_matcher.rb
2 parents 252f8f0 + 92c3e0e commit 0b23e89

File tree

3 files changed

+59
-64
lines changed

3 files changed

+59
-64
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'rspec/matchers'
2+
# Special matcher for comparing encoded strings so that
3+
# we don't run any expectation failures through the Differ,
4+
# which also relies on EncodedString. Instead, confirm the
5+
# strings have the same bytes.
6+
RSpec::Matchers.define :be_identical_string do |expected|
7+
8+
if String.method_defined?(:encoding)
9+
match do
10+
expected_encoding? &&
11+
actual.bytes.to_a == expected.bytes.to_a
12+
end
13+
14+
failure_message do
15+
"expected\n#{actual.inspect} (#{actual.encoding.name}) to be identical to\n"\
16+
"#{expected.inspect} (#{expected.encoding.name})\n"\
17+
"The exact bytes are printed below for more detail:\n"\
18+
"#{actual.bytes.to_a}\n"\
19+
"#{expected.bytes.to_a}\n"\
20+
end
21+
22+
# Depends on chaining :with_same_encoding for it to
23+
# check for string encoding.
24+
def expected_encoding?
25+
if defined?(@expect_same_encoding) && @expect_same_encoding
26+
actual.encoding == expected.encoding
27+
else
28+
true
29+
end
30+
end
31+
else
32+
match do
33+
actual.split(//) == expected.split(//)
34+
end
35+
36+
failure_message do
37+
"expected\n#{actual.inspect} to be identical to\n#{expected.inspect}\n"
38+
end
39+
end
40+
41+
chain :with_same_encoding do
42+
@expect_same_encoding ||= true
43+
end
44+
end
45+
RSpec::Matchers.alias_matcher :a_string_identical_to, :be_identical_string
46+
RSpec::Matchers.alias_matcher :be_diffed_as, :be_identical_string

spec/rspec/support/differ_spec.rb

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,8 @@
33
require 'ostruct'
44
require 'timeout'
55
require 'rspec/support/differ'
6+
require 'rspec/support/spec/string_matcher'
67

7-
# Special matcher for comparing strings so that
8-
# we don't run any expectation failures through the Differ.
9-
# Instead, confirm the strings have the same bytes.
10-
RSpec::Matchers.define :be_diffed_as do |expected|
11-
12-
if String.method_defined?(:encoding)
13-
match do
14-
actual.bytes.to_a == expected.bytes.to_a
15-
end
16-
17-
failure_message do
18-
"expected\n#{actual.inspect} to be identical to\n#{expected.inspect}\n"\
19-
"The exact bytes are printed below for more detail:\n"\
20-
"#{actual.bytes.to_a}\n"\
21-
"#{expected.bytes.to_a}\n"\
22-
end
23-
else
24-
match do
25-
actual.split(//) == expected.split(//)
26-
end
27-
28-
failure_message do
29-
"expected\n#{actual.inspect} to be identical to\n#{expected.inspect}\n"
30-
end
31-
end
32-
end
338
module RSpec
349
module Support
3510
describe Differ do

spec/rspec/support/encoded_string_spec.rb

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
11
# encoding: utf-8
22
require 'spec_helper'
33
require 'rspec/support/encoded_string'
4-
5-
# Special matcher for comparing encoded strings so that
6-
# we don't run any expectation failures through the Differ,
7-
# which also relies on EncodedString. Instead, confirm the
8-
# strings have the same encoding and same bytes.
9-
RSpec::Matchers.define :be_identical_string do |expected|
10-
11-
if String.method_defined?(:encoding)
12-
match do
13-
actual.encoding == expected.encoding &&
14-
actual.bytes.to_a == expected.bytes.to_a
15-
end
16-
17-
failure_message do
18-
"expected\n#{actual.inspect} (#{actual.encoding.name}) to be identical to\n"\
19-
"#{expected.inspect} (#{expected.encoding.name})\n"\
20-
"The exact bytes are printed below for more detail:\n"\
21-
"#{actual.bytes.to_a}\n"\
22-
"#{expected.bytes.to_a}\n"\
23-
end
24-
else
25-
match do |actual|
26-
actual.split(//) == expected.split(//)
27-
end
28-
end
29-
end
30-
RSpec::Matchers.alias_matcher :a_string_identical_to, :be_identical_string
4+
require 'rspec/support/spec/string_matcher'
315

326
module RSpec::Support
337
describe EncodedString do
@@ -93,7 +67,7 @@ module RSpec::Support
9367
resulting_string = build_encoded_string(string, target_encoding).to_s
9468
replacement = EncodedString::REPLACE * 3
9569
expected_string = "I have a bad byt#{replacement}".force_encoding(target_encoding)
96-
expect(resulting_string).to be_identical_string(expected_string)
70+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
9771
end
9872
end
9973

@@ -115,13 +89,13 @@ module RSpec::Support
11589
it 'does nothing' do
11690
resulting_string = build_encoded_string(string, no_converter_encoding).to_s
11791
expected_string = "\x80".force_encoding(no_converter_encoding)
118-
expect(resulting_string).to be_identical_string(expected_string)
92+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
11993
end
12094
else
12195
it 'forces the encoding and replaces invalid characters with the REPLACE string' do
12296
resulting_string = build_encoded_string(string, no_converter_encoding).to_s
12397
expected_string = EncodedString::REPLACE.dup.force_encoding(no_converter_encoding)
124-
expect(resulting_string).to be_identical_string(expected_string)
98+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
12599
end
126100

127101
it 'does not mutate the input string' do
@@ -150,7 +124,7 @@ module RSpec::Support
150124
resulting_string = build_encoded_string(string, incompatible_encoding).to_s
151125
replacement = EncodedString::REPLACE
152126
expected_string = "#{replacement} hi I am not going to work".force_encoding('EUC-JP')
153-
expect(resulting_string).to be_identical_string(expected_string)
127+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
154128
end
155129
end
156130
end
@@ -166,7 +140,7 @@ module RSpec::Support
166140
it 'encodes and appends the string' do
167141
resulting_string = build_encoded_string(valid_unicode_string, utf8_encoding) << valid_ascii_string
168142
expected_string = "#{utf_8_euro_symbol}abcde".force_encoding('UTF-8')
169-
expect(resulting_string).to be_identical_string(expected_string)
143+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
170144
end
171145
end
172146

@@ -184,7 +158,7 @@ module RSpec::Support
184158
it 'replaces unconvertable characters with the REPLACE string' do
185159
resulting_string = build_encoded_string(valid_unicode_string, utf8_encoding) << ascii_string
186160
expected_string = "#{utf_8_euro_symbol}#{EncodedString::REPLACE}"
187-
expect(resulting_string).to be_identical_string(expected_string)
161+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
188162
end
189163
end
190164
end
@@ -196,7 +170,7 @@ module RSpec::Support
196170

197171
resulting_string = build_encoded_string(ascii_string, utf8_encoding) << other_ascii_string
198172
expected_string = 'abc123'.force_encoding(utf8_encoding)
199-
expect(resulting_string).to be_identical_string(expected_string)
173+
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
200174
end
201175
end
202176
end
@@ -217,8 +191,8 @@ module RSpec::Support
217191
resulting_string = build_encoded_string(wrapped_string, utf8_encoding).split(delimiter)
218192
exp1, exp2 = sprintf(wrapped_string_template, EncodedString::REPLACE).force_encoding(utf8_encoding).split(delimiter)
219193
expect(resulting_string).to match [
220-
a_string_identical_to(exp1),
221-
a_string_identical_to(exp2)
194+
a_string_identical_to(exp1).with_same_encoding,
195+
a_string_identical_to(exp2).with_same_encoding
222196
]
223197
end
224198
end
@@ -239,7 +213,7 @@ module RSpec::Support
239213
it 'makes no changes to the resulting string' do
240214
resulting_array = build_encoded_string(non_ascii_compatible_string).split("\n")
241215
expect(resulting_array).to match [
242-
a_string_identical_to(non_ascii_compatible_string)
216+
a_string_identical_to(non_ascii_compatible_string).with_same_encoding
243217
]
244218
end
245219
end
@@ -258,7 +232,7 @@ module RSpec::Support
258232
resulting_array = build_encoded_string(message_with_invalid_byte_sequence, utf8_encoding).split("\n")
259233
expected_string = "? ? ? I have bad bytes"
260234
expect(resulting_array).to match [
261-
a_string_identical_to(expected_string)
235+
a_string_identical_to(expected_string).with_same_encoding
262236
]
263237
end
264238
end

0 commit comments

Comments
 (0)