Contenu connexe
Similaire à Goの時刻に関するテスト (20)
Goの時刻に関するテスト
- 5. • 8 20 : true
• 21 7 : false
- 6. func TestCanDeliver(t *testing.T) {
hour := time.Now().Hour()
expected := 8 <= hour && hour <= 20 //
result := CanDeliver()
if expected == result {
t.Log("OK")
} else {
t.Fatal("NG")
}
}
- 13. func TestCanDeliver(t *testing.T) {
cases := []struct {
hour int
want bool
}{
{7, false}, {8, true}, {20, true}, {21, false},
}
for _, c := range cases {
got := CanDeliver(c.hour)
if got != c.want {
t.Errorf("CanDeliver(%d) => %t, want %t", c.hour, got, c.want)
}
}
}
- 15. var now = time.Now // time.Now now
func CanDeliver() bool {
hour := now().Hour() // now
// 8 20
return 8 <= hour && hour <= 20
}
now
- 16. func fakeHour(hour int) {
// time.Time func now
now = func() time.Time { return time.Date(2017, 7, 7, hour, 0, 0, 0, time.Local) }
}
func TestCanDeliver(t *testing.T) {
// snip...
for _, c := range cases {
fakeHour(c.hour) // fakeHour
got := CanDeliver()
if got != c.want {
t.Errorf("hour: %d, CanDeliver() => %t, want %t", c.hour, got, c.want)
}
}
now = time.Now // reset time
}
- 19. var myClock = clock.NewClock() // myClock clock.Clock
func CanDeliver() bool {
hour := myClock.Now().Hour() // myClock
// 8 20
return 8 <= hour && hour <= 20
}
myClock
- 20. func fakeHour(hour int) {
// FakeClock
myClock = fakeclock.NewFakeClock(time.Date(2017, 7, 7, hour, 0, 0, 0, time.Local))
}
func TestCanDeliver(t *testing.T) {
// snip...
for _, c := range cases {
fakeHour(c.hour) // fakeHour
got := CanDeliver()
if got != c.want {
t.Errorf("hour: %d, CanDeliver() => %t, want %t", c.hour, got, c.want)
}
}
myClock = clock.NewClock() // reset time
}
- 24. func CanDeliver(hour int) bool {
// 8 20
return 8 <= hour && hour <= 20
}
func CanDeliverNow() bool {
hour := time.Now().Hour()
return CanDeliver(hour)
}
- 28. func fakeHour(hour int) {
// time.Now func
monkey.Patch(
time.Now,
func() time.Time { return time.Date(2017, 7, 7, hour, 0, 0, 0, time.Local) },
)
}
func TestCanDeliver(t *testing.T) {
// snip...
for _, c := range cases {
fakeHour(c.hour) // fakeHour
got := CanDeliver()
if got != c.want {
t.Errorf("hour: %d, CanDeliver() => %t, want %t", c.hour, got, c.want)
}
}
monkey.Unpatch(time.Now) // reset time
}
- 32. /* src/time/time.go */
var fakeTime Time //
func Fake(t Time) {
fakeTime = t
}
func ResetFake() {
fakeTime = Time{}
}
func Now() Time {
if !fakeTime.IsZero() {
return fakeTime
}
sec, nsec := now()
return Time{sec + unixToInternal, nsec, Local}
}
- 34. func fakeHour(hour int) {
time.Fake(time.Date(2017, 7, 7, hour, 0, 0, 0, time.Local))
}
func TestCanDeliver(t *testing.T) {
// snip...
for _, c := range cases {
fakeHour(c.hour) // fakeHour
got := CanDeliver()
if got != c.want {
t.Errorf("hour: %d, CanDeliver() => %t, want %t", c.hour, got, c.want)
}
}
time.ResetFake() // reset time
}
- 35. % /usr/local/go1.8.3_faketime/bin/go test -v ./timepkg
=== RUN TestCanDeliver
--- PASS: TestCanDeliver (0.00s)
sample_test.go:22: 2017-07-07 07:00:00 +0900 JST
sample_test.go:22: 2017-07-07 08:00:00 +0900 JST
sample_test.go:22: 2017-07-07 20:00:00 +0900 JST
sample_test.go:22: 2017-07-07 21:00:00 +0900 JST
PASS
ok github.com/kawaken/golang-time-testing/timepkg 0.466s