2019年6月27日木曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 14(code quality assurance - Automated Testing)、Code Magnets(416)の解答を求めてみる。

コード

larger_test.go

package larger

import (
 "fmt"
 "testing"
)

func TestFirstLarger(t *testing.T) {
 want := 2
 got := Larger(2, 1)
 if got != want {
  t.Error(errorString(2, 1, got, want))
 }
}
func TestSecondLarger(t *testing.T) {
 want := 8
 got := Larger(4, 8)
 if got != want {
  t.Error(errorString(4, 8, got, want))
 }
}

func errorString(a, b, got, want int) string {
 return fmt.Sprintf("Larger(%d, %d) = %d, want %d", a, b, got, want)
}

larger.go

package larger

func Larger(a, b int) int {
 if a < b {
  return a
 }
 return b
}

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)

$ go test ~/go/src/headfirstgo/compare
--- FAIL: TestFirstLarger (0.00s)
    larger_test.go:12: Larger(2, 1) = 1, want 2
--- FAIL: TestSecondLarger (0.00s)
    larger_test.go:19: Larger(4, 8) = 4, want 8
FAIL
FAIL headfirstgo/compare 0.005s
$

0 コメント:

コメントを投稿