@@ -7,6 +7,7 @@ package main
7
7
import (
8
8
"bytes"
9
9
"testing"
10
+ "text/tabwriter"
10
11
11
12
"strings"
12
13
@@ -32,36 +33,190 @@ func TestStatusFormatVersion(t *testing.T) {
32
33
}
33
34
34
35
func TestBasicLine (t * testing.T ) {
35
-
36
36
project := dep.Project {}
37
+ aSemverConstraint , _ := gps .NewSemverConstraint ("1.2.3" )
37
38
38
- var tests = []struct {
39
- status BasicStatus
40
- expected string
39
+ tests := []struct {
40
+ name string
41
+ status BasicStatus
42
+ wantDotStatus []string
43
+ wantJSONStatus []string
44
+ wantTableStatus []string
41
45
}{
42
- {BasicStatus {
43
- Version : nil ,
44
- Revision : gps .Revision ("flooboofoobooo" ),
45
- }, `[label="\nflooboo"];` },
46
- {BasicStatus {
47
- Version : gps .NewVersion ("1.0.0" ),
48
- Revision : gps .Revision ("flooboofoobooo" ),
49
- }, `[label="\n1.0.0"];` },
46
+ {
47
+ name : "BasicStatus with ProjectRoot only" ,
48
+ status : BasicStatus {
49
+ ProjectRoot : "github.com/foo/bar" ,
50
+ },
51
+ wantDotStatus : []string {`[label="github.com/foo/bar"];` },
52
+ wantJSONStatus : []string {`"Version":""` , `"Revision":""` },
53
+ wantTableStatus : []string {`github.com/foo/bar 0` },
54
+ },
55
+ {
56
+ name : "BasicStatus with Revision" ,
57
+ status : BasicStatus {
58
+ ProjectRoot : "github.com/foo/bar" ,
59
+ Revision : gps .Revision ("flooboofoobooo" ),
60
+ },
61
+ wantDotStatus : []string {`[label="github.com/foo/bar\nflooboo"];` },
62
+ wantJSONStatus : []string {`"Version":""` , `"Revision":"flooboofoobooo"` , `"Constraint":""` },
63
+ wantTableStatus : []string {`github.com/foo/bar flooboo 0` },
64
+ },
65
+ {
66
+ name : "BasicStatus with Version and Revision" ,
67
+ status : BasicStatus {
68
+ ProjectRoot : "github.com/foo/bar" ,
69
+ Version : gps .NewVersion ("1.0.0" ),
70
+ Revision : gps .Revision ("flooboofoobooo" ),
71
+ },
72
+ wantDotStatus : []string {`[label="github.com/foo/bar\n1.0.0"];` },
73
+ wantJSONStatus : []string {`"Version":"1.0.0"` , `"Revision":"flooboofoobooo"` , `"Constraint":""` },
74
+ wantTableStatus : []string {`github.com/foo/bar 1.0.0 flooboo 0` },
75
+ },
76
+ {
77
+ name : "BasicStatus with Constraint, Version and Revision" ,
78
+ status : BasicStatus {
79
+ ProjectRoot : "github.com/foo/bar" ,
80
+ Constraint : aSemverConstraint ,
81
+ Version : gps .NewVersion ("1.0.0" ),
82
+ Revision : gps .Revision ("revxyz" ),
83
+ },
84
+ wantDotStatus : []string {`[label="github.com/foo/bar\n1.0.0"];` },
85
+ wantJSONStatus : []string {`"Revision":"revxyz"` , `"Constraint":"1.2.3"` , `"Version":"1.0.0"` },
86
+ wantTableStatus : []string {`github.com/foo/bar 1.2.3 1.0.0 revxyz 0` },
87
+ },
50
88
}
51
89
52
90
for _ , test := range tests {
53
- var buf bytes.Buffer
91
+ t .Run (test .name , func (t * testing.T ) {
92
+ var buf bytes.Buffer
54
93
55
- out := & dotOutput {
56
- p : & project ,
57
- w : & buf ,
58
- }
59
- out .BasicHeader ()
60
- out .BasicLine (& test .status )
61
- out .BasicFooter ()
94
+ dotout := & dotOutput {
95
+ p : & project ,
96
+ w : & buf ,
97
+ }
98
+ dotout .BasicHeader ()
99
+ dotout .BasicLine (& test .status )
100
+ dotout .BasicFooter ()
62
101
63
- if ok := strings .Contains (buf .String (), test .expected ); ! ok {
64
- t .Fatalf ("Did not find expected node label: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), test .status )
65
- }
102
+ for _ , wantStatus := range test .wantDotStatus {
103
+ if ok := strings .Contains (buf .String (), wantStatus ); ! ok {
104
+ t .Errorf ("Did not find expected node status: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), wantStatus )
105
+ }
106
+ }
107
+
108
+ buf .Reset ()
109
+
110
+ jsonout := & jsonOutput {w : & buf }
111
+
112
+ jsonout .BasicHeader ()
113
+ jsonout .BasicLine (& test .status )
114
+ jsonout .BasicFooter ()
115
+
116
+ for _ , wantStatus := range test .wantJSONStatus {
117
+ if ok := strings .Contains (buf .String (), wantStatus ); ! ok {
118
+ t .Errorf ("Did not find expected JSON status: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), wantStatus )
119
+ }
120
+ }
121
+
122
+ buf .Reset ()
123
+
124
+ tabw := tabwriter .NewWriter (& buf , 0 , 4 , 2 , ' ' , 0 )
125
+
126
+ tableout := & tableOutput {w : tabw }
127
+
128
+ tableout .BasicHeader ()
129
+ tableout .BasicLine (& test .status )
130
+ tableout .BasicFooter ()
131
+
132
+ for _ , wantStatus := range test .wantTableStatus {
133
+ if ok := strings .Contains (buf .String (), wantStatus ); ! ok {
134
+ t .Errorf ("Did not find expected Table status: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), wantStatus )
135
+ }
136
+ }
137
+ })
138
+ }
139
+ }
140
+
141
+ func TestBasicStatusGetConsolidatedConstraint (t * testing.T ) {
142
+ aSemverConstraint , _ := gps .NewSemverConstraint ("1.2.1" )
143
+
144
+ testCases := []struct {
145
+ name string
146
+ basicStatus BasicStatus
147
+ wantConstraint string
148
+ }{
149
+ {
150
+ name : "empty BasicStatus" ,
151
+ basicStatus : BasicStatus {},
152
+ wantConstraint : "" ,
153
+ },
154
+ {
155
+ name : "BasicStatus with Any Constraint" ,
156
+ basicStatus : BasicStatus {
157
+ Constraint : gps .Any (),
158
+ },
159
+ wantConstraint : "*" ,
160
+ },
161
+ {
162
+ name : "BasicStatus with Semver Constraint" ,
163
+ basicStatus : BasicStatus {
164
+ Constraint : aSemverConstraint ,
165
+ },
166
+ wantConstraint : "1.2.1" ,
167
+ },
168
+ {
169
+ name : "BasicStatus with Override" ,
170
+ basicStatus : BasicStatus {
171
+ Constraint : aSemverConstraint ,
172
+ hasOverride : true ,
173
+ },
174
+ wantConstraint : "1.2.1 (override)" ,
175
+ },
176
+ }
177
+
178
+ for _ , tc := range testCases {
179
+ t .Run (tc .name , func (t * testing.T ) {
180
+ if tc .basicStatus .getConsolidatedConstraint () != tc .wantConstraint {
181
+ t .Errorf ("unexpected consolidated constraint: \n \t (GOT) %v \n \t (WNT) %v" , tc .basicStatus .getConsolidatedConstraint (), tc .wantConstraint )
182
+ }
183
+ })
184
+ }
185
+ }
186
+
187
+ func TestBasicStatusGetConsolidatedVersion (t * testing.T ) {
188
+ testCases := []struct {
189
+ name string
190
+ basicStatus BasicStatus
191
+ wantVersion string
192
+ }{
193
+ {
194
+ name : "empty BasicStatus" ,
195
+ basicStatus : BasicStatus {},
196
+ wantVersion : "" ,
197
+ },
198
+ {
199
+ name : "BasicStatus with Version and Revision" ,
200
+ basicStatus : BasicStatus {
201
+ Version : gps .NewVersion ("1.0.0" ),
202
+ Revision : gps .Revision ("revxyz" ),
203
+ },
204
+ wantVersion : "1.0.0" ,
205
+ },
206
+ {
207
+ name : "BasicStatus with only Revision" ,
208
+ basicStatus : BasicStatus {
209
+ Revision : gps .Revision ("revxyz" ),
210
+ },
211
+ wantVersion : "revxyz" ,
212
+ },
213
+ }
214
+
215
+ for _ , tc := range testCases {
216
+ t .Run (tc .name , func (t * testing.T ) {
217
+ if tc .basicStatus .getConsolidatedVersion () != tc .wantVersion {
218
+ t .Errorf ("unexpected consolidated version: \n \t (GOT) %v \n \t (WNT) %v" , tc .basicStatus .getConsolidatedVersion (), tc .wantVersion )
219
+ }
220
+ })
66
221
}
67
222
}
0 commit comments