Hook up a parser parse tests
This commit is contained in:
parent
b6a1162606
commit
da4c96fbd5
35
parse.go
35
parse.go
@ -2,33 +2,32 @@ package hcl
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/multierror"
|
||||
)
|
||||
|
||||
// exprErrors are the errors built up from parsing. These should not
|
||||
// hclErrors are the errors built up from parsing. These should not
|
||||
// be accessed directly.
|
||||
var exprErrors []error
|
||||
var exprLock sync.Mutex
|
||||
var exprResult []map[string]interface{}
|
||||
var hclErrors []error
|
||||
var hclLock sync.Mutex
|
||||
var hclResult map[string]interface{}
|
||||
|
||||
/*
|
||||
// ExprParse parses the given expression and returns an executable
|
||||
// Interpolation.
|
||||
func ExprParse(v string) (Interpolation, error) {
|
||||
exprLock.Lock()
|
||||
defer exprLock.Unlock()
|
||||
exprErrors = nil
|
||||
exprResult = nil
|
||||
// Parse parses the given string and returns the result.
|
||||
func Parse(v string) (map[string]interface{}, error) {
|
||||
hclLock.Lock()
|
||||
defer hclLock.Unlock()
|
||||
hclErrors = nil
|
||||
hclResult = nil
|
||||
|
||||
// Parse
|
||||
exprParse(&exprLex{input: v})
|
||||
hclParse(&hclLex{Input: v})
|
||||
|
||||
// Build up the errors
|
||||
var err error
|
||||
if len(exprErrors) > 0 {
|
||||
err = &multierror.Error{Errors: exprErrors}
|
||||
exprResult = nil
|
||||
if len(hclErrors) > 0 {
|
||||
err = &multierror.Error{Errors: hclErrors}
|
||||
hclResult = nil
|
||||
}
|
||||
|
||||
return exprResult, err
|
||||
return hclResult, err
|
||||
}
|
||||
*/
|
||||
|
2
parse.y
2
parse.y
@ -22,7 +22,7 @@ package hcl
|
||||
top:
|
||||
object
|
||||
{
|
||||
exprResult = []map[string]interface{}{$1}
|
||||
hclResult = $1
|
||||
}
|
||||
|
||||
object:
|
||||
|
38
parse_test.go
Normal file
38
parse_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package hcl
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input string
|
||||
Output map[string]interface{}
|
||||
}{
|
||||
{
|
||||
"comment.hcl",
|
||||
map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
d, err := ioutil.ReadFile(filepath.Join(fixtureDir, tc.Input))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual, err := Parse(string(d))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actual, tc.Output) {
|
||||
t.Fatalf("Input: %s\n\nBad: %#v", tc.Input, actual)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user