質問編集履歴
1
書式の改善
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
自動化テスト
|
test
CHANGED
@@ -1,175 +1 @@
|
|
1
|
-
- 以下のコードの自動化テストを行いたいと思っており、minitestについて学んでいます
|
2
|
-
|
3
|
-
|
1
|
+
minitest を学ぶ上で情報がまとまっているおすすめのurlや書籍などありますでしょうか?
|
4
|
-
|
5
|
-
- 質問2:実際サンプルで自動化テストコードを書くとするとどう書けば良いでしょうか?
|
6
|
-
|
7
|
-
- ちなみに、Personクラスを親クラスとして、Fighter・Wizard・Priestクラスを子クラスとしてそれぞれのクラスのインスタンス同士を戦わせて(対戦組み合わせによって能力補正あり)、勝敗をreturnするというコードです
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
```Ruby
|
12
|
-
|
13
|
-
class Person
|
14
|
-
|
15
|
-
attr_reader :strength
|
16
|
-
|
17
|
-
attr_reader :cleverness
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def initialize(st, cl)
|
22
|
-
|
23
|
-
@strength = st
|
24
|
-
|
25
|
-
@cleverness = cl
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
class Fighter < Person
|
34
|
-
|
35
|
-
alias base_strength strength
|
36
|
-
|
37
|
-
def strength
|
38
|
-
|
39
|
-
base_strength * 1.5
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
alias base_cleverness cleverness
|
46
|
-
|
47
|
-
def cleverness
|
48
|
-
|
49
|
-
base_cleverness * 1.0
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
class Wizard < Person
|
58
|
-
|
59
|
-
alias base_strength strength
|
60
|
-
|
61
|
-
def strength
|
62
|
-
|
63
|
-
base_strength * 0.5
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
alias base_cleverness cleverness
|
70
|
-
|
71
|
-
def cleverness
|
72
|
-
|
73
|
-
base_cleverness * 3.0
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
class Priest < Person
|
82
|
-
|
83
|
-
alias base_strength strength
|
84
|
-
|
85
|
-
def strength
|
86
|
-
|
87
|
-
base_strength * 1.0
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
alias base_cleverness cleverness
|
94
|
-
|
95
|
-
def cleverness
|
96
|
-
|
97
|
-
base_cleverness * 2.0
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
module Game
|
106
|
-
|
107
|
-
TABLE = {}
|
108
|
-
|
109
|
-
TABLE[[Fighter, Wizard]] = { strength: 0.85 }
|
110
|
-
|
111
|
-
TABLE[[Wizard, Priest]] = { cleverness: 0.75 }
|
112
|
-
|
113
|
-
TABLE[[Priest, Fighter]] = { strength: 0.95, cleverness: 0.9 }
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
module_function
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
def battle(a, b)
|
122
|
-
|
123
|
-
a_power = calc_power(a, TABLE[[a.class, b.class]])
|
124
|
-
|
125
|
-
b_power = calc_power(b, TABLE[[b.class, a.class]])
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
if a_power > b_power
|
130
|
-
|
131
|
-
"Win"
|
132
|
-
|
133
|
-
elsif a_power < b_power
|
134
|
-
|
135
|
-
"Lose"
|
136
|
-
|
137
|
-
else
|
138
|
-
|
139
|
-
"Draw"
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
def calc_power(person, power_correction)
|
148
|
-
|
149
|
-
st = person.strength
|
150
|
-
|
151
|
-
cl = person.cleverness
|
152
|
-
|
153
|
-
if power_correction != nil
|
154
|
-
|
155
|
-
if power_correction[:strength]
|
156
|
-
|
157
|
-
st *= power_correction[:strength]
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
if power_correction[:cleverness]
|
162
|
-
|
163
|
-
cl *= power_correction[:cleverness]
|
164
|
-
|
165
|
-
end
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
st + cl
|
170
|
-
|
171
|
-
end
|
172
|
-
|
173
|
-
end
|
174
|
-
|
175
|
-
```
|