Skip to content

Commit

Permalink
✅ test: update and fix all error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 8, 2023
1 parent 86c933a commit 9415ac3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestBaseTypeVal(t *testing.T) {

val, err := goutil.BaseTypeVal(uint64(23))
is.NoErr(err)
is.Eq(int64(23), val)
is.Eq(uint64(23), val)

val, err = goutil.BaseTypeVal(23)
is.NoErr(err)
Expand Down
2 changes: 1 addition & 1 deletion reflects/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestBaseTypeVal(t *testing.T) {
want, give any
}{
{int64(23), 23},
{int64(23), uint(23)},
{uint64(23), uint(23)},
{23.4, 23.4},
// {23.4, float32(23.4)},
{"abc", "abc"},
Expand Down
5 changes: 2 additions & 3 deletions structs/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package structs_test
import (
"testing"

"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/testutil"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestInitDefaults_error(t *testing.T) {

u := &User{}
err = structs.InitDefaults(u)
assert.ErrMsg(t, err, comdef.ErrConvType.Error())
assert.ErrSubMsg(t, err, `parsing "abc": invalid syntax`)
assert.Eq(t, "inhere", u.Name)
assert.Eq(t, 0, u.Age)
// dump.P(u)
Expand Down Expand Up @@ -105,7 +104,7 @@ func TestInitDefaults_convTypeError(t *testing.T) {

u := &User{}
err := structs.InitDefaults(u)
assert.ErrMsg(t, err, comdef.ErrConvType.Error())
assert.ErrSubMsg(t, err, `parsing "abc": invalid syntax`)
assert.Eq(t, "inhere", u.Name)
assert.Eq(t, 0, u.Age)
// dump.P(u)
Expand Down
5 changes: 3 additions & 2 deletions strutil/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package strutil_test

import (
"testing"
"time"

"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
Expand Down Expand Up @@ -47,7 +46,9 @@ func TestBaseDecode(t *testing.T) {

// b23 hex
is.Eq("64P62OJ3", strutil.B32Hex.EncodeToString([]byte("12abc")))
is.Eq("68O34CPG70P3IC9M6GO32CO", strutil.B32Hex.EncodeToString([]byte(time.Now().Format("20060102150405"))))
// fmt.Println(time.Now().Format("20060102150405"))
dateStr := "20230908101122"
is.Eq("68O34CPG74O3GC9G64OJ4CG", strutil.B32Hex.EncodeToString([]byte(dateStr)))

is.Eq("YWJj", strutil.B64Encode("abc"))
is.Eq("abc", strutil.B64Decode("YWJj"))
Expand Down
2 changes: 1 addition & 1 deletion strutil/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ToTime(s string, layouts ...string) (t time.Time, err error) {
// custom layout
if len(layouts) > 0 {
if len(layouts[0]) > 0 {
return time.Parse(layouts[0], s)
return time.ParseInLocation(layouts[0], s, time.Local)
}

err = ErrDateLayout
Expand Down
5 changes: 5 additions & 0 deletions strutil/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"testing"

"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/timex"
)

func TestToTime(t *testing.T) {
is := assert.New(t)

testutil.SetTimeLocalUTC()
defer testutil.RestoreTimeLocal()

tests := map[string]string{
"20180927": "2018-09-27 00:00:00 +0000 UTC",
"2018-09-27": "2018-09-27 00:00:00 +0000 UTC",
Expand Down
25 changes: 25 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package testutil
import (
"io"
"os"
"time"
)

var oldStdout, oldStderr, newReader *os.File
Expand Down Expand Up @@ -127,3 +128,27 @@ func RestoreStderr(printData ...bool) (s string) {
newReader = nil
return
}

var timeLocBak *time.Location

// SetTimeLocal custom time.Local for testing.
func SetTimeLocal(tl *time.Location) {
if timeLocBak != nil {
panic("time local already set, please restore it before set")
}

timeLocBak = time.Local
time.Local = tl
}

// SetTimeLocalUTC custom time.Local=UTC for testing.
func SetTimeLocalUTC() {
SetTimeLocal(time.UTC)
}

// RestoreTimeLocal restore time.Local
func RestoreTimeLocal() {
if timeLocBak != nil {
time.Local = timeLocBak
}
}
10 changes: 10 additions & 0 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
Expand Down Expand Up @@ -47,3 +48,12 @@ func TestNewDirEnt(t *testing.T) {
assert.NotEmpty(t, de)
assert.False(t, de.IsDir())
}

// test testutil.SetTimeLocal
func TestSetTimeLocal(t *testing.T) {
testutil.SetTimeLocalUTC()
tt, err := time.ParseInLocation("2006-01-02 15:04:05", "2021-01-01 12:12:12", time.Local)
assert.NoError(t, err)
assert.Eq(t, "2021-01-01 12:12:12", tt.Format("2006-01-02 15:04:05"))
testutil.RestoreTimeLocal()
}
4 changes: 4 additions & 0 deletions timex/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/timex"
)
Expand Down Expand Up @@ -106,6 +107,9 @@ func TestTryToTime(t *testing.T) {
}

func TestParseRange(t *testing.T) {
testutil.SetTimeLocalUTC()
defer testutil.RestoreTimeLocal()

tests := []struct {
input string
start int64
Expand Down

0 comments on commit 9415ac3

Please sign in to comment.