github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/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  	"github.com/joomcode/cue/cue/format"
    21  	"github.com/joomcode/cue/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  		in: `
    48  		y = foo
    49  		`,
    50  		out: `
    51  let y = foo
    52  `,
    53  	}, {
    54  		simplify: true,
    55  		in: `
    56  		x1: 3 & _
    57  		x2: _ | {[string]: int}
    58  		x3: 4 & (9 | _)
    59  		x4: (_ | 9) & 4
    60  		x5: (_ & 9) & 4
    61  		x6: 4 & (_ & 9)
    62  		`,
    63  		out: `x1: 3
    64  x2: _
    65  x3: 4
    66  x4: 4
    67  x5: 9 & 4
    68  x6: 4 & 9
    69  `,
    70  
    71  		// 	}, {
    72  		// 		name: "slice",
    73  		// 		in: `package foo
    74  
    75  		// // keep comment
    76  		// l[3:4] // and this one
    77  
    78  		// a: len(l[3:4])
    79  		// b: len(l[a:_])
    80  		// c: len(l[_:x])
    81  		// d: len(l[_:_])
    82  		// `,
    83  		// 		out: `package foo
    84  
    85  		// import list6c6973 "list"
    86  
    87  		// // keep comment
    88  		// list6c6973.Slice(l, 3, 4)// and this one
    89  
    90  		// a: len(list6c6973.Slice(l, 3, 4))
    91  		// b: len(list6c6973.Slice(l, a, len(l)))
    92  		// c: len(list6c6973.Slice(l, 0, x))
    93  		// d: len(list6c6973.Slice(l, 0, len(l)))
    94  		// `,
    95  		// 	}, {
    96  		// 		name: "slice2",
    97  		// 		in: `package foo
    98  
    99  		// import "list"
   100  
   101  		// a: list.Contains("foo")
   102  		// b: len(l[_:_])
   103  		// `,
   104  		// 		out: `package foo
   105  
   106  		// import (
   107  		// 	"list"
   108  		// 	list6c6973 "list"
   109  		// )
   110  
   111  		// a: list.Contains("foo")
   112  		// b: len(list6c6973.Slice(l, 0, len(l)))
   113  		// `,
   114  	}}
   115  	for _, tc := range testCases {
   116  		t.Run(tc.name, func(t *testing.T) {
   117  			f, err := parser.ParseFile("", tc.in, parser.ParseComments)
   118  			if err != nil {
   119  				t.Fatal(err)
   120  			}
   121  
   122  			var opts []Option
   123  			if tc.simplify {
   124  				opts = append(opts, Simplify())
   125  			}
   126  			n := File(f, opts...)
   127  
   128  			b, err := format.Node(n)
   129  			if err != nil {
   130  				t.Fatal(err)
   131  			}
   132  			got := string(b)
   133  			if got != tc.out {
   134  				t.Errorf("got %v; want %v", got, tc.out)
   135  			}
   136  			_, err = parser.ParseFile("rewritten", got, parser.ParseComments)
   137  			if err != nil {
   138  				t.Fatal(err)
   139  			}
   140  		})
   141  	}
   142  }