Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add support for coloured diffs

  • Loading branch information...
commit dbe5aa32879f68a33ddc054a91f700420b27c5c0 1 parent 2c2fdb6
@alexcoplan alexcoplan authored
View
24 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,27 @@ def context_lines
3
end
+ def color(code, text)
+ "#{code}#{text}\e[0m"
+ end
+
+ def color_diff(diff)
+ return diff unless RSpec::Matchers.configuration.color?
+ lines = diff.lines.map do |line|
+ case line[0]
+ when "+"
+ color(line, "\e[33m") # yellow
+ when "-"
+ color(line, "\e[31m") # red
+ when "@"
+ line[1] == "@" ? color(line, "\e[34m") : line # blue
+ else
+ line
+ end
+ end
+ lines.join
+ end
+
def object_to_string(object)
case object
when Hash
View
11 lib/rspec/matchers/configuration.rb
@@ -39,6 +39,17 @@ def syntax
syntaxes
end
+ 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
8 spec/rspec/expectations/differ_spec.rb
@@ -4,7 +4,12 @@
module RSpec
module Expectations
describe Differ do
+ context "without --color" do
+
let(:differ) { RSpec::Expectations::Differ.new }
+ before { RSpec.configuration.color = false }
+
+ # color disabled context
describe '#diff_as_string' do
it "outputs unified diff of two strings" do
@@ -148,6 +153,9 @@ def inspect
diff.should == expected_diff
end
end
+ end # end context
+
+
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.