cuelang.org/go@v0.13.0/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 17 package gocode 18 19 import ( 20 "bytes" 21 "regexp" 22 "strings" 23 "testing" 24 25 "cuelang.org/go/cue/errors" 26 "cuelang.org/go/encoding/gocode/testdata/pkg1" 27 "cuelang.org/go/encoding/gocode/testdata/pkg2" 28 ) 29 30 type validator interface { 31 Validate() error 32 } 33 34 func TestPackages(t *testing.T) { 35 36 testCases := []struct { 37 name string 38 value validator 39 want string 40 }{{ 41 name: "failing int", 42 value: pkg2.PickMe(4), 43 want: "invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x", 44 }, { 45 name: "failing field with validator", 46 value: &pkg1.OtherStruct{A: "car"}, 47 want: ` 48 2 errors in empty disjunction: 49 conflicting values null and {A:strings.ContainsAny("X"),P:"cuelang.org/go/encoding/gocode/testdata/pkg2".PickMe} (mismatched types null and struct): 50 pkg1/instance.cue:x:x 51 A: invalid value "car" (does not satisfy strings.ContainsAny("X")): 52 pkg1/instance.cue:x:x 53 _:1:4 54 pkg1/instance.cue:x:x 55 `, 56 }, { 57 name: "failing field of type int", 58 value: &pkg1.MyStruct{A: 11, B: "dog"}, 59 want: ` 60 2 errors in empty disjunction: 61 conflicting values null and {A:<=10,B:(=~"cat"|*"dog"),O?:OtherStruct,I:"cuelang.org/go/encoding/gocode/testdata/pkg2".ImportMe} (mismatched types null and struct): 62 pkg1/instance.cue:x:x 63 A: invalid value 11 (out of bound <=10): 64 pkg1/instance.cue:x:x`, 65 }, { 66 name: "failing nested struct ", 67 value: &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "car", P: 6}}, 68 want: ` 69 4 errors in empty disjunction: 70 conflicting values null and {A:<=10,B:(=~"cat"|*"dog"),O?:OtherStruct,I:"cuelang.org/go/encoding/gocode/testdata/pkg2".ImportMe} (mismatched types null and struct): 71 pkg1/instance.cue:x:x 72 O: 2 errors in empty disjunction: 73 O: conflicting values null and {A:strings.ContainsAny("X"),P:"cuelang.org/go/encoding/gocode/testdata/pkg2".PickMe} (mismatched types null and struct): 74 pkg1/instance.cue:x:x 75 O.A: invalid value "car" (does not satisfy strings.ContainsAny("X")): 76 pkg1/instance.cue:x:x 77 _:1:4 78 pkg1/instance.cue:x:x 79 `, 80 }, { 81 name: "fail nested struct of different package", 82 value: &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "X", P: 4}}, 83 want: ` 84 4 errors in empty disjunction: 85 conflicting values null and {A:<=10,B:(=~"cat"|*"dog"),O?:OtherStruct,I:"cuelang.org/go/encoding/gocode/testdata/pkg2".ImportMe} (mismatched types null and struct): 86 pkg1/instance.cue:x:x 87 O: 2 errors in empty disjunction: 88 O: conflicting values null and {A:strings.ContainsAny("X"),P:"cuelang.org/go/encoding/gocode/testdata/pkg2".PickMe} (mismatched types null and struct): 89 pkg1/instance.cue:x:x 90 O.P: invalid value 4 (out of bound >5): 91 pkg2/instance.cue:x:x 92 `, 93 }, { 94 name: "all good", 95 value: &pkg1.MyStruct{ 96 A: 5, 97 B: "dog", 98 I: &pkg2.ImportMe{A: 1000, B: "a"}, 99 }, 100 want: "nil", 101 }} 102 for _, tc := range testCases { 103 t.Run(tc.name, func(t *testing.T) { 104 got := strings.TrimSpace(errStr(tc.value.Validate())) 105 want := strings.TrimSpace(tc.want) 106 if got != want { 107 t.Errorf("got:\n%q\nwant:\n%q", got, want) 108 } 109 }) 110 } 111 } 112 113 func errStr(err error) string { 114 if err == nil { 115 return "nil" 116 } 117 buf := &bytes.Buffer{} 118 errors.Print(buf, err, nil) 119 r := regexp.MustCompile(`.cue:\d+:\d+`) 120 return r.ReplaceAllString(buf.String(), ".cue:x:x") 121 }