Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Goの時刻に関するテスト

547 views

Published on

Goの時刻に関するテスト

Published in: Technology
  • Be the first to comment

Goの時刻に関するテスト

  1. 1. Go 2017/07/07 Umeda.go #2
  2. 2. • (@kawaken) • • LINE • Go 3 • goa
  3. 3. func CanDeliver() bool { hour := time.Now().Hour() // 8 20 return 8 <= hour && hour <= 20 } func
  4. 4. • 8 20 : true • 21 7 : false
  5. 5. 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") } }
  6. 6. • time.Now •
  7. 7. • • • • • 2 29
  8. 8. 1. 2. 3.
  9. 9. 1.
  10. 10. func CanDeliver(hour int) bool { // 8 20 return 8 <= hour && hour <= 20 } time.Now
  11. 11. 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) } } }
  12. 12. 2.
  13. 13. var now = time.Now // time.Now now func CanDeliver() bool { hour := now().Hour() // now // 8 20 return 8 <= hour && hour <= 20 } now
  14. 14. 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 }
  15. 15. 3.
  16. 16. code.cloudfoundry.org/clock • time • • clock/fakeclock/FakeClock
  17. 17. var myClock = clock.NewClock() // myClock clock.Clock func CanDeliver() bool { hour := myClock.Now().Hour() // myClock // 8 20 return 8 <= hour && hour <= 20 } myClock
  18. 18. 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 }
  19. 19. • • time.Now • • time.Now
  20. 20. time.Now
  21. 21. 4.
  22. 22. func CanDeliver(hour int) bool { // 8 20 return 8 <= hour && hour <= 20 } func CanDeliverNow() bool { hour := time.Now().Hour() return CanDeliver(hour) }
  23. 23. 5. Monkey patch
  24. 24. github.com/bouk/monkey • Go • •
  25. 25. func CanDeliver() bool { hour := time.Now().Hour() // 8 20 return 8 <= hour && hour <= 20 }
  26. 26. 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 }
  27. 27. 6. time
  28. 28. // time.Now func Now() Time { sec, nsec := now() return Time{sec + unixToInternal, nsec, Local} }
  29. 29. Now
  30. 30. /* 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} }
  31. 31. func CanDeliver() bool { hour := time.Now().Hour() // 8 20 return 8 <= hour && hour <= 20 }
  32. 32. 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 }
  33. 33. % /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
  34. 34. 1. 2. code.cloudfoundry.org/clock 3. monkey

×