...

Source file src/cuelang.org/go/pkg/internal/builtintest/testing.go

Documentation: cuelang.org/go/pkg/internal/builtintest

     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  	"testing"
    20  
    21  	"cuelang.org/go/cue/format"
    22  	"cuelang.org/go/internal/core/eval"
    23  	"cuelang.org/go/internal/core/export"
    24  	"cuelang.org/go/internal/core/runtime"
    25  	"cuelang.org/go/internal/core/validate"
    26  	"cuelang.org/go/internal/cuetxtar"
    27  )
    28  
    29  func Run(name string, t *testing.T) {
    30  	test := cuetxtar.TxTarTest{
    31  		Root: "./testdata",
    32  		Name: name,
    33  	}
    34  
    35  	r := runtime.New()
    36  
    37  	test.Run(t, func(t *cuetxtar.Test) {
    38  		a := t.Instance()
    39  
    40  		v, errs := r.Build(nil, a)
    41  		if errs != nil {
    42  			t.Fatal(errs)
    43  		}
    44  
    45  		e := eval.New(r)
    46  		ctx := e.NewContext(v)
    47  		v.Finalize(ctx)
    48  
    49  		if b := validate.Validate(ctx, v, &validate.Config{
    50  			AllErrors: true,
    51  		}); b != nil {
    52  			fmt.Fprintln(t, "Errors:")
    53  			t.WriteErrors(b.Err)
    54  			fmt.Fprintln(t, "")
    55  			fmt.Fprintln(t, "Result:")
    56  		}
    57  
    58  		p := export.All
    59  		p.ShowErrors = true
    60  
    61  		files, errs := p.Vertex(r, test.Name, v)
    62  		if errs != nil {
    63  			t.Fatal(errs)
    64  		}
    65  
    66  		b, err := format.Node(files)
    67  		if err != nil {
    68  			t.Fatal(err)
    69  		}
    70  
    71  		fmt.Fprint(t, string(b))
    72  	})
    73  }
    74  

View as plain text