cuelang.org/go@v0.13.0/tools/fix/fix_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 package fix 16 17 import ( 18 "testing" 19 20 "cuelang.org/go/cue/format" 21 "cuelang.org/go/cue/parser" 22 ) 23 24 func TestFile(t *testing.T) { 25 testCases := []struct { 26 name string 27 in string 28 out string 29 simplify bool 30 }{ 31 { 32 name: "rewrite integer division", 33 in: `package foo 34 35 a: 1 div 2 36 b: 3 mod 5 37 c: 2 quo 9 38 d: 1.0 rem 1.0 // pass on illegal values. 39 `, 40 out: `package foo 41 42 a: __div(1, 2) 43 b: __mod(3, 5) 44 c: __quo(2, 9) 45 d: __rem(1.0, 1.0) // pass on illegal values. 46 `, 47 }, 48 49 { 50 name: "simplify literal tops", 51 simplify: true, 52 in: ` 53 x1: 3 & _ 54 x2: _ | {[string]: int} 55 x3: 4 & (9 | _) 56 x4: (_ | 9) & 4 57 x5: (_ & 9) & 4 58 x6: 4 & (_ & 9) 59 `, 60 out: `x1: 3 61 x2: _ 62 x3: 4 63 x4: 4 64 x5: 9 & 4 65 x6: 4 & 9 66 `, 67 }, 68 69 { 70 name: "rewrite list addition", 71 in: `a: [7] 72 b: a + a 73 c: a + [8] 74 d: [9] + a 75 e: [0] + [1] 76 f: [0] + [1] + [2] 77 g: list.Concat([[0], [1, 2]]) + list.Concat([[3, 4], [5]]) 78 h: list.Concat([[0], [1, 2]]) + [3] + [4] + list.Concat([[5, 6], [7]]) 79 i: list.Concat(list.Concat([[0], [1, 2]]), list.Concat([[3, 4], [5]])) 80 `, 81 out: `import "list" 82 83 a: [7] 84 b: a + a 85 c: list.Concat([a, [8]]) 86 d: list.Concat([[9], a]) 87 e: list.Concat([[0], [1]]) 88 f: list.Concat([[0], [1], [2]]) 89 g: list.Concat([[0], [1, 2], [3, 4], [5]]) 90 h: list.Concat([[0], [1, 2], [3], [4], [5, 6], [7]]) 91 i: list.Concat(list.Concat([[0], [1, 2]]), list.Concat([[3, 4], [5]])) 92 `, 93 }, 94 95 { 96 name: "rewrite list multiplication", 97 in: `a: [7] 98 b: a * 3 99 c: 4 100 d: [7] * c 101 e: c * [8] 102 f: [9] * 5 103 g: ([9] * 5) + (6 * [10]) 104 `, 105 out: `import "list" 106 107 a: [7] 108 b: a * 3 109 c: 4 110 d: list.Repeat([7], c) 111 e: list.Repeat([8], c) 112 f: list.Repeat([9], 5) 113 g: (list.Repeat([9], 5)) + (list.Repeat([10], 6)) 114 `, 115 }, 116 } 117 for _, tc := range testCases { 118 t.Run(tc.name, func(t *testing.T) { 119 f, err := parser.ParseFile("", tc.in, parser.ParseComments) 120 if err != nil { 121 t.Fatal(err) 122 } 123 124 var opts []Option 125 if tc.simplify { 126 opts = append(opts, Simplify()) 127 } 128 File(f, opts...) 129 130 b, err := format.Node(f) 131 if err != nil { 132 t.Fatal(err) 133 } 134 got := string(b) 135 if got != tc.out { 136 t.Errorf("got %v; want %v", got, tc.out) 137 } 138 _, err = parser.ParseFile("rewritten", got, parser.ParseComments) 139 if err != nil { 140 t.Fatal(err) 141 } 142 }) 143 } 144 }