Created
April 9, 2010 08:59
-
-
Save aalin/361002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
raise LoadError.new('haxhax so we dont get wirble output in CruiseControl') if (ENV['USER'] == 'cruise' || ENV['USER'] == 'jenkins') | |
require 'wirble' | |
rescue LoadError | |
module Wirble | |
module Colorize | |
def self.colorize(str) | |
str | |
end | |
end | |
end | |
end | |
begin | |
raise LoadError.new('haxhax so we dont get colored diffs in CruiseControl') if (ENV['USER'] == 'cruise' || ENV['USER'] == 'jenkins') | |
require 'differ' | |
rescue LoadError | |
end | |
class DiffStringThing | |
module CoolColorFormatter | |
class << self | |
def format(change) | |
(change.change? && as_change(change)) || | |
(change.delete? && as_delete(change)) || | |
(change.insert? && as_insert(change)) || | |
'' | |
end | |
private | |
def as_insert(change) | |
"\033[30;42m#{change.insert}\033[0m" | |
end | |
def as_delete(change) | |
"\033[30;41m#{change.delete}\033[0m" | |
end | |
def as_change(change) | |
as_delete(change) << as_insert(change) | |
end | |
end | |
end | |
def initialize(expected, actual) | |
@expected = expected | |
@actual = actual | |
end | |
def diff_str | |
if deep_diffable? | |
Wirble::Colorize.colorize(@expected.deep_diff(@actual).inspect) | |
elsif numerics? | |
Wirble::Colorize.colorize((@actual - @expected).inspect) | |
elsif times? | |
a, seconds = (@actual - @expected).divmod(60) | |
a, minutes = a.divmod(60) | |
days, hours = a.divmod(24) | |
Wirble::Colorize.colorize("%dd %dh %dm %.3fs" % [days, hours, minutes, seconds]) | |
else | |
if defined?(Differ) | |
Differ.diff_by_char(@actual.to_s, @expected.to_s).format_as(CoolColorFormatter) | |
else | |
"gem install differ" | |
end | |
end | |
end | |
private | |
def deep_diffable? | |
@expected.respond_to?(:deep_diff) && @actual.is_a?(@expected.class) | |
end | |
def numerics? | |
expected_and_actual_are_of_class?(Numeric) | |
end | |
def times? | |
expected_and_actual_are_of_class?(Time) | |
end | |
def expected_and_actual_are_of_class?(klass) | |
@expected.is_a?(klass) && @actual.is_a?(klass) | |
end | |
end | |
module Spec | |
module Matchers | |
def self.deep_diffable?(a, b) | |
a.respond_to?(:deep_diff) && b.is_a?(a.class) | |
end | |
class PositiveOperatorMatcher < OperatorMatcher #:nodoc: | |
def __delegate_operator(actual, operator, expected) | |
return true if actual.__send__(operator, expected) | |
if ['==','===', '=~'].include?(operator) | |
using_string = "(using #{ operator })" | |
end | |
fail_with_message([ | |
"expected: #{Wirble::Colorize.colorize(expected.inspect)}", | |
" got: #{Wirble::Colorize.colorize(actual.inspect)} #{ using_string }", | |
"", | |
" diff: #{ DiffStringThing.new(expected, actual).diff_str }" | |
].compact.join("\n")) | |
end | |
end | |
def eql(expected) | |
Matcher.new :eql, expected do |_expected_| | |
match do |actual| | |
actual.eql?(_expected_) | |
end | |
failure_message_for_should do |actual| | |
<<-MESSAGE | |
expected #{Wirble::Colorize.colorize(_expected_.inspect)} | |
got #{Wirble::Colorize.colorize(actual.inspect)} | |
diff #{ DiffStringThing.new(_expected_, actual).diff_str } | |
(compared using eql?) | |
MESSAGE | |
end | |
failure_message_for_should_not do |actual| | |
<<-MESSAGE | |
expected #{Wirble::Colorize.colorize(actual.inspect)} | |
not to equal #{Wirble::Colorize.colorize(_expected_.inspect)} | |
(compared using eql?) | |
MESSAGE | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment