zclwrite: formatting round-trip test
The existing round trip test was testing that we can pass through a set of tokens through the AST entirely unmodified. This new round-trip test passes the source through the Format function and then checks that it has the same semantics, by evaluating it both before and after and expecting an identical set of values.
This commit is contained in:
parent
ac42b456f3
commit
1565d4f906
@ -4,10 +4,14 @@ import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
"github.com/zclconf/go-cty/cty/function/stdlib"
|
||||
"github.com/zclconf/go-zcl/zcl"
|
||||
"github.com/zclconf/go-zcl/zcl/zclsyntax"
|
||||
)
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
func TestRoundTripVerbatim(t *testing.T) {
|
||||
tests := []string{
|
||||
``,
|
||||
`foo = 1
|
||||
@ -55,3 +59,95 @@ baz = 1
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundTripFormat(t *testing.T) {
|
||||
// The goal of this test is to verify that the formatter doesn't change
|
||||
// the semantics of any expressions when it adds and removes whitespace.
|
||||
// String templates are the primary area of concern here, but we also
|
||||
// test some other things for completeness sake.
|
||||
//
|
||||
// The tests here must define zero or more attributes, which will be
|
||||
// extract with JustAttributes and evaluated both before and after
|
||||
// formatting.
|
||||
|
||||
tests := []string{
|
||||
"",
|
||||
"\n\n\n",
|
||||
"a=1\n",
|
||||
"a=\"hello\"\n",
|
||||
"a=\"${hello} world\"\n",
|
||||
"a=upper(\"hello\")\n",
|
||||
"a=upper(hello)\n",
|
||||
"a=[1,2,3,4,five]\n",
|
||||
"a={greeting=hello}\n",
|
||||
"a={\ngreeting=hello\n}\n",
|
||||
"a={\ngreeting=hello}\n",
|
||||
"a={greeting=hello\n}\n",
|
||||
"a={greeting=hello,number=five,sarcastic=\"${upper(hello)}\"\n}\n",
|
||||
"a={\ngreeting=hello\nnumber=five\nsarcastic=\"${upper(hello)}\"\n}\n",
|
||||
"a=<<EOT\nhello\nEOT\n\n",
|
||||
"a=[<<EOT\nhello\nEOT\n]\n",
|
||||
"a=[\n<<EOT\nhello\nEOT\n]\n",
|
||||
"a=[\n]\n",
|
||||
"a=1\nb=2\nc=3\n",
|
||||
"a=\"${\n5\n}\"\n",
|
||||
}
|
||||
|
||||
ctx := &zcl.EvalContext{
|
||||
Variables: map[string]cty.Value{
|
||||
"hello": cty.StringVal("hello"),
|
||||
"five": cty.NumberIntVal(5),
|
||||
},
|
||||
Functions: map[string]function.Function{
|
||||
"upper": stdlib.UpperFunc,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test, func(t *testing.T) {
|
||||
|
||||
attrsAsObj := func(src []byte, phase string) cty.Value {
|
||||
t.Logf("source %s:\n%s", phase, src)
|
||||
f, diags := zclsyntax.ParseConfig(src, "", zcl.Pos{Line: 1, Column: 1})
|
||||
if len(diags) != 0 {
|
||||
for _, diag := range diags {
|
||||
t.Logf(" - %s", diag.Error())
|
||||
}
|
||||
t.Fatalf("unexpected diagnostics in parse %s", phase)
|
||||
}
|
||||
|
||||
attrs, diags := f.Body.JustAttributes()
|
||||
if len(diags) != 0 {
|
||||
for _, diag := range diags {
|
||||
t.Logf(" - %s", diag.Error())
|
||||
}
|
||||
t.Fatalf("unexpected diagnostics in JustAttributes %s", phase)
|
||||
}
|
||||
|
||||
vals := map[string]cty.Value{}
|
||||
for k, attr := range attrs {
|
||||
val, diags := attr.Expr.Value(ctx)
|
||||
if len(diags) != 0 {
|
||||
for _, diag := range diags {
|
||||
t.Logf(" - %s", diag.Error())
|
||||
}
|
||||
t.Fatalf("unexpected diagnostics evaluating %s", phase)
|
||||
}
|
||||
vals[k] = val
|
||||
}
|
||||
return cty.ObjectVal(vals)
|
||||
}
|
||||
|
||||
src := []byte(test)
|
||||
before := attrsAsObj(src, "before")
|
||||
|
||||
formatted := Format(src)
|
||||
after := attrsAsObj(formatted, "after")
|
||||
|
||||
if !after.RawEquals(before) {
|
||||
t.Errorf("mismatching after format\nbefore: %#v\nafter: %#v", before, after)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user