diff --git a/lib/spec/expectations/fail_with.rb b/lib/spec/expectations/fail_with.rb index 66649ccdc..5e01f99df 100644 --- a/lib/spec/expectations/fail_with.rb +++ b/lib/spec/expectations/fail_with.rb @@ -31,9 +31,11 @@ def fail_with(message, expected=nil, target=nil) # :nodoc: end unless (differ.nil? || expected.nil? || target.nil?) if expected.is_a?(String) - message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected) + message << "\n\n Diff:" << self.differ.diff_as_string(target.to_s, expected) + elsif expected.is_a?(Hash) && target.is_a?(Hash) + message << "\n\n Diff:" << self.differ.diff_as_hash(target, expected) elsif !target.is_a?(Proc) - message << "\nDiff:" << self.differ.diff_as_object(target, expected) + message << "\n\n Diff:" << self.differ.diff_as_object(target, expected) end end Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message)) diff --git a/lib/spec/runner/differs/default.rb b/lib/spec/runner/differs/default.rb index 73f0dcae6..54e114bb2 100644 --- a/lib/spec/runner/differs/default.rb +++ b/lib/spec/runner/differs/default.rb @@ -45,6 +45,37 @@ def diff_as_string(data_new, data_old) def diff_as_object(target,expected) diff_as_string(PP.pp(target,""), PP.pp(expected,"")) end + + def diff_as_hash(target, expected) + contains_hash = false + contains_array = false + + extra_expected_keys = expected.keys - target.keys + extra_target_keys = target.keys - expected.keys + + o = "\n" + + o << "Expected hash contains keys that target hash does not: " << extra_expected_keys.inspect << "\n" if !extra_expected_keys.empty? + o << "Target hash contains keys that expected hash does not: " << extra_target_keys.inspect << "\n" if !extra_target_keys.empty? + + expected.delete_if do |key, value| + contains_hash = true if value.is_a?(Hash) + contains_array = true if value.is_a?(Array) + target[key] == value + end + + expected.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |key| + o << "Expected the key #{key.inspect} to be #{expected[key].inspect}, but was #{target[key].inspect}\n" + end + + o << "\n" + + if contains_hash || contains_array + o << diff_as_object(target, expected) + else + o + end + end protected def format diff --git a/spec/spec/expectations/differs/default_spec.rb b/spec/spec/expectations/differs/default_spec.rb index ba76cbd47..ce59d023c 100644 --- a/spec/spec/expectations/differs/default_spec.rb +++ b/spec/spec/expectations/differs/default_spec.rb @@ -73,6 +73,60 @@ def inspect diff = @differ.diff_as_object(expected,actual) diff.should == expected_diff end + + it "should output a friendly message if comparing simple hashes" do + expected = { "foo" => "bar" } + actual = { "foo" => "baz" } + + expected_diff = <<'EOD' + +Expected the key "foo" to be "bar", but was "baz" + +EOD + + + diff = @differ.diff_as_hash(actual, expected) + diff.should == expected_diff + end + + + it "should output a friendly message if comparing simple hashes that contain different keys" do + expected = { "bar" => "foo" } + actual = { "foo" => "baz" } + + expected_diff = <<'EOD' + +Expected hash contains keys that target hash does not: ["bar"] +Target hash contains keys that expected hash does not: ["foo"] +Expected the key "bar" to be "foo", but was nil + +EOD + + + diff = @differ.diff_as_hash(actual, expected) + diff.should == expected_diff + end + + it "should output diff message if the hash is complex (containing Array or Hash)" do + expected = { "foo" => "bar", "fizz" => [1, 2, 3] } + actual = { "foo" => "baz", "fizz" => [1, 2] } + + expected_diff = <<'EOD' + +Expected the key "fizz" to be [1, 2, 3], but was [1, 2] +Expected the key "foo" to be "bar", but was "baz" + + +@@ -1,2 +1,2 @@ +-{"foo"=>"bar", "fizz"=>[1, 2, 3]} ++{"foo"=>"baz", "fizz"=>[1, 2]} +EOD + + + diff = @differ.diff_as_hash(actual, expected) + diff.should == expected_diff + end + it "should output unified diff message of two objects" do expected = Spec::Fixtures::Animal.new "bob", "giraffe" diff --git a/spec/spec/expectations/fail_with_spec.rb b/spec/spec/expectations/fail_with_spec.rb index 8908a3d19..519fcce79 100644 --- a/spec/spec/expectations/fail_with_spec.rb +++ b/spec/spec/expectations/fail_with_spec.rb @@ -47,19 +47,20 @@ @differ.should_receive(:diff_as_string).and_return("diff") lambda { Spec::Expectations.fail_with "the message", "expected", "actual" - }.should fail_with("the message\nDiff:diff") + }.should fail_with("the message\n\n Diff:diff") end it "should call differ if expected/actual are not strings" do @differ.should_receive(:diff_as_object).and_return("diff") lambda { Spec::Expectations.fail_with "the message", :expected, :actual - }.should fail_with("the message\nDiff:diff") + }.should fail_with("the message\n\n Diff:diff") end it "should not call differ if expected or actual are procs" do @differ.should_not_receive(:diff_as_string) @differ.should_not_receive(:diff_as_object) + @differ.should_not_receive(:diff_as_hash) lambda { Spec::Expectations.fail_with "the message", lambda {}, lambda {} }.should fail_with("the message") diff --git a/spec/spec/runner/formatter/html_formatted-1.8.4.html b/spec/spec/runner/formatter/html_formatted-1.8.4.html index 33f108fc9..772c2c68e 100644 --- a/spec/spec/runner/formatter/html_formatted-1.8.4.html +++ b/spec/spec/runner/formatter/html_formatted-1.8.4.html @@ -255,7 +255,8 @@

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behavior driven development
diff --git a/spec/spec/runner/formatter/html_formatted-1.8.5.html b/spec/spec/runner/formatter/html_formatted-1.8.5.html
index a8b7ad405..55a830f3b 100644
--- a/spec/spec/runner/formatter/html_formatted-1.8.5.html
+++ b/spec/spec/runner/formatter/html_formatted-1.8.5.html
@@ -259,7 +259,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behavior driven development
diff --git a/spec/spec/runner/formatter/html_formatted-1.8.6.html b/spec/spec/runner/formatter/html_formatted-1.8.6.html
index 4b8fc0f75..2a1fcba62 100644
--- a/spec/spec/runner/formatter/html_formatted-1.8.6.html
+++ b/spec/spec/runner/formatter/html_formatted-1.8.6.html
@@ -265,7 +265,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behaviour driven development
diff --git a/spec/spec/runner/formatter/html_formatted-1.8.7.html b/spec/spec/runner/formatter/html_formatted-1.8.7.html
index 50b6162a8..9ece6c6e3 100644
--- a/spec/spec/runner/formatter/html_formatted-1.8.7.html
+++ b/spec/spec/runner/formatter/html_formatted-1.8.7.html
@@ -265,7 +265,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behaviour driven development
diff --git a/spec/spec/runner/formatter/html_formatted-1.9.1.html b/spec/spec/runner/formatter/html_formatted-1.9.1.html
index b1a9c65ef..a752656cb 100644
--- a/spec/spec/runner/formatter/html_formatted-1.9.1.html
+++ b/spec/spec/runner/formatter/html_formatted-1.9.1.html
@@ -270,7 +270,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behaviour driven development
diff --git a/spec/spec/runner/formatter/html_formatter_spec.rb b/spec/spec/runner/formatter/html_formatter_spec.rb
index 7a9ce6f5b..9ea21a673 100644
--- a/spec/spec/runner/formatter/html_formatter_spec.rb
+++ b/spec/spec/runner/formatter/html_formatter_spec.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../../../spec_helper'
+# require File.dirname(__FILE__) + '/../../../spec_helper'
 
 begin # See rescue all the way at the bottom
 
diff --git a/spec/spec/runner/formatter/text_mate_formatted-1.8.4.html b/spec/spec/runner/formatter/text_mate_formatted-1.8.4.html
index 9de8cd305..c556bd602 100644
--- a/spec/spec/runner/formatter/text_mate_formatted-1.8.4.html
+++ b/spec/spec/runner/formatter/text_mate_formatted-1.8.4.html
@@ -255,7 +255,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behavior driven development
diff --git a/spec/spec/runner/formatter/text_mate_formatted-1.8.6.html b/spec/spec/runner/formatter/text_mate_formatted-1.8.6.html
index aa8f74420..c39548f7f 100644
--- a/spec/spec/runner/formatter/text_mate_formatted-1.8.6.html
+++ b/spec/spec/runner/formatter/text_mate_formatted-1.8.6.html
@@ -261,7 +261,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behaviour driven development
diff --git a/spec/spec/runner/formatter/text_mate_formatted-1.8.7.html b/spec/spec/runner/formatter/text_mate_formatted-1.8.7.html
index 72c44a548..b32a65cd4 100644
--- a/spec/spec/runner/formatter/text_mate_formatted-1.8.7.html
+++ b/spec/spec/runner/formatter/text_mate_formatted-1.8.7.html
@@ -261,7 +261,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behaviour driven development
diff --git a/spec/spec/runner/formatter/text_mate_formatted-1.9.1.html b/spec/spec/runner/formatter/text_mate_formatted-1.9.1.html
index fa69dce17..696894932 100644
--- a/spec/spec/runner/formatter/text_mate_formatted-1.9.1.html
+++ b/spec/spec/runner/formatter/text_mate_formatted-1.9.1.html
@@ -266,7 +266,8 @@ 

RSpec Code Examples

expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
      got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)
-Diff:
+
+ Diff:
 @@ -1,4 +1,4 @@
  RSpec is a
 -behaviour driven development