Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
Merge commit 'radar/master' into LH_848
Browse files Browse the repository at this point in the history
[#848 state:resolved milestone:'Next Release']
  • Loading branch information
dchelimsky committed Jul 15, 2009
2 parents 5fff05d + 11e189e commit ce4498d
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 14 deletions.
6 changes: 4 additions & 2 deletions lib/spec/expectations/fail_with.rb
Expand Up @@ -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))
Expand Down
31 changes: 31 additions & 0 deletions lib/spec/runner/differs/default.rb
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions spec/spec/expectations/differs/default_spec.rb
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions spec/spec/expectations/fail_with_spec.rb
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/html_formatted-1.8.4.html
Expand Up @@ -255,7 +255,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behavior driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/html_formatted-1.8.5.html
Expand Up @@ -259,7 +259,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behavior driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/html_formatted-1.8.6.html
Expand Up @@ -265,7 +265,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behaviour driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/html_formatted-1.8.7.html
Expand Up @@ -265,7 +265,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behaviour driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/html_formatted-1.9.1.html
Expand Up @@ -270,7 +270,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behaviour driven development
Expand Down
2 changes: 1 addition & 1 deletion 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

Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/text_mate_formatted-1.8.4.html
Expand Up @@ -255,7 +255,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behavior driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/text_mate_formatted-1.8.6.html
Expand Up @@ -261,7 +261,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behaviour driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/text_mate_formatted-1.8.7.html
Expand Up @@ -261,7 +261,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behaviour driven development
Expand Down
3 changes: 2 additions & 1 deletion spec/spec/runner/formatter/text_mate_formatted-1.9.1.html
Expand Up @@ -266,7 +266,8 @@ <h1>RSpec Code Examples</h1>
<div class="failure" id="failure_5">
<div class="message"><pre>expected: &quot;RSpec is a\nbehaviour driven development\nframework for Ruby\n&quot;,
got: &quot;RSpec is a\nbehavior driven development\nframework for Ruby\n&quot; (using ==)
Diff:

Diff:
@@ -1,4 +1,4 @@
RSpec is a
-behaviour driven development
Expand Down

0 comments on commit ce4498d

Please sign in to comment.