Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Pact data-diffs for fun and profit
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