...

Source file src/cuelang.org/go/encoding/gocode/gen_test.go

Documentation: cuelang.org/go/encoding/gocode

     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      pkg1/instance.cue:x:x
    54  `,
    55  	}, {
    56  		name:  "failing field of type int",
    57  		value: &pkg1.MyStruct{A: 11, B: "dog"},
    58  		want: `
    59  2 errors in empty disjunction:
    60  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):
    61      pkg1/instance.cue:x:x
    62  A: invalid value 11 (out of bound <=10):
    63      pkg1/instance.cue:x:x
    64  `,
    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      pkg1/instance.cue:x:x
    76  O.A: invalid value "car" (does not satisfy strings.ContainsAny("X")):
    77      pkg1/instance.cue:x:x
    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      pkg1/instance.cue:x:x
    91  O.P: invalid value 4 (out of bound >5):
    92      pkg2/instance.cue:x:x
    93  `,
    94  	}, {
    95  		name: "all good",
    96  		value: &pkg1.MyStruct{
    97  			A: 5,
    98  			B: "dog",
    99  			I: &pkg2.ImportMe{A: 1000, B: "a"},
   100  		},
   101  		want: "nil",
   102  	}}
   103  	for _, tc := range testCases {
   104  		t.Run(tc.name, func(t *testing.T) {
   105  			got := strings.TrimSpace(errStr(tc.value.Validate()))
   106  			want := strings.TrimSpace(tc.want)
   107  			if got != want {
   108  				t.Errorf("got:\n%q\nwant:\n%q", got, want)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func errStr(err error) string {
   115  	if err == nil {
   116  		return "nil"
   117  	}
   118  	buf := &bytes.Buffer{}
   119  	errors.Print(buf, err, nil)
   120  	r := regexp.MustCompile(`.cue:\d+:\d+`)
   121  	return r.ReplaceAllString(buf.String(), ".cue:x:x")
   122  }
   123  

View as plain text