github.com/solo-io/cue@v0.4.7/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/rogpeppe/go-internal/txtar"
    22  	"github.com/solo-io/cue/cue/ast"
    23  	"github.com/solo-io/cue/cue/errors"
    24  	"github.com/solo-io/cue/internal/core/adt"
    25  	"github.com/solo-io/cue/internal/core/compile"
    26  	"github.com/solo-io/cue/internal/core/eval"
    27  	"github.com/solo-io/cue/internal/core/export"
    28  	"github.com/solo-io/cue/internal/core/runtime"
    29  	"github.com/solo-io/cue/internal/cuetest"
    30  	"github.com/solo-io/cue/internal/cuetxtar"
    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  import "strings"
    96  
    97  strings.MinRunes(4) & strings.MaxRunes(7)
    98  
    99  	`
   100  
   101  	archive := txtar.Parse([]byte(in))
   102  	a := cuetxtar.Load(archive, "/tmp/test")
   103  
   104  	r := runtime.New()
   105  	v, errs := compile.Files(nil, r, "", a[0].Files...)
   106  	if errs != nil {
   107  		t.Fatal(errs)
   108  	}
   109  
   110  	ctx := eval.NewContext(r, v)
   111  	v.Finalize(ctx)
   112  
   113  	p := export.All
   114  	p.ShowErrors = true
   115  
   116  	p = &export.Profile{
   117  		TakeDefaults:    true,
   118  		ShowOptional:    true,
   119  		ShowDefinitions: true,
   120  		ShowAttributes:  true,
   121  	}
   122  
   123  	x, errs := p.Value(r, "main", v)
   124  	if errs != nil {
   125  		t.Fatal(errs)
   126  	}
   127  
   128  	t.Error(string(formatNode(t, x)))
   129  }