Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Dec 25, 2022
1 parent 29f3af2 commit d43ec35
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 36 deletions.
104 changes: 97 additions & 7 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ package main

import (
"bytes"
"log"
"testing"

"github.com/shu-go/gli"
"github.com/shu-go/gotwant"
"github.com/xuri/excelize/v2"
)

func dummyCmd() *globalCmd {
func dummyCmd(args ...string) *globalCmd {
appargs := append([]string{"-o", "dummy"}, args...)
appargs = append(appargs, "dummycsvfile")

app := gli.NewWith(&globalCmd{})
icmd, _, _ := app.Parse([]string{"-o", "dummy", "dummyarg"})
icmd, _, err := app.Parse(appargs)
if err != nil {
log.Println(err)
}

return icmd.(*globalCmd)
}

Expand Down Expand Up @@ -50,19 +58,101 @@ func TestAis1(t *testing.T) {
}

func TestGuess(t *testing.T) {
cmd := dummyCmd()
tst := func(content, value string, args ...string) {
t.Helper()

cmd := dummyCmd(append([]string{"--header=0"}, args...)...)
oc, err := cmd.makeOutputContext(excelize.NewFile(), false)
gotwant.TestError(t, err, nil)
oc.inputs = []input{
newInput("test.csv", content),
}

err = cmd.convert(oc)
gotwant.TestError(t, err, nil)
testValue(t, oc, "test.csv", "A1", value)
}

t.Run("nohints", func(t *testing.T) {
tst("01", "01")
tst("11", "11")
tst("20220101", "2022/01/01")
tst("123456", "12:34:56")
tst("true", "TRUE")
tst("false", "FALSE")
})

t.Run("GuessNumber", func(t *testing.T) {
tst("01", "01")
tst("01", "1", "--columns", "#1:number")
})

t.Run("GuessBool", func(t *testing.T) {
tst("1", "1")
tst("1", "TRUE", "--columns", "#1:bool")
tst("true", "TRUE")
tst("false", "FALSE")
tst("01", "01", "--columns", "#1:bool")
})

t.Run("GuessDate", func(t *testing.T) {
tst("20220304", "2022/03/04")
tst("4-3-22", "2022/03/04", "--columns", "#1:date(d-m-y)")
tst("4-3-22", "2022/03/04", "--columns", "#1:date(2-1-06)")
tst("4-3-22", "4-3-22", "--columns", "#1:date(dd-mm-yyyy)") // failure
tst("Feb 4 2008", "2008/02/04", "--columns", "#1:date(Jan 2 2006)")
})

t.Run("GuessTime", func(t *testing.T) {
tst("123456", "12:34:56")
tst("012345", "012345") // failure
tst("012345", "01:23:45", "--columns", "#1:time")
tst("1 2 3", "01:02:03", "--columns", "#1:time(h m s)")
tst("1 2 3", "1 2 3", "--columns", "#1:time(hh mm ss)") // failure
})

t.Run("GuessDatetime", func(t *testing.T) {
tst("20220304", "2022/03/04", "--columns", "#1:datetime")
tst("20220304 123456", "2022/03/04 12:34:56", "--columns", "#1:datetime")
tst("Feb 4, 2008 4:45pm", "2008/02/04 16:45:00", "--columns", `#1:datetime(Jan 2\, 2006 3:04pm)`, "-d", ";")
})
}

func TestMultiple(t *testing.T) {
cmd := dummyCmd([]string{"--header=0"}...)
oc, err := cmd.makeOutputContext(excelize.NewFile(), false)
gotwant.TestError(t, err, nil)
oc.inputs = []input{
newInput("test.csv", "a,b,c\n01,11,20220101"),
newInput("test.csv", "ichi"),
newInput("test2.csv", "ni"),
}
err = cmd.convert(oc)
gotwant.TestError(t, err, nil)
testValue(t, oc, "test.csv", "A1", "ichi")
testValue(t, oc, "test2.csv", "A1", "ni")

//

oc.inputs = []input{
newInput("test.csv", "ichi"),
newInput("test3.csv", "san\nthree"),
}
err = cmd.convert(oc)
gotwant.TestError(t, err, nil)
testValue(t, oc, "test.csv", "A1", "ichi")
testValue(t, oc, "test2.csv", "A1", "ni")
testValue(t, oc, "test3.csv", "A1", "san")
testValue(t, oc, "test3.csv", "A2", "three")

testValue(t, oc, "test.csv", "A2", "01")
testValue(t, oc, "test.csv", "B2", "11")
testValue(t, oc, "test.csv", "C2", "2022/01/01")
//

oc.inputs = []input{
newInput("test3.csv", "san"),
}
err = cmd.convert(oc)
gotwant.TestError(t, err, nil)
testValue(t, oc, "test.csv", "A1", "ichi")
testValue(t, oc, "test2.csv", "A1", "ni")
testValue(t, oc, "test3.csv", "A1", "san")
testValue(t, oc, "test3.csv", "A2", "")
}
15 changes: 0 additions & 15 deletions test.bat

This file was deleted.

7 changes: 0 additions & 7 deletions test.csv

This file was deleted.

7 changes: 0 additions & 7 deletions test2.csv

This file was deleted.

0 comments on commit d43ec35

Please sign in to comment.