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