ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Go
2017/07/07
Umeda.go #2
? (@kawaken)
?
? LINE
? Go 3
? goa
³Ò´Ç¤Îʱ¿Ì¤Ëév¤¹¤ë¥Æ¥¹¥È
func CanDeliver() bool {
hour := time.Now().Hour()
// 8 20
return 8 <= hour && hour <= 20
}
func
? 8 20 : true
? 21 7 : false
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")
}
}
? time.Now
?
?
?
?
?
? 2 29
³Ò´Ç¤Îʱ¿Ì¤Ëév¤¹¤ë¥Æ¥¹¥È
1.
2.
3.
1.
func CanDeliver(hour int) bool {
// 8 20
return 8 <= hour && hour <= 20
}
time.Now
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)
}
}
}
2.
var now = time.Now // time.Now now
func CanDeliver() bool {
hour := now().Hour() // now
// 8 20
return 8 <= hour && hour <= 20
}
now
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
}
3.
code.cloudfoundry.org/clock
? time
?
? clock/fakeclock/FakeClock
var myClock = clock.NewClock() // myClock clock.Clock
func CanDeliver() bool {
hour := myClock.Now().Hour() // myClock
// 8 20
return 8 <= hour && hour <= 20
}
myClock
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
}
?
? time.Now
?
? time.Now
time.Now
4.
func CanDeliver(hour int) bool {
// 8 20
return 8 <= hour && hour <= 20
}
func CanDeliverNow() bool {
hour := time.Now().Hour()
return CanDeliver(hour)
}
5. Monkey patch
github.com/bouk/monkey
? Go
?
?
func CanDeliver() bool {
hour := time.Now().Hour()
// 8 20
return 8 <= hour && hour <= 20
}
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
}
6. time
// time.Now
func Now() Time {
sec, nsec := now()
return Time{sec + unixToInternal, nsec, Local}
}
Now
/* 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}
}
func CanDeliver() bool {
hour := time.Now().Hour()
// 8 20
return 8 <= hour && hour <= 20
}
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
}
% /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
1.
2. code.cloudfoundry.org/clock
3. monkey

More Related Content

³Ò´Ç¤Îʱ¿Ì¤Ëév¤¹¤ë¥Æ¥¹¥È

  • 4. func CanDeliver() bool { hour := time.Now().Hour() // 8 20 return 8 <= hour && hour <= 20 } func
  • 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") } }
  • 11. 1.
  • 12. func CanDeliver(hour int) bool { // 8 20 return 8 <= hour && hour <= 20 } time.Now
  • 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) } } }
  • 14. 2.
  • 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 }
  • 17. 3.
  • 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 }
  • 23. 4.
  • 24. func CanDeliver(hour int) bool { // 8 20 return 8 <= hour && hour <= 20 } func CanDeliverNow() bool { hour := time.Now().Hour() return CanDeliver(hour) }
  • 27. func CanDeliver() bool { hour := time.Now().Hour() // 8 20 return 8 <= hour && hour <= 20 }
  • 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 }
  • 30. // time.Now func Now() Time { sec, nsec := now() return Time{sec + unixToInternal, nsec, Local} }
  • 31. Now
  • 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} }
  • 33. func CanDeliver() bool { hour := time.Now().Hour() // 8 20 return 8 <= hour && hour <= 20 }
  • 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