|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package colors |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + target "github.com/go-openapi/testify/v2/assert" |
| 13 | + colorstub "github.com/go-openapi/testify/v2/assert/enable/colors" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMain(m *testing.M) { |
| 17 | + // we can't easily simulate arg flags in CI (uses gotestsum etc). |
| 18 | + // Similarly, env vars are evaluated too early. |
| 19 | + colorstub.Enable( |
| 20 | + func() []colorstub.Option { |
| 21 | + return []colorstub.Option{ |
| 22 | + colorstub.WithEnable(true), |
| 23 | + colorstub.WithSanitizedTheme(flags.theme), |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + os.Exit(m.Run()) |
| 28 | +} |
| 29 | + |
| 30 | +func TestAssertJSONEq(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + mockT := new(mockT) |
| 34 | + res := target.JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "worldwide", "foo": "bar"}`) |
| 35 | + |
| 36 | + target.False(t, res) |
| 37 | + |
| 38 | + output := mockT.errorString() |
| 39 | + t.Log(output) // best to visualize the output |
| 40 | + target.Contains(t, neuterize(output), neuterize(expectedColorizedDiff)) |
| 41 | +} |
| 42 | + |
| 43 | +func TestAssertJSONEq_Array(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + mockT := new(mockT) |
| 47 | + res := target.JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["bar", {"nested": "hash", "hello": "world"}]`) |
| 48 | + |
| 49 | + target.False(t, res) |
| 50 | + output := mockT.errorString() |
| 51 | + t.Log(output) // best to visualize the output |
| 52 | + target.Contains(t, neuterize(output), neuterize(expectedColorizedArrayDiff)) |
| 53 | +} |
| 54 | + |
| 55 | +func neuterize(str string) string { |
| 56 | + // remove blanks and replace escape sequences for readability |
| 57 | + blankRemover := strings.NewReplacer("\t", "", " ", "", "\x1b", "^[") |
| 58 | + return blankRemover.Replace(str) |
| 59 | +} |
| 60 | + |
| 61 | +type mockT struct { |
| 62 | + errorFmt string |
| 63 | + args []any |
| 64 | +} |
| 65 | + |
| 66 | +// Helper is like [testing.T.Helper] but does nothing. |
| 67 | +func (mockT) Helper() {} |
| 68 | + |
| 69 | +func (m *mockT) Errorf(format string, args ...any) { |
| 70 | + m.errorFmt = format |
| 71 | + m.args = args |
| 72 | +} |
| 73 | + |
| 74 | +func (m *mockT) Failed() bool { |
| 75 | + return m.errorFmt != "" |
| 76 | +} |
| 77 | + |
| 78 | +func (m *mockT) errorString() string { |
| 79 | + return fmt.Sprintf(m.errorFmt, m.args...) |
| 80 | +} |
| 81 | + |
| 82 | +// captured output (indentation is not checked) |
| 83 | +// |
| 84 | +//nolint:staticcheck // indeed we want to check the escape sequences in this test |
| 85 | +const ( |
| 86 | + expectedColorizedDiff = ` Not equal: |
| 87 | + expected: [0;92mmap[string]interface {}{"foo":"bar", "hello":"world"}[0m |
| 88 | + actual : [0;91mmap[string]interface {}{"foo":"bar", "hello":"worldwide"}[0m |
| 89 | +
|
| 90 | + Diff: |
| 91 | + --- Expected |
| 92 | + +++ Actual |
| 93 | + @@ -2,3 +2,3 @@ |
| 94 | + [0;92m (string) (len=3) "foo": (string) (len=3) "bar", |
| 95 | + [0m[0;91m- (string) (len=5) "hello": (string) (len=5) "world" |
| 96 | + [0m[0;93m+ (string) (len=5) "hello": (string) (len=9) "worldwide" |
| 97 | + [0m[0;92m } |
| 98 | + [0m |
| 99 | +` |
| 100 | + |
| 101 | + expectedColorizedArrayDiff = `Not equal: |
| 102 | + expected: [0;92m[]interface {}{"foo", map[string]interface {}{"hello":"world", "nested":"hash"}}[0m |
| 103 | + actual : [0;91m[]interface {}{"bar", map[string]interface {}{"hello":"world", "nested":"hash"}}[0m |
| 104 | + |
| 105 | + Diff: |
| 106 | + --- Expected |
| 107 | + +++ Actual |
| 108 | + @@ -1,3 +1,3 @@ |
| 109 | + [0;92m ([]interface {}) (len=2) { |
| 110 | + [0m[0;91m- (string) (len=3) "foo", |
| 111 | + [0m[0;93m+ (string) (len=3) "bar", |
| 112 | + [0m[0;92m (map[string]interface {}) (len=2) { |
| 113 | + [0m |
| 114 | +` |
| 115 | +) |
0 commit comments