cuelang.org/go@v0.10.1/internal/core/export/value_test.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 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"
    24  	"cuelang.org/go/internal/core/adt"
    25  	"cuelang.org/go/internal/core/compile"
    26  	"cuelang.org/go/internal/core/eval"
    27  	"cuelang.org/go/internal/core/export"
    28  	"cuelang.org/go/internal/core/runtime"
    29  	"cuelang.org/go/internal/cuedebug"
    30  	"cuelang.org/go/internal/cuetdtest"
    31  	"cuelang.org/go/internal/cuetxtar"
    32  	"golang.org/x/tools/txtar"
    33  )
    34  
    35  var exclude = map[string]string{
    36  	"scalardef": "incomplete",
    37  }
    38  
    39  func TestValue(t *testing.T) {
    40  	const debugValue = `
    41  -- in.cue --
    42  if false {
    43  	c4: { @step(4a) } @step(4b)
    44  	@step(4c)
    45  }
    46  `
    47  	var _ = debugValue // avoid "unused const" warnings
    48  
    49  	test := cuetxtar.TxTarTest{
    50  		Root:   "./testdata/main",
    51  		Name:   "value",
    52  		Skip:   exclude,
    53  		Matrix: cuetdtest.SmallMatrix,
    54  
    55  		// Uncomment to debug an isolated test case.
    56  		// DebugArchive: debugValue,
    57  	}
    58  
    59  	test.Run(t, func(t *cuetxtar.Test) {
    60  		r := t.Runtime()
    61  		a := t.Instance()
    62  
    63  		pkgID := a.ID()
    64  
    65  		v, err := r.Build(nil, a)
    66  		if err != nil {
    67  			t.Fatal(err)
    68  		}
    69  
    70  		ctx := eval.NewContext(r, v)
    71  		v.Finalize(ctx)
    72  
    73  		all := export.All
    74  		all.ShowErrors = true
    75  
    76  		evalWithOptions := export.Profile{
    77  			TakeDefaults:    true,
    78  			ShowOptional:    true,
    79  			ShowDefinitions: true,
    80  			ShowAttributes:  true,
    81  		}
    82  
    83  		for _, tc := range []struct {
    84  			name string
    85  			fn   func(r adt.Runtime, id string, v adt.Value) (ast.Expr, errors.Error)
    86  		}{
    87  			{"Simplified", export.Simplified.Value},
    88  			{"Raw", export.Raw.Value},
    89  			{"Final", export.Final.Value},
    90  			{"All", all.Value},
    91  			{"Eval", evalWithOptions.Value},
    92  		} {
    93  			fmt.Fprintln(t, "==", tc.name)
    94  			x, errs := tc.fn(r, pkgID, v)
    95  			errors.Print(t, errs, nil)
    96  			_, _ = t.Write(formatNode(t.T, x))
    97  			fmt.Fprintln(t)
    98  		}
    99  	})
   100  }
   101  
   102  // For debugging purposes. Do not delete.
   103  func TestValueX(t *testing.T) {
   104  	t.Skip()
   105  
   106  	in := `
   107  -- in.cue --
   108  	`
   109  	archive := txtar.Parse([]byte(in))
   110  	a := cuetxtar.Load(archive, t.TempDir())
   111  
   112  	r := runtime.New()
   113  	(*runtime.Runtime)(r).SetVersion(internal.DevVersion)
   114  	(*runtime.Runtime)(r).SetDebugOptions(&cuedebug.Config{Sharing: true})
   115  
   116  	v, errs := compile.Files(nil, r, "", a[0].Files...)
   117  	if errs != nil {
   118  		t.Fatal(errs)
   119  	}
   120  
   121  	ctx := eval.NewContext(r, v)
   122  	ctx.LogEval = 1
   123  	v.Finalize(ctx)
   124  
   125  	p := export.All
   126  	p.ShowErrors = true
   127  
   128  	p = &export.Profile{
   129  		TakeDefaults:    true,
   130  		ShowOptional:    true,
   131  		ShowDefinitions: true,
   132  		ShowAttributes:  true,
   133  	}
   134  
   135  	x, errs := p.Value(r, "main", v)
   136  	if errs != nil {
   137  		t.Fatal(errs)
   138  	}
   139  
   140  	t.Error(string(formatNode(t, x)))
   141  }