countdown: add basic countdown
This commit is contained in:
parent
5f02a8fbde
commit
d7840e9cb7
21
countdown/countdown.go
Normal file
21
countdown/countdown.go
Normal file
@ -0,0 +1,21 @@
|
||||
package countdown
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
finalWord = "Go!"
|
||||
countdownStart = 3
|
||||
)
|
||||
|
||||
func Countdown(out io.Writer) {
|
||||
for i := countdownStart; i > 0; i-- {
|
||||
fmt.Fprintf(out, "%d\n", i)
|
||||
// This is difficult to test!
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
fmt.Fprint(out, finalWord)
|
||||
}
|
22
countdown/countdown_test.go
Normal file
22
countdown/countdown_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package countdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCountdown(t *testing.T) {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
Countdown(buffer)
|
||||
|
||||
got := buffer.String()
|
||||
want := `3
|
||||
2
|
||||
1
|
||||
Go!`
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user