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