Skip to content

Improve diff when using match RSpec matcher #5

@stoivo

Description

@stoivo

Improve diff when using match RSpec matcher

Given the following test

  it "is" do
    expect({
      "cre" => "2",
      "created_at" => "2024",
    })
      .to match("created_at" => /\d+/)
  end

We get the following output

    1) Journal is
       Failure/Error:
         expect({
           "cre" => "2",
           "created_at" => "2024",
         })
           .to match("created_at" => /\d+/)
       
         expected {"cre"=>"2", "created_at"=>"2024"} to match {"created_at"=>/\d+/}
         Diff:
         @@ +1/-0/~1 @@
           extra key: "cre" ("2")
           new value: "created_at" (/\d+/ -> "2024") 

If we add "cre" => "2", to the expectation it passes

  it "is" do
    expect({
      "cre" => "2",
      "created_at" => "2024",
    })
      .to match("cre" => "2", "created_at" => /\d+/)
  end

Same thing for BigDecimal

expect({
  "cre" => "2",
  "created_at" => BigDecimal(10),
})
  .to match("created_at" => 10)
# new value: "created_at" (10 -> #<BigDecimal: 10.0>)

Same thing for Comparing against class

expect({
  "cre" => "2",
  "created_at" => BigDecimal(10),
})
  .to match("created_at" => BigDecimal)
# new value: "created_at" (BigDecimal -> #<BigDecimal: 10.0>)

Proposal
It would be nice if we could hide new value: "created_at" (/\d+/ -> "2024") from the diff since it is actually matching. Current diffing logic don't take which matcher into account

Other notes
If we figure out how we can get which matcher was i use we might want to make some changed to how we represent include diff?

expect({
  "cre" => "2",
  "created_at" => BigDecimal(10),
})
  .to include("a"=> 1,"created_at" => BigDecimal)
expected {"cre" => "2", "created_at" => #<BigDecimal: 10.0>} to include {"a" => 1}
Diff:
@@ +1/-1/~1 @@
missing key: "a" (1)
  extra key: "cre" ("2")
  new value: "created_at" (BigDecimal -> #<BigDecimal: 10.0>)

include matcher also uses this diff which isn't good

expect(["1", "2"])
  .to include("2", "3")
expect(["1", "2"])
 .to include("2", "3")

expected ["1", "2"] to include "3"
Diff:
@@ +0/-0/~2 @@
 new value: [0] ("2" -> "1")
 new value: [1] ("3" -> "2")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions