From fecb84aef0a48eb909bd71b008a79797b6ed4200 Mon Sep 17 00:00:00 2001 From: Joe Stein Date: Thu, 14 Jul 2022 13:33:40 -0400 Subject: [PATCH 1/2] Write failing test for `include` fuzzy matching --- .../integration/rspec/include_matcher_spec.rb | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/spec/integration/rspec/include_matcher_spec.rb b/spec/integration/rspec/include_matcher_spec.rb index 58f8d252..695bbd55 100644 --- a/spec/integration/rspec/include_matcher_spec.rb +++ b/spec/integration/rspec/include_matcher_spec.rb @@ -258,6 +258,46 @@ in_color(color_enabled) end end + + it "produces the correct failure message when fuzzy matching" do + as_both_colored_and_uncolored do |color_enabled| + snippet = <<~TEST.strip + expected = { number: a_kind_of(Numeric), city: /burb/i, state: "CA" } + actual = { number: 42, city: "Burbank", zip: "90210" } + expect(actual).to include(expected) + TEST + program = make_plain_test_program( + snippet, + color_enabled: color_enabled, + ) + + expected_output = build_expected_output( + color_enabled: color_enabled, + snippet: %|expect(actual).to include(expected)|, + expectation: proc { + line do + plain %|Expected | + actual %|{ number: 42, city: "Burbank", zip: "90210" }| + plain %| to include | + expected %|(state: "CA")| + plain %|.| + end + }, + diff: proc { + plain_line %| {| + plain_line %| number: 42,| + plain_line %| city: "Burbank",| + plain_line %| zip: "90210"| + expected_line %|- state: "CA"| + plain_line %| }| + }, + ) + + expect(program). + to produce_output_when_run(expected_output). + in_color(color_enabled) + end + end end context "that is large" do @@ -345,6 +385,61 @@ in_color(color_enabled) end end + + it "produces the correct failure message when fuzzy matching" do + as_both_colored_and_uncolored do |color_enabled| + snippet = <<~TEST.strip + expected = { + number: a_kind_of(Numeric), + street: "Yoshie Circles", + city: /burb/i, + zip: "90382" + } + actual = { + number: 42, + street: "Yoshie Circles", + city: "Burbank", + state: "CA", + zip: "90210" + } + expect(actual).to include(expected) + TEST + program = make_plain_test_program( + snippet, + color_enabled: color_enabled, + ) + + expected_output = build_expected_output( + color_enabled: color_enabled, + snippet: %|expect(actual).to include(expected)|, + expectation: proc { + line do + plain %| Expected | + actual %|{ number: 42, street: "Yoshie Circles", city: "Burbank", state: "CA", zip: "90210" }| + end + + line do + plain %|to include | + expected %|(zip: "90382")| + end + }, + diff: proc { + plain_line %| {| + plain_line %| number: 42,| + plain_line %| street: "Yoshie Circles",| + plain_line %| city: "Burbank",| + plain_line %| state: "CA",| + expected_line %|- zip: "90382"| + actual_line %|+ zip: "90210"| + plain_line %| }| + }, + ) + + expect(program). + to produce_output_when_run(expected_output). + in_color(color_enabled) + end + end end end end From bc2220ca47b5cee04476ff504fdcfdfba54e0ec2 Mon Sep 17 00:00:00 2001 From: Joe Stein Date: Thu, 14 Jul 2022 13:36:49 -0400 Subject: [PATCH 2/2] Add fuzzy value matching HashIncluding RSpec matcher --- .../rspec/operation_tree_builders/hash_including.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/super_diff/rspec/operation_tree_builders/hash_including.rb b/lib/super_diff/rspec/operation_tree_builders/hash_including.rb index e6ef2bd2..a02208fe 100644 --- a/lib/super_diff/rspec/operation_tree_builders/hash_including.rb +++ b/lib/super_diff/rspec/operation_tree_builders/hash_including.rb @@ -2,6 +2,8 @@ module SuperDiff module RSpec module OperationTreeBuilders class HashIncluding < SuperDiff::OperationTreeBuilders::Hash + include ::RSpec::Matchers::Composable + def self.applies_to?(expected, actual) ( SuperDiff::RSpec.a_hash_including_something?(expected) || @@ -23,7 +25,7 @@ def initialize(expected:, **rest) def should_add_noop_operation?(key) !expected.include?(key) || ( actual.include?(key) && - expected[key] == actual[key] + values_match?(expected[key], actual[key]) ) end end