cuelang.org/go@v0.10.1/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 name: "rewrite integer division", 32 in: `package foo 33 34 a: 1 div 2 35 b: 3 mod 5 36 c: 2 quo 9 37 d: 1.0 rem 1.0 // pass on illegal values. 38 `, 39 out: `package foo 40 41 a: __div(1, 2) 42 b: __mod(3, 5) 43 c: __quo(2, 9) 44 d: __rem(1.0, 1.0) // pass on illegal values. 45 `, 46 }, { 47 simplify: true, 48 in: ` 49 x1: 3 & _ 50 x2: _ | {[string]: int} 51 x3: 4 & (9 | _) 52 x4: (_ | 9) & 4 53 x5: (_ & 9) & 4 54 x6: 4 & (_ & 9) 55 `, 56 out: `x1: 3 57 x2: _ 58 x3: 4 59 x4: 4 60 x5: 9 & 4 61 x6: 4 & 9 62 `, 63 64 // }, { 65 // name: "slice", 66 // in: `package foo 67 68 // // keep comment 69 // l[3:4] // and this one 70 71 // a: len(l[3:4]) 72 // b: len(l[a:_]) 73 // c: len(l[_:x]) 74 // d: len(l[_:_]) 75 // `, 76 // out: `package foo 77 78 // import list6c6973 "list" 79 80 // // keep comment 81 // list6c6973.Slice(l, 3, 4)// and this one 82 83 // a: len(list6c6973.Slice(l, 3, 4)) 84 // b: len(list6c6973.Slice(l, a, len(l))) 85 // c: len(list6c6973.Slice(l, 0, x)) 86 // d: len(list6c6973.Slice(l, 0, len(l))) 87 // `, 88 // }, { 89 // name: "slice2", 90 // in: `package foo 91 92 // import "list" 93 94 // a: list.Contains("foo") 95 // b: len(l[_:_]) 96 // `, 97 // out: `package foo 98 99 // import ( 100 // "list" 101 // list6c6973 "list" 102 // ) 103 104 // a: list.Contains("foo") 105 // b: len(list6c6973.Slice(l, 0, len(l))) 106 // `, 107 }} 108 for _, tc := range testCases { 109 t.Run(tc.name, func(t *testing.T) { 110 f, err := parser.ParseFile("", tc.in, parser.ParseComments) 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 var opts []Option 116 if tc.simplify { 117 opts = append(opts, Simplify()) 118 } 119 n := File(f, opts...) 120 121 b, err := format.Node(n) 122 if err != nil { 123 t.Fatal(err) 124 } 125 got := string(b) 126 if got != tc.out { 127 t.Errorf("got %v; want %v", got, tc.out) 128 } 129 _, err = parser.ParseFile("rewritten", got, parser.ParseComments) 130 if err != nil { 131 t.Fatal(err) 132 } 133 }) 134 } 135 }