diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f869ba3..2b92fe9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,5 +21,11 @@ jobs: - name: Build run: go build -v ./... - - name: Test - run: go test -v ./... + - name: Test XLS + run: go test -v ./xls + + - name: Test XLSX + run: go test -v ./xlsx + + - name: Test CommonXL + run: go test -v ./commonxl diff --git a/xlsx/comp_test.go b/xlsx/comp_test.go index b6d2a3d..f5297a7 100644 --- a/xlsx/comp_test.go +++ b/xlsx/comp_test.go @@ -7,7 +7,7 @@ import ( "testing" ) -func TestManyFiles(t *testing.T) { +func TestAllFiles(t *testing.T) { err := filepath.Walk("../testdata", func(p string, info os.FileInfo, err error) error { if info.IsDir() { return nil @@ -17,7 +17,7 @@ func TestManyFiles(t *testing.T) { } wb, err := Open(p) if err != nil { - return nil //err + return err } sheets, err := wb.List() @@ -25,7 +25,6 @@ func TestManyFiles(t *testing.T) { return err } for _, s := range sheets { - //log.Println(s) sheet, err := wb.Get(s) if err != nil { return err diff --git a/xlsx/simple_test.go b/xlsx/simple_test.go index 56d48d8..55ffae8 100644 --- a/xlsx/simple_test.go +++ b/xlsx/simple_test.go @@ -1,20 +1,52 @@ package xlsx import ( + "bufio" + "log" "os" "strings" "testing" + + "github.com/pbnjay/grate/commonxl" ) -var testFiles = []string{ - "../testdata/multi_test.xlsx", - "../testdata/basic.xlsx", - "../testdata/basic2.xlsx", +var testFilePairs = [][]string{ + {"../testdata/basic.xlsx", "../testdata/basic.tsv"}, + + // TODO: custom formatter support + //{"../testdata/basic2.xlsx", "../testdata/basic2.tsv"}, + + // TODO: datetime and fraction formatter support + //{"../testdata/multi_test.xlsx", "../testdata/multi_test.tsv"}, } -func TestLoading(t *testing.T) { - for _, fn := range testFiles { - wb, err := Open(fn) +func loadTestData(fn string, ff *commonxl.Formatter) (*commonxl.Sheet, error) { + f, err := os.Open(fn) + if err != nil { + return nil, err + } + xs := &commonxl.Sheet{ + Formatter: ff, + } + + row := 0 + s := bufio.NewScanner(f) + for s.Scan() { + record := strings.Split(s.Text(), "\t") + for i, val := range record { + xs.Put(row, i, val, 0) + } + row++ + } + return xs, f.Close() +} + +func TestBasic(t *testing.T) { + for _, fnames := range testFilePairs { + var trueData *commonxl.Sheet + log.Println("Testing ", fnames[0]) + + wb, err := Open(fnames[0]) if err != nil { t.Fatal(err) } @@ -23,14 +55,30 @@ func TestLoading(t *testing.T) { if err != nil { t.Fatal(err) } + firstLoad := true for _, s := range sheets { sheet, err := wb.Get(s) if err != nil { t.Fatal(err) } + xsheet := sheet.(*commonxl.Sheet) + if firstLoad { + trueData, err = loadTestData(fnames[1], xsheet.Formatter) + if err != nil { + t.Fatal(err) + } + firstLoad = false + } - for sheet.Next() { - sheet.Strings() + for xrow, xdata := range xsheet.Rows { + for xcol, xval := range xdata { + //t.Logf("at %s (%d,%d) expect '%v'", fnames[0], xrow, xcol, trueData.Rows[xrow][xcol]) + if !trueData.Rows[xrow][xcol].Equal(xval) { + t.Logf("mismatch at %s (%d,%d): '%v' <> '%v' expected", fnames[0], xrow, xcol, + xval, trueData.Rows[xrow][xcol]) + t.Fail() + } + } } } @@ -40,120 +88,3 @@ func TestLoading(t *testing.T) { } } } - -func TestBasic(t *testing.T) { - trueFile, err := os.ReadFile("../testdata/basic.tsv") - if err != nil { - t.Skip() - } - lines := strings.Split(string(trueFile), "\n") - - fn := "../testdata/basic.xlsx" - wb, err := Open(fn) - if err != nil { - t.Fatal(err) - } - - sheets, err := wb.List() - if err != nil { - t.Fatal(err) - } - for _, s := range sheets { - sheet, err := wb.Get(s) - if err != nil { - t.Fatal(err) - } - - i := 0 - for sheet.Next() { - row := strings.Join(sheet.Strings(), "\t") - if lines[i] != row { - t.Fatalf("line %d mismatch: '%s' <> '%s'", i, row, lines[i]) - } - i++ - } - } - - err = wb.Close() - if err != nil { - t.Fatal(err) - } -} - -func TestBasic2(t *testing.T) { - trueFile, err := os.ReadFile("../testdata/basic2.tsv") - if err != nil { - t.Skip() - } - lines := strings.Split(string(trueFile), "\n") - - fn := "../testdata/basic2.xlsx" - wb, err := Open(fn) - if err != nil { - t.Fatal(err) - } - - sheets, err := wb.List() - if err != nil { - t.Fatal(err) - } - for _, s := range sheets { - sheet, err := wb.Get(s) - if err != nil { - t.Fatal(err) - } - - i := 0 - for sheet.Next() { - row := strings.Join(sheet.Strings(), "\t") - if lines[i] != row { - t.Fatalf("line %d mismatch: '%s' <> '%s'", i, row, lines[i]) - } - i++ - } - } - - err = wb.Close() - if err != nil { - t.Fatal(err) - } -} - -func TestMulti(t *testing.T) { - trueFile, err := os.ReadFile("../testdata/multi_test.tsv") - if err != nil { - t.Skip() - } - lines := strings.Split(string(trueFile), "\n") - - fn := "../testdata/multi_test.xlsx" - wb, err := Open(fn) - if err != nil { - t.Fatal(err) - } - - sheets, err := wb.List() - if err != nil { - t.Fatal(err) - } - for _, s := range sheets { - sheet, err := wb.Get(s) - if err != nil { - t.Fatal(err) - } - - i := 0 - for sheet.Next() { - row := strings.Join(sheet.Strings(), "\t") - if lines[i] != row { - t.Fatalf("line %d mismatch: '%s' <> '%s'", i, row, lines[i]) - } - i++ - } - } - - err = wb.Close() - if err != nil { - t.Fatal(err) - } -}