hcl/zclwrite/round_trip_test.go
Martin Atkins 598740b638 zclwrite: method for writing tokens to a writer
This now allows for round-tripping some input from bytes to tokens and
then back to bytes again. With no other changes, we expect this to produce
an identical result.
2017-06-07 07:06:23 -07:00

59 lines
999 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
block "thing" {
foobar = 1 # quite stylish
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)
}
})
}
}