2014-05-14 20:54:19 -04:00
|
|
|
package csvstruct
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
2014-05-14 21:11:21 -04:00
|
|
|
"reflect"
|
2014-05-14 20:54:19 -04:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDecode(t *testing.T) {
|
|
|
|
|
type row struct {
|
|
|
|
|
Foo, Bar, Baz string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range []struct {
|
|
|
|
|
data string
|
|
|
|
|
out []row
|
|
|
|
|
}{{
|
|
|
|
|
data: `Foo,Bar,Baz
|
|
|
|
|
a,b,c
|
|
|
|
|
d,e,f`,
|
|
|
|
|
out: []row{{"a", "b", "c"}, {"d", "e", "f"}},
|
|
|
|
|
}, {
|
|
|
|
|
// Rows that only have partial data are only partially filled.
|
2014-05-15 08:31:35 -04:00
|
|
|
data: `Foo,Bar,Baz
|
|
|
|
|
a,,
|
|
|
|
|
,b,`,
|
|
|
|
|
out: []row{{"a", "", ""}, {"", "b", ""}},
|
|
|
|
|
}, {
|
|
|
|
|
// Rows that don't define all the columns are partially filled.
|
2014-05-14 20:54:19 -04:00
|
|
|
data: `Foo,Bar
|
|
|
|
|
a,
|
|
|
|
|
,b`,
|
|
|
|
|
out: []row{{"a", "", ""}, {"", "b", ""}},
|
2014-05-15 08:31:35 -04:00
|
|
|
/*}, {
|
|
|
|
|
// Entirely disjoint columns produce empty structs.
|
|
|
|
|
data: `Qux
|
|
|
|
|
d`,
|
|
|
|
|
out: []row{{"", "", ""}}, */
|
2014-05-14 20:54:19 -04:00
|
|
|
}} {
|
|
|
|
|
d := NewDecoder(strings.NewReader(c.data))
|
2014-05-14 21:11:21 -04:00
|
|
|
rows := []row{}
|
2014-05-14 20:54:19 -04:00
|
|
|
var row row
|
|
|
|
|
for {
|
|
|
|
|
err := d.DecodeNext(&row)
|
|
|
|
|
if err == io.EOF {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
|
break
|
|
|
|
|
}
|
2014-05-14 21:11:21 -04:00
|
|
|
rows = append(rows, row)
|
|
|
|
|
}
|
2014-05-15 08:31:35 -04:00
|
|
|
if !reflect.DeepEqual(rows, c.out) {
|
2014-05-14 21:11:21 -04:00
|
|
|
t.Errorf("unexpected result, got %v, want %v", rows, c.out)
|
2014-05-14 20:54:19 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|