blob: d1bb8c29909691bfd0eb6792055e6968d9a331c6 [file] [log] [blame]
Marcel van Lohuizen240a9952019-08-05 10:29:13 +02001// 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// +build !gen
16
17package gocode
18
19import (
20 "strings"
21 "testing"
22
23 "cuelang.org/go/encoding/gocode/testdata/pkg1"
24 "cuelang.org/go/encoding/gocode/testdata/pkg2"
25)
26
27func TestPackages(t *testing.T) {
28 testCases := []struct {
29 name string
30 got error
31 want string
32 }{{
33 name: "failing int",
34 got: func() error {
35 v := pkg2.PickMe(4)
36 return v.Validate()
37 }(),
38 want: "invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x",
39 }, {
40 name: "failing field with validator",
41 got: func() error {
42 v := &pkg1.OtherStruct{A: "car"}
43 return v.Validate()
44 }(),
Marcel van Lohuizenbfbcd502019-08-23 09:14:15 +020045 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",
Marcel van Lohuizen240a9952019-08-05 10:29:13 +020046 }, {
47 name: "failing field of type int",
48 got: func() error {
49 v := &pkg1.MyStruct{A: 11, B: "dog"}
50 return v.Validate()
51 }(),
52 want: "A: invalid value 11 (out of bound <=10):\n pkg1/instance.cue:x:x",
53 }, {
54 name: "failing nested struct ",
55 got: func() error {
Marcel van Lohuizenbfbcd502019-08-23 09:14:15 +020056 v := &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "car", P: 6}}
Marcel van Lohuizen240a9952019-08-05 10:29:13 +020057 return v.Validate()
58 }(),
59 want: "O.A: invalid value \"car\" (does not satisfy strings.ContainsAny(\"X\")):\n pkg1/instance.cue:x:x",
60 }, {
61 name: "fail nested struct of different package",
62 got: func() error {
63 v := &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "X", P: 4}}
64 return v.Validate()
65 }(),
66 want: "O.P: invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x",
67 }, {
68 name: "all good",
69 got: func() error {
70 v := &pkg1.MyStruct{
71 A: 5,
72 B: "dog",
73 I: &pkg2.ImportMe{A: 1000, B: "a"},
74 }
75 return v.Validate()
76 }(),
77 want: "nil",
78 }}
79 for _, tc := range testCases {
80 t.Run(tc.name, func(t *testing.T) {
81 got := strings.TrimSpace(errStr(tc.got))
82 want := tc.want
83 if got != want {
84 t.Errorf("got:\n%q\nwant:\n%q", got, want)
85 }
86
87 })
88 }
89}