Hook up a parser parse tests

This commit is contained in:
Mitchell Hashimoto 2014-07-31 14:08:31 -07:00
parent b6a1162606
commit da4c96fbd5
3 changed files with 56 additions and 19 deletions

View File

@ -2,33 +2,32 @@ package hcl
import ( import (
"sync" "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. // be accessed directly.
var exprErrors []error var hclErrors []error
var exprLock sync.Mutex var hclLock sync.Mutex
var exprResult []map[string]interface{} var hclResult map[string]interface{}
/* // Parse parses the given string and returns the result.
// ExprParse parses the given expression and returns an executable func Parse(v string) (map[string]interface{}, error) {
// Interpolation. hclLock.Lock()
func ExprParse(v string) (Interpolation, error) { defer hclLock.Unlock()
exprLock.Lock() hclErrors = nil
defer exprLock.Unlock() hclResult = nil
exprErrors = nil
exprResult = nil
// Parse // Parse
exprParse(&exprLex{input: v}) hclParse(&hclLex{Input: v})
// Build up the errors // Build up the errors
var err error var err error
if len(exprErrors) > 0 { if len(hclErrors) > 0 {
err = &multierror.Error{Errors: exprErrors} err = &multierror.Error{Errors: hclErrors}
exprResult = nil hclResult = nil
} }
return exprResult, err return hclResult, err
} }
*/

View File

@ -22,7 +22,7 @@ package hcl
top: top:
object object
{ {
exprResult = []map[string]interface{}{$1} hclResult = $1
} }
object: object:

38
parse_test.go Normal file
View 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)
}
}
}