hcl/zclwrite/round_trip_test.go
Martin Atkins 5477fecfad zclsyntax: require newlines after block items
Previously we tolerated EOF as an alias for newline, but a file without
a newline at the end is a edge case primarily limited to contrived
examples in unit tests, and requiring it simplifies tasks such as code
generation in zclwrite, since we can always assume that every block item
comes with a built-in line terminator.
2017-06-09 08:19:47 -07:00

58 lines
962 B
Go

package zclwrite
import (
"bytes"
"testing"
"github.com/zclconf/go-zcl/zcl"
)
func TestRoundTrip(t *testing.T) {
tests := []string{
``,
`foo = 1
`,
`
foobar = 1
baz = 1
`,
`
# this file is awesome
# tossed salads and scrambled eggs
foobar = 1
baz = 1
# and they all lived happily ever after
`,
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
src := []byte(test)
file, diags := parse(src, "", zcl.Pos{Line: 1, Column: 1})
if len(diags) != 0 {
for _, diag := range diags {
t.Logf(" - %s", diag.Error())
}
t.Fatalf("unexpected diagnostics")
}
wr := &bytes.Buffer{}
n, err := file.WriteTo(wr)
if n != len(test) {
t.Errorf("wrong number of bytes %d; want %d", n, len(test))
}
if err != nil {
t.Fatalf("error from WriteTo")
}
result := wr.Bytes()
if !bytes.Equal(result, src) {
t.Errorf("wrong result\nresult:\n%s\ninput:\n%s", result, src)
}
})
}
}