96 lines
1.6 KiB
Go
96 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type SpySleeper struct {
|
|
Calls int
|
|
}
|
|
|
|
func (s *SpySleeper) Sleep() {
|
|
s.Calls++
|
|
}
|
|
|
|
type SpyTime struct {
|
|
durationSlept time.Duration
|
|
}
|
|
|
|
func (s *SpyTime) Sleep(duration time.Duration) {
|
|
s.durationSlept = duration
|
|
}
|
|
|
|
type SpyCountdownOperations struct {
|
|
Calls []string
|
|
}
|
|
|
|
func (s *SpyCountdownOperations) Sleep() {
|
|
s.Calls = append(s.Calls, sleep)
|
|
}
|
|
|
|
func (s *SpyCountdownOperations) Write(p []byte) (n int, err error) {
|
|
s.Calls = append(s.Calls, write)
|
|
return
|
|
}
|
|
|
|
const write = "write"
|
|
const sleep = "sleep"
|
|
|
|
func TestCountdown(t *testing.T) {
|
|
t.Run("default countdown test", func(t *testing.T) {
|
|
buffer := &bytes.Buffer{}
|
|
spySleeper := &SpySleeper{}
|
|
|
|
Countdown(buffer, spySleeper)
|
|
|
|
got := buffer.String()
|
|
want := `3
|
|
2
|
|
1
|
|
Go!`
|
|
|
|
if got != want {
|
|
t.Errorf("got %q want %q", got, want)
|
|
}
|
|
|
|
if spySleeper.Calls != 4 {
|
|
t.Errorf("not enough calls to sleeper, want 4 got %d", spySleeper.Calls)
|
|
}
|
|
})
|
|
|
|
t.Run("sleep before every print", func(t *testing.T) {
|
|
spySleepPrinter := &SpyCountdownOperations{}
|
|
Countdown(spySleepPrinter, spySleepPrinter)
|
|
|
|
want := []string{
|
|
sleep,
|
|
write,
|
|
sleep,
|
|
write,
|
|
sleep,
|
|
write,
|
|
sleep,
|
|
write,
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, spySleepPrinter.Calls) {
|
|
t.Errorf("wanted calls %v got %v", want, spySleepPrinter.Calls)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestConfigurableSleeper(t *testing.T) {
|
|
sleepTime := 5 * time.Second
|
|
|
|
spyTime := &SpyTime{}
|
|
sleeper := ConfigurableSleeper{sleepTime, spyTime.Sleep}
|
|
sleeper.Sleep()
|
|
|
|
if spyTime.durationSlept != sleepTime {
|
|
t.Errorf("should have slept for %v but slept for %v", sleepTime, spyTime.durationSlept)
|
|
}
|
|
}
|