This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Unify be_identical_string/be_diffed_as in spec/string_matcher.rb #176
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'rspec/matchers' | ||
# Special matcher 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 bytes. | ||
RSpec::Matchers.define :be_identical_string do |expected| | ||
|
||
if String.method_defined?(:encoding) | ||
match do | ||
expected_encoding? && | ||
actual.bytes.to_a == expected.bytes.to_a | ||
end | ||
|
||
failure_message do | ||
"expected\n#{actual.inspect} (#{actual.encoding.name}) to be identical to\n"\ | ||
"#{expected.inspect} (#{expected.encoding.name})\n"\ | ||
"The exact bytes are printed below for more detail:\n"\ | ||
"#{actual.bytes.to_a}\n"\ | ||
"#{expected.bytes.to_a}\n"\ | ||
end | ||
|
||
# Depends on chaining :with_same_encoding for it to | ||
# check for string encoding. | ||
def expected_encoding? | ||
if defined?(@expect_same_encoding) && @expect_same_encoding | ||
actual.encoding == expected.encoding | ||
else | ||
true | ||
end | ||
end | ||
else | ||
match do | ||
actual.split(//) == expected.split(//) | ||
end | ||
|
||
failure_message do | ||
"expected\n#{actual.inspect} to be identical to\n#{expected.inspect}\n" | ||
end | ||
end | ||
|
||
chain :with_same_encoding do | ||
@expect_same_encoding ||= true | ||
end | ||
end | ||
RSpec::Matchers.alias_matcher :a_string_identical_to, :be_identical_string | ||
RSpec::Matchers.alias_matcher :be_diffed_as, :be_identical_string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this blow up on 1.8.7?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really not sure how to get the best semantics from the matchers and also the least hacky implementation of the matcher. I'm not married to this implementation, but I liked the semantics of
be_identical_string(str).with_same_encoding
better than adding a newbe_identical_encoded_string
that is called frombe_identical_string
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just missed the encoding check above