質問編集履歴
1
書式の改善
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
自動化テスト
|
body
CHANGED
@@ -1,88 +1,1 @@
|
|
1
|
-
- 以下のコードの自動化テストを行いたいと思っており、minitestについて学んでいます
|
2
|
-
|
1
|
+
minitest を学ぶ上で情報がまとまっているおすすめのurlや書籍などありますでしょうか?
|
3
|
-
- 質問2:実際サンプルで自動化テストコードを書くとするとどう書けば良いでしょうか?
|
4
|
-
- ちなみに、Personクラスを親クラスとして、Fighter・Wizard・Priestクラスを子クラスとしてそれぞれのクラスのインスタンス同士を戦わせて(対戦組み合わせによって能力補正あり)、勝敗をreturnするというコードです
|
5
|
-
|
6
|
-
```Ruby
|
7
|
-
class Person
|
8
|
-
attr_reader :strength
|
9
|
-
attr_reader :cleverness
|
10
|
-
|
11
|
-
def initialize(st, cl)
|
12
|
-
@strength = st
|
13
|
-
@cleverness = cl
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class Fighter < Person
|
18
|
-
alias base_strength strength
|
19
|
-
def strength
|
20
|
-
base_strength * 1.5
|
21
|
-
end
|
22
|
-
|
23
|
-
alias base_cleverness cleverness
|
24
|
-
def cleverness
|
25
|
-
base_cleverness * 1.0
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class Wizard < Person
|
30
|
-
alias base_strength strength
|
31
|
-
def strength
|
32
|
-
base_strength * 0.5
|
33
|
-
end
|
34
|
-
|
35
|
-
alias base_cleverness cleverness
|
36
|
-
def cleverness
|
37
|
-
base_cleverness * 3.0
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class Priest < Person
|
42
|
-
alias base_strength strength
|
43
|
-
def strength
|
44
|
-
base_strength * 1.0
|
45
|
-
end
|
46
|
-
|
47
|
-
alias base_cleverness cleverness
|
48
|
-
def cleverness
|
49
|
-
base_cleverness * 2.0
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
module Game
|
54
|
-
TABLE = {}
|
55
|
-
TABLE[[Fighter, Wizard]] = { strength: 0.85 }
|
56
|
-
TABLE[[Wizard, Priest]] = { cleverness: 0.75 }
|
57
|
-
TABLE[[Priest, Fighter]] = { strength: 0.95, cleverness: 0.9 }
|
58
|
-
|
59
|
-
module_function
|
60
|
-
|
61
|
-
def battle(a, b)
|
62
|
-
a_power = calc_power(a, TABLE[[a.class, b.class]])
|
63
|
-
b_power = calc_power(b, TABLE[[b.class, a.class]])
|
64
|
-
|
65
|
-
if a_power > b_power
|
66
|
-
"Win"
|
67
|
-
elsif a_power < b_power
|
68
|
-
"Lose"
|
69
|
-
else
|
70
|
-
"Draw"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def calc_power(person, power_correction)
|
75
|
-
st = person.strength
|
76
|
-
cl = person.cleverness
|
77
|
-
if power_correction != nil
|
78
|
-
if power_correction[:strength]
|
79
|
-
st *= power_correction[:strength]
|
80
|
-
end
|
81
|
-
if power_correction[:cleverness]
|
82
|
-
cl *= power_correction[:cleverness]
|
83
|
-
end
|
84
|
-
end
|
85
|
-
st + cl
|
86
|
-
end
|
87
|
-
end
|
88
|
-
```
|