File: summary_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (162 lines) | stat: -rw-r--r-- 4,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2019, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package ctxerr_test

import (
	"strings"
	"testing"

	"github.com/maxatome/go-testdeep/internal/color"
	"github.com/maxatome/go-testdeep/internal/ctxerr"
	"github.com/maxatome/go-testdeep/internal/test"
)

func errorSummaryToString(s ctxerr.ErrorSummary, prefix string, uncolorized bool) string {
	var buf strings.Builder
	s.AppendSummary(&buf, prefix, !uncolorized)
	return buf.String()
}

func TestErrorSummary(t *testing.T) {
	defer color.SaveState()()

	var expectedColorized bool
	r := func(s string) string {
		if s[0] == '\n' {
			s = s[1:]
		}
		var repl *strings.Replacer
		if expectedColorized {
			repl = strings.NewReplacer(
				"*", "\x1b[1;31m", // bold red
				"+", "\x1b[0;31m", // red light
				"^", "\x1b[0m", // red off
				"~", "", // just ignore, for vertical alignment purpose
			)
		} else {
			repl = strings.NewReplacer(
				"*", "", // bold red
				"+", "", // red light
				"^", "", // red off
				"~", "", // just ignore, for vertical alignment purpose
			)
		}
		return repl.Replace(s)
	}

	testCases := []struct {
		name              string
		envColorized      bool
		forceUncolorized  bool
		expectedColorized bool
	}{
		{
			name:              "no color via env",
			envColorized:      false,
			expectedColorized: false,
		},
		{
			name:              "colorized",
			envColorized:      true,
			expectedColorized: true,
		},
		{
			name:              "colorized, but force uncolorized",
			envColorized:      true,
			forceUncolorized:  true,
			expectedColorized: false,
		},
		{
			name:              "no color via env and force uncolorized",
			envColorized:      false,
			forceUncolorized:  true,
			expectedColorized: false,
		},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			expectedColorized = tc.expectedColorized

			color.SaveState(tc.envColorized)

			t.Logf("colorized=%t force=%t expected=%t", tc.envColorized, tc.forceUncolorized, tc.expectedColorized)

			//
			// errorSummaryString
			summary := ctxerr.NewSummary("foobar")

			test.EqualStr(t, errorSummaryToString(summary, "", tc.forceUncolorized),
				r(`+foobar^`))
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized),
				r(`----+foobar^`))

			summary = ctxerr.NewSummary("foo\nbar")
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(`
----+foo^
----+bar^`))

			//
			// ErrorSummaryItem
			summary = ctxerr.ErrorSummaryItem{
				Label: "the_label",
				Value: "foo\nbar",
			}
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(`
----*the_label: +foo^
----~           +bar^`))

			summary = ctxerr.ErrorSummaryItem{
				Label:       "the_label",
				Value:       "foo\nbar",
				Explanation: "And the\nexplanation...",
			}
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(`
----*the_label: +foo^
----~           +bar^
----+And the^
----+explanation...^`))

			//
			// ErrorSummaryItems
			summary = ctxerr.ErrorSummaryItems{
				{
					Label:       "first label",
					Value:       "foo\nbar",
					Explanation: "And the\nexplanation...",
				},
				{
					Label: "2nd label",
					Value: "zip\nzap",
				},
				{
					Label: "3rd big label",
					Value: "666",
				},
			}
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(`
----*  first label: +foo^
----~               +bar^
----+And the^
----+explanation...^
----*    2nd label: +zip^
----~               +zap^
----*3rd big label: +666^`))

			//
			// NewSummaryReason
			summary = ctxerr.NewSummaryReason(666, "")
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(`
----*  value: +666^
----+it failed but didn't say why^`))

			summary = ctxerr.NewSummaryReason(666, "evil number not accepted!")
			test.EqualStr(t, errorSummaryToString(summary, "----", tc.forceUncolorized), r(`
----*        value: +666^
----*it failed coz: +evil number not accepted!^`))
		})
	}
}