2017-09-11 23:00:31 +00:00
|
|
|
|
package hclwrite
|
2017-06-10 14:47:15 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"reflect"
|
|
|
|
|
|
2017-06-10 21:11:47 +00:00
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-09-09 23:08:19 +00:00
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2017-06-10 14:47:15 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-06-10 21:11:47 +00:00
|
|
|
|
func TestFormat(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
``,
|
|
|
|
|
``,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`a=1`,
|
|
|
|
|
`a = 1`,
|
|
|
|
|
},
|
2017-06-24 16:08:49 +00:00
|
|
|
|
{
|
|
|
|
|
`a=b.c`,
|
|
|
|
|
`a = b.c`,
|
|
|
|
|
},
|
2018-05-23 23:56:29 +00:00
|
|
|
|
{
|
|
|
|
|
`a=b[c]`,
|
|
|
|
|
`a = b[c]`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`a=b()[c]`,
|
|
|
|
|
`a = b()[c]`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`a=["hello"][0]`,
|
|
|
|
|
`a = ["hello"][0]`,
|
|
|
|
|
},
|
2017-06-10 21:11:47 +00:00
|
|
|
|
{
|
|
|
|
|
`( a+2 )`,
|
|
|
|
|
`(a + 2)`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-06-24 16:12:24 +00:00
|
|
|
|
`( a*2 )`,
|
|
|
|
|
`(a * 2)`,
|
2017-06-10 21:11:47 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`( a+-2 )`,
|
|
|
|
|
`(a + -2)`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-06-24 16:12:24 +00:00
|
|
|
|
`( a*-2 )`,
|
|
|
|
|
`(a * -2)`,
|
2017-06-10 21:11:47 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`(-2+1)`,
|
|
|
|
|
`(-2 + 1)`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-06-24 16:12:24 +00:00
|
|
|
|
`foo(1, -2,a*b, b,c)`,
|
|
|
|
|
`foo(1, -2, a * b, b, c)`,
|
2017-06-10 21:11:47 +00:00
|
|
|
|
},
|
2019-05-15 22:32:00 +00:00
|
|
|
|
{
|
|
|
|
|
`foo(a,b...)`,
|
|
|
|
|
`foo(a, b...)`,
|
|
|
|
|
},
|
2017-06-10 21:11:47 +00:00
|
|
|
|
{
|
|
|
|
|
`a="hello ${ name }"`,
|
|
|
|
|
`a = "hello ${name}"`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`a="hello ${~ name ~}"`,
|
|
|
|
|
`a = "hello ${~name~}"`,
|
|
|
|
|
},
|
2019-01-16 10:33:06 +00:00
|
|
|
|
{
|
|
|
|
|
`a="${b}${c}${ d } ${e}"`,
|
|
|
|
|
`a = "${b}${c}${d} ${e}"`,
|
|
|
|
|
},
|
2019-08-09 21:00:04 +00:00
|
|
|
|
{
|
|
|
|
|
`"%{if true}${var.foo}%{endif}"`,
|
|
|
|
|
`"%{if true}${var.foo}%{endif}"`,
|
|
|
|
|
},
|
2017-06-10 21:11:47 +00:00
|
|
|
|
{
|
|
|
|
|
`b{}`,
|
|
|
|
|
`b {}`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
"${
|
|
|
|
|
hello
|
|
|
|
|
}"
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
"${
|
|
|
|
|
hello
|
|
|
|
|
}"
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo(
|
|
|
|
|
1,
|
|
|
|
|
- 2,
|
2017-06-24 16:12:24 +00:00
|
|
|
|
a*b,
|
2017-06-10 21:11:47 +00:00
|
|
|
|
b,
|
|
|
|
|
c,
|
|
|
|
|
)
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo(
|
|
|
|
|
1,
|
|
|
|
|
-2,
|
2017-06-24 16:12:24 +00:00
|
|
|
|
a * b,
|
2017-06-10 21:11:47 +00:00
|
|
|
|
b,
|
|
|
|
|
c,
|
|
|
|
|
)
|
|
|
|
|
`,
|
|
|
|
|
},
|
2018-12-14 23:53:02 +00:00
|
|
|
|
{
|
|
|
|
|
`a?b:c`,
|
|
|
|
|
`a ? b : c`,
|
|
|
|
|
},
|
2017-06-10 21:11:47 +00:00
|
|
|
|
{
|
|
|
|
|
`[ [ ] ]`,
|
|
|
|
|
`[[]]`,
|
|
|
|
|
},
|
2018-12-14 22:19:04 +00:00
|
|
|
|
{
|
2018-12-14 23:53:02 +00:00
|
|
|
|
`[for x in y : x]`,
|
|
|
|
|
`[for x in y : x]`,
|
2018-12-14 22:19:04 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-12-14 23:53:02 +00:00
|
|
|
|
`[for x in [y] : x]`,
|
|
|
|
|
`[for x in [y] : x]`,
|
2018-12-14 22:19:04 +00:00
|
|
|
|
},
|
2017-06-10 21:11:47 +00:00
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
a
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
a
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
[[
|
|
|
|
|
a
|
|
|
|
|
]]
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
[[
|
|
|
|
|
a
|
|
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le = 2
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = 2
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = 2
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
# here we go
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
# here we go
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {} # here we go
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {} # here we go
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
zebra = "striped" # baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = "bonce" # baz
|
|
|
|
|
zebra = "striped" # baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = (
|
|
|
|
|
"bonce"
|
|
|
|
|
) # baz
|
|
|
|
|
zebra = "striped" # baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
a = 1 # foo
|
|
|
|
|
bungle = (
|
|
|
|
|
"bonce"
|
|
|
|
|
) # baz
|
|
|
|
|
zebra = "striped" # baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
a="apple"# foo
|
|
|
|
|
bungle=(# woo parens
|
|
|
|
|
"bonce"
|
|
|
|
|
)# baz
|
|
|
|
|
zebra="striped"# baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
a = "apple" # foo
|
|
|
|
|
bungle = ( # woo parens
|
|
|
|
|
"bonce"
|
|
|
|
|
) # baz
|
|
|
|
|
zebra = "striped" # baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
𝒜 = 1 # foo
|
|
|
|
|
bungle = "🇬🇧" # baz
|
|
|
|
|
zebra = "striped" # baz
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
𝒜 = 1 # foo
|
|
|
|
|
bungle = "🇬🇧" # baz
|
|
|
|
|
zebra = "striped" # baz
|
2018-10-31 13:21:11 +00:00
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
# ...
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
# ...
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo = {
|
|
|
|
|
# ...
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo = {
|
|
|
|
|
# ...
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo = [
|
|
|
|
|
# ...
|
|
|
|
|
]
|
|
|
|
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
|
|
|
|
bar = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
blahblahblah = x
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
blahblahblah = x
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
${{ blahblahblah = x }}
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
${ { blahblahblah = x } }
|
|
|
|
|
EOT
|
|
|
|
|
}
|
2019-01-16 10:33:06 +00:00
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
${a}${b}${ c } ${d}
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<-EOT
|
|
|
|
|
${a}${b}${c} ${d}
|
|
|
|
|
EOT
|
|
|
|
|
}
|
2019-03-15 20:19:41 +00:00
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baz {
|
|
|
|
|
default="string"
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baz {
|
|
|
|
|
default = "string"
|
|
|
|
|
}
|
2019-07-22 19:57:18 +00:00
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
baz = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bar {
|
|
|
|
|
foo = "bar"
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
`
|
|
|
|
|
foo {
|
|
|
|
|
bar = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
baz = <<EOT
|
|
|
|
|
Foo bar baz
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bar {
|
|
|
|
|
foo = "bar"
|
|
|
|
|
}
|
2017-06-10 21:11:47 +00:00
|
|
|
|
`,
|
|
|
|
|
},
|
2019-07-25 00:18:11 +00:00
|
|
|
|
{
|
|
|
|
|
`
|
|
|
|
|
module "foo" {
|
|
|
|
|
foo = <<EOF
|
|
|
|
|
5
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module "x" {
|
|
|
|
|
a = "b"
|
|
|
|
|
abcde = "456"
|
|
|
|
|
}`,
|
|
|
|
|
`
|
|
|
|
|
module "foo" {
|
|
|
|
|
foo = <<EOF
|
|
|
|
|
5
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module "x" {
|
|
|
|
|
a = "b"
|
|
|
|
|
abcde = "456"
|
|
|
|
|
}`,
|
|
|
|
|
},
|
2017-06-10 21:11:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
|
tokens := lexConfig([]byte(test.input))
|
|
|
|
|
format(tokens)
|
|
|
|
|
t.Logf("tokens %s\n", spew.Sdump(tokens))
|
|
|
|
|
got := string(tokens.Bytes())
|
|
|
|
|
|
|
|
|
|
if got != test.want {
|
|
|
|
|
t.Errorf("wrong result\ninput:\n%s\ngot:\n%s\nwant:\n%s", test.input, got, test.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-10 14:47:15 +00:00
|
|
|
|
func TestLinesForFormat(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
tokens Tokens
|
|
|
|
|
want []formatLine
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNewline},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNewline},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
|
|
|
|
&Token{Type: hclsyntax.TokenComment, Bytes: []byte("#foo\n")},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
comment: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenComment, Bytes: []byte("#foo\n")},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEqual},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
assign: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenEqual},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEqual},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
|
|
|
|
&Token{Type: hclsyntax.TokenComment, Bytes: []byte("#foo\n")},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenIdent},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
assign: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenEqual},
|
|
|
|
|
&Token{Type: hclsyntax.TokenNumberLit},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
comment: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenComment, Bytes: []byte("#foo\n")},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Tokens{
|
|
|
|
|
// A comment goes into a comment cell only if it is after
|
|
|
|
|
// some non-comment tokens, since whole-line comments must
|
|
|
|
|
// stay flush with the indent level.
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenComment, Bytes: []byte("#foo\n")},
|
|
|
|
|
&Token{Type: hclsyntax.TokenEOF},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
[]formatLine{
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{
|
2017-09-11 23:40:37 +00:00
|
|
|
|
&Token{Type: hclsyntax.TokenComment, Bytes: []byte("#foo\n")},
|
2017-06-10 14:47:15 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
lead: Tokens{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
|
got := linesForFormat(test.tokens)
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|