Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kadai4 by kaznishi #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions kadai4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Usage

```
$ go build omikuji.go
$ ./omokuji
```

上記を実行するとWebサーバが立ち上がるので、localhost:8080にアクセスしてください。
Binary file added kadai4/fortune/debug.test
Binary file not shown.
50 changes: 50 additions & 0 deletions kadai4/fortune/fortune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package fortune

import (
"math/rand"
"time"
)

var fortuneList = []string{"大吉", "中吉", "小吉", "吉", "末吉", "凶", "大凶"}

// Fortune はおみくじの構造体です
type Fortune struct {
Content string `json:"content"`
}

// FortuneSelector はおみくじ選択処理を行う構造体です
type FortuneSelector struct {
Qlock Qlock
}

// DefaultQlock はGetCurrenttimeで普通に現在時刻を返す時計です
type DefaultQlock struct{}

// GetCurrentTime は現在時刻を返す関数です
func (d DefaultQlock) GetCurrentTime() time.Time {
return time.Now()
}

// Qlock は時計のインターフェースです
type Qlock interface {
GetCurrentTime() time.Time
}

// SelectFortune はおみくじリストの中からひとつ選んでFortuneオブジェクトを返す関数です
func (f FortuneSelector) SelectFortune() Fortune {
t := f.Qlock.GetCurrentTime()
if t.Month() == 1 && (1 <= t.Day() && t.Day() <= 3) {
return f.selectFortuneOnlyDaikichi()
}
return f.selectFortuneRandom()
}

func (f FortuneSelector) selectFortuneOnlyDaikichi() Fortune {
return Fortune{Content: "大吉"}
}

func (f FortuneSelector) selectFortuneRandom() Fortune {
rand.Seed(time.Now().UnixNano())
content := fortuneList[rand.Int()%len(fortuneList)]
return Fortune{Content: content}
}
51 changes: 51 additions & 0 deletions kadai4/fortune/fortune_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fortune

import (
"testing"
"time"
)

func TestSelectFortuneDefault(t *testing.T) {
var dq DefaultQlock
fs := FortuneSelector{Qlock: dq}
f := fs.SelectFortune()

var fortuneList = []string{"大吉", "中吉", "小吉", "吉", "末吉", "凶", "大凶"}
exists := false
for _, v := range fortuneList {
if v == f.Content {
exists = true
break
}
}
if exists == false {
t.Fatalf("unexpected f.Content: %s", f.Content)
}
}

type ManualQlock struct {
Time time.Time
}

func (d ManualQlock) GetCurrentTime() time.Time {
return d.Time
}

func TestSelectFortuneNewYear(t *testing.T) {

timeList := []time.Time{
time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
time.Date(2000, time.January, 2, 12, 0, 0, 0, time.UTC),
time.Date(2000, time.January, 3, 23, 59, 0, 0, time.UTC),
}

for _, ti := range timeList {
mq := ManualQlock{Time: ti}
fs := FortuneSelector{Qlock: mq}
f := fs.SelectFortune()

if f.Content != "大吉" {
t.Fatalf("unexpected f.Content when: %s", ti)
}
}
}
30 changes: 30 additions & 0 deletions kadai4/omikuji.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/gopherdojo/dojo2/kadai4/fortune"
)

func handler(w http.ResponseWriter, r *http.Request) {
var dq fortune.DefaultQlock
fs := fortune.FortuneSelector{Qlock: dq}
f := fs.SelectFortune()

var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(f); err != nil {
log.Fatal(err)
}
fmt.Fprint(w, buf.String())
}

func main() {
fmt.Println("Web Server Starting...")
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
42 changes: 42 additions & 0 deletions kadai4/omikuji_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/gopherdojo/dojo2/kadai4/fortune"
)

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")
}
f := fortune.Fortune{}
if err := json.Unmarshal(b, &f); err != nil {
t.Fatal("failed json unmarshal")
}
// fortuneListのうちどれかが返ってきていればOK
var fortuneList = []string{"大吉", "中吉", "小吉", "吉", "末吉", "凶", "大凶"}
exists := false
for _, v := range fortuneList {
if v == f.Content {
exists = true
break
}
}
if exists == false {
t.Fatalf("unexpected f.Content: %s", f.Content)
}
}