Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
isuzuki committed Aug 16, 2019
1 parent 446a029 commit bc3d9dd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
16 changes: 10 additions & 6 deletions kadai4/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package main
import (
"encoding/json"
"log"
"math/rand"
"net/http"
"time"

"github.com/gopherdojo/dojo6/kadai4/omikuji"
)

func main() {
rand.Seed(time.Now().UnixNano())

http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Expand All @@ -24,9 +21,16 @@ type Response struct {
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

_, m, d := time.Now().Date()
ret := omikuji.Do(int(m), d)
res := Response{Msg: ret}
omikuji := omikuji.Omikuji{time.Now()}
ret, err := omikuji.Do()
res := Response{}

if err != nil {
res.Msg = err.Error()
log.Println("Error:", err)
} else {
res.Msg = ret
}

if err := json.NewEncoder(w).Encode(res); err != nil {
log.Println("Error:", err)
Expand Down
1 change: 0 additions & 1 deletion kadai4/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestHandler(t *testing.T) {
}

res := &Response{}
// TODO: 実行時のseeder設定により、大吉しか返ってないので、テストが通る
const expected = "大吉"
if err := json.Unmarshal(b, res); err != nil {
t.Fatalf("JSON unmarshall error: %v", err)
Expand Down
28 changes: 24 additions & 4 deletions kadai4/omikuji/omikuji.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package omikuji

import (
"fmt"
"math/rand"
"time"
)

var omikuji = map[int]string{
Expand All @@ -13,19 +15,37 @@ var omikuji = map[int]string{
5: "大吉",
}

func Do(month, day int) string {
type Omikuji struct {
Time time.Time
}

func (o *Omikuji) SetSeed(seed int64) {
rand.Seed(seed)
}

type OmikujiError struct {
Msg string
}

func (err *OmikujiError) Error() string {
return fmt.Sprintf(err.Msg)
}

func (o *Omikuji) Do() (string, error) {
var i int

_, m, d := o.Time.Date()
// 1/1 ~ 1/3のみ大吉を出す
if month == 1 && day >= 1 && day <= 3 {
if int(m) == 1 && d >= 1 && d <= 3 {
i = 5
} else {
i = rand.Intn(len(omikuji))
}

s, ok := omikuji[i]
if !ok {
panic("omikuji panic.")
return "", &OmikujiError{"おみくじが引けませんでした。"}
}

return s
return s, nil
}
23 changes: 17 additions & 6 deletions kadai4/omikuji/omikuji_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ package omikuji_test

import (
"testing"
"time"

"github.com/gopherdojo/dojo6/kadai4/omikuji"
)

func Test_SpecificPeriodLottery(t *testing.T) {
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},
}
Expand All @@ -15,11 +26,11 @@ func Test_SpecificPeriodLottery(t *testing.T) {

for m, days := range periods {
for _, d := range days {
for i := 0; i < 20; i++ {
actual := omikuji.Do(m, d)
if expect != actual {
t.Errorf(`Omikuji error: expect="%s" actual="%s"`, expect, actual)
}
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)
}
}
}
Expand Down

0 comments on commit bc3d9dd

Please sign in to comment.