github.com/tenntenn/testtime@v0.2.3-0.20221118081726-55bcd1f05226/time_test.go (about) 1 package testtime_test 2 3 import ( 4 "testing" 5 "time" 6 _ "unsafe" 7 8 "github.com/tenntenn/testtime" 9 ) 10 11 func Test(t *testing.T) { 12 13 t.Run("SetTime", func(t *testing.T) { 14 tm := parseTime(t, "2022/11/17 17:21:00") 15 testtime.SetTime(t, tm) 16 if !testtime.Now().Equal(tm) { 17 t.Error("testtime.Now() must be", tm) 18 } 19 }) 20 21 t.Run("SetFunc", func(t *testing.T) { 22 tm := parseTime(t, "2022/11/17 17:23:00") 23 testtime.SetFunc(t, func() time.Time { return tm }) 24 25 if !testtime.Now().Equal(tm) { 26 t.Error("testtime.Now() must be", tm) 27 } 28 }) 29 30 testtime.SetTime(t, time.Time{}) 31 if !testtime.Now().IsZero() { 32 t.Error("testtime.Now() must be zero value") 33 } 34 } 35 36 func parseTime(t *testing.T, s string) time.Time { 37 t.Helper() 38 tm, err := time.Parse("2006/01/02 15:04:05", s) 39 if err != nil { 40 t.Fatal("unexpected error:", err) 41 } 42 return tm 43 }