2015-10-11 23:27:43 +00:00
|
|
|
package parser
|
|
|
|
|
2015-10-15 21:57:57 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-15 23:00:02 +00:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2015-10-15 21:57:57 +00:00
|
|
|
"testing"
|
2015-10-15 23:00:02 +00:00
|
|
|
|
|
|
|
"github.com/fatih/hcl/scanner"
|
2015-10-15 21:57:57 +00:00
|
|
|
)
|
2015-10-11 23:27:43 +00:00
|
|
|
|
2015-10-16 19:57:56 +00:00
|
|
|
func TestParseType(t *testing.T) {
|
|
|
|
src := `foo = true`
|
|
|
|
p := New([]byte(src))
|
|
|
|
p.enableTrace = true
|
|
|
|
|
|
|
|
n, err := p.Parse()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("n = %+v\n", n)
|
|
|
|
|
|
|
|
Walk(n, func(node Node) bool {
|
|
|
|
fmt.Printf("node = %+v\n", node)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:00:02 +00:00
|
|
|
func TestObjectKey(t *testing.T) {
|
|
|
|
keys := []struct {
|
|
|
|
exp []scanner.TokenType
|
|
|
|
src string
|
|
|
|
}{
|
|
|
|
{[]scanner.TokenType{scanner.IDENT}, `foo {}`},
|
|
|
|
{[]scanner.TokenType{scanner.IDENT}, `foo = {}`},
|
2015-10-16 11:16:12 +00:00
|
|
|
{[]scanner.TokenType{scanner.IDENT}, `foo = bar`},
|
|
|
|
{[]scanner.TokenType{scanner.IDENT}, `foo = 123`},
|
2015-10-15 23:00:02 +00:00
|
|
|
{[]scanner.TokenType{scanner.IDENT}, `foo = "${var.bar}`},
|
|
|
|
{[]scanner.TokenType{scanner.STRING}, `"foo" {}`},
|
|
|
|
{[]scanner.TokenType{scanner.STRING}, `"foo" = {}`},
|
|
|
|
{[]scanner.TokenType{scanner.STRING}, `"foo" = "${var.bar}`},
|
|
|
|
{[]scanner.TokenType{scanner.IDENT, scanner.IDENT}, `foo bar {}`},
|
|
|
|
{[]scanner.TokenType{scanner.IDENT, scanner.STRING}, `foo "bar" {}`},
|
|
|
|
{[]scanner.TokenType{scanner.STRING, scanner.IDENT}, `"foo" bar {}`},
|
|
|
|
{[]scanner.TokenType{scanner.IDENT, scanner.IDENT, scanner.IDENT}, `foo bar baz {}`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
p := New([]byte(k.src))
|
|
|
|
keys, err := p.parseObjectKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := []scanner.TokenType{}
|
|
|
|
for _, o := range keys {
|
|
|
|
tokens = append(tokens, o.token.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(t, k.exp, tokens)
|
2015-10-12 07:37:37 +00:00
|
|
|
}
|
2015-10-11 23:27:43 +00:00
|
|
|
|
2015-10-15 23:00:02 +00:00
|
|
|
errKeys := []struct {
|
|
|
|
src string
|
|
|
|
}{
|
|
|
|
{`foo 12 {}`},
|
|
|
|
{`foo bar = {}`},
|
|
|
|
{`foo []`},
|
|
|
|
{`12 {}`},
|
2015-10-11 23:49:07 +00:00
|
|
|
}
|
2015-10-15 21:57:57 +00:00
|
|
|
|
2015-10-15 23:00:02 +00:00
|
|
|
for _, k := range errKeys {
|
|
|
|
p := New([]byte(k.src))
|
|
|
|
_, err := p.parseObjectKey()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case '%s' should give an error", k.src)
|
|
|
|
}
|
2015-10-15 21:57:57 +00:00
|
|
|
}
|
2015-10-15 23:00:02 +00:00
|
|
|
}
|
2015-10-15 21:57:57 +00:00
|
|
|
|
2015-10-15 23:00:02 +00:00
|
|
|
// equals fails the test if exp is not equal to act.
|
|
|
|
func equals(tb testing.TB, exp, act interface{}) {
|
|
|
|
if !reflect.DeepEqual(exp, act) {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
|
|
|
|
tb.FailNow()
|
2015-10-15 21:57:57 +00:00
|
|
|
}
|
2015-10-11 23:27:43 +00:00
|
|
|
}
|