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