[***]countdown: mock with sequence
This commit is contained in:
parent
56be76b275
commit
72e6ac9b77
@ -2,22 +2,32 @@ package countdown
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SpySleeper struct {
|
const (
|
||||||
Calls int
|
write = "write"
|
||||||
|
sleep = "sleep"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SpyCountdownOperations struct {
|
||||||
|
Calls []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SpySleeper) Sleep() {
|
func (s *SpyCountdownOperations) Sleep() {
|
||||||
s.Calls++
|
s.Calls = append(s.Calls, sleep)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SpyCountdownOperations) Write(p []byte) (n int, err error) {
|
||||||
|
s.Calls = append(s.Calls, write)
|
||||||
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCountdown(t *testing.T) {
|
func TestCountdown(t *testing.T) {
|
||||||
|
t.Run("print 3 to Go!", func(t *testing.T) {
|
||||||
buffer := &bytes.Buffer{}
|
buffer := &bytes.Buffer{}
|
||||||
sleeper := &SpySleeper{}
|
Countdown(buffer, &SpyCountdownOperations{})
|
||||||
|
|
||||||
Countdown(buffer, sleeper)
|
|
||||||
|
|
||||||
got := buffer.String()
|
got := buffer.String()
|
||||||
want := `3
|
want := `3
|
||||||
@ -28,8 +38,19 @@ Go!`
|
|||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("got %q want %q", got, want)
|
t.Errorf("got %q want %q", got, want)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if sleeper.Calls != 3 {
|
t.Run("sleep before every print", func(t *testing.T) {
|
||||||
t.Errorf("not enough calls to sleeper, want 3 got %d", sleeper.Calls)
|
spySleepPrinter := &SpyCountdownOperations{}
|
||||||
|
|
||||||
|
Countdown(spySleepPrinter, spySleepPrinter)
|
||||||
|
|
||||||
|
want := []string{
|
||||||
|
write, sleep, write, sleep, write, sleep, write,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(want, spySleepPrinter.Calls) {
|
||||||
|
t.Errorf("wanted %v got %v", want, spySleepPrinter.Calls)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user