Skip to content

Instantly share code, notes, and snippets.

@mdub
Created September 6, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdub/ed9fd384718bc6d02a39 to your computer and use it in GitHub Desktop.
Save mdub/ed9fd384718bc6d02a39 to your computer and use it in GitHub Desktop.
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