scanner: add a real example test

This commit is contained in:
Fatih Arslan 2015-10-06 20:57:10 +03:00
parent b5330a1d78
commit 85e52052c6
2 changed files with 109 additions and 52 deletions

View File

@ -242,49 +242,106 @@ func TestFloat(t *testing.T) {
testTokenList(t, tokenLists["float"]) testTokenList(t, tokenLists["float"])
} }
func TestComplexHCL(t *testing.T) { func TestRealExample(t *testing.T) {
// complexHCL = `// This comes from Terraform, as a test complexHCL := `// This comes from Terraform, as a test
// variable "foo" { variable "foo" {
// default = "bar" default = "bar"
// description = "bar" description = "bar"
// } }
//
// provider "aws" { provider "aws" {
// access_key = "foo" access_key = "foo"
// secret_key = "bar" secret_key = "bar"
// } }
//
// provider "do" { resource "aws_security_group" "firewall" {
// api_key = "${var.foo}" count = 5
// } }
//
// resource "aws_security_group" "firewall" { resource aws_instance "web" {
// count = 5 ami = "${var.foo}"
// } security_groups = [
// "foo",
// resource aws_instance "web" { "${aws_security_group.firewall.foo}"
// ami = "${var.foo}" ]
// security_groups = [
// "foo", network_interface {
// "${aws_security_group.firewall.foo}" device_index = 0
// ] description = "Main network interface"
// }
// network_interface { }`
// device_index = 0
// description = "Main network interface" literals := []struct {
// } token token.Token
// } literal string
// }{
// resource "aws_instance" "db" { {token.COMMENT, `// This comes from Terraform, as a test`},
// security_groups = "${aws_security_group.firewall.*.id}" {token.IDENT, `variable`},
// VPC = "foo" {token.STRING, `"foo"`},
// {token.LBRACE, `{`},
// depends_on = ["aws_instance.web"] {token.IDENT, `default`},
// } {token.ASSIGN, `=`},
// {token.STRING, `"bar"`},
// output "web_ip" { {token.IDENT, `description`},
// value = "${aws_instance.web.private_ip}" {token.ASSIGN, `=`},
// }` {token.STRING, `"bar"`},
{token.RBRACE, `}`},
{token.IDENT, `provider`},
{token.STRING, `"aws"`},
{token.LBRACE, `{`},
{token.IDENT, `access_key`},
{token.ASSIGN, `=`},
{token.STRING, `"foo"`},
{token.IDENT, `secret_key`},
{token.ASSIGN, `=`},
{token.STRING, `"bar"`},
{token.RBRACE, `}`},
{token.IDENT, `resource`},
{token.STRING, `"aws_security_group"`},
{token.STRING, `"firewall"`},
{token.LBRACE, `{`},
{token.IDENT, `count`},
{token.ASSIGN, `=`},
{token.NUMBER, `5`},
{token.RBRACE, `}`},
{token.IDENT, `resource`},
{token.IDENT, `aws_instance`},
{token.STRING, `"web"`},
{token.LBRACE, `{`},
{token.IDENT, `ami`},
{token.ASSIGN, `=`},
{token.STRING, `"${var.foo}"`},
{token.IDENT, `security_groups`},
{token.ASSIGN, `=`},
{token.LBRACK, `[`},
{token.STRING, `"foo"`},
{token.COMMA, `,`},
{token.STRING, `"${aws_security_group.firewall.foo}"`},
{token.RBRACK, `]`},
{token.IDENT, `network_interface`},
{token.LBRACE, `{`},
{token.IDENT, `device_index`},
{token.ASSIGN, `=`},
{token.NUMBER, `0`},
{token.IDENT, `description`},
{token.ASSIGN, `=`},
{token.STRING, `"Main network interface"`},
{token.RBRACE, `}`},
{token.RBRACE, `}`},
{token.EOF, ``},
}
s := NewScanner([]byte(complexHCL))
for _, l := range literals {
tok := s.Scan()
if l.token != tok {
t.Errorf("got: %s want %s for %s\n", tok, l.token, s.TokenText())
}
if l.literal != s.TokenText() {
t.Errorf("got: %s want %s\n", s.TokenText(), l.literal)
}
}
} }

View File

@ -46,17 +46,17 @@ var tokens = [...]string{
BOOL: "BOOL", BOOL: "BOOL",
STRING: "STRING", STRING: "STRING",
LBRACK: "[", LBRACK: "LBRACK",
LBRACE: "{", LBRACE: "LBRACE",
COMMA: ",", COMMA: "COMMA",
PERIOD: ".", PERIOD: "PERIOD",
RBRACK: "]", RBRACK: "RBRACK",
RBRACE: "}", RBRACE: "RBRACE",
ASSIGN: "=", ASSIGN: "ASSIGN",
ADD: "+", ADD: "ADD",
SUB: "-", SUB: "SUB",
} }
// String returns the string corresponding to the token tok. // String returns the string corresponding to the token tok.