go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
743 stars 54 forks source link

support for json.RawMessage? #62

Closed dionysius closed 1 week ago

dionysius commented 3 months ago

import "encoding/json"

func TestJsonRawMessage(t *testing.T) {
    type T json.RawMessage

    a := T(`{"foo":1,"bar":2}`)
    b := T(`{"bar":2,"foo":1}`)

    // just informative begin
    aI := map[string]interface{}{}
    bI := map[string]interface{}{}

    _ = json.Unmarshal(a, &aI)
    _ = json.Unmarshal(b, &bI)

    fmt.Println(aI)
    fmt.Println(bI)
    // end

    diff := deep.Equal(a, b)
    if len(diff) != 0 {
        t.Fatalf("expected 0 diff, got %d: %s", len(diff), diff)
    }
}
go test -run ^TestJsonRawMessage$ github.com/go-test/deep

map[bar:2 foo:1]
map[bar:2 foo:1]
--- FAIL: TestJsonRawMessage (0.00s)
    .../deep/deep_test.go:1603: expected 0 diff, got 8: [slice[2]: 102 != 98 slice[3]: 111 != 97 slice[4]: 111 != 114 slice[7]: 49 != 50 slice[10]: 98 != 102 slice[11]: 97 != 111 slice[12]: 114 != 111 slice[15]: 50 != 49]
FAIL
FAIL    github.com/go-test/deep 0.002s
FAIL

might be tricky tho, since the actual type within there can be any json supported type

daniel-nichter commented 1 week ago

Sorry for slow reply. Direct support would be too messy for this pkg; it'd delve too much into the realm of JSON. There's a possible workaround, though: https://github.com/go-test/deep/blob/v1.1.0/deep.go#L215-L217

Deep uses an Equal method. Using that is the way to go because it'll shift the JSON-specific logic into some pkg or receive that knows about JSON.