| 1 |
/* |
| 2 |
Copyright 2016 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 generators |
| 18 |
|
| 19 |
import ( |
| 20 |
"path/filepath" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/gengo/types" |
| 24 |
) |
| 25 |
|
| 26 |
func TestRemoveLastDir(t *testing.T) { |
| 27 |
table := map[string]struct{ newPath, removedDir string }{ |
| 28 |
"a/b/c": {"a/c", "b"}, |
| 29 |
} |
| 30 |
for slashInput, expect := range table { |
| 31 |
input := filepath.FromSlash(slashInput) |
| 32 |
|
| 33 |
gotPath, gotRemoved := removeLastDir(input) |
| 34 |
if e, a := filepath.FromSlash(expect.newPath), gotPath; e != a { |
| 35 |
t.Errorf("%v: wanted %v, got %v", input, e, a) |
| 36 |
} |
| 37 |
if e, a := filepath.FromSlash(expect.removedDir), gotRemoved; e != a { |
| 38 |
t.Errorf("%v: wanted %v, got %v", input, e, a) |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestInputIncludes(t *testing.T) { |
| 44 |
inputs := []string{"a", "a/b", "c/d/..."} |
| 45 |
|
| 46 |
if !inputIncludes(inputs, &types.Package{Path: "a"}) { |
| 47 |
t.Errorf("Expected 'a' to match") |
| 48 |
} |
| 49 |
if !inputIncludes(inputs, &types.Package{Path: "a/b"}) { |
| 50 |
t.Errorf("Expected 'a/b' to match") |
| 51 |
} |
| 52 |
if inputIncludes(inputs, &types.Package{Path: "a/b/c"}) { |
| 53 |
t.Errorf("Expected 'a/b/c' to not match") |
| 54 |
} |
| 55 |
if inputIncludes(inputs, &types.Package{Path: "c"}) { |
| 56 |
t.Errorf("Expected 'c' to not match") |
| 57 |
} |
| 58 |
if !inputIncludes(inputs, &types.Package{Path: "c/d"}) { |
| 59 |
t.Errorf("Expected 'c/d' to match") |
| 60 |
} |
| 61 |
if !inputIncludes(inputs, &types.Package{Path: "c/d/e"}) { |
| 62 |
t.Errorf("Expected 'c/d/e' to match via /... syntax") |
| 63 |
} |
| 64 |
if inputIncludes(inputs, &types.Package{Path: "z"}) { |
| 65 |
t.Errorf("Expected 'z' to not match") |
| 66 |
} |
| 67 |
if inputIncludes(inputs, &types.Package{Path: "c/z"}) { |
| 68 |
t.Errorf("Expected 'c/z' to not match") |
| 69 |
} |
| 70 |
} |