From 7e036f75e912cfd6763a17d9ab28452013acc2e8 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Tue, 4 Dec 2018 18:37:17 +0900 Subject: [PATCH 1/7] add --- kadai4/yoheimiyamoto/README.md | 4 ++++ kadai4/yoheimiyamoto/main.go | 23 ++++++++++++++++++ kadai4/yoheimiyamoto/omikuji/play.go | 32 ++++++++++++++++++++++++++ kadai4/yoheimiyamoto/omikuji/result.go | 7 ++++++ 4 files changed, 66 insertions(+) create mode 100644 kadai4/yoheimiyamoto/README.md create mode 100644 kadai4/yoheimiyamoto/main.go create mode 100644 kadai4/yoheimiyamoto/omikuji/play.go create mode 100644 kadai4/yoheimiyamoto/omikuji/result.go diff --git a/kadai4/yoheimiyamoto/README.md b/kadai4/yoheimiyamoto/README.md new file mode 100644 index 0000000..7eaa6be --- /dev/null +++ b/kadai4/yoheimiyamoto/README.md @@ -0,0 +1,4 @@ +## 概要 +* JSON形式でおみくじの結果を返す +* 正月(1/1-1/3)だけ大吉にする +* ハンドラのテストを書いてみる diff --git a/kadai4/yoheimiyamoto/main.go b/kadai4/yoheimiyamoto/main.go new file mode 100644 index 0000000..6b7729e --- /dev/null +++ b/kadai4/yoheimiyamoto/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + + "github.com/YoheiMiyamoto/dojo4/kadai4/yoheimiyamoto/omikuji" +) + +func main() { + http.HandleFunc("/", handler) + http.ListenAndServe(":8080", nil) +} + +func handler(w http.ResponseWriter, r *http.Request) { + result := omikuji.Play() + err := json.NewEncoder(w).Encode(result) + if err != nil { + log.Fatal(err) + return + } +} diff --git a/kadai4/yoheimiyamoto/omikuji/play.go b/kadai4/yoheimiyamoto/omikuji/play.go new file mode 100644 index 0000000..b7ad481 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/play.go @@ -0,0 +1,32 @@ +package omikuji + +import ( + "math/rand" + "time" +) + +func Play() *Result { + t := getType() + return &Result{t} +} + +func getType() string { + // 三が日は大吉にする + t := time.Now() + if t.Month() == 1 { + if t.Day() == 1 || t.Day() == 2 || t.Day() == 3 { + return "大吉" + } + } + + // 三が日以外はランダム + types := []string{ + "大吉", + "中吉", + "小吉", + "凶", + "大凶", + } + rand.Seed(time.Now().UnixNano()) + return types[rand.Intn(len(types))] +} diff --git a/kadai4/yoheimiyamoto/omikuji/result.go b/kadai4/yoheimiyamoto/omikuji/result.go new file mode 100644 index 0000000..d99d139 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/result.go @@ -0,0 +1,7 @@ +package omikuji + +// Result ... +type Result struct { + Type string `json:"type"` + // Message string `json:"message` +} From 38535c0e42ac78adf1591b925697a15aee48d464 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Tue, 4 Dec 2018 18:39:46 +0900 Subject: [PATCH 2/7] update --- kadai4/yoheimiyamoto/omikuji/play.go | 1 + kadai4/yoheimiyamoto/omikuji/result.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/kadai4/yoheimiyamoto/omikuji/play.go b/kadai4/yoheimiyamoto/omikuji/play.go index b7ad481..48f092f 100644 --- a/kadai4/yoheimiyamoto/omikuji/play.go +++ b/kadai4/yoheimiyamoto/omikuji/play.go @@ -10,6 +10,7 @@ func Play() *Result { return &Result{t} } +// 吉凶を取得(大吉,中吉,小吉,凶,大凶) func getType() string { // 三が日は大吉にする t := time.Now() diff --git a/kadai4/yoheimiyamoto/omikuji/result.go b/kadai4/yoheimiyamoto/omikuji/result.go index d99d139..70a2616 100644 --- a/kadai4/yoheimiyamoto/omikuji/result.go +++ b/kadai4/yoheimiyamoto/omikuji/result.go @@ -2,6 +2,6 @@ package omikuji // Result ... type Result struct { - Type string `json:"type"` + Type string `json:"type"` // 吉凶(大吉,中吉,小吉,凶,大凶) // Message string `json:"message` } From a8c1288b615c8cb91634c13359e3eeb2d8f5cec7 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Tue, 4 Dec 2018 18:57:42 +0900 Subject: [PATCH 3/7] update --- kadai4/yoheimiyamoto/main.go | 13 +------- kadai4/yoheimiyamoto/omikuji/handler.go | 16 ++++++++++ kadai4/yoheimiyamoto/omikuji/handler_test.go | 31 ++++++++++++++++++++ kadai4/yoheimiyamoto/omikuji/play.go | 12 ++++---- kadai4/yoheimiyamoto/omikuji/play_test.go | 15 ++++++++++ 5 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 kadai4/yoheimiyamoto/omikuji/handler.go create mode 100644 kadai4/yoheimiyamoto/omikuji/handler_test.go create mode 100644 kadai4/yoheimiyamoto/omikuji/play_test.go diff --git a/kadai4/yoheimiyamoto/main.go b/kadai4/yoheimiyamoto/main.go index 6b7729e..248a593 100644 --- a/kadai4/yoheimiyamoto/main.go +++ b/kadai4/yoheimiyamoto/main.go @@ -1,23 +1,12 @@ package main import ( - "encoding/json" - "log" "net/http" "github.com/YoheiMiyamoto/dojo4/kadai4/yoheimiyamoto/omikuji" ) func main() { - http.HandleFunc("/", handler) + http.HandleFunc("/", omikuji.Handler) http.ListenAndServe(":8080", nil) } - -func handler(w http.ResponseWriter, r *http.Request) { - result := omikuji.Play() - err := json.NewEncoder(w).Encode(result) - if err != nil { - log.Fatal(err) - return - } -} diff --git a/kadai4/yoheimiyamoto/omikuji/handler.go b/kadai4/yoheimiyamoto/omikuji/handler.go new file mode 100644 index 0000000..f0e4c42 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/handler.go @@ -0,0 +1,16 @@ +package omikuji + +import ( + "encoding/json" + "log" + "net/http" +) + +func Handler(w http.ResponseWriter, r *http.Request) { + result := play() + err := json.NewEncoder(w).Encode(result) + if err != nil { + log.Fatal(err) + return + } +} diff --git a/kadai4/yoheimiyamoto/omikuji/handler_test.go b/kadai4/yoheimiyamoto/omikuji/handler_test.go new file mode 100644 index 0000000..6c9b932 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/handler_test.go @@ -0,0 +1,31 @@ +package omikuji + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" +) + +func TestHandler(t *testing.T) { + // どうにかして、現在時刻を三が日に変更する必要がある + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + Handler(w, r) + rw := w.Result() + defer rw.Body.Close() + if rw.StatusCode != http.StatusOK { + t.Fatal("unexpected status code") + } + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Fatal("unexpected error") + } + const expected = "" + if string(b) != expected { + { + t.Fatalf("unexpected response: %s", string(b)) + } + } +} diff --git a/kadai4/yoheimiyamoto/omikuji/play.go b/kadai4/yoheimiyamoto/omikuji/play.go index 48f092f..97aef23 100644 --- a/kadai4/yoheimiyamoto/omikuji/play.go +++ b/kadai4/yoheimiyamoto/omikuji/play.go @@ -5,17 +5,17 @@ import ( "time" ) -func Play() *Result { - t := getType() +func play() *Result { + now := time.Now() + t := getType(now) return &Result{t} } // 吉凶を取得(大吉,中吉,小吉,凶,大凶) -func getType() string { +func getType(now time.Time) string { // 三が日は大吉にする - t := time.Now() - if t.Month() == 1 { - if t.Day() == 1 || t.Day() == 2 || t.Day() == 3 { + if now.Month() == 1 { + if now.Day() == 1 || now.Day() == 2 || now.Day() == 3 { return "大吉" } } diff --git a/kadai4/yoheimiyamoto/omikuji/play_test.go b/kadai4/yoheimiyamoto/omikuji/play_test.go new file mode 100644 index 0000000..1a6c302 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/play_test.go @@ -0,0 +1,15 @@ +package omikuji + +import ( + "testing" + "time" +) + +func TestGetType(t *testing.T) { + now := time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) + r := getType(now) + const expected = "大吉" + if r != expected { + t.Fatalf("expected: %s, actual: %s", expected, r) + } +} From 313021b365646af6d2d3409c2ceff386734e2551 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Tue, 4 Dec 2018 19:06:53 +0900 Subject: [PATCH 4/7] Update handler_test.go --- kadai4/yoheimiyamoto/omikuji/handler_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kadai4/yoheimiyamoto/omikuji/handler_test.go b/kadai4/yoheimiyamoto/omikuji/handler_test.go index 6c9b932..b3855d4 100644 --- a/kadai4/yoheimiyamoto/omikuji/handler_test.go +++ b/kadai4/yoheimiyamoto/omikuji/handler_test.go @@ -1,6 +1,7 @@ package omikuji import ( + "encoding/json" "io/ioutil" "net/http" "net/http/httptest" @@ -22,10 +23,13 @@ func TestHandler(t *testing.T) { if err != nil { t.Fatal("unexpected error") } - const expected = "" - if string(b) != expected { - { - t.Fatalf("unexpected response: %s", string(b)) - } + var actual Result + err = json.Unmarshal(b, &actual) + if err != nil { + t.Fatal(err) + } + expected := Result{"大吉"} + if actual != expected { + t.Fatalf("actual: %v, expected: %v", actual, expected) } } From 1b0b3c9cd37c8aac0770e4d152745e158fe04f6c Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Wed, 5 Dec 2018 12:47:29 +0900 Subject: [PATCH 5/7] update --- .../omikuji/{play.go => client.go} | 19 +++++-- kadai4/yoheimiyamoto/omikuji/client_test.go | 26 ++++++++++ kadai4/yoheimiyamoto/omikuji/handler.go | 3 +- kadai4/yoheimiyamoto/omikuji/play_test.go | 15 ------ kadai4/yoheimiyamoto/test/main.go | 35 +++++++++++++ kadai4/yoheimiyamoto/test2/main.go | 52 +++++++++++++++++++ 6 files changed, 131 insertions(+), 19 deletions(-) rename kadai4/yoheimiyamoto/omikuji/{play.go => client.go} (69%) create mode 100644 kadai4/yoheimiyamoto/omikuji/client_test.go delete mode 100644 kadai4/yoheimiyamoto/omikuji/play_test.go create mode 100644 kadai4/yoheimiyamoto/test/main.go create mode 100644 kadai4/yoheimiyamoto/test2/main.go diff --git a/kadai4/yoheimiyamoto/omikuji/play.go b/kadai4/yoheimiyamoto/omikuji/client.go similarity index 69% rename from kadai4/yoheimiyamoto/omikuji/play.go rename to kadai4/yoheimiyamoto/omikuji/client.go index 97aef23..e0ab4f8 100644 --- a/kadai4/yoheimiyamoto/omikuji/play.go +++ b/kadai4/yoheimiyamoto/omikuji/client.go @@ -5,9 +5,22 @@ import ( "time" ) -func play() *Result { - now := time.Now() - t := getType(now) +type client struct { + now now +} + +type now func() time.Time + +func NewClient() *client { + return &client{ + now(func() time.Time { + return time.Now() + }), + } +} + +func (c *client) play() *Result { + t := getType(c.now()) return &Result{t} } diff --git a/kadai4/yoheimiyamoto/omikuji/client_test.go b/kadai4/yoheimiyamoto/omikuji/client_test.go new file mode 100644 index 0000000..2f1a4b1 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/client_test.go @@ -0,0 +1,26 @@ +package omikuji + +import ( + "testing" + "time" +) + +func TestGetType(t *testing.T) { + now := time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) + r := getType(now) + const expected = "大吉" + if r != expected { + t.Fatalf("expected: %s, actual: %s", expected, r) + } +} + +func TestPlay(t *testing.T) { + c := &client{ + now(func() time.Time { return time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) }), + } + actual := *c.play() + expected := Result{"大吉"} + if actual != expected { + t.Fatalf("actual: %v, expected: %v", actual, expected) + } +} diff --git a/kadai4/yoheimiyamoto/omikuji/handler.go b/kadai4/yoheimiyamoto/omikuji/handler.go index f0e4c42..5f2da26 100644 --- a/kadai4/yoheimiyamoto/omikuji/handler.go +++ b/kadai4/yoheimiyamoto/omikuji/handler.go @@ -7,7 +7,8 @@ import ( ) func Handler(w http.ResponseWriter, r *http.Request) { - result := play() + c := NewClient() + result := c.play() err := json.NewEncoder(w).Encode(result) if err != nil { log.Fatal(err) diff --git a/kadai4/yoheimiyamoto/omikuji/play_test.go b/kadai4/yoheimiyamoto/omikuji/play_test.go deleted file mode 100644 index 1a6c302..0000000 --- a/kadai4/yoheimiyamoto/omikuji/play_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package omikuji - -import ( - "testing" - "time" -) - -func TestGetType(t *testing.T) { - now := time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) - r := getType(now) - const expected = "大吉" - if r != expected { - t.Fatalf("expected: %s, actual: %s", expected, r) - } -} diff --git a/kadai4/yoheimiyamoto/test/main.go b/kadai4/yoheimiyamoto/test/main.go new file mode 100644 index 0000000..5ed3892 --- /dev/null +++ b/kadai4/yoheimiyamoto/test/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "time" +) + +type Greeting struct { + Clock Clock +} + +type Clock interface { + Now() time.Time +} + +type clock struct { + time time.Time +} + +func (clock) Now() time.Time { + return time.Now() +} + +// モック用のストラクとをわざわざ作っている。 +type mockClock struct{} + +func (mockClock) Now() time.Time { + return time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) +} + +func main() { + // g := Greeting{clock{}} + g := Greeting{mockClock{}} + fmt.Println(g.Clock.Now()) +} diff --git a/kadai4/yoheimiyamoto/test2/main.go b/kadai4/yoheimiyamoto/test2/main.go new file mode 100644 index 0000000..81cda3f --- /dev/null +++ b/kadai4/yoheimiyamoto/test2/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "io" + "time" +) + +type Clock interface { + Now() time.Time +} + +type ClockFunc func() time.Time + +func (f ClockFunc) Now() time.Time { + return f() +} + +type Greeting struct { + Clock Clock +} + +func (g *Greeting) now() time.Time { + if g.Clock == nil { + return time.Now() + } + return g.Clock.Now() +} + +func (g *Greeting) Do(w io.Writer) error { + h := g.now().Hour() + var msg string + switch { + case h >= 4 && h <= 9: + msg = "おはよう" + case h >= 10 && h <= 16: + msg = "こんにちは" + default: + msg = "こんばんは" + } + + _, err := fmt.Fprint(w, msg) + if err != nil { + return err + } + + return nil +} + +func main() { + +} From 0f8ca6ad870b6eed3358d9704bb8d898e568c5ef Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Wed, 5 Dec 2018 13:45:23 +0900 Subject: [PATCH 6/7] clean --- kadai4/yoheimiyamoto/test/main.go | 35 -------------------- kadai4/yoheimiyamoto/test2/main.go | 52 ------------------------------ 2 files changed, 87 deletions(-) delete mode 100644 kadai4/yoheimiyamoto/test/main.go delete mode 100644 kadai4/yoheimiyamoto/test2/main.go diff --git a/kadai4/yoheimiyamoto/test/main.go b/kadai4/yoheimiyamoto/test/main.go deleted file mode 100644 index 5ed3892..0000000 --- a/kadai4/yoheimiyamoto/test/main.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -type Greeting struct { - Clock Clock -} - -type Clock interface { - Now() time.Time -} - -type clock struct { - time time.Time -} - -func (clock) Now() time.Time { - return time.Now() -} - -// モック用のストラクとをわざわざ作っている。 -type mockClock struct{} - -func (mockClock) Now() time.Time { - return time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) -} - -func main() { - // g := Greeting{clock{}} - g := Greeting{mockClock{}} - fmt.Println(g.Clock.Now()) -} diff --git a/kadai4/yoheimiyamoto/test2/main.go b/kadai4/yoheimiyamoto/test2/main.go deleted file mode 100644 index 81cda3f..0000000 --- a/kadai4/yoheimiyamoto/test2/main.go +++ /dev/null @@ -1,52 +0,0 @@ -package main - -import ( - "fmt" - "io" - "time" -) - -type Clock interface { - Now() time.Time -} - -type ClockFunc func() time.Time - -func (f ClockFunc) Now() time.Time { - return f() -} - -type Greeting struct { - Clock Clock -} - -func (g *Greeting) now() time.Time { - if g.Clock == nil { - return time.Now() - } - return g.Clock.Now() -} - -func (g *Greeting) Do(w io.Writer) error { - h := g.now().Hour() - var msg string - switch { - case h >= 4 && h <= 9: - msg = "おはよう" - case h >= 10 && h <= 16: - msg = "こんにちは" - default: - msg = "こんばんは" - } - - _, err := fmt.Fprint(w, msg) - if err != nil { - return err - } - - return nil -} - -func main() { - -} From d3ead4711b6faf2cca31d61ed5f9fd7db40285c8 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Wed, 5 Dec 2018 16:35:04 +0900 Subject: [PATCH 7/7] Update client_test.go --- kadai4/yoheimiyamoto/omikuji/client_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/kadai4/yoheimiyamoto/omikuji/client_test.go b/kadai4/yoheimiyamoto/omikuji/client_test.go index 2f1a4b1..4442e51 100644 --- a/kadai4/yoheimiyamoto/omikuji/client_test.go +++ b/kadai4/yoheimiyamoto/omikuji/client_test.go @@ -6,11 +6,17 @@ import ( ) func TestGetType(t *testing.T) { - now := time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local) - r := getType(now) - const expected = "大吉" + t.Run("", func(t *testing.T) { + testGetType(t, time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local), "大吉") + }) +} + +// TestGetTypeのテストヘルパー +func testGetType(t *testing.T, in time.Time, expected string) { + t.Helper() + r := getType(in) if r != expected { - t.Fatalf("expected: %s, actual: %s", expected, r) + t.Errorf("expected: %s, actual: %s", expected, r) } }