dequoter/textfilters_test.go
Leon Mika cb9c05a57b
All checks were successful
Build / build (push) Successful in 3m1s
Release Build / build (push) Successful in 5m11s
Added CSV to ASCII table
2026-08-07 05:10:16 +00:00

92 lines
1.7 KiB
Go

package main
import (
"context"
"testing"
)
func TestCSVToASCIITable(t *testing.T) {
filter := TextFilters["csv-to-ascii-table"].Filter
tests := []struct {
name string
input string
want string
wantErr bool
}{
{
name: "basic table with quoted value",
input: "alpha,bravo\n" +
"1,b\n" +
"\"long value\",something\n",
want: "alpha bravo\n" +
"---------- ---------\n" +
"1 b\n" +
"long value something\n" +
"---------- ---------\n",
},
{
name: "no trailing newline",
input: "a,b\n1,2",
want: "a b\n" +
"- -\n" +
"1 2\n" +
"- -",
},
{
name: "quoted field containing comma",
input: "name,desc\n\"x, y\",z\n",
want: "name desc\n" +
"---- ----\n" +
"x, y z\n" +
"---- ----\n",
},
{
name: "ragged rows padded with empty cells",
input: "a,b,c\n1\n2,3\n",
want: "a b c\n" +
"- - -\n" +
"1\n" +
"2 3\n" +
"- - -\n",
},
{
name: "header only",
input: "one,two\n",
want: "one two\n" +
"--- ---\n" +
"--- ---\n",
},
{
name: "empty input unchanged",
input: "",
want: "",
},
{
name: "invalid CSV returns error",
input: "a,\"b\nc",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := filter(context.Background(), tt.input)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got output %q", resp.Output)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.Output != tt.want {
t.Errorf("output mismatch\ngot:\n%s\nwant:\n%s", resp.Output, tt.want)
}
if resp.Append {
t.Errorf("Append should be false")
}
})
}
}