github.com/oam-dev/kubevela@v1.9.11/references/cuegen/option_test.go (about) 1 /* 2 Copyright 2023 The KubeVela 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 cuegen 18 19 import ( 20 goast "go/ast" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestWithAnyTypes(t *testing.T) { 27 tests := []struct { 28 name string 29 opts []Option 30 extra map[string]Type 31 }{ 32 { 33 name: "default", 34 opts: nil, 35 extra: map[string]Type{}, 36 }, 37 { 38 name: "single", 39 opts: []Option{WithTypes(map[string]Type{ 40 "foo": TypeAny, 41 "bar": TypeEllipsis, 42 })}, 43 extra: map[string]Type{"foo": TypeAny, "bar": TypeEllipsis}, 44 }, 45 { 46 name: "multiple", 47 opts: []Option{WithTypes(map[string]Type{ 48 "foo": TypeAny, 49 "bar": TypeEllipsis, 50 }), WithTypes(map[string]Type{ 51 "baz": TypeEllipsis, 52 "qux": TypeAny, 53 })}, 54 extra: map[string]Type{ 55 "foo": TypeAny, 56 "bar": TypeEllipsis, 57 "baz": TypeEllipsis, 58 "qux": TypeAny, 59 }, 60 }, 61 } 62 63 for _, tt := range tests { 64 opts := options{types: map[string]Type{}} 65 for _, opt := range tt.opts { 66 opt(&opts) 67 } 68 assert.Equal(t, opts.types, tt.extra, tt.name) 69 } 70 } 71 72 func TestWithNullable(t *testing.T) { 73 tests := []struct { 74 name string 75 opts []Option 76 want bool 77 }{ 78 {name: "default", opts: nil, want: false}, 79 {name: "true", opts: []Option{WithNullable()}, want: true}, 80 } 81 82 for _, tt := range tests { 83 opts := options{nullable: false} 84 for _, opt := range tt.opts { 85 opt(&opts) 86 } 87 assert.Equal(t, opts.nullable, tt.want, tt.name) 88 } 89 } 90 91 func TestWithTypeFilter(t *testing.T) { 92 tests := []struct { 93 name string 94 opts []Option 95 true []string 96 false []string 97 }{ 98 { 99 name: "default", 100 opts: nil, 101 true: []string{"foo", "bar"}, 102 false: []string{}, 103 }, 104 { 105 name: "nil", 106 opts: []Option{WithTypeFilter(nil)}, 107 true: []string{"foo", "bar"}, 108 }, 109 { 110 name: "single", 111 opts: []Option{WithTypeFilter(func(typ *goast.TypeSpec) bool { return typ.Name.Name == "foo" })}, 112 true: []string{"foo"}, 113 false: []string{"bar", "baz"}, 114 }, 115 { 116 name: "multiple", 117 opts: []Option{WithTypeFilter(func(typ *goast.TypeSpec) bool { return typ.Name.Name == "foo" }), 118 WithTypeFilter(func(typ *goast.TypeSpec) bool { return typ.Name.Name == "bar" })}, 119 true: []string{"bar"}, 120 false: []string{"foo", "baz"}, 121 }, 122 } 123 124 for _, tt := range tests { 125 opts := options{typeFilter: func(_ *goast.TypeSpec) bool { return true }} 126 for _, opt := range tt.opts { 127 if opt != nil { 128 opt(&opts) 129 } 130 } 131 for _, typ := range tt.true { 132 assert.True(t, opts.typeFilter(&goast.TypeSpec{Name: &goast.Ident{Name: typ}}), tt.name) 133 } 134 for _, typ := range tt.false { 135 assert.False(t, opts.typeFilter(&goast.TypeSpec{Name: &goast.Ident{Name: typ}}), tt.name) 136 } 137 } 138 } 139 140 func TestDefaultOptions(t *testing.T) { 141 opts := newDefaultOptions() 142 143 assert.Equal(t, opts.types, map[string]Type{ 144 "map[string]interface{}": TypeEllipsis, "map[string]any": TypeEllipsis, 145 "interface{}": TypeAny, "any": TypeAny, 146 }) 147 assert.Equal(t, opts.nullable, false) 148 // assert can't compare function 149 assert.True(t, opts.typeFilter(nil)) 150 }