Permalink
Browse files

Diff single line and multiline strings differently

  • Loading branch information...
1 parent 43b0918 commit 1273d4d963799f1e358dd0046b826c169dc380a1 @dchelimsky dchelimsky committed Jan 4, 2012
Showing with 31 additions and 3 deletions.
  1. +1 −1 lib/rspec/expectations/differ.rb
  2. +30 −2 spec/rspec/expectations/differ_spec.rb
@@ -64,7 +64,7 @@ def object_to_string(object)
%(#{PP.singleline_pp(k, "")} => #{PP.singleline_pp(object[k], "")})
end.join(",\n")
when String
- object.inspect
+ object =~ /\n/ ? object : object.inspect
else
PP.pp(object,"")
end
@@ -56,7 +56,7 @@ def inspect
expected = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'charlie', :width, 'quite wide' ]
actual = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'tango' , :width, 'very wide' ]
- expected_diff = <<'EOD'
+ expected_diff = <<EOD
@@ -5,7 +5,7 @@
@@ -70,7 +70,6 @@ def inspect
+ "quite wide"]
EOD
-
diff = @differ.diff_as_object(expected,actual)
diff.should == expected_diff
end
@@ -108,9 +107,38 @@ def inspect
:width => "quite wide"
EOD
+ diff = @differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
+
+ it "outputs unified diff of single line strings" do
+ expected = "this is one string"
+ actual = "this is another string"
+
+ expected_diff = <<EOD
+
+@@ -1,2 +1,2 @@
+-"this is another string"
++"this is one string"
+EOD
diff = @differ.diff_as_object(expected,actual)
diff.should == expected_diff
end
+ it "outputs unified diff of multi line strings" do
+ expected = "this is:\n one string"
+ actual = "this is:\n another string"
+
+ expected_diff = <<EOD
+
+@@ -1,3 +1,3 @@
+ this is:
+- another string
++ one string
+EOD
+
+ diff = @differ.diff_as_object(expected,actual)
+ diff.should == expected_diff
+ end
end

0 comments on commit 1273d4d

Please sign in to comment.