stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.51k stars 1.56k forks source link

assert: add InDelta for maps with a deltas map #1519

Open julian776 opened 6 months ago

julian776 commented 6 months ago

Summary

Add functionality to compare a custom delta for each map value.

Changes

Include the function to compare each delta.

Motivation

These changes now let you compare each map value with its own custom delta, instead of having to use the same for all of them.

Example usage

func TestHardcodedValues(t *testing.T) {
    expexted := map[string]float64{
        "foo": 1.0,
        "bar": 7,
        "baz": math.NaN(),
    }

    actual := map[string]float64{
        "foo": 1.01,
        "bar": 7.99,
        "baz": math.NaN(),
    }

    deltas := map[interface{}]float64{
        "foo": 0.1,
        "bar": 1,
        "baz": 0.1,
    }

    assert.InMapDeltasMapValues(
        t,
        expexted,
        actual,
        deltas,
    )
}

Related issues

Incorporate functionality #1496.

hendrywiranto commented 6 months ago

Hi, I hate to break this to you but currently testify doesn't accept PR that expands the API as stated by this comment https://github.com/stretchr/testify/pull/1500#issuecomment-1793674608

but I may be wrong, need to wait maintainer's response

julian776 commented 6 months ago

I wasn't aware of this, but no problem if that's the case.