11package output
22
33import (
4+ "encoding/json"
5+ "strconv"
46 "testing"
7+
8+ "github.com/google/go-cmp/cmp"
59)
610
11+ func TestNewResult (t * testing.T ) {
12+ t .Parallel ()
13+
14+ tests := []struct {
15+ desc string
16+ input map [string ]any
17+ want Result
18+ wantErr bool
19+ }{
20+ {
21+ desc : "no metadata is an error" ,
22+ wantErr : true ,
23+ },
24+ {
25+ desc : "missing msg is an error" ,
26+ input : map [string ]any {},
27+ wantErr : true ,
28+ },
29+ {
30+ desc : "non-string msg is an error" ,
31+ input : map [string ]any {"msg" : 123 },
32+ wantErr : true ,
33+ },
34+ {
35+ desc : "msg only" ,
36+ input : map [string ]any {"msg" : "message" },
37+ want : Result {
38+ Message : "message" ,
39+ Metadata : make (map [string ]any ),
40+ },
41+ },
42+ {
43+ desc : "msg with location and metadata" ,
44+ input : map [string ]any {
45+ "msg" : "message" ,
46+ "_loc" : map [string ]any {
47+ "file" : "some_file.json" ,
48+ "line" : json .Number ("123" ),
49+ },
50+ "other" : "metadata" ,
51+ },
52+ want : Result {
53+ Message : "message" ,
54+ Location : & Location {
55+ File : "some_file.json" ,
56+ Line : json .Number ("123" ),
57+ },
58+ Metadata : map [string ]any {"other" : "metadata" },
59+ },
60+ },
61+ }
62+
63+ for _ , tc := range tests {
64+ t .Run (tc .desc , func (t * testing.T ) {
65+ t .Parallel ()
66+
67+ got , err := NewResult (tc .input )
68+ if gotErr := err != nil ; gotErr != tc .wantErr {
69+ t .Fatalf ("NewResult() error = %v, want %v" , err , tc .wantErr )
70+ }
71+ if tc .wantErr {
72+ return
73+ }
74+ if diff := cmp .Diff (got , tc .want ); diff != "" {
75+ t .Errorf ("NewResult() produced an unexpected diff:\n %s" , diff )
76+ }
77+ })
78+ }
79+ }
80+
781func TestCheckResultsHelpers (t * testing.T ) {
82+ t .Parallel ()
83+
884 tests := []struct {
985 desc string
1086 results CheckResults
@@ -70,6 +146,8 @@ func TestCheckResultsHelpers(t *testing.T) {
70146
71147 for _ , tc := range tests {
72148 t .Run (tc .desc , func (t * testing.T ) {
149+ t .Parallel ()
150+
73151 if gotFailure := tc .results .HasFailure (); gotFailure != tc .wantHasFailure {
74152 t .Errorf ("HasFailure() = %v, want %v" , gotFailure , tc .wantHasFailure )
75153 }
@@ -87,6 +165,8 @@ func TestCheckResultsHelpers(t *testing.T) {
87165}
88166
89167func TestExitCode (t * testing.T ) {
168+ t .Parallel ()
169+
90170 warning := CheckResult {
91171 Warnings : []Result {{}},
92172 }
@@ -110,15 +190,21 @@ func TestExitCode(t *testing.T) {
110190 {results : CheckResults {warning , failure }, expected : 1 },
111191 }
112192
113- for _ , testCase := range testCases {
114- actual := testCase .results .ExitCode ()
115- if actual != testCase .expected {
116- t .Errorf ("Unexpected error code. expected %v, actual %v" , testCase .expected , actual )
117- }
193+ for i , testCase := range testCases {
194+ t .Run (strconv .Itoa (i ), func (t * testing.T ) {
195+ t .Parallel ()
196+
197+ actual := testCase .results .ExitCode ()
198+ if actual != testCase .expected {
199+ t .Errorf ("Unexpected error code. expected %v, actual %v" , testCase .expected , actual )
200+ }
201+ })
118202 }
119203}
120204
121205func TestExitCodeFailOnWarn (t * testing.T ) {
206+ t .Parallel ()
207+
122208 warning := CheckResult {
123209 Warnings : []Result {{}},
124210 }
@@ -137,10 +223,14 @@ func TestExitCodeFailOnWarn(t *testing.T) {
137223 {results : CheckResults {warning , failure }, expected : 2 },
138224 }
139225
140- for _ , testCase := range testCases {
141- actual := testCase .results .ExitCodeFailOnWarn ()
142- if actual != testCase .expected {
143- t .Errorf ("Unexpected error code. expected %v, actual %v" , testCase .expected , actual )
144- }
226+ for i , testCase := range testCases {
227+ t .Run (strconv .Itoa (i ), func (t * testing.T ) {
228+ t .Parallel ()
229+
230+ actual := testCase .results .ExitCodeFailOnWarn ()
231+ if actual != testCase .expected {
232+ t .Errorf ("Unexpected error code. expected %v, actual %v" , testCase .expected , actual )
233+ }
234+ })
145235 }
146236}
0 commit comments