From 474c5a23b0f742f3d3d367534e17e4267face9da Mon Sep 17 00:00:00 2001 From: nas Date: Wed, 25 Sep 2019 01:25:20 +0900 Subject: [PATCH 01/12] =?UTF-8?q?=E6=9C=80=E5=B0=8F=E6=A7=8B=E6=88=90?= =?UTF-8?q?=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/Makefile | 38 ++++++++++++++++++++++++++ kadai4/nas/fortune/fortune.go | 43 ++++++++++++++++++++++++++++++ kadai4/nas/fortune/fortune_test.go | 25 +++++++++++++++++ kadai4/nas/fortune/go.mod | 3 +++ kadai4/nas/fortune/main.go | 14 ++++++++++ 5 files changed, 123 insertions(+) create mode 100644 kadai4/nas/fortune/Makefile create mode 100644 kadai4/nas/fortune/fortune.go create mode 100644 kadai4/nas/fortune/fortune_test.go create mode 100644 kadai4/nas/fortune/go.mod create mode 100644 kadai4/nas/fortune/main.go diff --git a/kadai4/nas/fortune/Makefile b/kadai4/nas/fortune/Makefile new file mode 100644 index 0000000..fb6a352 --- /dev/null +++ b/kadai4/nas/fortune/Makefile @@ -0,0 +1,38 @@ +# CONST +BINARYNAME=fortune + +export GO111MODULE=on + +# command +.PHONY: help +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +.PHONY: build +build: ## go build + go build -v -o ${BINARYNAME} + +#.PHONY: test +#test: ## go test +# go test -v -cover ./pkg/${BINARYNAME} + +.PHONY: clean +clean: ## go clean + go clean -cache -testcache + +.PHONY: analyze +analyze: ## do static code analysis + goimports -l -w . + go vet ./... + golint ./... + +.PHONY: test +test: ## go test ... + go test ./... + +.PHONY: remove +remove: ## remove binary and test output data + rm -f ./${BINARYNAME} + +.PHONY: all +all: remove clean test analyze build ## run 'build' with 'remove', 'clean', 'test' and 'analyze' diff --git a/kadai4/nas/fortune/fortune.go b/kadai4/nas/fortune/fortune.go new file mode 100644 index 0000000..05246d0 --- /dev/null +++ b/kadai4/nas/fortune/fortune.go @@ -0,0 +1,43 @@ +package main + +import "time" + +const ( + // Great fortune type + Great = "大吉" + // High fortune type + High = "中吉" + // Middle fortune type + Middle = "吉" + // Low fortune type + Low = "凶" +) + +// Date behave today +type Date interface { + Today() time.Time +} + +// DateFunc return time +type DateFunc func() time.Time + +// Parameter behave random +type Parameter interface { + Random() float64 +} + +// ParameterFunc return double number +type ParameterFunc func() float64 + +// FortuneConfig has +type FortuneConfig struct{} + +// Fortune has any fortune type +type Fortune struct { + Type string +} + +// Draw return random Fortune +func Draw() (*Fortune, error) { + return &Fortune{Great}, nil +} diff --git a/kadai4/nas/fortune/fortune_test.go b/kadai4/nas/fortune/fortune_test.go new file mode 100644 index 0000000..ca11587 --- /dev/null +++ b/kadai4/nas/fortune/fortune_test.go @@ -0,0 +1,25 @@ +package main + +import ( + "testing" + "time" +) + +func TestDraw(t *testing.T) { + cases := []struct { + name string + date time.Time + Parameter float64 + want string + }{ + {"Great", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.00, Great}, + } + + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + if got := Great; tt.want != got { + t.Errorf("Draw() => %s, but want %s", got, tt.want) + } + }) + } +} diff --git a/kadai4/nas/fortune/go.mod b/kadai4/nas/fortune/go.mod new file mode 100644 index 0000000..2acac98 --- /dev/null +++ b/kadai4/nas/fortune/go.mod @@ -0,0 +1,3 @@ +module github.com/gopherdojo/dojo7/kadai4/nas/fortune + +go 1.13 diff --git a/kadai4/nas/fortune/main.go b/kadai4/nas/fortune/main.go new file mode 100644 index 0000000..d0a8be9 --- /dev/null +++ b/kadai4/nas/fortune/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "net/http" +) + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "Hello, HTTPサーバ") +} +func main() { + http.HandleFunc("/", handler) + http.ListenAndServe(":8080", nil) +} From 0fad18c4fdeec8d64b8f4c7a3e8370270c8cf5c1 Mon Sep 17 00:00:00 2001 From: nas Date: Wed, 25 Sep 2019 22:49:01 +0900 Subject: [PATCH 02/12] =?UTF-8?q?=E3=81=8A=E3=81=BF=E3=81=8F=E3=81=98?= =?UTF-8?q?=E3=81=AE=E5=8B=95=E3=81=8F=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/fortune.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/kadai4/nas/fortune/fortune.go b/kadai4/nas/fortune/fortune.go index 05246d0..c1ab2b8 100644 --- a/kadai4/nas/fortune/fortune.go +++ b/kadai4/nas/fortune/fortune.go @@ -29,15 +29,26 @@ type Parameter interface { // ParameterFunc return double number type ParameterFunc func() float64 -// FortuneConfig has -type FortuneConfig struct{} - -// Fortune has any fortune type +// Fortune has type Fortune struct { + Date + Parameter +} + +// Lack has any fortune type +type Lack struct { Type string } // Draw return random Fortune -func Draw() (*Fortune, error) { - return &Fortune{Great}, nil +func (f *Fortune) Draw() (*Lack, error) { + //d := today(f.Date) + return &Lack{Great}, nil +} + +func today(d Date) time.Time { + if d == nil { + return time.Now() + } + return d.Today() } From d18c1b42cfda9a98f5f8c1ab6e3197c2c6295b94 Mon Sep 17 00:00:00 2001 From: nas Date: Wed, 25 Sep 2019 23:21:18 +0900 Subject: [PATCH 03/12] =?UTF-8?q?isNewYear=20=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/fortune.go | 41 ++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/kadai4/nas/fortune/fortune.go b/kadai4/nas/fortune/fortune.go index c1ab2b8..f8de19a 100644 --- a/kadai4/nas/fortune/fortune.go +++ b/kadai4/nas/fortune/fortune.go @@ -3,24 +3,29 @@ package main import "time" const ( - // Great fortune type + // Great lack type Great = "大吉" - // High fortune type + // High lack type High = "中吉" - // Middle fortune type + // Middle lack type Middle = "吉" - // Low fortune type + // Low lack type Low = "凶" ) // Date behave today type Date interface { - Today() time.Time + Now() time.Time } // DateFunc return time type DateFunc func() time.Time +// Now return time +func (f DateFunc) Now() time.Time { + return f() +} + // Parameter behave random type Parameter interface { Random() float64 @@ -29,20 +34,28 @@ type Parameter interface { // ParameterFunc return double number type ParameterFunc func() float64 +// Random return float64 +func (f ParameterFunc) Random() float64 { + return f() +} + // Fortune has type Fortune struct { Date Parameter } -// Lack has any fortune type +// Lack has any lack type type Lack struct { Type string } // Draw return random Fortune func (f *Fortune) Draw() (*Lack, error) { - //d := today(f.Date) + d := today(f.Date) + if isNewYear(d) { + return &Lack{Great}, nil + } return &Lack{Great}, nil } @@ -50,5 +63,17 @@ func today(d Date) time.Time { if d == nil { return time.Now() } - return d.Today() + return d.Now() +} + +func isNewYear(d time.Time) bool { + _, month, day := d.Date() + if month != time.January { + return false + } + return map[int]bool{ + 1: true, + 2: true, + 3: true, + }[day] } From b7cb96a237e0864b4d5d9840cd974302ccbbf7da Mon Sep 17 00:00:00 2001 From: nas Date: Wed, 25 Sep 2019 23:31:14 +0900 Subject: [PATCH 04/12] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/Makefile | 2 +- kadai4/nas/fortune/fortune_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/kadai4/nas/fortune/Makefile b/kadai4/nas/fortune/Makefile index fb6a352..5f7906c 100644 --- a/kadai4/nas/fortune/Makefile +++ b/kadai4/nas/fortune/Makefile @@ -28,7 +28,7 @@ analyze: ## do static code analysis .PHONY: test test: ## go test ... - go test ./... + go test -v -cover ./... .PHONY: remove remove: ## remove binary and test output data diff --git a/kadai4/nas/fortune/fortune_test.go b/kadai4/nas/fortune/fortune_test.go index ca11587..2010e35 100644 --- a/kadai4/nas/fortune/fortune_test.go +++ b/kadai4/nas/fortune/fortune_test.go @@ -23,3 +23,23 @@ func TestDraw(t *testing.T) { }) } } + +func TestIsNewYear(t *testing.T) { + cases := []struct { + name string + date time.Time + want bool + }{ + {"True", time.Date(2019, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), true}, + {"FalseMonth", time.Date(2019, 2, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), false}, + {"FalseDay", time.Date(2019, 1, 4, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), false}, + } + + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + if got := isNewYear(tt.date); tt.want != got { + t.Errorf("isNewYear(%v) => want %t, but got %t", tt.date, tt.want, got) + } + }) + } +} From 7ce6e348f0cf5ba9cbc465761af7e109385273ea Mon Sep 17 00:00:00 2001 From: nas Date: Thu, 26 Sep 2019 01:18:22 +0900 Subject: [PATCH 05/12] =?UTF-8?q?=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/fortune.go | 50 ++++++++++++++++------ kadai4/nas/fortune/fortune_test.go | 68 +++++++++++++++++++++++++++--- 2 files changed, 101 insertions(+), 17 deletions(-) diff --git a/kadai4/nas/fortune/fortune.go b/kadai4/nas/fortune/fortune.go index f8de19a..05becbb 100644 --- a/kadai4/nas/fortune/fortune.go +++ b/kadai4/nas/fortune/fortune.go @@ -1,6 +1,10 @@ package main -import "time" +import ( + "fmt" + "math/rand" + "time" +) const ( // Great lack type @@ -28,14 +32,14 @@ func (f DateFunc) Now() time.Time { // Parameter behave random type Parameter interface { - Random() float64 + Float64() float64 } // ParameterFunc return double number type ParameterFunc func() float64 -// Random return float64 -func (f ParameterFunc) Random() float64 { +// Float64 return float64 +func (f ParameterFunc) Float64() float64 { return f() } @@ -45,18 +49,19 @@ type Fortune struct { Parameter } -// Lack has any lack type -type Lack struct { - Type string -} - // Draw return random Fortune -func (f *Fortune) Draw() (*Lack, error) { +func (f *Fortune) Draw() (string, error) { d := today(f.Date) if isNewYear(d) { - return &Lack{Great}, nil + return Great, nil } - return &Lack{Great}, nil + + p := random(f.Parameter) + l, err := draw(p) + if err != nil { + return "", err + } + return l, nil } func today(d Date) time.Time { @@ -77,3 +82,24 @@ func isNewYear(d time.Time) bool { 3: true, }[day] } + +func random(p Parameter) float64 { + if p == nil { + return rand.Float64() + } + return p.Float64() +} + +func draw(p float64) (string, error) { + switch { + case 6.0/6.0 >= p && p > 5.0/6.0: + return Great, nil + case 5.0/6.0 >= p && p > 3.0/6.0: + return High, nil + case 3.0/6.0 >= p && p > 1.0/6.0: + return Middle, nil + case 1.0/6.0 >= p && p >= 0.0/6.0: + return Low, nil + } + return "", fmt.Errorf("Can't draw, please redraw") +} diff --git a/kadai4/nas/fortune/fortune_test.go b/kadai4/nas/fortune/fortune_test.go index 2010e35..25bdf8b 100644 --- a/kadai4/nas/fortune/fortune_test.go +++ b/kadai4/nas/fortune/fortune_test.go @@ -5,25 +5,58 @@ import ( "time" ) -func TestDraw(t *testing.T) { +func TestFortuneDraw(t *testing.T) { cases := []struct { name string date time.Time - Parameter float64 + parameter float64 want string }{ - {"Great", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.00, Great}, + {"Low", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.16, Low}, + {"Middle", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.5, Middle}, + {"High", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.83, High}, + {"Great", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1, Great}, + {"NewYear", time.Date(2019, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0, Great}, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { - if got := Great; tt.want != got { - t.Errorf("Draw() => %s, but want %s", got, tt.want) + f := &Fortune{ + Date: DateFunc(func() time.Time { return tt.date }), + Parameter: ParameterFunc(func() float64 { return tt.parameter }), + } + got, err := f.Draw() + if err != nil { + t.Errorf("Unexpected Error : %v", err) + } + if tt.want != got { + t.Errorf("Draw() => want %s, but got %s", tt.want, got) } }) } } +func TestFortuneDrawError(t *testing.T) { + test := struct { + name string + date time.Time + parameter float64 + want string + }{"Invalid", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.01, ""} + + f := &Fortune{ + Date: DateFunc(func() time.Time { return test.date }), + Parameter: ParameterFunc(func() float64 { return test.parameter }), + } + got, err := f.Draw() + if err == nil { + t.Error("Expected Error but nil") + } + if test.want != got { + t.Errorf("Draw() => want%s, but got %s", test.want, got) + } +} + func TestIsNewYear(t *testing.T) { cases := []struct { name string @@ -43,3 +76,28 @@ func TestIsNewYear(t *testing.T) { }) } } + +func TestDraw(t *testing.T) { + cases := []struct { + name string + parameter float64 + want string + }{ + {"Low", 0.16, Low}, + {"Middle", 0.5, Middle}, + {"High", 0.83, High}, + {"Great", 1, Great}, + } + + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + got, err := draw(tt.parameter) + if err != nil { + t.Errorf("Unexpected Error : %v", err) + } + if tt.want != got { + t.Errorf("Draw() => want %s, but got %s", tt.want, got) + } + }) + } +} From 0c8056904f147cba3523946106c52b9e67b978f1 Mon Sep 17 00:00:00 2001 From: nas Date: Thu, 26 Sep 2019 01:30:40 +0900 Subject: [PATCH 06/12] =?UTF-8?q?=E9=96=A2=E6=95=B0=E5=90=8D=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/fortune.go | 4 ++-- kadai4/nas/fortune/fortune_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kadai4/nas/fortune/fortune.go b/kadai4/nas/fortune/fortune.go index 05becbb..14ef7bd 100644 --- a/kadai4/nas/fortune/fortune.go +++ b/kadai4/nas/fortune/fortune.go @@ -57,7 +57,7 @@ func (f *Fortune) Draw() (string, error) { } p := random(f.Parameter) - l, err := draw(p) + l, err := check(p) if err != nil { return "", err } @@ -90,7 +90,7 @@ func random(p Parameter) float64 { return p.Float64() } -func draw(p float64) (string, error) { +func check(p float64) (string, error) { switch { case 6.0/6.0 >= p && p > 5.0/6.0: return Great, nil diff --git a/kadai4/nas/fortune/fortune_test.go b/kadai4/nas/fortune/fortune_test.go index 25bdf8b..e4a8a8a 100644 --- a/kadai4/nas/fortune/fortune_test.go +++ b/kadai4/nas/fortune/fortune_test.go @@ -77,7 +77,7 @@ func TestIsNewYear(t *testing.T) { } } -func TestDraw(t *testing.T) { +func TestCheck(t *testing.T) { cases := []struct { name string parameter float64 @@ -91,7 +91,7 @@ func TestDraw(t *testing.T) { for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { - got, err := draw(tt.parameter) + got, err := check(tt.parameter) if err != nil { t.Errorf("Unexpected Error : %v", err) } From ffe5b9d3ccee2fecd2f2260958b363796c6f29ec Mon Sep 17 00:00:00 2001 From: nas Date: Thu, 26 Sep 2019 01:44:57 +0900 Subject: [PATCH 07/12] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/fortune_test.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/kadai4/nas/fortune/fortune_test.go b/kadai4/nas/fortune/fortune_test.go index e4a8a8a..8e10008 100644 --- a/kadai4/nas/fortune/fortune_test.go +++ b/kadai4/nas/fortune/fortune_test.go @@ -12,9 +12,6 @@ func TestFortuneDraw(t *testing.T) { parameter float64 want string }{ - {"Low", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.16, Low}, - {"Middle", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.5, Middle}, - {"High", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.83, High}, {"Great", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1, Great}, {"NewYear", time.Date(2019, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0, Great}, } @@ -36,13 +33,12 @@ func TestFortuneDraw(t *testing.T) { } } -func TestFortuneDrawError(t *testing.T) { +func TestFortuneDrawInValid(t *testing.T) { test := struct { - name string date time.Time parameter float64 want string - }{"Invalid", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.01, ""} + }{time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.01, ""} f := &Fortune{ Date: DateFunc(func() time.Time { return test.date }), @@ -101,3 +97,18 @@ func TestCheck(t *testing.T) { }) } } + +func TestCheckInValid(t *testing.T) { + test := struct { + parameter float64 + want string + }{1.01, ""} + + got, err := check(test.parameter) + if err == nil { + t.Error("Expected Error but nil") + } + if test.want != got { + t.Errorf("check() => want %s, but got %s", test.want, got) + } +} From 16524ec18e4bb295ff8f2755cbda533aafa5aca6 Mon Sep 17 00:00:00 2001 From: nas Date: Thu, 26 Sep 2019 21:36:03 +0900 Subject: [PATCH 08/12] =?UTF-8?q?=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=83=AA=E6=A7=8B=E6=88=90=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/{ => pkg/fortune}/fortune.go | 2 +- kadai4/nas/fortune/{ => pkg/fortune}/fortune_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename kadai4/nas/fortune/{ => pkg/fortune}/fortune.go (99%) rename kadai4/nas/fortune/{ => pkg/fortune}/fortune_test.go (99%) diff --git a/kadai4/nas/fortune/fortune.go b/kadai4/nas/fortune/pkg/fortune/fortune.go similarity index 99% rename from kadai4/nas/fortune/fortune.go rename to kadai4/nas/fortune/pkg/fortune/fortune.go index 14ef7bd..0efdfd5 100644 --- a/kadai4/nas/fortune/fortune.go +++ b/kadai4/nas/fortune/pkg/fortune/fortune.go @@ -1,4 +1,4 @@ -package main +package fortune import ( "fmt" diff --git a/kadai4/nas/fortune/fortune_test.go b/kadai4/nas/fortune/pkg/fortune/fortune_test.go similarity index 99% rename from kadai4/nas/fortune/fortune_test.go rename to kadai4/nas/fortune/pkg/fortune/fortune_test.go index 8e10008..4f73b73 100644 --- a/kadai4/nas/fortune/fortune_test.go +++ b/kadai4/nas/fortune/pkg/fortune/fortune_test.go @@ -1,4 +1,4 @@ -package main +package fortune import ( "testing" From 19f12851473d9c2c27a9616eba340a1fe467fff7 Mon Sep 17 00:00:00 2001 From: nas Date: Thu, 26 Sep 2019 22:19:12 +0900 Subject: [PATCH 09/12] =?UTF-8?q?json=E3=83=9E=E3=83=83=E3=83=94=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E7=94=A8=E6=A7=8B=E9=80=A0=E4=BD=93=E3=82=92=E5=AE=9A?= =?UTF-8?q?=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/pkg/fortune/fortune.go | 13 +++++++++---- kadai4/nas/fortune/pkg/fortune/fortune_test.go | 17 +++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/kadai4/nas/fortune/pkg/fortune/fortune.go b/kadai4/nas/fortune/pkg/fortune/fortune.go index 0efdfd5..21980d2 100644 --- a/kadai4/nas/fortune/pkg/fortune/fortune.go +++ b/kadai4/nas/fortune/pkg/fortune/fortune.go @@ -49,19 +49,24 @@ type Fortune struct { Parameter } +// Lack is drew results +type Lack struct { + Lack string `json:"lack"` +} + // Draw return random Fortune -func (f *Fortune) Draw() (string, error) { +func (f *Fortune) Draw() (*Lack, error) { d := today(f.Date) if isNewYear(d) { - return Great, nil + return &Lack{Great}, nil } p := random(f.Parameter) l, err := check(p) if err != nil { - return "", err + return nil, err } - return l, nil + return &Lack{l}, nil } func today(d Date) time.Time { diff --git a/kadai4/nas/fortune/pkg/fortune/fortune_test.go b/kadai4/nas/fortune/pkg/fortune/fortune_test.go index 4f73b73..0160408 100644 --- a/kadai4/nas/fortune/pkg/fortune/fortune_test.go +++ b/kadai4/nas/fortune/pkg/fortune/fortune_test.go @@ -1,19 +1,20 @@ package fortune import ( + "reflect" "testing" "time" ) -func TestFortuneDraw(t *testing.T) { +func TestFortune_Draw(t *testing.T) { cases := []struct { name string date time.Time parameter float64 - want string + want *Lack }{ - {"Great", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1, Great}, - {"NewYear", time.Date(2019, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0, Great}, + {"Great", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1, &Lack{Great}}, + {"NewYear", time.Date(2019, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0, &Lack{Great}}, } for _, tt := range cases { @@ -26,19 +27,19 @@ func TestFortuneDraw(t *testing.T) { if err != nil { t.Errorf("Unexpected Error : %v", err) } - if tt.want != got { + if !reflect.DeepEqual(tt.want, got) { t.Errorf("Draw() => want %s, but got %s", tt.want, got) } }) } } -func TestFortuneDrawInValid(t *testing.T) { +func TestFortune_DrawInValid(t *testing.T) { test := struct { date time.Time parameter float64 - want string - }{time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.01, ""} + want *Lack + }{time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1.01, nil} f := &Fortune{ Date: DateFunc(func() time.Time { return test.date }), From 8be1b61d75f79e74b208f4758bab2f9ad4998b10 Mon Sep 17 00:00:00 2001 From: nas Date: Fri, 27 Sep 2019 00:13:03 +0900 Subject: [PATCH 10/12] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/main.go | 29 +++++++++++++- kadai4/nas/fortune/main_test.go | 70 +++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 kadai4/nas/fortune/main_test.go diff --git a/kadai4/nas/fortune/main.go b/kadai4/nas/fortune/main.go index d0a8be9..7a05ba1 100644 --- a/kadai4/nas/fortune/main.go +++ b/kadai4/nas/fortune/main.go @@ -1,12 +1,37 @@ package main import ( - "fmt" + "encoding/json" "net/http" + + "github.com/gopherdojo/dojo7/kadai4/nas/fortune/pkg/fortune" +) + +var ( + // MockDate is Date + MockDate fortune.Date + // MockParameter is Parameter + MockParameter fortune.Parameter ) func handler(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "Hello, HTTPサーバ") + w.Header().Set("Content-Type", "applicaiton/json; charset=utf-8") + + f := &fortune.Fortune{ + Date: MockDate, + Parameter: MockParameter, + } + + l, err := f.Draw() + if err != nil { + http.Error(w, "Server error", http.StatusInternalServerError) + return + } + + enc := json.NewEncoder(w) + if err := enc.Encode(l); err != nil { + http.Error(w, "Server error", http.StatusInternalServerError) + } } func main() { http.HandleFunc("/", handler) diff --git a/kadai4/nas/fortune/main_test.go b/kadai4/nas/fortune/main_test.go new file mode 100644 index 0000000..5af6f39 --- /dev/null +++ b/kadai4/nas/fortune/main_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/gopherdojo/dojo7/kadai4/nas/fortune/pkg/fortune" +) + +func TestHandler(t *testing.T) { + cases := []struct { + name string + date time.Time + parameter float64 + want string + }{ + {"OKGreat", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 1, "{\"lack\":\"大吉\"}\n"}, + {"OKHigh", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.83, "{\"lack\":\"中吉\"}\n"}, + {"OKMiddle", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.5, "{\"lack\":\"吉\"}\n"}, + {"OKLow", time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.16, "{\"lack\":\"凶\"}\n"}, + {"OKNewYear", time.Date(2019, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)), 0.00, "{\"lack\":\"大吉\"}\n"}, + } + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + teardown := setupTest(t, fortune.DateFunc(func() time.Time { return tt.date }), fortune.ParameterFunc(func() float64 { return tt.parameter })) + defer teardown() + 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") + } + if got := string(b); got != tt.want { + t.Errorf("unexpected response: %s, but want %s", got, tt.want) + } + }) + } +} + +func setupTest(t *testing.T, d fortune.Date, p fortune.Parameter) func() { + t.Helper() + MockDate = d + MockParameter = p + return func() { + MockDate = nil + MockParameter = nil + } +} + +func TestHandlerNG(t *testing.T) { + teardown := setupTest(t, fortune.DateFunc(func() time.Time { return time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)) }), fortune.ParameterFunc(func() float64 { return 1.01 })) + defer teardown() + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + handler(w, r) + rw := w.Result() + defer rw.Body.Close() + if rw.StatusCode != http.StatusInternalServerError { + t.Errorf("unexpected status code : %d", rw.StatusCode) + } +} From 3ba44af28331984d19f510e2d7d113f46c1b569e Mon Sep 17 00:00:00 2001 From: nas Date: Fri, 27 Sep 2019 00:18:42 +0900 Subject: [PATCH 11/12] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/main.go | 5 +++-- kadai4/nas/fortune/main_test.go | 4 ++-- kadai4/nas/fortune/pkg/fortune/fortune.go | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/kadai4/nas/fortune/main.go b/kadai4/nas/fortune/main.go index 7a05ba1..c1a70f1 100644 --- a/kadai4/nas/fortune/main.go +++ b/kadai4/nas/fortune/main.go @@ -14,7 +14,8 @@ var ( MockParameter fortune.Parameter ) -func handler(w http.ResponseWriter, r *http.Request) { +// FortuneHandler response fortune result json +func FortuneHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "applicaiton/json; charset=utf-8") f := &fortune.Fortune{ @@ -34,6 +35,6 @@ func handler(w http.ResponseWriter, r *http.Request) { } } func main() { - http.HandleFunc("/", handler) + http.HandleFunc("/", FortuneHandler) http.ListenAndServe(":8080", nil) } diff --git a/kadai4/nas/fortune/main_test.go b/kadai4/nas/fortune/main_test.go index 5af6f39..ea14507 100644 --- a/kadai4/nas/fortune/main_test.go +++ b/kadai4/nas/fortune/main_test.go @@ -29,7 +29,7 @@ func TestHandler(t *testing.T) { defer teardown() w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", nil) - handler(w, r) + FortuneHandler(w, r) rw := w.Result() defer rw.Body.Close() if rw.StatusCode != http.StatusOK { @@ -61,7 +61,7 @@ func TestHandlerNG(t *testing.T) { defer teardown() w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/", nil) - handler(w, r) + FortuneHandler(w, r) rw := w.Result() defer rw.Body.Close() if rw.StatusCode != http.StatusInternalServerError { diff --git a/kadai4/nas/fortune/pkg/fortune/fortune.go b/kadai4/nas/fortune/pkg/fortune/fortune.go index 21980d2..ad2e398 100644 --- a/kadai4/nas/fortune/pkg/fortune/fortune.go +++ b/kadai4/nas/fortune/pkg/fortune/fortune.go @@ -22,7 +22,7 @@ type Date interface { Now() time.Time } -// DateFunc return time +// DateFunc return time func type DateFunc func() time.Time // Now return time @@ -35,7 +35,7 @@ type Parameter interface { Float64() float64 } -// ParameterFunc return double number +// ParameterFunc return float64 func type ParameterFunc func() float64 // Float64 return float64 @@ -43,7 +43,7 @@ func (f ParameterFunc) Float64() float64 { return f() } -// Fortune has +// Fortune has several config type Fortune struct { Date Parameter From b1d478f6724a70b310a6eddce81d3449a1f1b4bb Mon Sep 17 00:00:00 2001 From: nas Date: Fri, 27 Sep 2019 00:24:12 +0900 Subject: [PATCH 12/12] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E5=90=8D?= =?UTF-8?q?=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/nas/fortune/main_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/kadai4/nas/fortune/main_test.go b/kadai4/nas/fortune/main_test.go index ea14507..9eff0df 100644 --- a/kadai4/nas/fortune/main_test.go +++ b/kadai4/nas/fortune/main_test.go @@ -10,7 +10,7 @@ import ( "github.com/gopherdojo/dojo7/kadai4/nas/fortune/pkg/fortune" ) -func TestHandler(t *testing.T) { +func TestFortuneHandler(t *testing.T) { cases := []struct { name string date time.Time @@ -46,17 +46,7 @@ func TestHandler(t *testing.T) { } } -func setupTest(t *testing.T, d fortune.Date, p fortune.Parameter) func() { - t.Helper() - MockDate = d - MockParameter = p - return func() { - MockDate = nil - MockParameter = nil - } -} - -func TestHandlerNG(t *testing.T) { +func TestFortuneHandlerNG(t *testing.T) { teardown := setupTest(t, fortune.DateFunc(func() time.Time { return time.Date(2019, 9, 25, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", 60*60*9)) }), fortune.ParameterFunc(func() float64 { return 1.01 })) defer teardown() w := httptest.NewRecorder() @@ -68,3 +58,13 @@ func TestHandlerNG(t *testing.T) { t.Errorf("unexpected status code : %d", rw.StatusCode) } } + +func setupTest(t *testing.T, d fortune.Date, p fortune.Parameter) func() { + t.Helper() + MockDate = d + MockParameter = p + return func() { + MockDate = nil + MockParameter = nil + } +}