github.com/solo-io/cue@v0.4.7/encoding/gocode/gen_test.go (about) 1 // Copyright 2019 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build !gen 16 // +build !gen 17 18 package gocode 19 20 import ( 21 "strings" 22 "testing" 23 24 "github.com/solo-io/cue/encoding/gocode/testdata/pkg1" 25 "github.com/solo-io/cue/encoding/gocode/testdata/pkg2" 26 ) 27 28 func TestPackages(t *testing.T) { 29 t.Skip("fix error messages") // TODO(errors) 30 31 testCases := []struct { 32 name string 33 got error 34 want string 35 }{{ 36 name: "failing int", 37 got: func() error { 38 v := pkg2.PickMe(4) 39 return v.Validate() 40 }(), 41 want: "invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x", 42 }, { 43 name: "failing field with validator", 44 got: func() error { 45 v := &pkg1.OtherStruct{A: "car"} 46 return v.Validate() 47 }(), 48 want: "A: invalid value \"car\" (does not satisfy strings.ContainsAny(\"X\")):\n pkg1/instance.cue:x:x\nP: invalid value 0 (out of bound >5):\n pkg2/instance.cue:x:x", 49 }, { 50 name: "failing field of type int", 51 got: func() error { 52 v := &pkg1.MyStruct{A: 11, B: "dog"} 53 return v.Validate() 54 }(), 55 want: "A: invalid value 11 (out of bound <=10):\n pkg1/instance.cue:x:x", 56 }, { 57 name: "failing nested struct ", 58 got: func() error { 59 v := &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "car", P: 6}} 60 return v.Validate() 61 }(), 62 want: "O.A: invalid value \"car\" (does not satisfy strings.ContainsAny(\"X\")):\n pkg1/instance.cue:x:x", 63 }, { 64 name: "fail nested struct of different package", 65 got: func() error { 66 v := &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "X", P: 4}} 67 return v.Validate() 68 }(), 69 want: "O.P: invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x", 70 }, { 71 name: "all good", 72 got: func() error { 73 v := &pkg1.MyStruct{ 74 A: 5, 75 B: "dog", 76 I: &pkg2.ImportMe{A: 1000, B: "a"}, 77 } 78 return v.Validate() 79 }(), 80 want: "nil", 81 }} 82 for _, tc := range testCases { 83 t.Run(tc.name, func(t *testing.T) { 84 got := strings.TrimSpace(errStr(tc.got)) 85 want := tc.want 86 if got != want { 87 t.Errorf("got:\n%q\nwant:\n%q", got, want) 88 } 89 90 }) 91 } 92 }