@@ -13,6 +13,7 @@ import (
13
13
"strings"
14
14
"testing"
15
15
"text/tabwriter"
16
+ "text/template"
16
17
17
18
"github.com/golang/dep"
18
19
"github.com/golang/dep/gps"
@@ -41,31 +42,40 @@ func TestBasicLine(t *testing.T) {
41
42
project := dep.Project {}
42
43
aSemverConstraint , _ := gps .NewSemverConstraint ("1.2.3" )
43
44
45
+ templateString := "PR:{{.ProjectRoot}}, Const:{{.Constraint}}, Ver:{{.Version}}, Rev:{{.Revision}}, Lat:{{.Latest}}, PkgCt:{{.PackageCount}}"
46
+ equalityTestTemplate := `{{if eq .Constraint "1.2.3"}}Constraint is 1.2.3{{end}}|{{if eq .Version "flooboo"}}Version is flooboo{{end}}|{{if eq .Latest "unknown"}}Latest is unknown{{end}}`
47
+
44
48
tests := []struct {
45
- name string
46
- status BasicStatus
47
- wantDotStatus []string
48
- wantJSONStatus []string
49
- wantTableStatus []string
49
+ name string
50
+ status BasicStatus
51
+ wantDotStatus []string
52
+ wantJSONStatus []string
53
+ wantTableStatus []string
54
+ wantTemplateStatus []string
55
+ wantEqTemplateStatus []string
50
56
}{
51
57
{
52
58
name : "BasicStatus with ProjectRoot only" ,
53
59
status : BasicStatus {
54
60
ProjectRoot : "github.com/foo/bar" ,
55
61
},
56
- wantDotStatus : []string {`[label="github.com/foo/bar"];` },
57
- wantJSONStatus : []string {`"Version":""` , `"Revision":""` },
58
- wantTableStatus : []string {`github.com/foo/bar 0` },
62
+ wantDotStatus : []string {`[label="github.com/foo/bar"];` },
63
+ wantJSONStatus : []string {`"Version":""` , `"Revision":""` },
64
+ wantTableStatus : []string {`github.com/foo/bar 0` },
65
+ wantTemplateStatus : []string {`PR:github.com/foo/bar, Const:, Ver:, Rev:, Lat:, PkgCt:0` },
66
+ wantEqTemplateStatus : []string {`||` },
59
67
},
60
68
{
61
69
name : "BasicStatus with Revision" ,
62
70
status : BasicStatus {
63
71
ProjectRoot : "github.com/foo/bar" ,
64
72
Revision : gps .Revision ("flooboofoobooo" ),
65
73
},
66
- wantDotStatus : []string {`[label="github.com/foo/bar\nflooboo"];` },
67
- wantJSONStatus : []string {`"Version":""` , `"Revision":"flooboofoobooo"` , `"Constraint":""` },
68
- wantTableStatus : []string {`github.com/foo/bar flooboo 0` },
74
+ wantDotStatus : []string {`[label="github.com/foo/bar\nflooboo"];` },
75
+ wantJSONStatus : []string {`"Version":""` , `"Revision":"flooboofoobooo"` , `"Constraint":""` },
76
+ wantTableStatus : []string {`github.com/foo/bar flooboo 0` },
77
+ wantTemplateStatus : []string {`PR:github.com/foo/bar, Const:, Ver:flooboo, Rev:flooboofoobooo, Lat:, PkgCt:0` },
78
+ wantEqTemplateStatus : []string {`|Version is flooboo|` },
69
79
},
70
80
{
71
81
name : "BasicStatus with Version and Revision" ,
@@ -74,9 +84,11 @@ func TestBasicLine(t *testing.T) {
74
84
Version : gps .NewVersion ("1.0.0" ),
75
85
Revision : gps .Revision ("flooboofoobooo" ),
76
86
},
77
- wantDotStatus : []string {`[label="github.com/foo/bar\n1.0.0"];` },
78
- wantJSONStatus : []string {`"Version":"1.0.0"` , `"Revision":"flooboofoobooo"` , `"Constraint":""` },
79
- wantTableStatus : []string {`github.com/foo/bar 1.0.0 flooboo 0` },
87
+ wantDotStatus : []string {`[label="github.com/foo/bar\n1.0.0"];` },
88
+ wantJSONStatus : []string {`"Version":"1.0.0"` , `"Revision":"flooboofoobooo"` , `"Constraint":""` },
89
+ wantTableStatus : []string {`github.com/foo/bar 1.0.0 flooboo 0` },
90
+ wantTemplateStatus : []string {`PR:github.com/foo/bar, Const:, Ver:1.0.0, Rev:flooboofoobooo, Lat:, PkgCt:0` },
91
+ wantEqTemplateStatus : []string {`||` },
80
92
},
81
93
{
82
94
name : "BasicStatus with Constraint, Version and Revision" ,
@@ -86,19 +98,23 @@ func TestBasicLine(t *testing.T) {
86
98
Version : gps .NewVersion ("1.0.0" ),
87
99
Revision : gps .Revision ("revxyz" ),
88
100
},
89
- wantDotStatus : []string {`[label="github.com/foo/bar\n1.0.0"];` },
90
- wantJSONStatus : []string {`"Revision":"revxyz"` , `"Constraint":"1.2.3"` , `"Version":"1.0.0"` },
91
- wantTableStatus : []string {`github.com/foo/bar 1.2.3 1.0.0 revxyz 0` },
101
+ wantDotStatus : []string {`[label="github.com/foo/bar\n1.0.0"];` },
102
+ wantJSONStatus : []string {`"Revision":"revxyz"` , `"Constraint":"1.2.3"` , `"Version":"1.0.0"` },
103
+ wantTableStatus : []string {`github.com/foo/bar 1.2.3 1.0.0 revxyz 0` },
104
+ wantTemplateStatus : []string {`PR:github.com/foo/bar, Const:1.2.3, Ver:1.0.0, Rev:revxyz, Lat:, PkgCt:0` },
105
+ wantEqTemplateStatus : []string {`Constraint is 1.2.3||` },
92
106
},
93
107
{
94
108
name : "BasicStatus with update error" ,
95
109
status : BasicStatus {
96
110
ProjectRoot : "github.com/foo/bar" ,
97
111
hasError : true ,
98
112
},
99
- wantDotStatus : []string {`[label="github.com/foo/bar"];` },
100
- wantJSONStatus : []string {`"Version":""` , `"Revision":""` , `"Latest":"unknown"` },
101
- wantTableStatus : []string {`github.com/foo/bar unknown 0` },
113
+ wantDotStatus : []string {`[label="github.com/foo/bar"];` },
114
+ wantJSONStatus : []string {`"Version":""` , `"Revision":""` , `"Latest":"unknown"` },
115
+ wantTableStatus : []string {`github.com/foo/bar unknown 0` },
116
+ wantTemplateStatus : []string {`PR:github.com/foo/bar, Const:, Ver:, Rev:, Lat:unknown, PkgCt:0` },
117
+ wantEqTemplateStatus : []string {`||Latest is unknown` },
102
118
},
103
119
}
104
120
@@ -149,6 +165,33 @@ func TestBasicLine(t *testing.T) {
149
165
t .Errorf ("Did not find expected Table status: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), wantStatus )
150
166
}
151
167
}
168
+
169
+ buf .Reset ()
170
+ template , _ := template .New ("status" ).Parse (templateString )
171
+ templateout := & templateOutput {w : & buf , tmpl : template }
172
+ templateout .BasicHeader ()
173
+ templateout .BasicLine (& test .status )
174
+ templateout .BasicFooter ()
175
+
176
+ for _ , wantStatus := range test .wantTemplateStatus {
177
+ if ok := strings .Contains (buf .String (), wantStatus ); ! ok {
178
+ t .Errorf ("Did not find expected template status: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), wantStatus )
179
+ }
180
+ }
181
+
182
+ // The following test is to ensure that certain fields usable with string operations such as .eq
183
+ buf .Reset ()
184
+ template , _ = template .New ("status" ).Parse (equalityTestTemplate )
185
+ templateout = & templateOutput {w : & buf , tmpl : template }
186
+ templateout .BasicHeader ()
187
+ templateout .BasicLine (& test .status )
188
+ templateout .BasicFooter ()
189
+
190
+ for _ , wantStatus := range test .wantEqTemplateStatus {
191
+ if ok := strings .Contains (buf .String (), wantStatus ); ! ok {
192
+ t .Errorf ("Did not find expected template status: \n \t (GOT) %v \n \t (WNT) %v" , buf .String (), wantStatus )
193
+ }
194
+ }
152
195
})
153
196
}
154
197
}
0 commit comments