計算機科学のブログ

xUnit(The xUnit Example) スイートにまとめる(How Suite It Is)

テスト駆動開発 (Kent Beck(著)、和田 卓人(翻訳)、オーム社)の第Ⅱ部(xUnit(The xUnit Example))、第23章(スイートにまとめる(How Suite It Is))をPythonではなくGo言語で取り組んでみる。

コード

package main

import (
	"errors"
	"fmt"
)

type testResult struct {
	runCount, errCount int
}

func newTestResult() *testResult {
	return &testResult{runCount: 0, errCount: 0}
}
func (tr *testResult) started() {
	tr.runCount++
}
func (tr *testResult) failed() {
	tr.errCount++
}
func (tr *testResult) summary() string {
	return fmt.Sprintf("%v run, %v failed", tr.runCount, tr.errCount)
}

type wasRun struct {
	log string
}

func newWasRun() *wasRun {
	return &wasRun{}
}
func (wr *wasRun) setUp() {
	wr.log = "setUp "
}
func (wr *wasRun) tearDown() {
	wr.log += "tearDown "
}
func (wr *wasRun) testMethod() {
	wr.log += "testMethod "
}
func (wr *wasRun) testBrokenMethod() error {
	return errors.New("Error(testBrokenMethod)")
}
func (wr *wasRun) runTestMethod(tr *testResult) {
	tr.started()
	wr.setUp()
	wr.testMethod()
	wr.tearDown()
}
func (wr *wasRun) testTestMethod(tr *testResult) {
	wr.runTestMethod(tr)
	got := wr.log
	want := "setUp testMethod tearDown "
	if got != want {
		fmt.Printf("runTestMethod: got '%v', want '%v'\n", got, want)
	}
}
func (wr *wasRun) runTestResult(tr *testResult) {
	tr.started()
	wr.setUp()
	wr.testMethod()
	wr.tearDown()
}
func (wr *wasRun) testResult(tr *testResult) {
	wr.runTestResult(tr)
	got := tr.summary()
	want := "1 run, 0 failed"
	if got != want {
		fmt.Printf("runTestResult: got '%v', want '%v'\n", got, want)
	}
}
func (wr *wasRun) runTestFailedResult(tr *testResult) {
	tr.started()
	wr.setUp()
	err := wr.testBrokenMethod()
	if err != nil {
		tr.failed()
	}
	wr.tearDown()
}
func (wr *wasRun) testFailedResult(tr *testResult) {
	wr.runTestFailedResult(tr)
	got := tr.summary()
	want := "1 run, 1 failed"
	if got != want {
		fmt.Printf("runTestFailedResult: got '%v', want '%v'\n", got, want)
	}
}
func (wr *wasRun) runTestFailedResultFormatting(tr *testResult) {
	tr.started()
	tr.failed()
}
func (wr *wasRun) testFailedResultFormatting(tr *testResult) {
	wr.runTestFailedResultFormatting(tr)
	got := tr.summary()
	want := "1 run, 1 failed"
	if got != want {
		fmt.Printf("runTestFailedResultFormatting: got '%v', want '%v'\n", got, want)
	}
}
func (wr *wasRun) runAll(tr *testResult) {
	wr.runTestMethod(tr)
	wr.runTestFailedResult(tr)
}
func (wr *wasRun) testAll(tr *testResult) {
	wr.runAll(tr)
	got := tr.summary()
	want := "2 run, 1 failed"
	if got != want {
		fmt.Printf("runAll: got %v, want %v", got, want)
	}
}
func main() {
	wr := newWasRun()
	wr.testTestMethod(newTestResult())
	wr.testResult(newTestResult())
	wr.testFailedResult(newTestResult())
	wr.testFailedResultFormatting(newTestResult())
	wr.testAll(newTestResult())
}

入出力結果(Terminal, Zsh)

% go run ./main.go 
runTestMethod result.summary:  1 run, 0 failed
% go run ./main.go
runTestMethod: got setUp testMethod tearDown , want setUp testMethod tearDownrunTestFailedResult: got 1 run, 0 failed, want 1 run, 1 failed
% go run ./main.go
runTestMethod: got 'setUp testMethod tearDown ', want 'setUp testMethod tearDown'runTestFailedResult: got '1 run, 0 failed', want '1 run, 1 failed'
% go run ./main.go
runTestMethod: got 'setUp testMethod tearDown ', want 'setUp testMethod tearDown'
runTestFailedResult: got '1 run, 0 failed', want '1 run, 1 failed'
% go run ./main.go
runTestFailedResult: got '1 run, 0 failed', want '1 run, 1 failed'
% go run ./main.go
% go run ./main.go
%