| 1 |
// Copyright (c) 2019, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package ctxerr_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/color" |
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/test" |
| 16 |
) |
| 17 |
|
| 18 |
func errorSummaryToString(s ctxerr.ErrorSummary, prefix string, uncolorized bool) string { |
| 19 |
var buf strings.Builder |
| 20 |
s.AppendSummary(&buf, prefix, !uncolorized) |
| 21 |
return buf.String() |
| 22 |
} |
| 23 |
|
| 24 |
func TestErrorSummary(t *testing.T) { |
| 25 |
defer color.SaveState()() |
| 26 |
|
| 27 |
var expectedColorized bool |
| 28 |
r := func(s string) string { |
| 29 |
if s[0] == '\n' { |
| 30 |
s = s[1:] |
| 31 |
} |
| 32 |
var repl *strings.Replacer |
| 33 |
if expectedColorized { |
| 34 |
repl = strings.NewReplacer( |
| 35 |
"*", "\x1b[1;31m", // bold red |
| 36 |
"+", "\x1b[0;31m", // red light |
| 37 |
"^", "\x1b[0m", // red off |
| 38 |
"~", "", // just ignore, for vertical alignment purpose |
| 39 |
) |
| 40 |
} else { |
| 41 |
repl = strings.NewReplacer( |
| 42 |
"*", "", // bold red |
| 43 |
"+", "", // red light |
| 44 |
"^", "", // red off |
| 45 |
"~", "", // just ignore, for vertical alignment purpose |
| 46 |
) |
| 47 |
} |
| 48 |
return repl.Replace(s) |
| 49 |
} |
| 50 |
|
| 51 |
testCases := []struct { |
| 52 |
name string |
| 53 |
envColorized bool |
| 54 |
forceUncolorized bool |
| 55 |
expectedColorized bool |
| 56 |
}{ |
| 57 |
{ |
| 58 |
name: "no color via env", |
| 59 |
envColorized: false, |
| 60 |
expectedColorized: false, |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "colorized", |
| 64 |
envColorized: true, |
| 65 |
expectedColorized: true, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "colorized, but force uncolorized", |
| 69 |
envColorized: true, |
| 70 |
forceUncolorized: true, |
| 71 |
expectedColorized: false, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "no color via env and force uncolorized", |
| 75 |
envColorized: false, |
| 76 |
forceUncolorized: true, |
| 77 |
expectedColorized: false, |
| 78 |
}, |
| 79 |
} |
| 80 |
for _, tc := range testCases { |
| 81 |
t.Run(tc.name, func(t *testing.T) { |
| 82 |
expectedColorized = tc.expectedColorized |
| 83 |
|
| 84 |
color.SaveState(tc.envColorized) |
| 85 |
|
| 86 |
t.Logf("colorized=%t force=%t expected=%t", tc.envColorized, tc.forceUncolorized, tc.expectedColorized) |
| 87 |
|
| 88 |
// |
| 89 |
// errorSummaryString |
| 90 |
summary := ctxerr.NewSummary("foobar") |
| 91 |
|
| 92 |
test.EqualStr(t, errorSummaryToString(summary, "", tc.forceUncolorized), |
| 93 |
r(`+foobar^`)) |
| 94 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), |
| 95 |
r(`----+foobar^`)) |
| 96 |
|
| 97 |
summary = ctxerr.NewSummary("foo\nbar") |
| 98 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(` |
| 99 |
----+foo^ |
| 100 |
----+bar^`)) |
| 101 |
|
| 102 |
// |
| 103 |
// ErrorSummaryItem |
| 104 |
summary = ctxerr.ErrorSummaryItem{ |
| 105 |
Label: "the_label", |
| 106 |
Value: "foo\nbar", |
| 107 |
} |
| 108 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(` |
| 109 |
----*the_label: +foo^ |
| 110 |
----~ +bar^`)) |
| 111 |
|
| 112 |
summary = ctxerr.ErrorSummaryItem{ |
| 113 |
Label: "the_label", |
| 114 |
Value: "foo\nbar", |
| 115 |
Explanation: "And the\nexplanation...", |
| 116 |
} |
| 117 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(` |
| 118 |
----*the_label: +foo^ |
| 119 |
----~ +bar^ |
| 120 |
----+And the^ |
| 121 |
----+explanation...^`)) |
| 122 |
|
| 123 |
// |
| 124 |
// ErrorSummaryItems |
| 125 |
summary = ctxerr.ErrorSummaryItems{ |
| 126 |
{ |
| 127 |
Label: "first label", |
| 128 |
Value: "foo\nbar", |
| 129 |
Explanation: "And the\nexplanation...", |
| 130 |
}, |
| 131 |
{ |
| 132 |
Label: "2nd label", |
| 133 |
Value: "zip\nzap", |
| 134 |
}, |
| 135 |
{ |
| 136 |
Label: "3rd big label", |
| 137 |
Value: "666", |
| 138 |
}, |
| 139 |
} |
| 140 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(` |
| 141 |
----* first label: +foo^ |
| 142 |
----~ +bar^ |
| 143 |
----+And the^ |
| 144 |
----+explanation...^ |
| 145 |
----* 2nd label: +zip^ |
| 146 |
----~ +zap^ |
| 147 |
----*3rd big label: +666^`)) |
| 148 |
|
| 149 |
// |
| 150 |
// NewSummaryReason |
| 151 |
summary = ctxerr.NewSummaryReason(666, "") |
| 152 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(` |
| 153 |
----* value: +666^ |
| 154 |
----+it failed but didn't say why^`)) |
| 155 |
|
| 156 |
summary = ctxerr.NewSummaryReason(666, "evil number not accepted!") |
| 157 |
test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(` |
| 158 |
----* value: +666^ |
| 159 |
----*it failed coz: +evil number not accepted!^`)) |
| 160 |
}) |
| 161 |
} |
| 162 |
} |