cuelang.org/go@v0.10.1/pkg/internal/builtintest/testing.go (about)

     1  // Copyright 2020 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 builtintest
    16  
    17  import (
    18  	"fmt"
    19  	"regexp"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue/format"
    23  	"cuelang.org/go/internal/core/eval"
    24  	"cuelang.org/go/internal/core/export"
    25  	"cuelang.org/go/internal/core/validate"
    26  	"cuelang.org/go/internal/cuetdtest"
    27  	"cuelang.org/go/internal/cuetxtar"
    28  )
    29  
    30  func Run(name string, t *testing.T) {
    31  	test := cuetxtar.TxTarTest{
    32  		Root:   "./testdata",
    33  		Name:   name,
    34  		Matrix: cuetdtest.SmallMatrix,
    35  	}
    36  
    37  	// Find common patterns that we want to ignore for testing purposes.
    38  	// TODO(evalv3): remove once new implementation is stable.
    39  	re := regexp.MustCompile(` \(and \d* more errors\)`)
    40  
    41  	test.Run(t, func(t *cuetxtar.Test) {
    42  		r := t.Runtime()
    43  		a := t.Instance()
    44  
    45  		v, errs := r.Build(nil, a)
    46  		if errs != nil {
    47  			t.Fatal(errs)
    48  		}
    49  
    50  		e := eval.New(r)
    51  		ctx := e.NewContext(v)
    52  		v.Finalize(ctx)
    53  
    54  		if b := validate.Validate(ctx, v, &validate.Config{
    55  			AllErrors: true,
    56  		}); b != nil {
    57  			fmt.Fprintln(t, "Errors:")
    58  			t.WriteErrors(b.Err)
    59  			fmt.Fprintln(t, "")
    60  			fmt.Fprintln(t, "Result:")
    61  		}
    62  
    63  		p := export.All
    64  		p.ShowErrors = true
    65  
    66  		files, errs := p.Vertex(r, test.Name, v)
    67  		if errs != nil {
    68  			t.Fatal(errs)
    69  		}
    70  
    71  		b, err := format.Node(files)
    72  		if err != nil {
    73  			t.Fatal(err)
    74  		}
    75  
    76  		s := re.ReplaceAllString(string(b), "")
    77  
    78  		fmt.Fprint(t, s)
    79  	})
    80  }