parser: add official HCL tests, WIP

This commit is contained in:
Fatih Arslan 2015-10-18 23:25:17 +03:00
parent 393af546c0
commit 9468aa324e
17 changed files with 176 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package parser
import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"runtime"
@ -213,6 +214,82 @@ func TestObjectKey(t *testing.T) {
}
}
// Official HCL tests
func TestParse(t *testing.T) {
cases := []struct {
Name string
Err bool
}{
{
"assign_colon.hcl",
true,
},
{
"comment.hcl",
false,
},
{
"comment_single.hcl",
false,
},
{
"empty.hcl",
false,
},
{
"list_comma.hcl",
false,
},
{
"multiple.hcl",
false,
},
{
"structure.hcl",
false,
},
{
"structure_basic.hcl",
false,
},
{
"structure_empty.hcl",
false,
},
{
"complex.hcl",
false,
},
{
"assign_deep.hcl",
true,
},
{
"types.hcl",
false,
},
{
"array_comment.hcl",
false,
},
}
const fixtureDir = "./test-fixtures"
for _, tc := range cases {
d, err := ioutil.ReadFile(filepath.Join(fixtureDir, tc.Name))
if err != nil {
t.Fatalf("err: %s", err)
}
p := New(d)
_, err = p.Parse()
if (err != nil) != tc.Err {
t.Fatalf("Input: %s\n\nError: %s", tc.Name, err)
}
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {

View File

@ -0,0 +1,4 @@
foo = [
"1",
"2", # comment
]

View File

@ -0,0 +1,6 @@
resource = [{
"foo": {
"bar": {},
"baz": [1, 2, "foo"],
}
}]

View File

@ -0,0 +1,5 @@
resource = [{
foo = [{
bar = {}
}]
}]

View File

@ -0,0 +1,15 @@
// Foo
/* Bar */
/*
/*
Baz
*/
# Another
# Multiple
# Lines
foo = "bar"

View File

@ -0,0 +1 @@
# Hello

View File

@ -0,0 +1,42 @@
// This comes from Terraform, as a test
variable "foo" {
default = "bar"
description = "bar"
}
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
provider "do" {
api_key = "${var.foo}"
}
resource "aws_security_group" "firewall" {
count = 5
}
resource aws_instance "web" {
ami = "${var.foo}"
security_groups = [
"foo",
"${aws_security_group.firewall.foo}"
]
network_interface {
device_index = 0
description = "Main network interface"
}
}
resource "aws_instance" "db" {
security_groups = "${aws_security_group.firewall.*.id}"
VPC = "foo"
depends_on = ["aws_instance.web"]
}
output "web_ip" {
value = "${aws_instance.web.private_ip}"
}

View File

@ -0,0 +1 @@
foo.bar = "baz"

View File

View File

@ -0,0 +1 @@
foo = [1, 2, "foo"]

View File

@ -0,0 +1 @@
foo = [1, 2, "foo",]

View File

@ -0,0 +1,2 @@
foo = "bar"
key = 7

View File

@ -0,0 +1,3 @@
default = {
"eu-west-1": "ami-b1cf19c6",
}

View File

@ -0,0 +1,5 @@
// This is a test structure for the lexer
foo bar "baz" {
key = 7
foo = "bar"
}

View File

@ -0,0 +1,5 @@
foo {
value = 7
"value" = 8
"complex::value" = 9
}

View File

@ -0,0 +1 @@
resource "foo" "bar" {}

View File

@ -0,0 +1,7 @@
foo = "bar"
bar = 7
baz = [1,2,3]
foo = -12
bar = 3.14159
foo = true
bar = false