Marcel van Lohuizen | b8cad1a | 2020-02-03 16:55:24 +0100 | [diff] [blame] | 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 | ) |
| 24 | |
| 25 | func TestAttributeBody(t *testing.T) { |
| 26 | testdata := []struct { |
| 27 | in, out string |
| 28 | err string |
| 29 | }{{ |
| 30 | in: "", |
| 31 | out: "[{ 0}]", |
| 32 | }, { |
| 33 | in: "bb", |
| 34 | out: "[{bb 0}]", |
| 35 | }, { |
| 36 | in: "a,", |
| 37 | out: "[{a 0} { 0}]", |
| 38 | }, { |
| 39 | in: `"a",`, |
| 40 | out: "[{a 0} { 0}]", |
| 41 | }, { |
| 42 | in: "a,b", |
| 43 | out: "[{a 0} {b 0}]", |
| 44 | }, { |
| 45 | in: `foo,"bar",#"baz"#`, |
| 46 | out: "[{foo 0} {bar 0} {baz 0}]", |
| 47 | }, { |
| 48 | in: `bar=str`, |
| 49 | out: "[{bar=str 3}]", |
| 50 | }, { |
| 51 | in: `bar="str"`, |
| 52 | out: "[{bar=str 3}]", |
| 53 | }, { |
| 54 | in: `foo.bar="str"`, |
| 55 | out: "[{foo.bar=str 7}]", |
| 56 | }, { |
| 57 | in: `bar=,baz=`, |
| 58 | out: "[{bar= 3} {baz= 3}]", |
| 59 | }, { |
| 60 | in: `foo=1,bar="str",baz=free form`, |
| 61 | out: "[{foo=1 3} {bar=str 3} {baz=free form 3}]", |
| 62 | }, { |
| 63 | in: `""" |
| 64 | """`, |
| 65 | out: "[{ 0}]", |
| 66 | }, { |
| 67 | in: `#''' |
| 68 | \#x20 |
| 69 | '''#`, |
| 70 | out: "[{ 0}]", |
| 71 | }, { |
| 72 | in: "'' ,b", |
| 73 | err: "invalid attribute", |
| 74 | }, { |
| 75 | in: "' ,b", |
| 76 | err: "not terminated", |
| 77 | }, { |
| 78 | in: `"\ "`, |
| 79 | err: "invalid attribute", |
| 80 | }, { |
| 81 | in: `# `, |
| 82 | err: "invalid attribute", |
| 83 | }} |
| 84 | for _, tc := range testdata { |
| 85 | t.Run(tc.in, func(t *testing.T) { |
| 86 | pa := ParseAttrBody(token.NoPos, tc.in) |
| 87 | err := pa.Err |
| 88 | |
| 89 | if tc.err != "" { |
| 90 | if !strings.Contains(err.Error(), tc.err) { |
| 91 | t.Errorf("error was %v; want %v", err, tc.err) |
| 92 | } |
| 93 | return |
| 94 | } |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | |
| 99 | if got := fmt.Sprint(pa.Fields); got != tc.out { |
| 100 | t.Errorf("got %v; want %v", got, tc.out) |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |