tests: fix unit tests

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-03-25 14:35:55 -07:00
committed by Solomon Hykes
parent 7ad541feb1
commit 524f77df65
5 changed files with 24 additions and 107 deletions

View File

@@ -2,55 +2,34 @@ package compiler
import (
"testing"
"github.com/stretchr/testify/require"
)
// Test that a non-existing field is detected correctly
func TestFieldNotExist(t *testing.T) {
c := &Compiler{}
root, err := c.Compile("test.cue", `foo: "bar"`)
if err != nil {
t.Fatal(err)
}
if v := root.Get("foo"); !v.Exists() {
// value should exist
t.Fatal(v)
}
if v := root.Get("bar"); v.Exists() {
// value should NOT exist
t.Fatal(v)
}
require.NoError(t, err)
require.True(t, root.Get("foo").Exists())
require.False(t, root.Get("bar").Exists())
}
// Test that a non-existing definition is detected correctly
func TestDefNotExist(t *testing.T) {
c := &Compiler{}
root, err := c.Compile("test.cue", `foo: #bla: "bar"`)
if err != nil {
t.Fatal(err)
}
if v := root.Get("foo.#bla"); !v.Exists() {
// value should exist
t.Fatal(v)
}
if v := root.Get("foo.#nope"); v.Exists() {
// value should NOT exist
t.Fatal(v)
}
require.NoError(t, err)
require.True(t, root.Get("foo.#bla").Exists())
require.False(t, root.Get("foo.#nope").Exists())
}
func TestJSON(t *testing.T) {
c := &Compiler{}
v, err := c.Compile("", `foo: hello: "world"`)
if err != nil {
t.Fatal(err)
}
b1 := v.JSON()
if string(b1) != `{"foo":{"hello":"world"}}` {
t.Fatal(b1)
}
require.NoError(t, err)
require.Equal(t, `{"foo":{"hello":"world"}}`, string(v.JSON()))
// Reproduce a bug where Value.Get().JSON() ignores Get()
b2 := v.Get("foo").JSON()
if string(b2) != `{"hello":"world"}` {
t.Fatal(b2)
}
require.Equal(t, `{"hello":"world"}`, string(v.Get("foo").JSON()))
}