Skip to content

Commit

Permalink
don't diff single line strings
Browse files Browse the repository at this point in the history
Closes #5.
  • Loading branch information
dchelimsky committed Jun 14, 2010
1 parent 23f41a6 commit 108291c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Rake::RDocTask.new do |rdoc|
end

task :clobber do
rm_rf 'doc'
rm_rf 'pkg'
rm_rf 'tmp'
rm_rf 'coverage'
Expand Down
8 changes: 5 additions & 3 deletions lib/rspec/expectations/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def diff_as_string(data_new, data_old)
file_length_difference = 0
diffs.each do |piece|
begin
hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
file_length_difference)
hunk = Diff::LCS::Hunk.new(
data_old, data_new, piece, context_lines, file_length_difference
)
file_length_difference = hunk.file_length_difference
next unless oldhunk
# Hunks may overlap, which is why we need to be careful when our
Expand All @@ -44,7 +45,8 @@ def diff_as_object(target,expected)
diff_as_string(PP.pp(target,""), PP.pp(expected,""))
end

protected
protected

def format
:unified
end
Expand Down
36 changes: 27 additions & 9 deletions lib/rspec/expectations/fail_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,39 @@ def differ
# raises a RSpec::Expectations::ExpectationNotMetError with message
#
# When a differ has been assigned and fail_with is passed
# <code>expected</code> and <code>target</code>, passes them
# <code>expected</code> and <code>actual</code>, passes them
# to the differ to append a diff message to the failure message.
def fail_with(message, expected=nil, target=nil) # :nodoc:
if message.nil?
def fail_with(message, expected=nil, actual=nil) # :nodoc:
if !message
raise ArgumentError, "Failure message is nil. Does your matcher define the " +
"appropriate failure_message_for_* method to return a string?"
end
unless (differ.nil? || expected.nil? || target.nil?)
if expected.is_a?(String)
message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
elsif !target.is_a?(Proc)
message << "\nDiff:" << self.differ.diff_as_object(target, expected)

if actual && expected
if all_strings?(actual, expected)
if any_multiline_strings?(actual, expected)
message << "\nDiff:" << self.differ.diff_as_string(actual, expected)
end
elsif no_procs?(actual, expected)
message << "\nDiff:" << self.differ.diff_as_object(actual, expected)
end
end
Kernel::raise(RSpec::Expectations::ExpectationNotMetError.new(message))

raise(RSpec::Expectations::ExpectationNotMetError.new(message))
end

private

def no_procs?(*args)
args.none? {|a| Proc === a}
end

def all_strings?(*args)
args.all? {|a| String === a}
end

def any_multiline_strings?(*args)
all_strings?(*args) && args.any? {|a| a =~ /\n/}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rspec-expectations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["David Chelimsky", "Chad Humphries"]
s.date = %q{2010-06-08}
s.date = %q{2010-06-13}
s.description = %q{rspec expectations (should[_not] and matchers)}
s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
s.extra_rdoc_files = [
Expand Down
30 changes: 18 additions & 12 deletions spec/rspec/expectations/fail_with_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
require 'spec_helper'

describe RSpec::Expectations, "#fail_with with diff" do
let(:differ) { double("differ") }

before(:each) do
@differ = mock("differ")
RSpec::Expectations.stub(:differ) { @differ }
RSpec::Expectations.stub(:differ) { differ }
end

it "should not call differ if no expected/actual" do
it "does not call differ if no expected/actual" do
lambda {
RSpec::Expectations.fail_with "the message"
}.should fail_with("the message")
end

it "should call differ if expected/actual are presented separately" do
@differ.should_receive(:diff_as_string).and_return("diff")
it "calls differ if expected/actual are presented separately" do
differ.should_receive(:diff_as_string).and_return("diff")
lambda {
RSpec::Expectations.fail_with "the message", "expected", "actual"
RSpec::Expectations.fail_with "the message", "expected\nthis", "actual"
}.should fail_with("the message\nDiff:diff")
end

it "should call differ if expected/actual are not strings" do
@differ.should_receive(:diff_as_object).and_return("diff")
it "does not call differ if expected/actual are single line strings" do
differ.should_not_receive(:diff_as_string)
RSpec::Expectations.fail_with ("the message", "expected", "actual") rescue nil
end

it "calls differ if expected/actual are not strings" do
differ.should_receive(:diff_as_object).and_return("diff")
lambda {
RSpec::Expectations.fail_with "the message", :expected, :actual
RSpec::Expectations.fail_with "the message", Object.new, Object.new
}.should fail_with("the message\nDiff: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)
it "does not call differ if expected or actual are procs" do
differ.should_not_receive(:diff_as_string)
differ.should_not_receive(:diff_as_object)
lambda {
RSpec::Expectations.fail_with "the message", lambda {}, lambda {}
}.should fail_with("the message")
Expand Down

0 comments on commit 108291c

Please sign in to comment.