2017-09-11 23:00:31 +00:00
|
|
|
package hclwrite
|
2017-05-29 23:59:20 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-06 15:53:13 +00:00
|
|
|
"fmt"
|
2017-05-29 23:59:20 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-06-06 15:53:13 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2018-08-01 15:45:22 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2017-05-29 23:59:20 +00:00
|
|
|
)
|
|
|
|
|
2017-06-06 15:53:13 +00:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
src string
|
2018-08-01 15:45:22 +00:00
|
|
|
want TestTreeNode
|
2017-06-06 15:53:13 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"",
|
2018-08-01 15:45:22 +00:00
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-06-09 15:19:47 +00:00
|
|
|
"a = 1\n",
|
2018-08-01 15:45:22 +00:00
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-06 15:53:13 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-10 23:06:09 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " 1",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-07 15:24:33 +00:00
|
|
|
},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-09 15:19:47 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2017-06-09 15:19:47 +00:00
|
|
|
"# aye aye aye\na = 1\n",
|
2018-08-01 15:45:22 +00:00
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
|
|
|
Val: "# aye aye aye\n",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-10 23:06:09 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " 1",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-09 15:19:47 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-09 15:00:38 +00:00
|
|
|
{
|
|
|
|
"a = 1 # because it is\n",
|
2018-08-01 15:45:22 +00:00
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-09 15:00:38 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
2017-06-09 15:00:38 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-09 15:00:38 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
2017-06-09 15:00:38 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " 1",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-09 15:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
Val: " # because it is\n",
|
2017-06-09 15:00:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2017-06-09 15:19:47 +00:00
|
|
|
"# bee bee bee\n\nb = 1\n", // two newlines separate the comment from the attribute
|
2018-08-01 15:45:22 +00:00
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens", // Only lead/line comments attached to an object have type "comments"
|
|
|
|
Val: "# bee bee bee\n\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-09 15:19:47 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " 1",
|
2017-06-09 15:19:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
|
|
|
Val: "",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-02 19:39:03 +00:00
|
|
|
{
|
|
|
|
"a = (\n 1 + 2\n)\nb = 3\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{Type: "comments"},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " (\n 1 + 2\n)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{Type: "comments"},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{Type: "comments"},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " 3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{Type: "comments"},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
"b {}\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Block",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2020-08-21 18:20:48 +00:00
|
|
|
{
|
|
|
|
Type: "blockLabels",
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " {",
|
2019-09-09 20:16:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Body",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"b label {}\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Block",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
|
|
|
},
|
|
|
|
{
|
2020-08-21 18:20:48 +00:00
|
|
|
Type: "blockLabels",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " label",
|
|
|
|
},
|
|
|
|
},
|
2019-09-09 20:16:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " {",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Body",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"b \"label\" {}\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Block",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
|
|
|
},
|
|
|
|
{
|
2020-08-21 18:20:48 +00:00
|
|
|
Type: "blockLabels",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "quoted",
|
|
|
|
Val: ` "label"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " {",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Body",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"b \"label1\" /* foo */ \"label2\" {}\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Block",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "blockLabels",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "quoted",
|
|
|
|
Val: ` "label1"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// The comment between the labels just
|
|
|
|
// becomes an "unstructured tokens"
|
|
|
|
// node, because this isn't a place
|
|
|
|
// where we expect comments to attach
|
|
|
|
// to a particular object as
|
|
|
|
// documentation.
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: ` /* foo */`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "quoted",
|
|
|
|
Val: ` "label2"`,
|
|
|
|
},
|
|
|
|
},
|
2019-09-09 20:16:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " {",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Body",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "}",
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-09 15:19:47 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-11 00:16:19 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
"b {\n a = 1\n}\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Block",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "b",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2020-08-21 18:20:48 +00:00
|
|
|
{
|
|
|
|
Type: "blockLabels",
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " {",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-11 00:16:19 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " 1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "}",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a = foo\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-11 00:16:19 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "comments",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
"a = foo.bar\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
2017-06-11 00:16:19 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: ".",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "bar",
|
|
|
|
},
|
|
|
|
},
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "comments",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2017-06-11 00:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
"a = foo[0]\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
2018-07-14 20:07:39 +00:00
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
|
|
|
Type: "TraverseIndex",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "[",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "number",
|
|
|
|
Val: "0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "]",
|
|
|
|
},
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "comments",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
2018-08-01 15:45:22 +00:00
|
|
|
{
|
2020-04-29 03:38:45 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a = foo.0\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "TraverseIndex",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: ".",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "number",
|
|
|
|
Val: "0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
2018-08-01 15:45:22 +00:00
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
2018-07-14 20:07:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-13 20:02:20 +00:00
|
|
|
{
|
|
|
|
"a = foo.*\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: ".*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a = foo.*.bar\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: ".*.bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a = foo[*]\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "[*]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a = foo[*].bar\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "[*].bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-06 01:03:57 +00:00
|
|
|
{
|
|
|
|
"a = foo[bar]\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "[",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-29 04:08:43 +00:00
|
|
|
{
|
|
|
|
"a = foo[bar.baz]\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "[",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: ".",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-06 01:03:57 +00:00
|
|
|
{
|
|
|
|
"a = foo[bar].baz\n",
|
|
|
|
TestTreeNode{
|
|
|
|
Type: "Body",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Attribute",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: " =",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Expression",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: " foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "[",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Traversal",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "TraverseName",
|
|
|
|
Children: []TestTreeNode{
|
|
|
|
{
|
|
|
|
Type: "identifier",
|
|
|
|
Val: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "].baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "comments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "Tokens",
|
|
|
|
Val: "\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-06-06 15:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.src, func(t *testing.T) {
|
2017-09-11 23:40:37 +00:00
|
|
|
file, diags := parse([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1})
|
2017-06-06 15:53:13 +00:00
|
|
|
if len(diags) > 0 {
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf(" - %s", diag.Error())
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected diagnostics")
|
|
|
|
}
|
|
|
|
|
2018-08-01 15:45:22 +00:00
|
|
|
got := makeTestTree(file.body)
|
2017-06-06 15:53:13 +00:00
|
|
|
|
2018-08-01 15:45:22 +00:00
|
|
|
if !cmp.Equal(got, test.want) {
|
|
|
|
diff := cmp.Diff(got, test.want)
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\ninput:\n%s\n\ngot:\n%s\nwant:%s\n\ndiff:\n%s",
|
|
|
|
test.src,
|
|
|
|
spew.Sdump(got),
|
|
|
|
spew.Sdump(test.want),
|
|
|
|
diff,
|
|
|
|
)
|
2017-06-06 15:53:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPartitionTokens(t *testing.T) {
|
|
|
|
tests := []struct {
|
2017-09-11 23:40:37 +00:00
|
|
|
tokens hclsyntax.Tokens
|
|
|
|
rng hcl.Range
|
2017-06-06 15:53:13 +00:00
|
|
|
wantStart int
|
|
|
|
wantEnd int
|
|
|
|
}{
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{},
|
|
|
|
hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 0},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-06 15:53:13 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 4},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-11 23:40:37 +00:00
|
|
|
hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 4},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-06 15:53:13 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 4},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 4},
|
|
|
|
End: hcl.Pos{Byte: 8},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 8},
|
|
|
|
End: hcl.Pos{Byte: 12},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-11 23:40:37 +00:00
|
|
|
hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 4},
|
|
|
|
End: hcl.Pos{Byte: 8},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-06 15:53:13 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 4},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 4},
|
|
|
|
End: hcl.Pos{Byte: 8},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 8},
|
|
|
|
End: hcl.Pos{Byte: 12},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-11 23:40:37 +00:00
|
|
|
hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 8},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-06 15:53:13 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 0},
|
|
|
|
End: hcl.Pos{Byte: 4},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 4},
|
|
|
|
End: hcl.Pos{Byte: 8},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
|
|
|
Range: hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 8},
|
|
|
|
End: hcl.Pos{Byte: 12},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-11 23:40:37 +00:00
|
|
|
hcl.Range{
|
|
|
|
Start: hcl.Pos{Byte: 4},
|
|
|
|
End: hcl.Pos{Byte: 12},
|
2017-06-06 15:53:13 +00:00
|
|
|
},
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyConfig := &pretty.Config{
|
|
|
|
Diffable: true,
|
|
|
|
IncludeUnexported: true,
|
|
|
|
PrintStringers: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
gotStart, gotEnd := partitionTokens(test.tokens, test.rng)
|
|
|
|
|
|
|
|
if gotStart != test.wantStart || gotEnd != test.wantEnd {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\ntokens: %s\nrange: %#v\ngot: %d, %d\nwant: %d, %d",
|
|
|
|
prettyConfig.Sprint(test.tokens), test.rng,
|
|
|
|
gotStart, test.wantStart,
|
|
|
|
gotEnd, test.wantEnd,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:04:27 +00:00
|
|
|
func TestPartitionLeadCommentTokens(t *testing.T) {
|
|
|
|
tests := []struct {
|
2017-09-11 23:40:37 +00:00
|
|
|
tokens hclsyntax.Tokens
|
2017-06-08 16:04:27 +00:00
|
|
|
wantStart int
|
|
|
|
}{
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{},
|
2017-06-08 16:04:27 +00:00
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenComment,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenComment,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenComment,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenComment,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenNewline,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
hclsyntax.Tokens{
|
2017-06-08 16:04:27 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenComment,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenNewline,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenComment,
|
2017-06-08 16:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyConfig := &pretty.Config{
|
|
|
|
Diffable: true,
|
|
|
|
IncludeUnexported: true,
|
|
|
|
PrintStringers: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
gotStart := partitionLeadCommentTokens(test.tokens)
|
|
|
|
|
|
|
|
if gotStart != test.wantStart {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\ntokens: %s\ngot: %d\nwant: %d",
|
|
|
|
prettyConfig.Sprint(test.tokens),
|
|
|
|
gotStart, test.wantStart,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-29 23:59:20 +00:00
|
|
|
func TestLexConfig(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
want Tokens
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`a b `,
|
|
|
|
Tokens{
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`a`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`b`),
|
|
|
|
SpacesBefore: 2,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenEOF,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte{},
|
|
|
|
SpacesBefore: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
foo "bar" "baz" {
|
|
|
|
pizza = " cheese "
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
Tokens{
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenNewline,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte{'\n'},
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`foo`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenOQuote,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`"`),
|
|
|
|
SpacesBefore: 1,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenQuotedLit,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`bar`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenCQuote,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`"`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenOQuote,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`"`),
|
|
|
|
SpacesBefore: 1,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenQuotedLit,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`baz`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenCQuote,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`"`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenOBrace,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`{`),
|
|
|
|
SpacesBefore: 1,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenNewline,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte("\n"),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenIdent,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`pizza`),
|
|
|
|
SpacesBefore: 4,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenEqual,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`=`),
|
|
|
|
SpacesBefore: 1,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenOQuote,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`"`),
|
|
|
|
SpacesBefore: 1,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenQuotedLit,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(` cheese `),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenCQuote,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`"`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenNewline,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte("\n"),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenCBrace,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte(`}`),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenNewline,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte("\n"),
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Type: hclsyntax.TokenEOF,
|
2017-05-29 23:59:20 +00:00
|
|
|
Bytes: []byte{},
|
|
|
|
SpacesBefore: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyConfig := &pretty.Config{
|
|
|
|
Diffable: true,
|
|
|
|
IncludeUnexported: true,
|
|
|
|
PrintStringers: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.input, func(t *testing.T) {
|
|
|
|
got := lexConfig([]byte(test.input))
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
diff := prettyConfig.Compare(test.want, got)
|
|
|
|
t.Errorf(
|
|
|
|
"wrong result\ninput: %s\ndiff: %s", test.input, diff,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|