cuelang.org/go@v0.10.1/internal/attrs_test.go (about)

     1  // Copyright 2020 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  package internal
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue/token"
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  type keyVals [][3]string
    27  
    28  func TestAttributeBody(t *testing.T) {
    29  	testdata := []struct {
    30  		in  string
    31  		out keyVals
    32  		err string
    33  	}{{
    34  		in:  "",
    35  		out: keyVals{{}},
    36  	}, {
    37  		in:  "bb",
    38  		out: keyVals{{"", "bb", "bb"}},
    39  	}, {
    40  		in:  "a,",
    41  		out: keyVals{{"", "a", "a"}, {"", ""}},
    42  	}, {
    43  		in:  `"a",`,
    44  		out: keyVals{{"", "a", `"a"`}, {"", ""}},
    45  	}, {
    46  		in:  "a,b",
    47  		out: keyVals{{"", "a", "a"}, {"", "b", "b"}},
    48  	}, {
    49  		in:  `foo,"bar",#"baz"#`,
    50  		out: keyVals{{"", "foo", "foo"}, {"", "bar", `"bar"`}, {"", "baz", `#"baz"#`}},
    51  	}, {
    52  		in:  `foo,bar,baz`,
    53  		out: keyVals{{"", "foo", "foo"}, {"", "bar", "bar"}, {"", "baz", "baz"}},
    54  	}, {
    55  		in:  `1,map[int]string`,
    56  		out: keyVals{{"", "1", "1"}, {"", "map[int]string", "map[int]string"}},
    57  	}, {
    58  		in:  `bar=str`,
    59  		out: keyVals{{"bar", "str", "bar=str"}},
    60  	}, {
    61  		in:  `bar="str"`,
    62  		out: keyVals{{"bar", "str", `bar="str"`}},
    63  	}, {
    64  		in:  `foo.bar="str"`,
    65  		out: keyVals{{"foo.bar", "str", `foo.bar="str"`}},
    66  	}, {
    67  		in:  `bar=,baz=`,
    68  		out: keyVals{{"bar", "", "bar="}, {"baz", "", "baz="}},
    69  	}, {
    70  		in:  `foo=1,bar="str",baz=free form`,
    71  		out: keyVals{{"foo", "1", "foo=1"}, {"bar", "str", `bar="str"`}, {"baz", "free form", "baz=free form"}},
    72  	}, {
    73  		in:  `foo=1,bar="str",baz=free form  `,
    74  		out: keyVals{{"foo", "1", "foo=1"}, {"bar", "str", `bar="str"`}, {"baz", "free form", "baz=free form  "}},
    75  	}, {
    76  		in:  `foo=1,bar="str"  ,baz="free form  "`,
    77  		out: keyVals{{"foo", "1", "foo=1"}, {"bar", "str", `bar="str"  `}, {"baz", "free form  ", `baz="free form  "`}},
    78  	}, {
    79  		in: `"""
    80  		"""`,
    81  		out: keyVals{{"", "", `"""
    82  		"""`}},
    83  	}, {
    84  		in: `#'''
    85  			\#x20
    86  			'''#`,
    87  		out: keyVals{{"", " ", `#'''
    88  			\#x20
    89  			'''#`}},
    90  	}, {
    91  		in:  "'' ,b",
    92  		out: keyVals{{"", "", "'' "}, {"", "b", "b"}},
    93  	}, {
    94  		in:  "' ,b",
    95  		err: "error scanning attribute text",
    96  	}, {
    97  		in:  `"\ "`,
    98  		err: "error scanning attribute text",
    99  	}, {
   100  		in:  `# `,
   101  		out: keyVals{{"", "#", "# "}},
   102  	}}
   103  	for i, tc := range testdata {
   104  		t.Run(fmt.Sprintf("%d-%s", i, tc.in), func(t *testing.T) {
   105  			f := token.NewFile("test", -1, len(tc.in))
   106  			pos := f.Pos(0, token.NoRelPos)
   107  			pa := ParseAttrBody(pos, tc.in)
   108  			err := pa.Err
   109  
   110  			if tc.err != "" {
   111  				if err == nil {
   112  					t.Fatalf("unexpected success when error was expected (%#v)", pa.Fields)
   113  				}
   114  				if !strings.Contains(err.Error(), tc.err) {
   115  					t.Errorf("error was %v; want %v", err, tc.err)
   116  				}
   117  				return
   118  			}
   119  			if err != nil {
   120  				t.Fatal(err)
   121  			}
   122  			var kvs keyVals
   123  			for _, kv := range pa.Fields {
   124  				kvs = append(kvs, [3]string{kv.Key(), kv.Value(), kv.Text()})
   125  			}
   126  			if diff := cmp.Diff(tc.out, kvs); diff != "" {
   127  				t.Errorf("unexpected result; diff (-want +got)\n%s", diff)
   128  			}
   129  		})
   130  	}
   131  }