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