Skip to content

Commit

Permalink
Clarify output behavior for non-strings
Browse files Browse the repository at this point in the history
The OutputWriter accepts Row's with values that are non-strings.
Add unit tests to better clarify how they are being rendered.

Currently they all get converted golang string representations.
As a result, numeric literals renders fine, but containers like maps
look awkward in tabular view, and arguably wrong in JSON/YAML form.

YAML/JSON output will be fixed in a followup commit
  • Loading branch information
vuil committed Sep 29, 2023
1 parent df102b0 commit cbdebba
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions component/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/stretchr/testify/require"
)

var mapValue = map[string]string{
"f": "foo",
"b": "bar",
}

func TestNewOutputWriterTable(t *testing.T) {
var b bytes.Buffer
tab := NewOutputWriter(&b, string(TableOutputType), "a", "b", "c")
Expand Down Expand Up @@ -261,6 +266,87 @@ func TestNewOutputWriterJSON(t *testing.T) {
require.Contains(t, lines[11], "]")
}

func TestNewOutputWriterNonStrings(t *testing.T) {
var b bytes.Buffer
tab := NewOutputWriter(&b, string(TableOutputType), "a", "b", "c")
require.NotNil(t, tab)
tab.AddRow("1", mapValue, 2)
tab.Render()

output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
// note: the trailing spaces in this string are intentional
expected := `
A B C
1 map[b:bar f:foo] 2
`

require.Equal(t, expected[1:], output)
}

func TestNewOutputWriterYAMLNonStrings(t *testing.T) {
var b bytes.Buffer
tab := NewOutputWriter(&b, string(YAMLOutputType), "a", "b", "c")
require.NotNil(t, tab)
tab.AddRow("1", mapValue, 2)
tab.Render()

output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
expected := `
- a: "1"
b: map[b:bar f:foo]
c: "2"
`
require.Equal(t, expected[1:], output)
}

func TestNewOutputWriterJSONNonStrings(t *testing.T) {
var b bytes.Buffer
tab := NewOutputWriter(&b, string(JSONOutputType), "a", "b", "c")
require.NotNil(t, tab)
tab.AddRow("1", mapValue, 2)
tab.Render()

output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
expected := `
[
{
"a": "1",
"b": "map[b:bar f:foo]",
"c": "2"
}
]`
require.Equal(t, expected[1:], output)
}

func TestNewOutputWriterTableListNonStrings(t *testing.T) {
var b bytes.Buffer
tab := NewOutputWriter(&b, string(ListTableOutputType), "a", "b", "c")
require.NotNil(t, tab)
tab.AddRow("1", mapValue, 2)
tab.AddRow("3", "4", "5")
tab.Render()

output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
expected := `
A: 1, 3
B: map[b:bar f:foo], 4
C: 2, 5
`
require.Equal(t, expected[1:], output)
}

func TestOutputWriterCharactersInKeys(t *testing.T) {
var b bytes.Buffer
tab := NewOutputWriter(&b, string(JSONOutputType), "a key with spaces", "one/two", "zip:zap")
Expand Down

0 comments on commit cbdebba

Please sign in to comment.