| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package gengo |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
) |
| 26 |
|
| 27 |
func TestExtractCommentTags(t *testing.T) { |
| 28 |
commentLines := []string{ |
| 29 |
"Human comment that is ignored.", |
| 30 |
"+foo=value1", |
| 31 |
"+bar", |
| 32 |
"+foo=value2", |
| 33 |
"+baz=qux,zrb=true", |
| 34 |
"+bip=\"value3\"", |
| 35 |
} |
| 36 |
|
| 37 |
a := ExtractCommentTags("+", commentLines) |
| 38 |
e := map[string][]string{ |
| 39 |
"foo": {"value1", "value2"}, |
| 40 |
"bar": {""}, |
| 41 |
"baz": {"qux,zrb=true"}, |
| 42 |
"bip": {`"value3"`}, |
| 43 |
} |
| 44 |
if !reflect.DeepEqual(e, a) { |
| 45 |
t.Errorf("Wrong result:\n%v", cmp.Diff(e, a)) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestExtractSingleBoolCommentTag(t *testing.T) { |
| 50 |
commentLines := []string{ |
| 51 |
"Human comment that is ignored.", |
| 52 |
"+TRUE=true", |
| 53 |
"+FALSE=false", |
| 54 |
"+MULTI=true", |
| 55 |
"+MULTI=false", |
| 56 |
"+MULTI=multi", |
| 57 |
"+NOTBOOL=blue", |
| 58 |
"+EMPTY", |
| 59 |
} |
| 60 |
|
| 61 |
testCases := []struct { |
| 62 |
key string |
| 63 |
def bool |
| 64 |
exp bool |
| 65 |
err string // if set, ignore exp. |
| 66 |
}{ |
| 67 |
{"TRUE", false, true, ""}, |
| 68 |
{"FALSE", true, false, ""}, |
| 69 |
{"MULTI", false, true, ""}, |
| 70 |
{"NOTBOOL", false, true, "is not boolean"}, |
| 71 |
{"EMPTY", false, true, "is not boolean"}, |
| 72 |
{"ABSENT", true, true, ""}, |
| 73 |
{"ABSENT", false, false, ""}, |
| 74 |
} |
| 75 |
|
| 76 |
for i, tc := range testCases { |
| 77 |
v, err := ExtractSingleBoolCommentTag("+", tc.key, tc.def, commentLines) |
| 78 |
if err != nil && tc.err == "" { |
| 79 |
t.Errorf("[%d]: unexpected failure: %v", i, err) |
| 80 |
} else if err == nil && tc.err != "" { |
| 81 |
t.Errorf("[%d]: expected failure: %v", i, tc.err) |
| 82 |
} else if err != nil { |
| 83 |
if !strings.Contains(err.Error(), tc.err) { |
| 84 |
t.Errorf("[%d]: unexpected error: expected %q, got %q", i, tc.err, err) |
| 85 |
} |
| 86 |
} else if v != tc.exp { |
| 87 |
t.Errorf("[%d]: unexpected value: expected %t, got %t", i, tc.exp, v) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestExtractExtendedCommentTags(t *testing.T) { |
| 93 |
mktags := func(t ...Tag) []Tag { return t } |
| 94 |
mkstrs := func(s ...string) []string { return s } |
| 95 |
|
| 96 |
cases := []struct { |
| 97 |
name string |
| 98 |
comments []string |
| 99 |
prefixes []string |
| 100 |
expect map[string][]Tag |
| 101 |
}{{ |
| 102 |
name: "no args", |
| 103 |
comments: []string{ |
| 104 |
"Human comment that is ignored", |
| 105 |
"+simpleNoVal", |
| 106 |
"+simpleWithVal=val", |
| 107 |
"+duplicateNoVal", |
| 108 |
"+duplicateNoVal", |
| 109 |
"+duplicateWithVal=val1", |
| 110 |
"+duplicateWithVal=val2", |
| 111 |
}, |
| 112 |
expect: map[string][]Tag{ |
| 113 |
"simpleNoVal": mktags(Tag{"simpleNoVal", nil, ""}), |
| 114 |
"simpleWithVal": mktags(Tag{"simpleWithVal", nil, "val"}), |
| 115 |
"duplicateNoVal": mktags( |
| 116 |
Tag{"duplicateNoVal", nil, ""}, |
| 117 |
Tag{"duplicateNoVal", nil, ""}), |
| 118 |
"duplicateWithVal": mktags( |
| 119 |
Tag{"duplicateWithVal", nil, "val1"}, |
| 120 |
Tag{"duplicateWithVal", nil, "val2"}), |
| 121 |
}, |
| 122 |
}, { |
| 123 |
name: "empty parens", |
| 124 |
comments: []string{ |
| 125 |
"Human comment that is ignored", |
| 126 |
"+simpleNoVal()", |
| 127 |
"+simpleWithVal()=val", |
| 128 |
"+duplicateNoVal()", |
| 129 |
"+duplicateNoVal()", |
| 130 |
"+duplicateWithVal()=val1", |
| 131 |
"+duplicateWithVal()=val2", |
| 132 |
}, |
| 133 |
expect: map[string][]Tag{ |
| 134 |
"simpleNoVal": mktags(Tag{"simpleNoVal", nil, ""}), |
| 135 |
"simpleWithVal": mktags(Tag{"simpleWithVal", nil, "val"}), |
| 136 |
"duplicateNoVal": mktags( |
| 137 |
Tag{"duplicateNoVal", nil, ""}, |
| 138 |
Tag{"duplicateNoVal", nil, ""}), |
| 139 |
"duplicateWithVal": mktags( |
| 140 |
Tag{"duplicateWithVal", nil, "val1"}, |
| 141 |
Tag{"duplicateWithVal", nil, "val2"}), |
| 142 |
}, |
| 143 |
}, { |
| 144 |
name: "mixed no args and empty parens", |
| 145 |
comments: []string{ |
| 146 |
"Human comment that is ignored", |
| 147 |
"+noVal", |
| 148 |
"+withVal=val1", |
| 149 |
"+noVal()", |
| 150 |
"+withVal()=val2", |
| 151 |
}, |
| 152 |
expect: map[string][]Tag{ |
| 153 |
"noVal": mktags( |
| 154 |
Tag{"noVal", nil, ""}, |
| 155 |
Tag{"noVal", nil, ""}), |
| 156 |
"withVal": mktags( |
| 157 |
Tag{"withVal", nil, "val1"}, |
| 158 |
Tag{"withVal", nil, "val2"}), |
| 159 |
}, |
| 160 |
}, { |
| 161 |
name: "with args", |
| 162 |
comments: []string{ |
| 163 |
"Human comment that is ignored", |
| 164 |
"+simpleNoVal(arg)", |
| 165 |
"+simpleWithVal(arg)=val", |
| 166 |
"+duplicateNoVal(arg1)", |
| 167 |
"+duplicateNoVal(arg2)", |
| 168 |
"+duplicateWithVal(arg1)=val1", |
| 169 |
"+duplicateWithVal(arg2)=val2", |
| 170 |
}, |
| 171 |
expect: map[string][]Tag{ |
| 172 |
"simpleNoVal": mktags(Tag{"simpleNoVal", mkstrs("arg"), ""}), |
| 173 |
"simpleWithVal": mktags(Tag{"simpleWithVal", mkstrs("arg"), "val"}), |
| 174 |
"duplicateNoVal": mktags( |
| 175 |
Tag{"duplicateNoVal", mkstrs("arg1"), ""}, |
| 176 |
Tag{"duplicateNoVal", mkstrs("arg2"), ""}), |
| 177 |
"duplicateWithVal": mktags( |
| 178 |
Tag{"duplicateWithVal", mkstrs("arg1"), "val1"}, |
| 179 |
Tag{"duplicateWithVal", mkstrs("arg2"), "val2"}), |
| 180 |
}, |
| 181 |
}, { |
| 182 |
name: "mixed no args and empty parens", |
| 183 |
comments: []string{ |
| 184 |
"Human comment that is ignored", |
| 185 |
"+noVal", |
| 186 |
"+withVal=val1", |
| 187 |
"+noVal(arg)", |
| 188 |
"+withVal(arg)=val2", |
| 189 |
}, |
| 190 |
expect: map[string][]Tag{ |
| 191 |
"noVal": mktags( |
| 192 |
Tag{"noVal", nil, ""}, |
| 193 |
Tag{"noVal", mkstrs("arg"), ""}), |
| 194 |
"withVal": mktags( |
| 195 |
Tag{"withVal", nil, "val1"}, |
| 196 |
Tag{"withVal", mkstrs("arg"), "val2"}), |
| 197 |
}, |
| 198 |
}, { |
| 199 |
name: "prefixes", |
| 200 |
comments: []string{ |
| 201 |
"Human comment that is ignored", |
| 202 |
"+pfx1Foo", |
| 203 |
"+pfx2Foo=val1", |
| 204 |
"+pfx3Bar", |
| 205 |
"+pfx4Bar=val", |
| 206 |
"+pfx1Foo(arg)", |
| 207 |
"+pfx2Foo(arg)=val2", |
| 208 |
"+pfx3Bar(arg)", |
| 209 |
"+pfx4Bar(arg)=val", |
| 210 |
}, |
| 211 |
prefixes: []string{"pfx1Foo", "pfx2Foo"}, |
| 212 |
expect: map[string][]Tag{ |
| 213 |
"pfx1Foo": mktags( |
| 214 |
Tag{"pfx1Foo", nil, ""}, |
| 215 |
Tag{"pfx1Foo", mkstrs("arg"), ""}), |
| 216 |
"pfx2Foo": mktags( |
| 217 |
Tag{"pfx2Foo", nil, "val1"}, |
| 218 |
Tag{"pfx2Foo", mkstrs("arg"), "val2"}), |
| 219 |
}, |
| 220 |
}} |
| 221 |
|
| 222 |
for _, tc := range cases { |
| 223 |
result, _ := ExtractFunctionStyleCommentTags("+", tc.prefixes, tc.comments) |
| 224 |
if !reflect.DeepEqual(result, tc.expect) { |
| 225 |
t.Errorf("case %q: wrong result:\n%v", tc.name, cmp.Diff(tc.expect, result)) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func TestParseTagKey(t *testing.T) { |
| 231 |
mkss := func(s ...string) []string { return s } |
| 232 |
|
| 233 |
cases := []struct { |
| 234 |
input string |
| 235 |
expectKey string |
| 236 |
expectArgs []string |
| 237 |
err bool |
| 238 |
}{ |
| 239 |
{"simple", "simple", nil, false}, |
| 240 |
{"parens()", "parens", nil, false}, |
| 241 |
{"withArgLower(arg)", "withArgLower", mkss("arg"), false}, |
| 242 |
{"withArgUpper(ARG)", "withArgUpper", mkss("ARG"), false}, |
| 243 |
{"withArgMixed(ArG)", "withArgMixed", mkss("ArG"), false}, |
| 244 |
{"withArgs(arg1, arg2)", "", nil, true}, |
| 245 |
{"trailingParen(arg))", "", nil, true}, |
| 246 |
{"trailingSpace(arg) ", "", nil, true}, |
| 247 |
{"argWithDash(arg-name) ", "", nil, true}, |
| 248 |
{"argWithUnder(arg_name) ", "", nil, true}, |
| 249 |
} |
| 250 |
for _, tc := range cases { |
| 251 |
key, args, err := parseTagKey(tc.input, nil) |
| 252 |
if err != nil && tc.err == false { |
| 253 |
t.Errorf("[%q]: expected success, got: %v", tc.input, err) |
| 254 |
continue |
| 255 |
} |
| 256 |
if err == nil { |
| 257 |
if tc.err == true { |
| 258 |
t.Errorf("[%q]: expected failure, got: %v(%v)", tc.input, key, args) |
| 259 |
continue |
| 260 |
} |
| 261 |
if key != tc.expectKey { |
| 262 |
t.Errorf("[%q]\nexpected key: %q, got: %q", tc.input, tc.expectKey, key) |
| 263 |
} |
| 264 |
if len(args) != len(tc.expectArgs) { |
| 265 |
t.Errorf("[%q]: expected %d args, got: %q", tc.input, len(tc.expectArgs), args) |
| 266 |
continue |
| 267 |
} |
| 268 |
for i := range tc.expectArgs { |
| 269 |
if want, got := tc.expectArgs[i], args[i]; got != want { |
| 270 |
t.Errorf("[%q]\nexpected %q, got %q", tc.input, want, got) |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func TestParseTagKeyWithTagNames(t *testing.T) { |
| 278 |
mkss := func(s ...string) []string { return s } |
| 279 |
|
| 280 |
cases := []struct { |
| 281 |
input string |
| 282 |
expectKey string |
| 283 |
expectArgs []string |
| 284 |
}{ |
| 285 |
{"name", "name", nil}, |
| 286 |
{"name()", "name", nil}, |
| 287 |
{"name(arg)", "name", mkss("arg")}, |
| 288 |
{"nameNoMatch", "", nil}, |
| 289 |
{"nameNoMatch()", "", nil}, |
| 290 |
{"nameNoMatch(arg)", "", nil}, |
| 291 |
} |
| 292 |
for _, tc := range cases { |
| 293 |
key, args, err := parseTagKey(tc.input, []string{"name"}) |
| 294 |
if err != nil { |
| 295 |
t.Errorf("[%q]: expected success, got: %v", tc.input, err) |
| 296 |
continue |
| 297 |
} |
| 298 |
if key != tc.expectKey { |
| 299 |
t.Errorf("[%q]\nexpected key: %q, got: %q", tc.input, tc.expectKey, key) |
| 300 |
} |
| 301 |
if len(args) != len(tc.expectArgs) { |
| 302 |
t.Errorf("[%q]: expected %d args, got: %q", tc.input, len(tc.expectArgs), args) |
| 303 |
continue |
| 304 |
} |
| 305 |
for i := range tc.expectArgs { |
| 306 |
if want, got := tc.expectArgs[i], args[i]; got != want { |
| 307 |
t.Errorf("[%q]\nexpected %q, got %q", tc.input, want, got) |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
func TestParseTagArgs(t *testing.T) { |
| 314 |
mkss := func(s ...string) []string { return s } |
| 315 |
|
| 316 |
cases := []struct { |
| 317 |
input string |
| 318 |
expect []string |
| 319 |
err bool |
| 320 |
}{ |
| 321 |
{")", nil, false}, |
| 322 |
{"lower)", mkss("lower"), false}, |
| 323 |
{"CAPITAL)", mkss("CAPITAL"), false}, |
| 324 |
{"MiXeD)", mkss("MiXeD"), false}, |
| 325 |
{"mIxEd)", mkss("mIxEd"), false}, |
| 326 |
{"_under)", nil, true}, |
| 327 |
{"has space", nil, true}, |
| 328 |
{"has-dash", nil, true}, |
| 329 |
{`"hasQuotes"`, nil, true}, |
| 330 |
{"multiple, args)", nil, true}, |
| 331 |
{"noClosingParen", nil, true}, |
| 332 |
{"extraParen))", nil, true}, |
| 333 |
{"trailingSpace) ", nil, true}, |
| 334 |
} |
| 335 |
for _, tc := range cases { |
| 336 |
ret, err := parseTagArgs(tc.input) |
| 337 |
if err != nil && tc.err == false { |
| 338 |
t.Errorf("[%q]: expected success, got: %v", tc.input, err) |
| 339 |
continue |
| 340 |
} |
| 341 |
if err == nil { |
| 342 |
if tc.err == true { |
| 343 |
t.Errorf("[%q]: expected failure, got: %q", tc.input, ret) |
| 344 |
continue |
| 345 |
} |
| 346 |
if len(ret) != len(tc.expect) { |
| 347 |
t.Errorf("[%q]: expected %d results, got: %q", tc.input, len(tc.expect), ret) |
| 348 |
continue |
| 349 |
} |
| 350 |
for i := range tc.expect { |
| 351 |
if want, got := tc.expect[i], ret[i]; got != want { |
| 352 |
t.Errorf("[%q]\nexpected %q, got %q", tc.input, want, got) |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |