-
Notifications
You must be signed in to change notification settings - Fork 141
/
pretty_formatter_test.go
211 lines (156 loc) · 6.31 KB
/
pretty_formatter_test.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package formatters
import (
"fmt"
"testing"
"github.com/zalando/zally/cli/zally/domain"
"github.com/zalando/zally/cli/zally/tests"
)
func TestPrettyFormatViolations(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
t.Run("FormatViolations returns violations and header", func(t *testing.T) {
var mustViolation domain.Violation
mustViolation.Title = "Must Title"
mustViolation.RuleLink = "http://example.com/mustViolation"
mustViolation.ViolationType = "MUST"
mustViolation.Decription = "Must Description"
mustViolation.Paths = []string{"/path/one", "/path/two"}
violations := []domain.Violation{mustViolation}
actualResult := prettyFormatter.FormatViolations("MUST", violations)
expectedResult := fmt.Sprintf("MUST\n====\n\n%s", prettyFormatter.formatViolation(&mustViolation))
tests.AssertEquals(t, expectedResult, actualResult)
})
t.Run("FormatViolations returns nothing when no violations", func(t *testing.T) {
result := prettyFormatter.FormatViolations("MUST", []domain.Violation{})
tests.AssertEquals(t, "", result)
})
}
func TestPrettyFormatViolationsCount(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
t.Run("Converts ViolationsCount to string", func(t *testing.T) {
var count domain.ViolationsCount
count.Must = 1
count.Should = 2
count.May = 3
count.Hint = 4
actualResult := prettyFormatter.FormatViolationsCount(&count)
expectedResult := "Summary:\n" +
"========\n\n" +
"MUST violations: 1\n" +
"SHOULD violations: 2\n" +
"MAY violations: 3\n" +
"HINT violations: 4\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
}
func TestPrettyFormatRule(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
t.Run("Formats single rule", func(t *testing.T) {
var rule domain.Rule
rule.Title = "Must Rule"
rule.Type = "MUST"
rule.Code = "166"
rule.IsActive = true
rule.URL = "https://example.com/rule"
result := prettyFormatter.FormatRule(&rule)
tests.AssertEquals(
t,
"\x1b[31m166\x1b[0m \x1b[31mMUST\x1b[0m: Must Rule\n\thttps://example.com/rule\n\n",
result)
})
}
func TestPrettyFormatMessage(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
t.Run("Formats nothing when no message", func(t *testing.T) {
actualResult := prettyFormatter.FormatMessage("")
tests.AssertEquals(t, "", actualResult)
})
t.Run("Formats message when specified", func(t *testing.T) {
actualResult := prettyFormatter.FormatMessage("Congratulations!")
expectedResult := "\n\x1b[32mCongratulations!\x1b[0m\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
}
func TestPrettyFormatServerMessage(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
t.Run("Formats nothing when no message", func(t *testing.T) {
actualResult := prettyFormatter.FormatServerMessage("")
tests.AssertEquals(t, "", actualResult)
})
t.Run("Formats message when specified", func(t *testing.T) {
actualResult := prettyFormatter.FormatServerMessage("Hello world!")
expectedResult := "\n\nServer message:\n===============\n\n\x1b[32mHello world!\x1b[0m\n\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
}
func TestPrettyFormatViolationInPrettyFormat(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
var violation domain.Violation
violation.Title = "Test Title"
violation.RuleLink = "http://example.com/violation"
violation.ViolationType = "MUST"
violation.Decription = "Test Description"
t.Run("Converts violation to string in pretty format with just paths", func(t *testing.T) {
violation.Paths = []string{"/path/one", "/path/two"}
actualResult := prettyFormatter.formatViolation(&violation)
expectedResult := "\x1b[31mMUST\x1b[0m \x1b[31mTest Title\x1b[0m\n" +
"\tTest Description\n" +
"\thttp://example.com/violation\n" +
"\t\t/path/one\n" +
"\t\t/path/two\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
t.Run("Converts violation to string in pretty format with paths and pointer", func(t *testing.T) {
violation.Pointer = "/pointer/1"
actualResult := prettyFormatter.formatViolation(&violation)
expectedResult := "\x1b[31mMUST\x1b[0m \x1b[31mTest Title\x1b[0m\n" +
"\tTest Description\n" +
"\thttp://example.com/violation\n" +
"\t\t/pointer/1\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
t.Run("Converts violation to string in pretty format with just pointer", func(t *testing.T) {
violation.Paths = nil
actualResult := prettyFormatter.formatViolation(&violation)
expectedResult := "\x1b[31mMUST\x1b[0m \x1b[31mTest Title\x1b[0m\n" +
"\tTest Description\n" +
"\thttp://example.com/violation\n" +
"\t\t/pointer/1\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
}
func TestPrettyFormatHeader(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true))
t.Run("formatHeader adds a line", func(t *testing.T) {
actualResult := prettyFormatter.formatHeader("Header")
expectedResult := "Header\n======\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
t.Run("formatHeader returns empty string when no header", func(t *testing.T) {
result := prettyFormatter.formatHeader("")
tests.AssertEquals(t, "", result)
})
}
func TestTextFormatMessage(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(false))
t.Run("Formats nothing when no message", func(t *testing.T) {
actualResult := prettyFormatter.FormatServerMessage("")
tests.AssertEquals(t, "", actualResult)
})
t.Run("Formats message when specified", func(t *testing.T) {
actualResult := prettyFormatter.FormatMessage("Congratulations!")
expectedResult := "\nCongratulations!\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
}
func TestTextFormatServerMessage(t *testing.T) {
prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(false))
t.Run("Formats nothing when no message", func(t *testing.T) {
actualResult := prettyFormatter.FormatServerMessage("")
tests.AssertEquals(t, "", actualResult)
})
t.Run("Formats message when specified", func(t *testing.T) {
actualResult := prettyFormatter.FormatServerMessage("Hello world!")
expectedResult := "\n\nServer message:\n===============\n\nHello world!\n\n\n"
tests.AssertEquals(t, expectedResult, actualResult)
})
}