1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cue
16
17 import (
18 "fmt"
19 "strings"
20 "testing"
21
22 "github.com/google/go-cmp/cmp"
23 )
24
25 func TestMarshalling(t *testing.T) {
26 testCases := []struct {
27 filename string
28 input string
29 pkg string
30 }{{
31 filename: "foo.cue",
32 pkg: "foo",
33 input: `package foo
34
35 A: int
36 B: string
37 `,
38 }, {
39 filename: "bar.cue",
40 pkg: "bar",
41 input: `package bar
42
43 "Hello world!"
44 `,
45 }, {
46 filename: "qux.cue",
47 input: `
48 "Hello world!"
49 `,
50 }, {
51 filename: "baz.cue",
52 pkg: "baz",
53 input: `package baz
54
55 import "strings"
56
57 a: strings.TrimSpace(" Hello world! ")
58 `}}
59 for _, tc := range testCases {
60 t.Run(tc.filename, func(t *testing.T) {
61 r := &Runtime{}
62 inst, err := r.Compile(tc.filename, tc.input)
63 if err != nil {
64 t.Fatal(err)
65 }
66 inst.ImportPath = "test/pkg"
67 want := fmt.Sprint(inst.Value())
68
69 val := inst.Value()
70 b, err := r.Marshal(&val)
71 if err != nil {
72 t.Fatal(err)
73 }
74
75 r2 := &Runtime{}
76 instances, err := r2.Unmarshal(b)
77 if err != nil {
78 t.Fatal(err)
79 }
80 inst = instances[0]
81
82 if inst.ImportPath != "test/pkg" {
83 t.Error("import path was not restored")
84 }
85 got := fmt.Sprint(inst.Value())
86
87 if got != want {
88 t.Errorf("\ngot: %q;\nwant: %q", got, want)
89 }
90 })
91 }
92 }
93
94 func TestMarshalMultiPackage(t *testing.T) {
95 files := func(s ...string) (a []fileData) {
96 for i, s := range s {
97 a = append(a, fileData{fmt.Sprintf("file%d.cue", i), []byte(s)})
98 }
99 return a
100 }
101 insts := func(i ...*instanceData) []*instanceData { return i }
102 pkg1 := &instanceData{
103 true,
104 "mod.test/foo/pkg1",
105 files(`
106 package pkg1
107
108 Object: "World"
109 `),
110 }
111 pkg2 := &instanceData{
112 true,
113 "mod.test/foo/pkg2",
114 files(`
115 package pkg
116
117 Number: 12
118 `),
119 }
120
121 testCases := []struct {
122 instances []*instanceData
123 emit string
124 }{{
125 insts(&instanceData{true, "", files(`test: "ok"`)}),
126 `{test: "ok"}`,
127 }, {
128 insts(&instanceData{true, "",
129 files(
130 `package test
131
132 import math2 "math"
133
134 "Pi: \(math2.Pi)!"`)}),
135 `"Pi: 3.14159265358979323846264338327950288419716939937510582097494459!"`,
136 }, {
137 insts(pkg1, &instanceData{true, "",
138 files(
139 `package test
140
141 import "mod.test/foo/pkg1"
142
143 "Hello \(pkg1.Object)!"`),
144 }),
145 `"Hello World!"`,
146 }, {
147 insts(pkg1, &instanceData{true, "",
148 files(
149 `package test
150
151 import pkg2 "mod.test/foo/pkg1"
152 "Hello \(pkg1)!"
153
154 pkg1: pkg2.Object`),
155 }),
156 `"Hello World!"`,
157 }, {
158 insts(pkg2, &instanceData{true, "",
159 files(
160 `package test
161
162 import "mod.test/foo/pkg2"
163
164 "Hello \(pkg.Number)!"`),
165 }),
166 `"Hello 12!"`,
167 }}
168
169 strValue := func(a []*Instance) (ret []string) {
170 for _, i := range a {
171 ret = append(ret, strings.TrimSpace((fmt.Sprint(i.Value()))))
172 }
173 return ret
174 }
175
176 for _, tc := range testCases {
177 t.Run("", func(t *testing.T) {
178 r := &Runtime{}
179
180 insts, err := compileInstances(r, tc.instances)
181 if err != nil {
182 t.Fatal(err)
183 }
184 want := strValue(insts)
185
186 vals := make([]InstanceOrValue, 0)
187 for _, inst := range insts {
188 val := inst.Value()
189 vals = append(vals, &val)
190 }
191
192 b, err := r.Marshal(vals...)
193 if err != nil {
194 t.Fatal(err)
195 }
196
197 r2 := &Runtime{}
198 insts, err = r2.Unmarshal(b)
199 if err != nil {
200 t.Fatal(err)
201 }
202 got := strValue(insts)
203
204 if diff := cmp.Diff(got, want); diff != "" {
205 t.Error(diff)
206 }
207 })
208 }
209 }
210
View as plain text