github.com/oam-dev/kubevela@v1.9.11/references/cuegen/util_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 gotypes "go/types" 21 "math" 22 "strconv" 23 "testing" 24 25 cueast "cuelang.org/go/cue/ast" 26 cuetoken "cuelang.org/go/cue/token" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestIdent(t *testing.T) { 31 tests := []struct { 32 name string 33 isDef bool 34 want string 35 }{ 36 {name: "test", isDef: true, want: "#test"}, 37 {name: "test", isDef: false, want: "test"}, 38 {name: "Test", isDef: true, want: "#Test"}, 39 {name: "Test", isDef: false, want: "Test"}, 40 } 41 42 for _, tt := range tests { 43 assert.Equal(t, tt.want, Ident(tt.name, tt.isDef).String()) 44 } 45 } 46 47 func TestBasicType(t *testing.T) { 48 tests := []struct { 49 name string 50 typ *gotypes.Basic 51 want string 52 }{ 53 {name: "uintptr", typ: gotypes.Typ[gotypes.Uintptr], want: "uint64"}, 54 {name: "byte", typ: gotypes.Typ[gotypes.Byte], want: "uint8"}, 55 {name: "int", typ: gotypes.Typ[gotypes.Int], want: "int"}, 56 {name: "int8", typ: gotypes.Typ[gotypes.Int8], want: "int8"}, 57 {name: "int16", typ: gotypes.Typ[gotypes.Int16], want: "int16"}, 58 {name: "int32", typ: gotypes.Typ[gotypes.Int32], want: "int32"}, 59 {name: "int64", typ: gotypes.Typ[gotypes.Int64], want: "int64"}, 60 {name: "uint", typ: gotypes.Typ[gotypes.Uint], want: "uint"}, 61 {name: "uint8", typ: gotypes.Typ[gotypes.Uint8], want: "uint8"}, 62 {name: "uint16", typ: gotypes.Typ[gotypes.Uint16], want: "uint16"}, 63 {name: "uint32", typ: gotypes.Typ[gotypes.Uint32], want: "uint32"}, 64 {name: "uint64", typ: gotypes.Typ[gotypes.Uint64], want: "uint64"}, 65 {name: "float32", typ: gotypes.Typ[gotypes.Float32], want: "float32"}, 66 {name: "float64", typ: gotypes.Typ[gotypes.Float64], want: "float64"}, 67 {name: "string", typ: gotypes.Typ[gotypes.String], want: "string"}, 68 {name: "bool", typ: gotypes.Typ[gotypes.Bool], want: "bool"}, 69 } 70 71 for _, tt := range tests { 72 assert.Equal(t, tt.want, basicType(tt.typ).(*cueast.Ident).String(), tt.name) 73 } 74 } 75 76 func TestBasicLabel(t *testing.T) { 77 overflowInt64 := strconv.FormatInt(math.MaxInt64, 10) + "0" 78 overflowUint64 := strconv.FormatUint(math.MaxUint64, 10) + "0" 79 overflowFloat64 := strconv.FormatFloat(math.MaxFloat64, 'f', -1, 64) + "0" 80 81 tests := []struct { 82 name string 83 typ *gotypes.Basic 84 v string 85 wantErr bool 86 want *cueast.BasicLit 87 }{ 88 {name: "int", typ: gotypes.Typ[gotypes.Int], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 89 {name: "int8", typ: gotypes.Typ[gotypes.Int8], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 90 {name: "int16", typ: gotypes.Typ[gotypes.Int16], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 91 {name: "int32", typ: gotypes.Typ[gotypes.Int32], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 92 {name: "int64", typ: gotypes.Typ[gotypes.Int64], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 93 {name: "uint", typ: gotypes.Typ[gotypes.Uint], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 94 {name: "uint8", typ: gotypes.Typ[gotypes.Uint8], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 95 {name: "uint16", typ: gotypes.Typ[gotypes.Uint16], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 96 {name: "uint32", typ: gotypes.Typ[gotypes.Uint32], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 97 {name: "uint64", typ: gotypes.Typ[gotypes.Uint64], v: "123", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.INT, Value: "123"}}, 98 {name: "float32", typ: gotypes.Typ[gotypes.Float32], v: "123.456", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.FLOAT, Value: "123.456"}}, 99 {name: "float64", typ: gotypes.Typ[gotypes.Float64], v: "123.456", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.FLOAT, Value: "123.456"}}, 100 {name: "string", typ: gotypes.Typ[gotypes.String], v: "abc", wantErr: false, want: &cueast.BasicLit{Kind: cuetoken.STRING, Value: `"abc"`}}, 101 {name: "bool", typ: gotypes.Typ[gotypes.Bool], v: "true", wantErr: false, want: cueast.NewBool(true)}, 102 {name: "bool", typ: gotypes.Typ[gotypes.Bool], v: "false", wantErr: false, want: cueast.NewBool(false)}, 103 {name: "int_error", typ: gotypes.Typ[gotypes.Int], v: "abc", wantErr: true}, 104 {name: "uint_error", typ: gotypes.Typ[gotypes.Uint], v: "abc", wantErr: true}, 105 {name: "float_error", typ: gotypes.Typ[gotypes.Float64], v: "abc", wantErr: true}, 106 {name: "bool_error", typ: gotypes.Typ[gotypes.Bool], v: "abc", wantErr: true}, 107 {name: "type_error", typ: gotypes.Typ[gotypes.Complex64], v: "abc", wantErr: true}, 108 {name: "int_overflow", typ: gotypes.Typ[gotypes.Int], v: overflowInt64, wantErr: true}, 109 {name: "uint_overflow", typ: gotypes.Typ[gotypes.Uint], v: overflowUint64, wantErr: true}, 110 {name: "int64_overflow", typ: gotypes.Typ[gotypes.Int64], v: overflowInt64, wantErr: true}, 111 {name: "uint64_overflow", typ: gotypes.Typ[gotypes.Uint64], v: overflowUint64, wantErr: true}, 112 {name: "float64_overflow", typ: gotypes.Typ[gotypes.Float64], v: overflowFloat64, wantErr: true}, 113 } 114 115 for _, tt := range tests { 116 got, err := basicLabel(tt.typ, tt.v) 117 if tt.wantErr { 118 assert.Error(t, err, tt.name) 119 } else { 120 assert.NoError(t, err, tt.name) 121 assert.Equal(t, tt.want, got, tt.name) 122 } 123 } 124 }