テスト駆動開発のパターン(Patterns for Test-Driven Development) レッドバーのパターン(Red Bar Patterns)
テスト駆動開発 (Kent Beck(著)、和田 卓人(翻訳)、オーム社)の第Ⅲ部(テスト駆動開発のパターン(Patterns for Tejst-Driven Development))、第26章(レッドバーのパターン(Red Bar Patterns))をJavaではなくGo言語で取り組んでみる。
コード
sample_test.go
package sample
import (
"fmt"
"testing"
)
func TestReducer(t *testing.T) {
reducer := newReducer(newPolygon())
got := reducer.result().npoints
want := 0
if got != want {
t.Errorf("reducer.result().snpoints got %v, want %v", got, want)
}
}
func TestServer(t *testing.T) {
socket := newSocket()
msg := "hello"
socket.writre(msg)
got := socket.read()
want := msg
if got != want {
t.Errorf("socket.read() got %v, want %v", got, want)
}
}
func TestRecordStoreLearning(t *testing.T) {
store := newRecordStore()
id := store.addRecord([]byte{5, 6}, 0, 2)
got := store.getRecordSize(id)
want := byte(2)
if got != want {
t.Errorf("store.getRecordSize(%v) got %v, want %v", id, got, want)
}
buffer := []byte{5, 6}
tests := []struct {
desc string
got, want byte
}{
{fmt.Sprintf("store.getRecord(%v, %v, 0)", id, buffer),
store.getRecord(id, buffer, 0), 2},
{"buffer[0]", buffer[0], 5},
{"buffer[1]", buffer[1], 6},
}
for _, test := range tests {
if test.got != test.want {
t.Errorf("%v got %v, want %v", test.desc, test.got, test.want)
}
}
}
sample.go
package sample
type reducer struct {
polygon
}
func newReducer(p polygon) reducer {
return reducer{}
}
func (r reducer) result() polygon {
return r.polygon
}
type polygon struct {
npoints int
}
func newPolygon() polygon {
return polygon{}
}
type socket struct{}
func newSocket() *socket {
return &socket{}
}
func (s *socket) writre(msg string) {
}
func (s *socket) read() string {
return "hello"
}
type recordStore struct {
buffer []byte
}
func newRecordStore() *recordStore {
return &recordStore{}
}
func (rs *recordStore) addRecord(buffer []byte, n, m int) byte {
for _, b := range buffer {
rs.buffer = append(rs.buffer, b)
}
return byte(0)
}
func (rs *recordStore) getRecord(id byte, buffer []byte, n int) byte {
return byte(2)
}
func (rs *recordStore) getRecordSize(id byte) byte {
return byte(2)
}
入出力結果(Terminal, Zsh)
% go test
PASS
ok _/Users/.../sample 0.344s
% go test
--- FAIL: TestServer (0.00s)
sample_test.go:21: socket.read() got , want hello
FAIL
exit status 1
FAIL _/Users/.../sample 0.311s
% go test
PASS
ok _/Users/.../sample 0.224s
% go test
--- FAIL: TestRecordStoreLearning (0.00s)
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 20 [running]:
testing.tRunner.func1.1(0x1134fa0, 0xc0000d0040)
/opt/local/lib/go/src/testing/testing.go:1072 +0x30d
testing.tRunner.func1(0xc000082a80)
/opt/local/lib/go/src/testing/testing.go:1075 +0x41a
panic(0x1134fa0, 0xc0000d0040)
/opt/local/lib/go/src/runtime/panic.go:969 +0x1b9
_/Users/.../sample.TestRecordStoreLearning(0xc000082a80)
/Users/.../sample/sample_test.go:42 +0xb0
testing.tRunner(0xc000082a80, 0x114d858)
/opt/local/lib/go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
/opt/local/lib/go/src/testing/testing.go:1168 +0x2b3
exit status 2
FAIL _/Users/.../sample 0.298s
% go test
--- FAIL: TestRecordStoreLearning (0.00s)
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 20 [running]:
testing.tRunner.func1.1(0x11352c0, 0xc0000c4040)
/opt/local/lib/go/src/testing/testing.go:1072 +0x30d
testing.tRunner.func1(0xc000082a80)
/opt/local/lib/go/src/testing/testing.go:1075 +0x41a
panic(0x11352c0, 0xc0000c4040)
/opt/local/lib/go/src/runtime/panic.go:969 +0x1b9
_/Users/.../sample.TestRecordStoreLearning(0xc000082a80)
/Users/.../sample/sample_test.go:42 +0x125
testing.tRunner(0xc000082a80, 0x114db78)
/opt/local/lib/go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
/opt/local/lib/go/src/testing/testing.go:1168 +0x2b3
exit status 2
FAIL _/Users/.../sample 0.285s
% go test
--- FAIL: TestRecordStoreLearning (0.00s)
sample_test.go:47: store.getRecord(0, [5 6], 0) got 0, want 2
FAIL
exit status 1
FAIL _/Users/.../sample 0.284s
% go test
PASS
ok _/Users/.../sample 0.220s
%