2017-05-18 14:57:04 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-05-22 05:34:23 +00:00
|
|
|
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-05-28 00:35:44 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-05-18 14:57:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse_nonObject(t *testing.T) {
|
|
|
|
src := `true`
|
|
|
|
file, diags := Parse([]byte(src), "")
|
|
|
|
if len(diags) != 1 {
|
|
|
|
t.Errorf("got %d diagnostics; want 1", len(diags))
|
|
|
|
}
|
2017-05-20 20:32:12 +00:00
|
|
|
if file == nil {
|
|
|
|
t.Errorf("got nil File; want actual file")
|
|
|
|
}
|
|
|
|
if file.Body == nil {
|
|
|
|
t.Fatalf("got nil Body; want actual body")
|
|
|
|
}
|
2018-02-17 18:26:58 +00:00
|
|
|
if file.Body.(*body).val == nil {
|
2017-05-20 20:32:12 +00:00
|
|
|
t.Errorf("got nil Body object; want placeholder object")
|
2017-05-18 14:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 05:34:23 +00:00
|
|
|
|
2017-06-12 14:25:09 +00:00
|
|
|
func TestParseTemplate(t *testing.T) {
|
|
|
|
src := `{"greeting": "hello ${\"world\"}"}`
|
|
|
|
file, diags := Parse([]byte(src), "")
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("got %d diagnostics on parse; want 0", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if file == nil {
|
|
|
|
t.Errorf("got nil File; want actual file")
|
|
|
|
}
|
|
|
|
if file.Body == nil {
|
|
|
|
t.Fatalf("got nil Body; want actual body")
|
|
|
|
}
|
|
|
|
attrs, diags := file.Body.JustAttributes()
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("got %d diagnostics on decode; want 0", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
val, diags := attrs["greeting"].Expr.Value(&hcl.EvalContext{})
|
2017-06-12 14:25:09 +00:00
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("got %d diagnostics on eval; want 0", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !val.RawEquals(cty.StringVal("hello world")) {
|
|
|
|
t.Errorf("wrong result %#v; want %#v", val, cty.StringVal("hello world"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTemplateUnwrap(t *testing.T) {
|
|
|
|
src := `{"greeting": "${true}"}`
|
|
|
|
file, diags := Parse([]byte(src), "")
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("got %d diagnostics on parse; want 0", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if file == nil {
|
|
|
|
t.Errorf("got nil File; want actual file")
|
|
|
|
}
|
|
|
|
if file.Body == nil {
|
|
|
|
t.Fatalf("got nil Body; want actual body")
|
|
|
|
}
|
|
|
|
attrs, diags := file.Body.JustAttributes()
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("got %d diagnostics on decode; want 0", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
val, diags := attrs["greeting"].Expr.Value(&hcl.EvalContext{})
|
2017-06-12 14:25:09 +00:00
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("got %d diagnostics on eval; want 0", len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !val.RawEquals(cty.True) {
|
|
|
|
t.Errorf("wrong result %#v; want %#v", val, cty.True)
|
|
|
|
}
|
|
|
|
}
|