mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
test(gsheet2csv): cover empty rows, blank lines, and comments
This commit is contained in:
parent
8a928d0e9c
commit
758fee503d
@ -131,20 +131,58 @@ func TestNewReaderFromURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadSkipsEmptyRecords(t *testing.T) {
|
// TestReadSkipsEmptyRowsAndComments verifies that ReadAll drops all-empty rows,
|
||||||
reader := NewReader(strings.NewReader("a,b\n,,\n\"\",\"\"\nvalue,\n"))
|
// blank lines, and comment lines, while preserving rows that contain any
|
||||||
rows, err := reader.ReadAll()
|
// content (including partially empty rows).
|
||||||
if err != nil {
|
func TestReadSkipsEmptyRowsAndComments(t *testing.T) {
|
||||||
t.Fatal(err)
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
expected [][]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "all empty rows",
|
||||||
|
input: "header\n,,\n\"\",\"\"\n",
|
||||||
|
expected: [][]string{{"header"}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty line",
|
||||||
|
input: "header\n\nvalue,c\n",
|
||||||
|
expected: [][]string{{"header"}, {"value", "c"}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "comment line",
|
||||||
|
input: "# a comment\nheader\nvalue,c\n",
|
||||||
|
expected: [][]string{{"header"}, {"value", "c"}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "quoted comment line",
|
||||||
|
input: "header\n\"# quoted comment\"\nvalue,c\n",
|
||||||
|
expected: [][]string{{"header"}, {"value", "c"}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "partially empty row is kept",
|
||||||
|
input: "header\nvalue,\n\"\",\"\"\n",
|
||||||
|
expected: [][]string{{"header"}, {"value", ""}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
want := [][]string{{"a", "b"}, {"value", ""}}
|
|
||||||
if len(rows) != len(want) {
|
for _, tt := range tests {
|
||||||
t.Fatalf("rows = %v, want %v", rows, want)
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
}
|
reader := NewReader(strings.NewReader(tt.input))
|
||||||
for i := range want {
|
got, err := reader.ReadAll()
|
||||||
if !slices.Equal(rows[i], want[i]) {
|
if err != nil {
|
||||||
t.Fatalf("rows = %v, want %v", rows, want)
|
t.Fatalf("ReadAll() error: %v", err)
|
||||||
}
|
}
|
||||||
|
if len(got) != len(tt.expected) {
|
||||||
|
t.Fatalf("ReadAll() = %v, want %v", got, tt.expected)
|
||||||
|
}
|
||||||
|
for i := range got {
|
||||||
|
if !slices.Equal(got[i], tt.expected[i]) {
|
||||||
|
t.Errorf("ReadAll() row %d = %v, want %v", i, got[i], tt.expected[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user