[***]countdown: mock with sequence
This commit is contained in:
parent
56be76b275
commit
72e6ac9b77
@ -2,34 +2,55 @@ 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) {
|
||||||
buffer := &bytes.Buffer{}
|
t.Run("print 3 to Go!", func(t *testing.T) {
|
||||||
sleeper := &SpySleeper{}
|
buffer := &bytes.Buffer{}
|
||||||
|
Countdown(buffer, &SpyCountdownOperations{})
|
||||||
|
|
||||||
Countdown(buffer, sleeper)
|
got := buffer.String()
|
||||||
|
want := `3
|
||||||
got := buffer.String()
|
|
||||||
want := `3
|
|
||||||
2
|
2
|
||||||
1
|
1
|
||||||
Go!`
|
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