fix panic on decode of "t=0t d{}"

This commit is contained in:
Mitchell Hashimoto 2016-06-21 13:18:51 -07:00
parent 352bb4b5e3
commit 6816a5c3fb
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 15 additions and 7 deletions

View File

@ -338,11 +338,8 @@ func TestDecode_interfaceInline(t *testing.T) {
Err bool
Out interface{}
}{
{
"t t e{{}}",
true,
nil,
},
{"t t e{{}}", true, nil},
{"t=0t d {}", true, map[string]interface{}{"t": 0}},
}
for _, tc := range cases {

View File

@ -133,6 +133,12 @@ type ObjectItem struct {
}
func (o *ObjectItem) Pos() token.Pos {
// I'm not entirely sure what causes this, but removing this causes
// a test failure. We should investigate at some point.
if len(o.Keys) == 0 {
return token.Pos{}
}
return o.Keys[0].Pos()
}

View File

@ -8,6 +8,8 @@ import (
"runtime"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/token"
)
@ -338,12 +340,15 @@ func TestParse_inline(t *testing.T) {
Err bool
}{
{"t t e{{}}", true},
{"o{{}}", true},
{"t t e d N{{}}", true},
{"t t e d{{}}", true},
}
for _, tc := range cases {
_, err := Parse([]byte(tc.Value))
ast, err := Parse([]byte(tc.Value))
if (err != nil) != tc.Err {
t.Fatalf("Input: %q\n\nError: %s", tc.Value, err)
t.Fatalf("Input: %q\n\nError: %s\n\nAST: %s", tc.Value, err, spew.Sdump(ast))
}
}
}