|
| 1 | +// SPDX-FileCopyrightText: (c) 2025 Rafal Zajac <rzajac@gmail.com> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package check |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ctx42/testing/internal/affirm" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_JSON(t *testing.T) { |
| 13 | + t.Run("equal", func(t *testing.T) { |
| 14 | + // --- Given --- |
| 15 | + want := ` {"hello": "world"} ` |
| 16 | + have := " { \"hello\"\t:\n\n \"world\"\n\n\t} " |
| 17 | + |
| 18 | + // --- When --- |
| 19 | + err := JSON(want, have) |
| 20 | + |
| 21 | + // --- Then --- |
| 22 | + affirm.Nil(t, err) |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("not equal", func(t *testing.T) { |
| 26 | + // --- Given --- |
| 27 | + want := ` {"hello": "world"} ` |
| 28 | + have := " { \"hello\"\t:\n\n \"ms\"\n\n\t} " |
| 29 | + opt := WithTrail("type.field") |
| 30 | + |
| 31 | + // --- When --- |
| 32 | + err := JSON(want, have, opt) |
| 33 | + |
| 34 | + // --- Then --- |
| 35 | + affirm.NotNil(t, err) |
| 36 | + wMsg := "expected JSON strings to be equal:\n" + |
| 37 | + " trail: type.field\n" + |
| 38 | + " want: {\"hello\":\"world\"}\n" + |
| 39 | + " have: {\"hello\":\"ms\"}" |
| 40 | + affirm.Equal(t, wMsg, err.Error()) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("invalid want JSON", func(t *testing.T) { |
| 44 | + // --- Given --- |
| 45 | + want := `{!!!}` |
| 46 | + have := `{"hello": "world"}` |
| 47 | + opt := WithTrail("type.field") |
| 48 | + |
| 49 | + // --- When --- |
| 50 | + err := JSON(want, have, opt) |
| 51 | + |
| 52 | + // --- Then --- |
| 53 | + affirm.NotNil(t, err) |
| 54 | + wMsg := "did not expect unmarshalling error:\n" + |
| 55 | + " trail: type.field\n" + |
| 56 | + " argument: want\n" + |
| 57 | + " error: invalid character '!' looking for beginning of " + |
| 58 | + "object key string" |
| 59 | + affirm.Equal(t, wMsg, err.Error()) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("invalid have JSON", func(t *testing.T) { |
| 63 | + // --- Given --- |
| 64 | + want := `{"hello": "world"}` |
| 65 | + have := `{!!!}` |
| 66 | + opt := WithTrail("type.field") |
| 67 | + |
| 68 | + // --- When --- |
| 69 | + err := JSON(want, have, opt) |
| 70 | + |
| 71 | + // --- Then --- |
| 72 | + affirm.NotNil(t, err) |
| 73 | + wMsg := "did not expect unmarshalling error:\n" + |
| 74 | + " trail: type.field\n" + |
| 75 | + " argument: have\n" + |
| 76 | + " error: invalid character '!' looking for beginning of " + |
| 77 | + "object key string" |
| 78 | + affirm.Equal(t, wMsg, err.Error()) |
| 79 | + }) |
| 80 | +} |
0 commit comments