Created
September 6, 2015 11:42
-
-
Save mdub/ed9fd384718bc6d02a39 to your computer and use it in GitHub Desktop.
Using Pact data-diffs for fun and profit
This file contains hidden or 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
require "spec_helper" | |
require "pact/matchers" | |
require "pact/matchers/unix_diff_formatter" | |
require "term/ansicolor" | |
RSpec::Matchers.define :match_data do |expected| | |
match do |actual| | |
difference(actual).empty? | |
end | |
failure_message_for_should do |actual| | |
diff = difference(actual) | |
formatted_diff = Pact::Matchers::UnixDiffFormatter.call(diff, colour: true) | |
colorize(formatted_diff) | |
end | |
def difference(actual) | |
diff(expected, actual) | |
end | |
def colorize(s) | |
s.split("\n").collect { |line| | |
::Term::ANSIColor.reset + line | |
}.join("\n") | |
end | |
include Pact::Matchers | |
end | |
describe "stuff" do | |
let(:data) do | |
{ | |
"x" => 1, | |
"y" => 2, | |
"z" => { | |
"foo" => 6 | |
} | |
} | |
end | |
it "matches" do | |
expect(data).to match_data({ | |
"x" => 2, | |
"z" => { | |
"foo" => 3 | |
} | |
}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment