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 15package fix 16 17import ( 18 "testing" 19 20 "cuelang.org/go/cue/format" 21 "cuelang.org/go/cue/parser" 22) 23 24func 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 34a: 1 div 2 35b: 3 mod 5 36c: 2 quo 9 37d: 1.0 rem 1.0 // pass on illegal values. 38`, 39 out: `package foo 40 41a: __div(1, 2) 42b: __mod(3, 5) 43c: __quo(2, 9) 44d: __rem(1.0, 1.0) // pass on illegal values. 45`, 46 }, { 47 name: "referenced quoted fields", 48 in: `package foo 49 50a: { 51 fiz: 4 52 faz: ` + "`fiz`" + ` 53 54 // biz 55 ` + "`biz`" + `: 5 // biz 56 buz: ` + "`biz`" + ` 57 in: 3 58 ref: ` + "`in`" + ` & x 59} 60`, 61 out: `package foo 62 63a: { 64 fiz: 4 65 faz: fiz 66 67 // biz 68 biz: 5 // biz 69 buz: biz 70 X1="in": 3 71 ref: X1 & x 72} 73`, 74 }, { 75 in: ` 76 y = foo 77 `, 78 out: ` 79let y = foo 80`, 81 }, { 82 simplify: true, 83 in: ` 84 x1: 3 & _ 85 x2: _ | {[string]: int} 86 x3: 4 & (9 | _) 87 x4: (_ | 9) & 4 88 x5: (_ & 9) & 4 89 x6: 4 & (_ & 9) 90 `, 91 out: `x1: 3 92x2: _ 93x3: 4 94x4: 4 95x5: 9 & 4 96x6: 4 & 9 97`, 98 99 // }, { 100 // name: "slice", 101 // in: `package foo 102 103 // // keep comment 104 // l[3:4] // and this one 105 106 // a: len(l[3:4]) 107 // b: len(l[a:_]) 108 // c: len(l[_:x]) 109 // d: len(l[_:_]) 110 // `, 111 // out: `package foo 112 113 // import list6c6973 "list" 114 115 // // keep comment 116 // list6c6973.Slice(l, 3, 4)// and this one 117 118 // a: len(list6c6973.Slice(l, 3, 4)) 119 // b: len(list6c6973.Slice(l, a, len(l))) 120 // c: len(list6c6973.Slice(l, 0, x)) 121 // d: len(list6c6973.Slice(l, 0, len(l))) 122 // `, 123 // }, { 124 // name: "slice2", 125 // in: `package foo 126 127 // import "list" 128 129 // a: list.Contains("foo") 130 // b: len(l[_:_]) 131 // `, 132 // out: `package foo 133 134 // import ( 135 // "list" 136 // list6c6973 "list" 137 // ) 138 139 // a: list.Contains("foo") 140 // b: len(list6c6973.Slice(l, 0, len(l))) 141 // `, 142 }} 143 for _, tc := range testCases { 144 t.Run(tc.name, func(t *testing.T) { 145 f, err := parser.ParseFile("", tc.in, parser.ParseComments) 146 if err != nil { 147 t.Fatal(err) 148 } 149 150 var opts []Option 151 if tc.simplify { 152 opts = append(opts, Simplify()) 153 } 154 n := File(f, opts...) 155 156 b, err := format.Node(n) 157 if err != nil { 158 t.Fatal(err) 159 } 160 got := string(b) 161 if got != tc.out { 162 t.Errorf("got %v; want %v", got, tc.out) 163 } 164 _, err = parser.ParseFile("rewritten", got, parser.ParseComments) 165 if err != nil { 166 t.Fatal(err) 167 } 168 }) 169 } 170} 171