1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 17:22:27 +08:00

refactor: rewrite all unit test functions with assert

This commit is contained in:
dudaodong
2022-01-09 14:46:17 +08:00
parent e4cd7dad35
commit 9cb9aa2f2f

View File

@@ -1,90 +1,67 @@
package datetime package datetime
import ( import (
"github.com/duke-git/lancet/internal"
"testing" "testing"
"time" "time"
"github.com/duke-git/lancet/internal"
) )
func TestAddDay(t *testing.T) { func TestAddDay(t *testing.T) {
now := time.Now() assert := internal.NewAssert(t, "TestAddDay")
now := time.Now()
after2Days := AddDay(now, 2) after2Days := AddDay(now, 2)
diff1 := after2Days.Sub(now) diff1 := after2Days.Sub(now)
if diff1.Hours() != 48 { assert.Equal(float64(48), diff1.Hours())
internal.LogFailedTestInfo(t, "AddDay", now, 48, diff1.Hours())
t.FailNow()
}
before2Days := AddDay(now, -2) before2Days := AddDay(now, -2)
diff2 := before2Days.Sub(now) diff2 := before2Days.Sub(now)
if diff2.Hours() != -48 { assert.Equal(float64(-48), diff2.Hours())
internal.LogFailedTestInfo(t, "AddDay", now, -48, diff2.Hours())
t.FailNow()
}
} }
func TestAddHour(t *testing.T) {
now := time.Now()
func TestAddHour(t *testing.T) {
assert := internal.NewAssert(t, "TestAddHour")
now := time.Now()
after2Hours := AddHour(now, 2) after2Hours := AddHour(now, 2)
diff1 := after2Hours.Sub(now) diff1 := after2Hours.Sub(now)
if diff1.Hours() != 2 { assert.Equal(float64(2), diff1.Hours())
internal.LogFailedTestInfo(t, "AddHour", now, 2, diff1.Hours())
t.FailNow()
}
before2Hours := AddHour(now, -2) before2Hours := AddHour(now, -2)
diff2 := before2Hours.Sub(now) diff2 := before2Hours.Sub(now)
if diff2.Hours() != -2 { assert.Equal(float64(-2), diff2.Hours())
internal.LogFailedTestInfo(t, "AddHour", now, -2, diff2.Hours())
t.FailNow()
}
} }
func TestAddMinute(t *testing.T) { func TestAddMinute(t *testing.T) {
now := time.Now() assert := internal.NewAssert(t, "TestAddMinute")
now := time.Now()
after2Minutes := AddMinute(now, 2) after2Minutes := AddMinute(now, 2)
diff1 := after2Minutes.Sub(now) diff1 := after2Minutes.Sub(now)
if diff1.Minutes() != 2 { assert.Equal(float64(2), diff1.Minutes())
internal.LogFailedTestInfo(t, "AddMinute", now, 2, diff1.Minutes())
t.FailNow()
}
before2Minutes := AddMinute(now, -2) before2Minutes := AddMinute(now, -2)
diff2 := before2Minutes.Sub(now) diff2 := before2Minutes.Sub(now)
if diff2.Minutes() != -2 { assert.Equal(float64(-2), diff2.Minutes())
internal.LogFailedTestInfo(t, "AddMinute", now, -2, diff2.Minutes())
t.FailNow()
}
} }
func TestGetNowDate(t *testing.T) { func TestGetNowDate(t *testing.T) {
date := GetNowDate() assert := internal.NewAssert(t, "TestGetNowDate")
expected := time.Now().Format("2006-01-02") expected := time.Now().Format("2006-01-02")
if date != expected { assert.Equal(expected, GetNowDate())
internal.LogFailedTestInfo(t, "GetNowDate", "", expected, date)
t.FailNow()
}
} }
func TestGetNotTime(t *testing.T) { func TestGetNotTime(t *testing.T) {
ts := GetNowTime() assert := internal.NewAssert(t, "TestGetNotTime")
expected := time.Now().Format("15:04:05") expected := time.Now().Format("15:04:05")
if ts != expected { assert.Equal(expected, GetNowTime())
internal.LogFailedTestInfo(t, "GetNowTime", "", expected, ts)
t.FailNow()
}
} }
func TestGetNowDateTime(t *testing.T) { func TestGetNowDateTime(t *testing.T) {
ts := GetNowDateTime() assert := internal.NewAssert(t, "TestGetNowDateTime")
expected := time.Now().Format("2006-01-02 15:04:05") expected := time.Now().Format("2006-01-02 15:04:05")
if ts != expected { assert.Equal(expected, GetNowDateTime())
internal.LogFailedTestInfo(t, "GetNowDateTime", "", expected, ts)
t.FailNow()
}
} }
//todo //todo
@@ -98,6 +75,8 @@ func TestGetNowDateTime(t *testing.T) {
//} //}
func TestFormatTimeToStr(t *testing.T) { func TestFormatTimeToStr(t *testing.T) {
assert := internal.NewAssert(t, "TestFormatTimeToStr")
datetime, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08") datetime, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08")
cases := []string{ cases := []string{
"yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd", "yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd",
@@ -110,16 +89,15 @@ func TestFormatTimeToStr(t *testing.T) {
"16:04:08", "2021/01"} "16:04:08", "2021/01"}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res := FormatTimeToStr(datetime, cases[i]) actual := FormatTimeToStr(datetime, cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "FormatTimeToStr", cases[i], expected[i], res)
t.FailNow()
}
}
}
} }
func TestFormatStrToTime(t *testing.T) { func TestFormatStrToTime(t *testing.T) {
assert := internal.NewAssert(t, "TestFormatStrToTime")
formats := []string{ formats := []string{
"2006-01-02 15:04:05", "2006-01-02", "2006-01-02 15:04:05", "2006-01-02",
"02-01-06 15:04:05", "2006/01/02 15:04:05", "02-01-06 15:04:05", "2006/01/02 15:04:05",
@@ -135,14 +113,11 @@ func TestFormatStrToTime(t *testing.T) {
"2021/01"} "2021/01"}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res, err := FormatStrToTime(datetimeStr[i], cases[i]) actual, err := FormatStrToTime(datetimeStr[i], cases[i])
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
expected, _ := time.Parse(formats[i], datetimeStr[i]) expected, _ := time.Parse(formats[i], datetimeStr[i])
if res != expected { assert.Equal(expected, actual)
internal.LogFailedTestInfo(t, "FormatTimeToStr", cases[i], expected, res)
t.FailNow()
}
} }
} }