cuelang.org/go@v0.10.1/tools/trim/trim_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 trim 16 17 import ( 18 "testing" 19 20 "cuelang.org/go/cue/build" 21 "cuelang.org/go/cue/errors" 22 "cuelang.org/go/internal/cuetdtest" 23 "cuelang.org/go/internal/cuetxtar" 24 "github.com/go-quicktest/qt" 25 ) 26 27 var ( 28 // TODO(evalv3): many broken tests in new evaluator, use FullMatrix to 29 // expose. This is probably due to the changed underlying representation. 30 // matrix = cuetdtest.FullMatrix 31 matrix = cuetdtest.DefaultOnlyMatrix 32 ) 33 34 const trace = false 35 36 func TestTrimFiles(t *testing.T) { 37 test := cuetxtar.TxTarTest{ 38 Root: "./testdata", 39 Name: "trim", 40 Matrix: matrix, 41 } 42 43 test.Run(t, func(t *cuetxtar.Test) { 44 45 a := t.Instance() 46 ctx := t.Context() 47 val := ctx.BuildInstance(a) 48 // Note: don't require val.Err to be nil because there are deliberate 49 // errors in some tests, to ensure trim still works even with some errors. 50 hadError := val.Err() != nil 51 52 files := a.Files 53 54 err := Files(files, val, &Config{Trace: trace}) 55 if err != nil { 56 t.WriteErrors(errors.Promote(err, "")) 57 } 58 59 // If the files could be built without an error before, 60 // they should still build without an error after trimming. 61 // This might not be true if, for example, unused imports are not removed. 62 // Note that we need a new build.Instance to build the ast.Files from scratch again. 63 if !hadError { 64 a := build.NewContext().NewInstance("", nil) 65 for _, file := range files { 66 a.AddSyntax(file) 67 } 68 val := ctx.BuildInstance(a) 69 qt.Assert(t, qt.IsNil(val.Err())) 70 } 71 72 for _, f := range files { 73 t.WriteFile(f) 74 } 75 }) 76 }