cuelang.org/go@v0.13.0/cue/interpreter/wasm/wasm_test.go (about)

     1  // Copyright 2023 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 wasm_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"cuelang.org/go/cue"
    22  	"cuelang.org/go/cue/ast"
    23  	"cuelang.org/go/cue/ast/astutil"
    24  	"cuelang.org/go/cue/cuecontext"
    25  	"cuelang.org/go/cue/errors"
    26  	"cuelang.org/go/cue/format"
    27  	"cuelang.org/go/cue/interpreter/wasm"
    28  	"cuelang.org/go/internal"
    29  	"cuelang.org/go/internal/cuetxtar"
    30  )
    31  
    32  // TestWasm tests Wasm using the API.
    33  func TestWasm(t *testing.T) {
    34  	test := cuetxtar.TxTarTest{
    35  		Root: "./testdata/cue",
    36  		Name: "wasm",
    37  	}
    38  
    39  	test.Run(t, func(t *cuetxtar.Test) {
    40  		ctx := cuecontext.New(cuecontext.Interpreter(wasm.New()))
    41  		if version, _ := t.Runtime().Settings(); version == internal.EvalV3 {
    42  			t.Skip("TODO: fix these tests on evalv3")
    43  		}
    44  		if t.HasTag("cuecmd") {
    45  			// if #cuecmd is set the test is only for the CUE command,
    46  			// not the API, so skip it.
    47  			return
    48  		}
    49  
    50  		bi := t.Instance()
    51  
    52  		v := ctx.BuildInstance(bi)
    53  		err := v.Validate()
    54  
    55  		if t.HasTag("error") {
    56  			// if #error is set we're checking for a specific error,
    57  			// so only print the error then bail out.
    58  			for _, err := range errors.Errors(err) {
    59  				t.WriteErrors(err)
    60  			}
    61  			return
    62  		}
    63  
    64  		// we got an unexpected error. print both the error
    65  		// and the CUE value, to aid debugging.
    66  		if err != nil {
    67  			fmt.Fprintln(t, "Errors:")
    68  			for _, err := range errors.Errors(err) {
    69  				t.WriteErrors(err)
    70  			}
    71  			fmt.Fprintln(t, "")
    72  			fmt.Fprintln(t, "Result:")
    73  		}
    74  
    75  		syntax := v.Syntax(
    76  			cue.Attributes(false), cue.Final(), cue.Definitions(true), cue.ErrorsAsValues(true),
    77  		)
    78  		file, err := astutil.ToFile(syntax.(ast.Expr))
    79  		if err != nil {
    80  			t.Fatal(err)
    81  		}
    82  
    83  		got, err := format.Node(file, format.UseSpaces(4), format.TabIndent(false))
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		fmt.Fprint(t, string(got))
    88  	})
    89  }