2022-04-17 12:30:33 +02:00
|
|
|
package report
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTable(t *testing.T) {
|
|
|
|
cols := []string{"Col1", "Col2"}
|
|
|
|
rows := [][]string{
|
|
|
|
{"foo", "bar" + strings.Repeat(", abc", 30)},
|
|
|
|
{"bar", "b & a | z"}}
|
|
|
|
|
|
|
|
t.Run("DefaultTable", func(t *testing.T) {
|
2022-10-10 16:34:07 +02:00
|
|
|
result, err := RenderFormat(rows, cols, Default)
|
2022-04-17 12:30:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Contains(t, result, "| bar | b & a | z |")
|
|
|
|
})
|
|
|
|
t.Run("MarkdownTable", func(t *testing.T) {
|
2022-10-10 16:34:07 +02:00
|
|
|
result, err := RenderFormat(rows, cols, Markdown)
|
2022-04-17 12:30:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// fmt.Println(result)
|
|
|
|
assert.Contains(t, result, "| bar | b & a \\| z")
|
|
|
|
})
|
|
|
|
t.Run("CsvExport", func(t *testing.T) {
|
2022-10-10 16:34:07 +02:00
|
|
|
result, err := RenderFormat(rows, cols, CSV)
|
2022-04-17 12:30:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Col1;Col2\nfoo;bar, abc, abc, abc, abc, abc, abc," +
|
|
|
|
" abc, abc, abc, abc, abc, abc, abc, abc, abc," +
|
|
|
|
" abc, abc, abc, abc, abc, abc, abc, abc, abc," +
|
|
|
|
" abc, abc, abc, abc, abc, abc\nbar;b & a \\| z\n"
|
|
|
|
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
})
|
|
|
|
t.Run("TsvExport", func(t *testing.T) {
|
2022-10-10 16:34:07 +02:00
|
|
|
result, err := RenderFormat(rows, cols, TSV)
|
2022-04-17 12:30:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Contains(t, result, "Col1\tCol2\nfoo\tbar, abc, abc")
|
|
|
|
})
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
2022-10-10 16:34:07 +02:00
|
|
|
_, err := RenderFormat(rows, cols, Format("invalid"))
|
2022-04-17 12:30:33 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|