Skip to content

Commit

Permalink
Fixed bug with error messages for hashes with a missing regexed key
Browse files Browse the repository at this point in the history
  • Loading branch information
playupchris committed Jun 27, 2011
1 parent fee5d32 commit 3f4803d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/rspec/matchers/match_hash.rb
Expand Up @@ -213,9 +213,9 @@ def to_s

def expected_to_s
if match?
@actual.is_a?(Regexp) ? markup(:matched, @expected.sub(@actual, "[#{@expected[@actual, 0]}]")) : @expected.inspect
@actual.is_a?(Regexp) ? markup(:matched, (@expected.is_a?(String) and not @expected.nil?) ? @expected.sub(@actual, "[#{@expected[@actual, 0]}]") : @expected.inspect) : @expected.inspect
else
@actual.is_a?(Regexp) ? markup(:additional, @expected.nil? ? @expected.inspect : @expected.sub(@actual, "[#{@expected[@actual, 0]}]") ) : markup(:additional, @expected.inspect)
markup(:additional, (@actual.is_a?(Regexp) and @actual.is_a?(String) and not @expected.nil?) ? @expected.sub(@actual, "[#{@expected[@actual, 0]}]") : @expected.inspect)
end
end
end
Expand Down
25 changes: 18 additions & 7 deletions spec/rspec/matchers/match_hash_spec.rb
Expand Up @@ -158,7 +158,7 @@ module Matchers
let(:failure_message ) {
paint <<-MESSAGE
\e[0m{
\"a\" => \e[31m- \e[1m/[A-Z]{3}/\e[0m\e[32m+ \e[1mabc\e[0m
\"a\" => \e[31m- \e[1m/[A-Z]{3}/\e[0m\e[32m+ \e[1m\"abc\"\e[0m
}
MESSAGE
}
Expand All @@ -173,7 +173,7 @@ module Matchers
paint <<-MESSAGE
\e[0m{
\"x\" => {
\"a\" => \e[31m- \e[1m/[A-Z]{3}/\e[0m\e[32m+ \e[1mabc\e[0m
\"a\" => \e[31m- \e[1m/[A-Z]{3}/\e[0m\e[32m+ \e[1m\"abc\"\e[0m
}
}
MESSAGE
Expand All @@ -183,11 +183,11 @@ module Matchers
end

context "has multiple regexes" do
let(:expected ) { {'x' => {'a' => /[A-Z]/ , 'b' => /[A-Z]{3}/, 'c' => /^[A-Z]{3}$/ }} }
let(:actual ) { { "x" => {"a" => "ABC", "b" => "BBC", "c" => "CBC"}} }
let(:failing ) { { "x" => {"a" => "ABC", "b" => "bbc", "c" => "CBC"}} }
let(:expected ) { { "x" => {"a" => /[A-Z]/ , 'b' => /[A-Z]{3}/, 'c' => /^[A-Z]{3}$/ }} }
let(:actual ) { { "x" => {"a" => "ABC" , "b" => "BBC" , "c" => "CBC" }} }
let(:failing ) { { "x" => {"a" => "ABC" , "b" => "bbc" , "c" => "CBC" }} }
let(:failure_message ) {
paint "\e[0m{\n \"x\" => {\n \"a\" => \e[33m~ \e[1m[A]BC\e[0m,\n \"c\" => \e[33m~ \e[1m[CBC]\e[0m,\n \"b\" => \e[31m- \e[1m/[A-Z]{3}/\e[0m\e[32m+ \e[1mbbc\e[0m\n }\n}\n"
paint "\e[0m{\n \"x\" => {\n \"a\" => \e[33m~ \e[1m[A]BC\e[0m,\n \"c\" => \e[33m~ \e[1m[CBC]\e[0m,\n \"b\" => \e[31m- \e[1m/[A-Z]{3}/\e[0m\e[32m+ \e[1m\"bbc\"\e[0m\n }\n}\n"
}

it_should_behave_like "a matcher"
Expand Down Expand Up @@ -360,7 +360,7 @@ module Matchers
it_should_behave_like "a partial matcher#{' in ruby 1.8' if RUBY_VERSION.to_f == 1.8}"
end

context "contains a hash with a nil" do
context "has a hash with a nil" do
let(:expected ) { { "a" => /[A-Z]/, "b" => /\d+/} }
let(:actual ) { { "a" => "ABC" , "b" => 1 } }
let(:failing ) { { "a" => "ABC" , "b" => nil } }
Expand All @@ -371,6 +371,17 @@ module Matchers
it_should_behave_like "a partial matcher"
end

context "has a hash with a missing regexed key" do
let(:expected ) { { "a" => /[A-Z]/, "b" => /\d+/} }
let(:actual ) { { "a" => "ABC" , "b" => 1 } }
let(:failing ) { { "a" => "ABC" } }
let(:failure_message ) {
paint "\e[0m{\n \"a\" => \e[33m~ \e[1m[A]BC\e[0m,\n\e[31m- \e[1m\"b\" => /\\d+/\e[0m\n}\n"
}

it_should_behave_like "a partial matcher"
end

end

end
Expand Down

0 comments on commit 3f4803d

Please sign in to comment.