Skip to content
This repository
Browse code

don't diff single line strings

Closes #5.
  • Loading branch information...
commit 108291c3b28174a386da51815cff0379f204216c 1 parent 23f41a6
David Chelimsky authored June 13, 2010
1  Rakefile
@@ -71,6 +71,7 @@ Rake::RDocTask.new do |rdoc|
71 71
 end
72 72
 
73 73
 task :clobber do
  74
+  rm_rf 'doc'
74 75
   rm_rf 'pkg'
75 76
   rm_rf 'tmp'
76 77
   rm_rf 'coverage'
8  lib/rspec/expectations/differ.rb
@@ -19,8 +19,9 @@ def diff_as_string(data_new, data_old)
19 19
         file_length_difference = 0
20 20
         diffs.each do |piece|
21 21
           begin
22  
-            hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
23  
-                                       file_length_difference)
  22
+            hunk = Diff::LCS::Hunk.new(
  23
+              data_old, data_new, piece, context_lines, file_length_difference
  24
+            )
24 25
             file_length_difference = hunk.file_length_difference      
25 26
             next unless oldhunk      
26 27
             # Hunks may overlap, which is why we need to be careful when our
@@ -44,7 +45,8 @@ def diff_as_object(target,expected)
44 45
         diff_as_string(PP.pp(target,""), PP.pp(expected,""))
45 46
       end
46 47
 
47  
-      protected
  48
+    protected
  49
+
48 50
       def format
49 51
         :unified
50 52
       end
36  lib/rspec/expectations/fail_with.rb
@@ -8,21 +8,39 @@ def differ
8 8
       # raises a RSpec::Expectations::ExpectationNotMetError with message
9 9
       #
10 10
       # When a differ has been assigned and fail_with is passed
11  
-      # <code>expected</code> and <code>target</code>, passes them
  11
+      # <code>expected</code> and <code>actual</code>, passes them
12 12
       # to the differ to append a diff message to the failure message.
13  
-      def fail_with(message, expected=nil, target=nil) # :nodoc:
14  
-        if message.nil?
  13
+      def fail_with(message, expected=nil, actual=nil) # :nodoc:
  14
+        if !message
15 15
           raise ArgumentError, "Failure message is nil. Does your matcher define the " +
16 16
                                "appropriate failure_message_for_* method to return a string?"
17 17
         end
18  
-        unless (differ.nil? || expected.nil? || target.nil?)
19  
-          if expected.is_a?(String)
20  
-            message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
21  
-          elsif !target.is_a?(Proc)
22  
-            message << "\nDiff:" << self.differ.diff_as_object(target, expected)
  18
+
  19
+        if actual && expected
  20
+          if all_strings?(actual, expected)
  21
+            if any_multiline_strings?(actual, expected)
  22
+              message << "\nDiff:" << self.differ.diff_as_string(actual, expected)
  23
+            end
  24
+          elsif no_procs?(actual, expected)
  25
+            message << "\nDiff:" << self.differ.diff_as_object(actual, expected)
23 26
           end
24 27
         end
25  
-        Kernel::raise(RSpec::Expectations::ExpectationNotMetError.new(message))
  28
+
  29
+        raise(RSpec::Expectations::ExpectationNotMetError.new(message))
  30
+      end
  31
+
  32
+    private
  33
+
  34
+      def no_procs?(*args)
  35
+        args.none? {|a| Proc === a}
  36
+      end
  37
+
  38
+      def all_strings?(*args)
  39
+        args.all? {|a| String === a}
  40
+      end
  41
+
  42
+      def any_multiline_strings?(*args)
  43
+        all_strings?(*args) && args.any? {|a| a =~ /\n/}
26 44
       end
27 45
     end
28 46
   end
2  rspec-expectations.gemspec
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9 9
 
10 10
   s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11 11
   s.authors = ["David Chelimsky", "Chad Humphries"]
12  
-  s.date = %q{2010-06-08}
  12
+  s.date = %q{2010-06-13}
13 13
   s.description = %q{rspec expectations (should[_not] and matchers)}
14 14
   s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
15 15
   s.extra_rdoc_files = [
30  spec/rspec/expectations/fail_with_spec.rb
... ...
@@ -1,34 +1,40 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe RSpec::Expectations, "#fail_with with diff" do
  4
+  let(:differ) { double("differ") }
  5
+
4 6
   before(:each) do
5  
-    @differ = mock("differ")
6  
-    RSpec::Expectations.stub(:differ) { @differ }
  7
+    RSpec::Expectations.stub(:differ) { differ }
7 8
   end
8 9
   
9  
-  it "should not call differ if no expected/actual" do
  10
+  it "does not call differ if no expected/actual" do
10 11
     lambda {
11 12
       RSpec::Expectations.fail_with "the message"
12 13
     }.should fail_with("the message")
13 14
   end
14 15
   
15  
-  it "should call differ if expected/actual are presented separately" do
16  
-    @differ.should_receive(:diff_as_string).and_return("diff")
  16
+  it "calls differ if expected/actual are presented separately" do
  17
+    differ.should_receive(:diff_as_string).and_return("diff")
17 18
     lambda {
18  
-      RSpec::Expectations.fail_with "the message", "expected", "actual"
  19
+      RSpec::Expectations.fail_with "the message", "expected\nthis", "actual"
19 20
     }.should fail_with("the message\nDiff:diff")
20 21
   end
21 22
   
22  
-  it "should call differ if expected/actual are not strings" do
23  
-    @differ.should_receive(:diff_as_object).and_return("diff")
  23
+  it "does not call differ if expected/actual are single line strings" do
  24
+    differ.should_not_receive(:diff_as_string)
  25
+    RSpec::Expectations.fail_with ("the message", "expected", "actual") rescue nil
  26
+  end
  27
+  
  28
+  it "calls differ if expected/actual are not strings" do
  29
+    differ.should_receive(:diff_as_object).and_return("diff")
24 30
     lambda {
25  
-      RSpec::Expectations.fail_with "the message", :expected, :actual
  31
+      RSpec::Expectations.fail_with "the message", Object.new, Object.new
26 32
     }.should fail_with("the message\nDiff:diff")
27 33
   end
28 34
   
29  
-  it "should not call differ if expected or actual are procs" do
30  
-    @differ.should_not_receive(:diff_as_string)
31  
-    @differ.should_not_receive(:diff_as_object)
  35
+  it "does not call differ if expected or actual are procs" do
  36
+    differ.should_not_receive(:diff_as_string)
  37
+    differ.should_not_receive(:diff_as_object)
32 38
     lambda {
33 39
       RSpec::Expectations.fail_with "the message", lambda {}, lambda {}
34 40
     }.should fail_with("the message")

0 notes on commit 108291c

Please sign in to comment.
Something went wrong with that request. Please try again.