c88641b147
The native parser's ranges don't include any surrounding comments, so we need to do a little more work to pick them out of the surrounding token sequences. This just takes care of _lead_ comments, which are those that appear as whole line comments above the item in question. Line comments, which appear after the item on the same line, will follow in a later commit.
57 lines
961 B
Go
57 lines
961 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)
|
|
}
|
|
})
|
|
}
|
|
}
|