Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 76 additions & 70 deletions libs/cmdio/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,85 +87,91 @@ func must[T any](a T, e error) T {
return a
}

var testCases = []testCase{
{
name: "Workspace with header and template",
v: dummyWorkspace1,
outputFormat: flags.OutputText,
headerTemplate: "id\tname",
template: "{{.WorkspaceId}}\t{{.WorkspaceName}}",
expected: `id name
// makeTestCases builds the table fresh on every call. The Workspace_Iterator
// rows wrap a stateful *dummyIterator that's consumed by Next; sharing one
// across iterations of TestRender (which `go test -count=N` does) makes the
// second run see an empty iterator and the test fails.
func makeTestCases() []testCase {
return []testCase{
{
name: "Workspace with header and template",
v: dummyWorkspace1,
outputFormat: flags.OutputText,
headerTemplate: "id\tname",
template: "{{.WorkspaceId}}\t{{.WorkspaceName}}",
expected: `id name
123 abc`,
},
{
name: "Workspace with no header and template",
v: dummyWorkspace1,
outputFormat: flags.OutputText,
template: "{{.WorkspaceId}}\t{{.WorkspaceName}}",
expected: `123 abc`,
},
{
name: "Workspace with no header and no template",
v: dummyWorkspace1,
outputFormat: flags.OutputText,
expected: `{
},
{
name: "Workspace with no header and template",
v: dummyWorkspace1,
outputFormat: flags.OutputText,
template: "{{.WorkspaceId}}\t{{.WorkspaceName}}",
expected: `123 abc`,
},
{
name: "Workspace with no header and no template",
v: dummyWorkspace1,
outputFormat: flags.OutputText,
expected: `{
"workspace_id": 123,
"workspace_name": "abc"
}
`,
},
{
name: "Workspace Iterator with header and template",
v: makeIterator(2),
outputFormat: flags.OutputText,
headerTemplate: "id\tname",
template: "{{range .}}{{.WorkspaceId}}\t{{.WorkspaceName}}\n{{end}}",
expected: `id name
},
{
name: "Workspace Iterator with header and template",
v: makeIterator(2),
outputFormat: flags.OutputText,
headerTemplate: "id\tname",
template: "{{range .}}{{.WorkspaceId}}\t{{.WorkspaceName}}\n{{end}}",
expected: `id name
123 abc
456 def
`,
},
{
name: "Workspace Iterator with no header and template",
v: makeIterator(2),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not keep test case data-oriented, so store v: 2 and call makeIterator(v) inside the test func.

outputFormat: flags.OutputText,
template: "{{range .}}{{.WorkspaceId}}\t{{.WorkspaceName}}\n{{end}}",
expected: `123 abc
},
{
name: "Workspace Iterator with no header and template",
v: makeIterator(2),
outputFormat: flags.OutputText,
template: "{{range .}}{{.WorkspaceId}}\t{{.WorkspaceName}}\n{{end}}",
expected: `123 abc
456 def
`,
},
{
name: "Workspace Iterator with no header and no template",
v: makeIterator(2),
outputFormat: flags.OutputText,
expected: string(must(json.MarshalIndent(makeWorkspaces(2), "", " "))) + "\n",
},
{
name: "Big Workspace Iterator with template",
v: makeIterator(234),
outputFormat: flags.OutputText,
headerTemplate: "id\tname",
template: "{{range .}}{{.WorkspaceId}}\t{{.WorkspaceName}}\n{{end}}",
expected: "id name\n" + makeBigOutput(234),
},
{
name: "Big Workspace Iterator with no template",
v: makeIterator(234),
outputFormat: flags.OutputText,
expected: string(must(json.MarshalIndent(makeWorkspaces(234), "", " "))) + "\n",
},
{
name: "io.Reader",
v: strings.NewReader("a test"),
outputFormat: flags.OutputText,
expected: "a test",
},
{
name: "io.Reader",
v: strings.NewReader("a test"),
outputFormat: flags.OutputJSON,
errMessage: "json output not supported",
},
},
{
name: "Workspace Iterator with no header and no template",
v: makeIterator(2),
outputFormat: flags.OutputText,
expected: string(must(json.MarshalIndent(makeWorkspaces(2), "", " "))) + "\n",
},
{
name: "Big Workspace Iterator with template",
v: makeIterator(234),
outputFormat: flags.OutputText,
headerTemplate: "id\tname",
template: "{{range .}}{{.WorkspaceId}}\t{{.WorkspaceName}}\n{{end}}",
expected: "id name\n" + makeBigOutput(234),
},
{
name: "Big Workspace Iterator with no template",
v: makeIterator(234),
outputFormat: flags.OutputText,
expected: string(must(json.MarshalIndent(makeWorkspaces(234), "", " "))) + "\n",
},
{
name: "io.Reader",
v: strings.NewReader("a test"),
outputFormat: flags.OutputText,
expected: "a test",
},
{
name: "io.Reader",
v: strings.NewReader("a test"),
outputFormat: flags.OutputJSON,
errMessage: "json output not supported",
},
}
}

// TestRenderJSONColorGate verifies defaultRenderer.renderJson honors the
Expand Down Expand Up @@ -209,7 +215,7 @@ func TestRenderJSONColorGate(t *testing.T) {
}

func TestRender(t *testing.T) {
for _, c := range testCases {
for _, c := range makeTestCases() {
t.Run(c.name, func(t *testing.T) {
output := &bytes.Buffer{}
ctx := t.Context()
Expand Down
Loading