Skip to content

Commit

Permalink
Add be_identical_string matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Jan 7, 2015
1 parent 773c031 commit e6e04bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
19 changes: 14 additions & 5 deletions lib/rspec/support/spec/encoding_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ module RSpec
module Support
module EncodingHelpers
module_function

if String.method_defined?(:encoding)

def expect_identical_string(str1, str2, expected_encoding=str1.encoding)
expect(str1.encoding).to eq(expected_encoding)
expect(str1.each_byte.to_a).to eq(str2.each_byte.to_a)
str1.encoding == expected_encoding &&
str1.each_byte.to_a == str2.each_byte.to_a
end

else

def expect_identical_string(str1, str2, _expected_encoding=nil)
expect(str1.split(//)).to eq(str2.split(//))
def expect_identical_string(str1, str2)
str1.split(//) == str2.split(//)
end
end
end
end
end
require 'rspec/expectations'
# Special matchers for comparing encoded strings so that
# we don't run any expectation failures through the Differ,
# which also relies on EncodedString. Instead, confirm the
# strings have the same encoding and same bytes.
RSpec::Matchers.define :be_identical_string do |expected|
match do |actual|
RSpec::Support::EncodingHelpers.expect_identical_string(actual, expected)
end
end
16 changes: 8 additions & 8 deletions spec/rspec/support/encoded_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module RSpec::Support
resulting_string = build_encoded_string(string, target_encoding).to_s
replacement = EncodedString::REPLACE * 3
expected_string = "I have a bad byt#{replacement}"
expect_identical_string(resulting_string, expected_string)
expect(resulting_string).to be_identical_string(expected_string)
end
end

Expand All @@ -60,13 +60,13 @@ module RSpec::Support
it 'does nothing' do
resulting_string = build_encoded_string(string, no_converter_encoding).to_s
expected_string = "\x80"
expect_identical_string(resulting_string, expected_string, no_converter_encoding)
expect(resulting_string).to be_identical_string(expected_string, no_converter_encoding)
end
else
it 'forces the encoding and replaces invalid characters with the REPLACE string' do
resulting_string = build_encoded_string(string, no_converter_encoding).to_s
expected_string = EncodedString::REPLACE
expect_identical_string(resulting_string, expected_string, no_converter_encoding)
expect(resulting_string).to be_identical_string(expected_string, no_converter_encoding)
end
end
end
Expand All @@ -89,7 +89,7 @@ module RSpec::Support
resulting_string = build_encoded_string(string, incompatible_encoding).to_s
replacement = EncodedString::REPLACE
expected_string = "#{replacement} hi I am not going to work"
expect_identical_string(resulting_string, expected_string)
expect(resulting_string).to be_identical_string(expected_string)
end
end
end
Expand All @@ -106,7 +106,7 @@ module RSpec::Support
resulting_string = build_encoded_string(valid_unicode_string, utf8_encoding) << valid_ascii_string
replacement = EncodedString::REPLACE * 2
expected_string = "#{utf_8_euro_symbol}abcd#{replacement}".force_encoding('UTF-8')
expect_identical_string(resulting_string, expected_string)
expect(resulting_string).to be_identical_string(expected_string)
end

it 'copes with encoded strings' do
Expand All @@ -123,7 +123,7 @@ module RSpec::Support
Tu avec carte {count} item has
Tu avec cart#{replacement} {count} it#{replacement}m has
EOS
expect_identical_string(resulting_string, expected_string)
expect(resulting_string).to be_identical_string(expected_string)
end
end

Expand All @@ -141,7 +141,7 @@ module RSpec::Support
it 'replaces unconvertable characters with a string representation of their hex value' do
resulting_string = build_encoded_string(valid_unicode_string, utf8_encoding) << ascii_string
expected_string = "#{utf_8_euro_symbol}?"
expect_identical_string(resulting_string, expected_string)
expect(resulting_string).to be_identical_string(expected_string)
end
end
end
Expand All @@ -153,7 +153,7 @@ module RSpec::Support

resulting_string = build_encoded_string(ascii_string, utf8_encoding) << other_ascii_string
expected_string = 'abc123'.force_encoding('ASCII-8BIT')
expect_identical_string(resulting_string, expected_string)
expect(resulting_string).to be_identical_string(expected_string)
end
end
end
Expand Down

0 comments on commit e6e04bc

Please sign in to comment.