5477fecfad
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.
110 lines
1.9 KiB
Go
110 lines
1.9 KiB
Go
package zcldec
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/zclconf/go-zcl/zcl"
|
|
"github.com/zclconf/go-zcl/zcl/zclsyntax"
|
|
)
|
|
|
|
func TestVariables(t *testing.T) {
|
|
tests := []struct {
|
|
config string
|
|
spec Spec
|
|
want []zcl.Traversal
|
|
}{
|
|
{
|
|
``,
|
|
&ObjectSpec{},
|
|
nil,
|
|
},
|
|
{
|
|
"a = foo\n",
|
|
&ObjectSpec{},
|
|
nil, // "a" is not actually used, so "foo" is not required
|
|
},
|
|
{
|
|
"a = foo\n",
|
|
&AttrSpec{
|
|
Name: "a",
|
|
},
|
|
[]zcl.Traversal{
|
|
{
|
|
zcl.TraverseRoot{
|
|
Name: "foo",
|
|
SrcRange: zcl.Range{
|
|
Start: zcl.Pos{Line: 1, Column: 5, Byte: 4},
|
|
End: zcl.Pos{Line: 1, Column: 8, Byte: 7},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"a = foo\n",
|
|
&ObjectSpec{
|
|
"a": &AttrSpec{
|
|
Name: "a",
|
|
},
|
|
},
|
|
[]zcl.Traversal{
|
|
{
|
|
zcl.TraverseRoot{
|
|
Name: "foo",
|
|
SrcRange: zcl.Range{
|
|
Start: zcl.Pos{Line: 1, Column: 5, Byte: 4},
|
|
End: zcl.Pos{Line: 1, Column: 8, Byte: 7},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
`
|
|
b {
|
|
a = foo
|
|
}
|
|
`,
|
|
&BlockSpec{
|
|
TypeName: "b",
|
|
Nested: &AttrSpec{
|
|
Name: "a",
|
|
},
|
|
},
|
|
[]zcl.Traversal{
|
|
{
|
|
zcl.TraverseRoot{
|
|
Name: "foo",
|
|
SrcRange: zcl.Range{
|
|
Start: zcl.Pos{Line: 3, Column: 7, Byte: 11},
|
|
End: zcl.Pos{Line: 3, Column: 10, Byte: 14},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
t.Run(fmt.Sprintf("%02d-%s", i, test.config), func(t *testing.T) {
|
|
file, diags := zclsyntax.ParseConfig([]byte(test.config), "", zcl.Pos{Line: 1, Column: 1, Byte: 0})
|
|
if len(diags) != 0 {
|
|
t.Errorf("wrong number of diagnostics from ParseConfig %d; want %d", len(diags), 0)
|
|
for _, diag := range diags {
|
|
t.Logf(" - %s", diag.Error())
|
|
}
|
|
}
|
|
body := file.Body
|
|
|
|
got := Variables(body, test.spec)
|
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|