Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goの時刻に関するテスト/Testing time in Go

Goの時刻に関するテスト/Testing time in Go

Goの時刻に関するテスト

Kentaro Kawano

July 07, 2017
Tweet

More Decks by Kentaro Kawano

Other Decks in Programming

Transcript

  1. func CanDeliver() bool { hour := time.Now().Hour() // 8࣌ʙ20࣌ͳΒ഑৴Մೳ return

    8 <= hour && hour <= 20 } ࣌ࠁΛऔಘͯ͠൑அ͢Δfunc
  2. μϝͳςετ 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") } }
  3. func CanDeliver(hour int) bool { // 8࣌ʙ20࣌ͳΒ഑৴Մೳ return 8 <=

    hour && hour <= 20 } ͦ΋ͦ΋time.NowΛ࢖͏ඞཁ͕ͳ͍
  4. 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) } } }
  5. var now = time.Now // time.NowΛnowʹೖΕ͓ͯ͘ func CanDeliver() bool {

    hour := now().Hour() // nowΛ࢖༻ͯ࣌͠ࠁΛऔಘ // 8࣌ʙ20࣌ͳΒ഑৴Մೳ return 8 <= hour && hour <= 20 } now Λςετ࣌ʹஔ͖׵͑Δ
  6. 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 }
  7. var myClock = clock.NewClock() // myClockʹclock.ClockΛ୅ೖ func CanDeliver() bool {

    hour := myClock.Now().Hour() // myClockΛ࢖༻ͯ࣌͠ࠁΛऔಘ // 8࣌ʙ20࣌ͳΒ഑৴Մೳ return 8 <= hour && hour <= 20 } myClock Λςετ࣌ʹஔ͖׵͑Δ
  8. 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 }
  9. func CanDeliver(hour int) bool { // 8࣌ʙ20࣌ͳΒ഑৴Մೳ return 8 <=

    hour && hour <= 20 } func CanDeliverNow() bool { hour := time.Now().Hour() return CanDeliver(hour) }
  10. 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 }
  11. // time.Now func Now() Time { sec, nsec := now()

    return Time{sec + unixToInternal, nsec, Local} }
  12. /* 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} }
  13. 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 }
  14. ΄ΜͱʹͣΕͯΜͷ͔ͳʁʁʁ % /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