@@ -19,85 +19,106 @@ import (
1919func TestPluginContext (t * testing.T ) {
2020 pluginID := test .RandomID ()
2121
22- var ctx pluginContext
23- cases := []struct {
22+ var pCtx pluginContext
23+ tests := []struct {
2424 pluginCtx pluginContext
2525 expValue string
2626 call func () string
2727 }{
28- {pluginContext {
29- p : types.Plugin {ID : pluginID },
30- trunc : false ,
31- }, pluginID , ctx .ID },
32- {pluginContext {
33- p : types.Plugin {ID : pluginID },
34- trunc : true ,
35- }, formatter .TruncateID (pluginID ), ctx .ID },
36- {pluginContext {
37- p : types.Plugin {Name : "plugin_name" },
38- }, "plugin_name" , ctx .Name },
39- {pluginContext {
40- p : types.Plugin {Config : types.PluginConfig {Description : "plugin_description" }},
41- }, "plugin_description" , ctx .Description },
28+ {
29+ pluginCtx : pluginContext {
30+ p : types.Plugin {ID : pluginID },
31+ trunc : false ,
32+ },
33+ expValue : pluginID ,
34+ call : pCtx .ID ,
35+ },
36+ {
37+ pluginCtx : pluginContext {
38+ p : types.Plugin {ID : pluginID },
39+ trunc : true ,
40+ },
41+ expValue : formatter .TruncateID (pluginID ),
42+ call : pCtx .ID ,
43+ },
44+ {
45+ pluginCtx : pluginContext {
46+ p : types.Plugin {Name : "plugin_name" },
47+ },
48+ expValue : "plugin_name" ,
49+ call : pCtx .Name ,
50+ },
51+ {
52+ pluginCtx : pluginContext {
53+ p : types.Plugin {Config : types.PluginConfig {Description : "plugin_description" }},
54+ },
55+ expValue : "plugin_description" ,
56+ call : pCtx .Description ,
57+ },
4258 }
4359
44- for _ , c := range cases {
45- ctx = c .pluginCtx
46- v := c .call ()
60+ for _ , tc := range tests {
61+ pCtx = tc .pluginCtx
62+ v := tc .call ()
4763 if strings .Contains (v , "," ) {
48- test .CompareMultipleValues (t , v , c .expValue )
49- } else if v != c .expValue {
50- t .Fatalf ("Expected %s, was %s\n " , c .expValue , v )
64+ test .CompareMultipleValues (t , v , tc .expValue )
65+ } else if v != tc .expValue {
66+ t .Fatalf ("Expected %s, was %s\n " , tc .expValue , v )
5167 }
5268 }
5369}
5470
5571func TestPluginContextWrite (t * testing.T ) {
56- cases := []struct {
72+ tests := []struct {
73+ doc string
5774 context formatter.Context
5875 expected string
5976 }{
60- // Errors
6177 {
62- formatter.Context {Format : "{{InvalidFunction}}" },
63- `template parsing error: template: :1: function "InvalidFunction" not defined` ,
78+ doc : "invalid function" ,
79+ context : formatter.Context {Format : "{{InvalidFunction}}" },
80+ expected : `template parsing error: template: :1: function "InvalidFunction" not defined` ,
6481 },
6582 {
66- formatter.Context {Format : "{{nil}}" },
67- `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command` ,
83+ doc : "nil template" ,
84+ context : formatter.Context {Format : "{{nil}}" },
85+ expected : `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command` ,
6886 },
69- // Table format
7087 {
71- formatter.Context {Format : NewFormat ("table" , false )},
72- `ID NAME DESCRIPTION ENABLED
88+ doc : "table format" ,
89+ context : formatter.Context {Format : NewFormat ("table" , false )},
90+ expected : `ID NAME DESCRIPTION ENABLED
7391pluginID1 foobar_baz description 1 true
7492pluginID2 foobar_bar description 2 false
7593` ,
7694 },
7795 {
78- formatter.Context {Format : NewFormat ("table" , true )},
79- `pluginID1
96+ doc : "table format, quiet" ,
97+ context : formatter.Context {Format : NewFormat ("table" , true )},
98+ expected : `pluginID1
8099pluginID2
81100` ,
82101 },
83102 {
84- formatter.Context {Format : NewFormat ("table {{.Name}}" , false )},
85- `NAME
103+ doc : "table format name col" ,
104+ context : formatter.Context {Format : NewFormat ("table {{.Name}}" , false )},
105+ expected : `NAME
86106foobar_baz
87107foobar_bar
88108` ,
89109 },
90110 {
91- formatter.Context {Format : NewFormat ("table {{.Name}}" , true )},
92- `NAME
111+ doc : "table format name col, quiet" ,
112+ context : formatter.Context {Format : NewFormat ("table {{.Name}}" , true )},
113+ expected : `NAME
93114foobar_baz
94115foobar_bar
95116` ,
96117 },
97- // Raw Format
98118 {
99- formatter.Context {Format : NewFormat ("raw" , false )},
100- `plugin_id: pluginID1
119+ doc : "raw format" ,
120+ context : formatter.Context {Format : NewFormat ("raw" , false )},
121+ expected : `plugin_id: pluginID1
101122name: foobar_baz
102123description: description 1
103124enabled: true
@@ -110,15 +131,16 @@ enabled: false
110131` ,
111132 },
112133 {
113- formatter.Context {Format : NewFormat ("raw" , true )},
114- `plugin_id: pluginID1
134+ doc : "raw format, quiet" ,
135+ context : formatter.Context {Format : NewFormat ("raw" , true )},
136+ expected : `plugin_id: pluginID1
115137plugin_id: pluginID2
116138` ,
117139 },
118- // Custom Format
119140 {
120- formatter.Context {Format : NewFormat ("{{.Name}}" , false )},
121- `foobar_baz
141+ doc : "custom format" ,
142+ context : formatter.Context {Format : NewFormat ("{{.Name}}" , false )},
143+ expected : `foobar_baz
122144foobar_bar
123145` ,
124146 },
@@ -129,8 +151,8 @@ foobar_bar
129151 {ID : "pluginID2" , Name : "foobar_bar" , Config : types.PluginConfig {Description : "description 2" }, Enabled : false },
130152 }
131153
132- for _ , tc := range cases {
133- t .Run (string ( tc .context . Format ) , func (t * testing.T ) {
154+ for _ , tc := range tests {
155+ t .Run (tc .doc , func (t * testing.T ) {
134156 var out bytes.Buffer
135157 tc .context .Output = & out
136158
0 commit comments