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..248a593 --- /dev/null +++ b/kadai4/yoheimiyamoto/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "net/http" + + "github.com/YoheiMiyamoto/dojo4/kadai4/yoheimiyamoto/omikuji" +) + +func main() { + http.HandleFunc("/", omikuji.Handler) + http.ListenAndServe(":8080", nil) +} diff --git a/kadai4/yoheimiyamoto/omikuji/client.go b/kadai4/yoheimiyamoto/omikuji/client.go new file mode 100644 index 0000000..e0ab4f8 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/client.go @@ -0,0 +1,46 @@ +package omikuji + +import ( + "math/rand" + "time" +) + +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} +} + +// 吉凶を取得(大吉,中吉,小吉,凶,大凶) +func getType(now time.Time) string { + // 三が日は大吉にする + if now.Month() == 1 { + if now.Day() == 1 || now.Day() == 2 || now.Day() == 3 { + return "大吉" + } + } + + // 三が日以外はランダム + types := []string{ + "大吉", + "中吉", + "小吉", + "凶", + "大凶", + } + rand.Seed(time.Now().UnixNano()) + return types[rand.Intn(len(types))] +} diff --git a/kadai4/yoheimiyamoto/omikuji/client_test.go b/kadai4/yoheimiyamoto/omikuji/client_test.go new file mode 100644 index 0000000..4442e51 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/client_test.go @@ -0,0 +1,32 @@ +package omikuji + +import ( + "testing" + "time" +) + +func TestGetType(t *testing.T) { + 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.Errorf("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 new file mode 100644 index 0000000..5f2da26 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/handler.go @@ -0,0 +1,17 @@ +package omikuji + +import ( + "encoding/json" + "log" + "net/http" +) + +func Handler(w http.ResponseWriter, r *http.Request) { + c := NewClient() + result := c.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..b3855d4 --- /dev/null +++ b/kadai4/yoheimiyamoto/omikuji/handler_test.go @@ -0,0 +1,35 @@ +package omikuji + +import ( + "encoding/json" + "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") + } + 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) + } +} diff --git a/kadai4/yoheimiyamoto/omikuji/result.go b/kadai4/yoheimiyamoto/omikuji/result.go new file mode 100644 index 0000000..70a2616 --- /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` +}