| 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 namer |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/gengo/v2/types" |
| 24 |
) |
| 25 |
|
| 26 |
func TestNameStrategy(t *testing.T) { |
| 27 |
u := types.Universe{} |
| 28 |
|
| 29 |
// Add some types. |
| 30 |
base := u.Type(types.Name{Package: "foo/bar", Name: "Baz"}) |
| 31 |
base.Kind = types.Struct |
| 32 |
|
| 33 |
tmp := u.Type(types.Name{Package: "", Name: "[]bar.Baz"}) |
| 34 |
tmp.Kind = types.Slice |
| 35 |
tmp.Elem = base |
| 36 |
|
| 37 |
tmp = u.Type(types.Name{Package: "", Name: "map[string]bar.Baz"}) |
| 38 |
tmp.Kind = types.Map |
| 39 |
tmp.Key = types.String |
| 40 |
tmp.Elem = base |
| 41 |
|
| 42 |
tmp = u.Type(types.Name{Package: "foo/other", Name: "Baz"}) |
| 43 |
tmp.Kind = types.Struct |
| 44 |
tmp.Members = []types.Member{{ |
| 45 |
Embedded: true, |
| 46 |
Type: base, |
| 47 |
}} |
| 48 |
|
| 49 |
tmp = u.Type(types.Name{Package: "", Name: "chan Baz"}) |
| 50 |
tmp.Kind = types.Chan |
| 51 |
tmp.Elem = base |
| 52 |
|
| 53 |
tmp = u.Type(types.Name{Package: "", Name: "[4]Baz"}) |
| 54 |
tmp.Kind = types.Array |
| 55 |
tmp.Elem = base |
| 56 |
tmp.Len = 4 |
| 57 |
|
| 58 |
u.Type(types.Name{Package: "", Name: "string"}) |
| 59 |
|
| 60 |
o := Orderer{NewPublicNamer(0)} |
| 61 |
order := o.OrderUniverse(u) |
| 62 |
orderedNames := make([]string, len(order)) |
| 63 |
for i, t := range order { |
| 64 |
orderedNames[i] = o.Name(t) |
| 65 |
} |
| 66 |
expect := []string{"Array4Baz", "Baz", "Baz", "ChanBaz", "MapStringToBaz", "SliceBaz", "String"} |
| 67 |
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) { |
| 68 |
t.Errorf("Wanted %#v, got %#v", e, a) |
| 69 |
} |
| 70 |
|
| 71 |
o = Orderer{NewRawNamer("my/package", nil)} |
| 72 |
order = o.OrderUniverse(u) |
| 73 |
orderedNames = make([]string, len(order)) |
| 74 |
for i, t := range order { |
| 75 |
orderedNames[i] = o.Name(t) |
| 76 |
} |
| 77 |
|
| 78 |
expect = []string{"[4]bar.Baz", "[]bar.Baz", "bar.Baz", "chan bar.Baz", "map[string]bar.Baz", "other.Baz", "string"} |
| 79 |
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) { |
| 80 |
t.Errorf("Wanted %#v, got %#v", e, a) |
| 81 |
} |
| 82 |
|
| 83 |
o = Orderer{NewRawNamer("foo/bar", nil)} |
| 84 |
order = o.OrderUniverse(u) |
| 85 |
orderedNames = make([]string, len(order)) |
| 86 |
for i, t := range order { |
| 87 |
orderedNames[i] = o.Name(t) |
| 88 |
} |
| 89 |
|
| 90 |
expect = []string{"Baz", "[4]Baz", "[]Baz", "chan Baz", "map[string]Baz", "other.Baz", "string"} |
| 91 |
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) { |
| 92 |
t.Errorf("Wanted %#v, got %#v", e, a) |
| 93 |
} |
| 94 |
|
| 95 |
o = Orderer{NewPublicNamer(1)} |
| 96 |
order = o.OrderUniverse(u) |
| 97 |
orderedNames = make([]string, len(order)) |
| 98 |
for i, t := range order { |
| 99 |
orderedNames[i] = o.Name(t) |
| 100 |
} |
| 101 |
expect = []string{"Array4BarBaz", "BarBaz", "ChanBarBaz", "MapStringToBarBaz", "OtherBaz", "SliceBarBaz", "String"} |
| 102 |
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) { |
| 103 |
t.Errorf("Wanted %#v, got %#v", e, a) |
| 104 |
} |
| 105 |
} |