| 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 types |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestExtractCommentTags(t *testing.T) { |
| 26 |
commentLines := []string{ |
| 27 |
"Human comment that is ignored.", |
| 28 |
"+foo=value1", |
| 29 |
"+bar", |
| 30 |
"+foo=value2", |
| 31 |
"+baz=qux,zrb=true", |
| 32 |
} |
| 33 |
|
| 34 |
a := ExtractCommentTags("+", commentLines) |
| 35 |
e := map[string][]string{ |
| 36 |
"foo": {"value1", "value2"}, |
| 37 |
"bar": {""}, |
| 38 |
"baz": {"qux,zrb=true"}, |
| 39 |
} |
| 40 |
if !reflect.DeepEqual(e, a) { |
| 41 |
t.Errorf("Wanted %q, got %q", e, a) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestExtractSingleBoolCommentTag(t *testing.T) { |
| 46 |
commentLines := []string{ |
| 47 |
"Human comment that is ignored.", |
| 48 |
"+TRUE=true", |
| 49 |
"+FALSE=false", |
| 50 |
"+MULTI=true", |
| 51 |
"+MULTI=false", |
| 52 |
"+MULTI=multi", |
| 53 |
"+NOTBOOL=blue", |
| 54 |
"+EMPTY", |
| 55 |
} |
| 56 |
|
| 57 |
testCases := []struct { |
| 58 |
key string |
| 59 |
def bool |
| 60 |
exp bool |
| 61 |
err string // if set, ignore exp. |
| 62 |
}{ |
| 63 |
{"TRUE", false, true, ""}, |
| 64 |
{"FALSE", true, false, ""}, |
| 65 |
{"MULTI", false, true, ""}, |
| 66 |
{"NOTBOOL", false, true, "is not boolean"}, |
| 67 |
{"EMPTY", false, true, "is not boolean"}, |
| 68 |
{"ABSENT", true, true, ""}, |
| 69 |
{"ABSENT", false, false, ""}, |
| 70 |
} |
| 71 |
|
| 72 |
for i, tc := range testCases { |
| 73 |
v, err := ExtractSingleBoolCommentTag("+", tc.key, tc.def, commentLines) |
| 74 |
if err != nil && tc.err == "" { |
| 75 |
t.Errorf("[%d]: unexpected failure: %v", i, err) |
| 76 |
} else if err == nil && tc.err != "" { |
| 77 |
t.Errorf("[%d]: expected failure: %v", i, tc.err) |
| 78 |
} else if err != nil { |
| 79 |
if !strings.Contains(err.Error(), tc.err) { |
| 80 |
t.Errorf("[%d]: unexpected error: expected %q, got %q", i, tc.err, err) |
| 81 |
} |
| 82 |
} else if v != tc.exp { |
| 83 |
t.Errorf("[%d]: unexpected value: expected %t, got %t", i, tc.exp, v) |
| 84 |
} |
| 85 |
} |
| 86 |
} |