Skip to content

Commit 09fa7e3

Browse files
committed
Add package: fortune
1 parent a32aa62 commit 09fa7e3

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Package fortune is a package that manages processing around fortune.
3+
*/
4+
package fortune
5+
6+
import (
7+
"math/rand"
8+
)
9+
10+
// Fortune means 運勢
11+
type Fortune string
12+
13+
const (
14+
// Daikichi means "大吉"
15+
Daikichi Fortune = "大吉"
16+
17+
// Chukichi means "中吉"
18+
Chukichi Fortune = "中吉"
19+
20+
// Shokichi means "小吉"
21+
Shokichi Fortune = "小吉"
22+
23+
// Kichi means "吉"
24+
Kichi Fortune = "吉"
25+
26+
// Suekichi means "末吉"
27+
Suekichi Fortune = "末吉"
28+
29+
// Kyo means "凶"
30+
Kyo Fortune = "凶"
31+
32+
// Daikyo means "大凶"
33+
Daikyo Fortune = "大凶"
34+
)
35+
36+
// DrawFortune draws a fortune.
37+
func DrawFortune() Fortune {
38+
fs := AllFortunes()
39+
return fs[rand.Intn(len(fs))]
40+
}
41+
42+
// AllFortunes returns all fortunes.
43+
func AllFortunes() []Fortune {
44+
return []Fortune{Daikichi, Chukichi, Shokichi, Kichi, Suekichi, Kyo, Daikyo}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package fortune
2+
3+
import (
4+
"math/rand"
5+
"testing"
6+
)
7+
8+
func TestFortune_DrawFortune(t *testing.T) {
9+
cases := map[string]struct {
10+
seed int64
11+
expected Fortune
12+
}{
13+
"KYOU": {seed: 0, expected: Kyo},
14+
"DAIKYOU": {seed: 1, expected: Daikyo},
15+
"SUEKICHI": {seed: 2, expected: Suekichi},
16+
"KICHI": {seed: 3, expected: Kichi},
17+
"CHUKICHI": {seed: 4, expected: Chukichi},
18+
"SHOKICHI": {seed: 5, expected: Shokichi},
19+
"DAICHIKI": {seed: 9, expected: Daikichi},
20+
}
21+
22+
for n, c := range cases {
23+
c := c
24+
t.Run(n, func(t *testing.T) {
25+
rand.Seed(c.seed)
26+
27+
expected := c.expected
28+
actual := DrawFortune()
29+
if actual != expected {
30+
t.Errorf(`unexpected response body: expected: "%s" actual: "%s"`, expected, actual)
31+
}
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)