diff --git a/kadai4/main.go b/kadai4/main.go new file mode 100644 index 0000000..8158735 --- /dev/null +++ b/kadai4/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "time" + + "github.com/gopherdojo/dojo6/kadai4/omikuji" +) + +func main() { + http.HandleFunc("/", handler) + http.ListenAndServe(":8080", nil) +} + +type Response struct { + Msg string `json:"msg"` +} + +func handler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + + omikuji := omikuji.Omikuji{time.Now()} + res := Response{omikuji.Do()} + + if err := json.NewEncoder(w).Encode(res); err != nil { + log.Println("Error:", err) + } +} diff --git a/kadai4/main_test.go b/kadai4/main_test.go new file mode 100644 index 0000000..6d03145 --- /dev/null +++ b/kadai4/main_test.go @@ -0,0 +1,37 @@ +package main + +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.Fatalf("unexpected status code: %d", rw.StatusCode) + } + + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Fatal("unexpected error") + } + + res := &Response{} + const expected = "大吉" + if err := json.Unmarshal(b, res); err != nil { + t.Fatalf("JSON unmarshall error: %v", err) + } + + if res.Msg != string(expected) { + t.Fatalf("unexpected response: %s", res.Msg) + } +} diff --git a/kadai4/omikuji/omikuji.go b/kadai4/omikuji/omikuji.go new file mode 100644 index 0000000..902892c --- /dev/null +++ b/kadai4/omikuji/omikuji.go @@ -0,0 +1,36 @@ +package omikuji + +import ( + "math/rand" + "time" +) + +var omikuji = []string{ + "凶", + "末吉", + "小吉", + "中吉", + "吉", + "大吉", +} + +type Omikuji struct { + Time time.Time +} + +func (o *Omikuji) SetSeed(seed int64) { + rand.Seed(seed) +} + +func (o *Omikuji) Do() string { + var i int + + _, m, d := o.Time.Date() + // 1/1 ~ 1/3のみ大吉を出す + if int(m) == 1 && d >= 1 && d <= 3 { + return "大吉" + } + + i = rand.Intn(len(omikuji)) + return omikuji[i] +} diff --git a/kadai4/omikuji/omikuji_test.go b/kadai4/omikuji/omikuji_test.go new file mode 100644 index 0000000..b16262c --- /dev/null +++ b/kadai4/omikuji/omikuji_test.go @@ -0,0 +1,37 @@ +package omikuji_test + +import ( + "testing" + "time" + + "github.com/gopherdojo/dojo6/kadai4/omikuji" +) + +func Test_NormalTime(t *testing.T) { + time := time.Date(2019, time.Month(8), 16, 0, 0, 0, 0, time.Local) + omikuji := omikuji.Omikuji{time} + expect := "大吉" + actual := omikuji.Do() + if expect != actual { + t.Errorf(`Omikuji error: expect="%s" actual="%s"`, expect, actual) + } +} + +func Test_SpecificPeriod(t *testing.T) { + periods := map[int][]int{ + 1: {1, 2, 3}, + } + + expect := "大吉" + + for m, days := range periods { + for _, d := range days { + time := time.Date(2019, time.Month(m), d, 0, 0, 0, 0, time.Local) + omikuji := omikuji.Omikuji{time} + actual := omikuji.Do() + if expect != actual { + t.Errorf(`Omikuji error: expect="%s" actual="%s"`, expect, actual) + } + } + } +}