pulumi/pkg/codegen/hcl2/model/pretty/display_test.go

272 lines
5.2 KiB
Go
Raw Permalink Normal View History

// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pretty
import (
"testing"
"github.com/stretchr/testify/assert"
)
type test struct {
expected string
formatter Formatter
}
func testFormatter(t *testing.T, tests []test) {
for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.expected, tt.formatter.String())
})
}
}
func TestIndentFormatter(t *testing.T) {
t.Parallel()
testFormatter(t, []test{
{
">>123",
&indent{
prefix: ">>",
inner: FromString("123"),
},
},
{
">>123\n>>456",
&indent{
prefix: ">>",
inner: FromString("123\n456"),
},
},
})
}
func TestObjectFormatter(t *testing.T) {
t.Parallel()
testFormatter(t, []test{
{
"{ fizz: abc, hello: world }",
&Object{
Properties: map[string]Formatter{
"fizz": FromString("abc"),
"hello": FromString("world"),
},
},
},
{
"{\n fizz: abc,\n hello: world,\n}",
(&Object{
Properties: map[string]Formatter{
"fizz": FromString("abc"),
"hello": FromString("world"),
},
}).Columns(18),
},
{
"{\n aFoo: bar?,\n bFizz: \n buzz,\n}",
(&Object{
Properties: map[string]Formatter{
"aFoo": &Wrap{
Postfix: "?",
PostfixSameline: true,
Value: FromString("bar"),
},
"bFizz": FromString("buzz"),
},
}).Columns(14),
},
})
}
func TestWrapFormatter(t *testing.T) {
t.Parallel()
testFormatter(t, []test{
{
"A(123456)",
Wrap{
Prefix: "A(",
Postfix: ")",
Value: FromString("123456"),
}.Columns(10),
},
{
"B(\n 123456\n)",
Wrap{
Prefix: "B(",
Postfix: ")",
Value: FromString("123456"),
}.Columns(9),
},
{
"C({\n 123456\n 123456\n})",
Wrap{
Prefix: "C(",
Postfix: ")",
Value: FromString("{\n 123456\n 123456\n}"),
}.Columns(6),
},
{
"foo-bar:\n fizz-buzz,",
Wrap{
Prefix: "foo-bar:",
Postfix: ",",
PostfixSameline: true,
Value: FromString("fizz-buzz"),
}.Columns(8),
},
})
}
func TestListFormatter(t *testing.T) {
t.Parallel()
commaList := &List{
AdjoinSeparator: true,
Separator: ", ",
Elements: []Formatter{
FromString("a"),
FromString("b"),
FromString("c"),
FromString("d"),
},
}
barList := &List{
Separator: " | ",
Elements: []Formatter{
FromString("a"),
FromString("b"),
FromString("c"),
FromString("d"),
},
}
testFormatter(t, []test{
{"a, b, c, d", commaList},
{"a,\nb,\nc,\nd", commaList.Columns(4)},
{"a | b | c | d", barList},
{" a\n| b\n| c\n| d", barList.Columns(4)},
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
{
` a
| b
| [
1,
2,
3,
4
]
| c`,
(&List{
Separator: " | ",
Elements: []Formatter{
FromString("a"),
FromString("b"),
&Wrap{
Prefix: "[",
Postfix: "]",
Value: &List{
AdjoinSeparator: true,
Separator: ", ",
Elements: []Formatter{
FromString("1"),
FromString("2"),
FromString("3"),
FromString("4"),
},
},
},
FromString("c"),
},
}).Columns(4),
},
})
}
func TestObjectTagging(t *testing.T) {
t.Parallel()
testFormatter(t, []test{
// Direct recursion
{`'T1 { foo: val1, rec: 'T1 }`, func() Formatter {
obj := &Object{
Properties: map[string]Formatter{
"foo": FromString("val1"),
},
}
obj.Properties["rec"] = obj
return obj
}()},
// Mutual recursion
{`'T1 { foo: val1, mutRec: { rec: 'T1 } }`, func() Formatter {
obj := &Object{
Properties: map[string]Formatter{
"foo": FromString("val1"),
},
}
obj.Properties["mutRec"] = &Object{
Properties: map[string]Formatter{
"rec": obj,
},
}
return obj
}()},
// Direct and mutual recursion
{`'T1 { foo: val1, mutRec: { rec: 'T1 }, rec: 'T1 }`, func() Formatter {
obj := &Object{
Properties: map[string]Formatter{
"foo": FromString("val1"),
},
}
obj.Properties["rec"] = obj
obj.Properties["mutRec"] = &Object{
Properties: map[string]Formatter{
"rec": obj,
},
}
return obj
}()},
// Non-recursive duplicates
{`{ one: { foo: val }, two: { foo: val } }`, func() Formatter {
inner := &Object{
Properties: map[string]Formatter{
"foo": FromString("val"),
},
}
return &Object{
Properties: map[string]Formatter{
"one": inner,
"two": inner,
},
}
}()},
// Structurally recursive object without matching pointer recursion.
{`'T1 { key: 'T1 }`, func() Formatter {
obj := &Object{
Properties: map[string]Formatter{},
}
obj.Properties["key"] = &Object{
Properties: map[string]Formatter{
"key": obj,
},
}
return obj
}()},
})
}