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