Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #157 from alexcoplan/color_diffs

Colored Diffs
  • Loading branch information...
commit 3b4f60c625fe2f60fb01269b780ddddd89930579 2 parents 2c2fdb6 + 83cdba4
@myronmarston myronmarston authored
View
29 lib/rspec/expectations/differ.rb
@@ -36,6 +36,7 @@ def diff_as_string(data_new, data_old)
end
#Handle the last remaining hunk
output << oldhunk.diff(format) << "\n"
+ color_diff output
end
def diff_as_object(actual, expected)
@@ -48,7 +49,7 @@ def diff_as_object(actual, expected)
"between #{actual} and #{expected} is empty. Check the " \
"implementation of #{actual}.==."
else
- diff
+ color_diff diff
end
end
@@ -62,6 +63,32 @@ def context_lines
3
end
+ def color(text, code)
+ "\e[#{code}m#{text}\e[0m"
+ end
+
+ def color_diff(diff)
+ return diff unless RSpec::Matchers.configuration.color?
+
+ red = 31
+ green = 32
+ blue = 34
+
+ lines = diff.lines.map do |line|
+ case line[0].chr
+ when "+"
+ color(line, green)
+ when "-"
+ color(line, red)
+ when "@"
+ line[1].chr == "@" ? color(line, blue) : line
+ else
+ line
+ end
+ end
+ lines.join
+ end
+
def object_to_string(object)
case object
when Hash
View
13 lib/rspec/matchers/configuration.rb
@@ -39,6 +39,19 @@ def syntax
syntaxes
end
+ # color config for expectations
+ # fallback if rspec core not available
+ if defined?(RSpec::Core)
+ def color?
+ RSpec.configuration.color
+ end
+ else
+ attr_writer :color
+ def color?
+ @color
+ end
+ end
+
# Adds `should` and `should_not` to the given classes
# or modules. This can be used to ensure `should` works
# properly on things like proxy objects (particular
View
24 spec/rspec/expectations/differ_spec.rb
@@ -4,8 +4,14 @@
module RSpec
module Expectations
describe Differ do
+ context "without --color" do
+
+ before { RSpec::Matchers.configuration.stub(:color? => false) }
+
let(:differ) { RSpec::Expectations::Differ.new }
+ # color disabled context
+
describe '#diff_as_string' do
it "outputs unified diff of two strings" do
expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
@@ -149,5 +155,23 @@ def inspect
end
end
end
+
+ context "with --color" do
+ before { RSpec::Matchers.configuration.stub(:color? => true) }
+
+ let(:differ) { RSpec::Expectations::Differ.new }
+
+ it "outputs colored diffs" do
+ expected = "foo bar baz"
+ actual = "foo bang baz"
+ expected_diff = "\n\e[34m@@ -1,2 +1,2 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m"
+
+
+ diff = differ.diff_as_string(expected,actual)
+ diff.should == expected_diff
+ end
+ end
+
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.