1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-25 23:23:21 -05:00

testing: Add tests for Lua dates

Also fix a timezone bug discovered while testing
This commit is contained in:
Aaron Lindsay 2017-11-07 05:58:58 -05:00
parent 6115a16f89
commit 5daed2f508
2 changed files with 33 additions and 1 deletions

View File

@ -63,9 +63,10 @@ func luaWeakCheckTableFieldInt(L *lua.LState, T *lua.LTable, n int, name string,
}
func luaDateNew(L *lua.LState) int {
// TODO make this track the user's timezone
v := L.Get(1)
if s, ok := v.(lua.LString); ok {
date, err := time.Parse(timeFormat, s.String())
date, err := time.ParseInLocation(timeFormat, s.String(), time.Local)
if err != nil {
L.ArgError(1, "error parsing date string: "+err.Error())
return 0

View File

@ -0,0 +1,31 @@
package handlers_test
import (
"testing"
)
func TestLuaDates(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
simpleLuaTest(t, d.clients[0], []LuaTest{
{"Year", "return date.new('0009-01-03').Year", "9"},
{"Month", "return date.new('3999-02-01').Month", "2"},
{"Day", "return date.new('1997-12-31').Day", "31"},
{"__tostring", "return date.new('0997-12-01')", "0997-12-01"},
{"__tostring 2", "return date.new(997, 12, 1)", "0997-12-01"},
{"__tostring 3", "return date.new({year=997, month=12, day=1})", "0997-12-01"},
{"__eq", "return date.new('2017-10-05') == date.new(2017, 10, 5)", "true"},
{"(not) __eq", "return date.new('0997-12-01') == date.new('1997-12-01')", "false"},
{"__lt", "return date.new('0997-12-01') < date.new('1997-12-01')", "true"},
{"(not) __lt", "return date.new('2001-12-01') < date.new('1997-12-01')", "false"},
{"not __lt self", "return date.new('2001-12-01') < date.new('2001-12-01')", "false"},
{"__le", "return date.new('0997-12-01') <= date.new('1997-12-01')", "true"},
{"(not) __le", "return date.new(2001, 12, 1) <= date.new('1997-12-01')", "false"},
{"__le self", "return date.new('2001-12-01') <= date.new(2001, 12, 1)", "true"},
{"__add", "return date.new('2001-12-30') + date.new({year=0, month=0, day=1})", "2001-12-31"},
{"__add", "return date.new('2001-12-30') + date.new({year=0, month=0, day=2})", "2002-01-01"},
{"__sub", "return date.new('2001-12-30') - date.new({year=1, month=1, day=1})", "2000-11-29"},
{"__sub", "return date.new('2058-03-01') - date.new({year=0, month=0, day=1})", "2058-02-28"},
{"__sub", "return date.new('2058-03-31') - date.new({year=0, month=1, day=0})", "2058-02-28"},
})
})
}