...

Source file src/cuelang.org/go/internal/core/export/value_test.go

Documentation: cuelang.org/go/internal/core/export

     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 export_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"cuelang.org/go/cue/ast"
    22  	"cuelang.org/go/cue/errors"
    23  	"cuelang.org/go/internal/core/adt"
    24  	"cuelang.org/go/internal/core/compile"
    25  	"cuelang.org/go/internal/core/eval"
    26  	"cuelang.org/go/internal/core/export"
    27  	"cuelang.org/go/internal/core/runtime"
    28  	"cuelang.org/go/internal/cuetxtar"
    29  	"golang.org/x/tools/txtar"
    30  )
    31  
    32  var exclude = map[string]string{
    33  	"scalardef": "incomplete",
    34  }
    35  
    36  func TestValue(t *testing.T) {
    37  	test := cuetxtar.TxTarTest{
    38  		Root: "./testdata/main",
    39  		Name: "value",
    40  		Skip: exclude,
    41  	}
    42  
    43  	r := runtime.New()
    44  
    45  	test.Run(t, func(t *cuetxtar.Test) {
    46  		a := t.Instance()
    47  
    48  		pkgID := a.ID()
    49  
    50  		v, err := r.Build(nil, a)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  
    55  		ctx := eval.NewContext(r, v)
    56  		v.Finalize(ctx)
    57  
    58  		all := export.All
    59  		all.ShowErrors = true
    60  
    61  		evalWithOptions := export.Profile{
    62  			TakeDefaults:    true,
    63  			ShowOptional:    true,
    64  			ShowDefinitions: true,
    65  			ShowAttributes:  true,
    66  		}
    67  
    68  		for _, tc := range []struct {
    69  			name string
    70  			fn   func(r adt.Runtime, id string, v adt.Value) (ast.Expr, errors.Error)
    71  		}{
    72  			{"Simplified", export.Simplified.Value},
    73  			{"Raw", export.Raw.Value},
    74  			{"Final", export.Final.Value},
    75  			{"All", all.Value},
    76  			{"Eval", evalWithOptions.Value},
    77  		} {
    78  			fmt.Fprintln(t, "==", tc.name)
    79  			x, errs := tc.fn(r, pkgID, v)
    80  			errors.Print(t, errs, nil)
    81  			_, _ = t.Write(formatNode(t.T, x))
    82  			fmt.Fprintln(t)
    83  		}
    84  	})
    85  }
    86  
    87  // For debugging purposes. Do not delete.
    88  func TestValueX(t *testing.T) {
    89  	t.Skip()
    90  
    91  	in := `
    92  -- in.cue --
    93  	`
    94  
    95  	adt.Verbosity = 1
    96  	t.Cleanup(func() { adt.Verbosity = 0 })
    97  
    98  	archive := txtar.Parse([]byte(in))
    99  	a := cuetxtar.Load(archive, t.TempDir())
   100  
   101  	r := runtime.New()
   102  	v, errs := compile.Files(nil, r, "", a[0].Files...)
   103  	if errs != nil {
   104  		t.Fatal(errs)
   105  	}
   106  
   107  	ctx := eval.NewContext(r, v)
   108  	v.Finalize(ctx)
   109  
   110  	p := export.All
   111  	p.ShowErrors = true
   112  
   113  	p = &export.Profile{
   114  		TakeDefaults:    true,
   115  		ShowOptional:    true,
   116  		ShowDefinitions: true,
   117  		ShowAttributes:  true,
   118  	}
   119  
   120  	x, errs := p.Value(r, "main", v)
   121  	if errs != nil {
   122  		t.Fatal(errs)
   123  	}
   124  
   125  	t.Error(string(formatNode(t, x)))
   126  }
   127  

View as plain text