-
Notifications
You must be signed in to change notification settings - Fork 827
Expand file tree
/
Copy pathlevel_spec.rb
More file actions
213 lines (181 loc) · 6.23 KB
/
level_spec.rb
File metadata and controls
213 lines (181 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require File.dirname(__FILE__) + '/../spec_helper'
require 'set'
describe RubyWarrior::Level do
before(:each) do
@profile = RubyWarrior::Profile.new
@floor = RubyWarrior::Floor.new
@level = RubyWarrior::Level.new(@profile, 1)
@level.floor = @floor
@level.stubs(:failed?).returns(false)
end
it "should consider passed when warrior is on stairs" do
@level.warrior = RubyWarrior::Units::Warrior.new
@floor.add(@level.warrior, 0, 0, :north)
@floor.place_stairs(0, 0)
@level.should be_passed
end
it "should default time bonus to zero" do
@level.time_bonus.should be_zero
end
it "should have a grade relative to ace score" do
@level.ace_score = 100
@level.grade_for(110).should == "S"
@level.grade_for(100).should == "S"
@level.grade_for(99).should == "A"
@level.grade_for(89).should == "B"
@level.grade_for(79).should == "C"
@level.grade_for(69).should == "D"
@level.grade_for(59).should == "F"
end
it "should have no grade if there is no ace score" do
@level.ace_score.should be_nil
@level.grade_for(100).should be_nil
end
it "should load file contents into level" do
@level.stubs(:load_path).returns('path/to/level.rb')
File.expects(:read).with('path/to/level.rb').returns("description 'foo'")
@level.load_level
@level.description.should == 'foo'
end
it "should have a player path from profile" do
@profile.stubs(:player_path).returns('path/to/player')
@level.player_path.should == 'path/to/player'
end
it "should have a load path from profile tower with level number in it" do
@profile.stubs(:tower_path).returns('path/to/tower')
@level.load_path.should == 'path/to/tower/level_001.rb'
end
it "should exist if file exists" do
@level.stubs(:load_path).returns('/foo/bar')
File.expects(:exist?).with('/foo/bar').returns(true)
@level.exists?.should be_true
end
it "should load player and player path" do
@level.stubs(:player_path).returns('player/path')
$:.expects(:<<).with('player/path')
@level.expects(:load).with('player.rb')
@level.load_player
end
it "should generate player files" do
@level.expects(:load_level)
generator = stub
generator.expects(:generate)
RubyWarrior::PlayerGenerator.expects(:new).with(@level).returns(generator)
@level.generate_player_files
end
it "should setup warrior with profile abilities" do
@profile.abilities = [:foo, :bar]
warrior = stub_everything
warrior.expects(:add_abilities).with(:foo, :bar)
@level.setup_warrior(warrior)
end
it "should setup warrior with profile name" do
@profile.warrior_name = "Joe"
warrior = stub_everything
warrior.expects(:name=).with("Joe")
@level.setup_warrior(warrior)
end
describe "playing" do
before(:each) do
@level.stubs(:load_level)
end
it "should load level once when playing multiple turns" do
@level.expects(:load_level)
@level.play(2)
end
it "should call prepare_turn and play_turn on each object specified number of times" do
object = RubyWarrior::Units::Base.new
object.expects(:prepare_turn).times(2)
object.expects(:perform_turn).times(2)
@floor.add(object, 0, 0, :north)
@level.play(2)
end
it "should return immediately when passed" do
object = RubyWarrior::Units::Base.new
object.expects(:turn).times(0)
@floor.add(object, 0, 0, :north)
@level.stubs(:passed?).returns(true)
@level.play(2)
end
it "should return true if the level is passed" do
@level.stubs(:passed?).returns(true)
@level.play(2).should be_true
end
it "should return false if the level is not passed" do
@level.stubs(:failed?).returns(true)
@level.play(2).should be_false
end
it "should yield to block in play method for each turn" do
int = 0
@level.play(2) do
int += 1
end
int.should == 2
end
it "should count down time_bonus once each turn" do
@level.time_bonus = 10
@level.play(3)
@level.time_bonus.should == 7
end
it "should count down time_bonus below 0" do
@level.time_bonus = 2
@level.play(5)
@level.time_bonus.should be_zero
end
it "should have a pretty score calculation" do
@level.score_calculation(123, 45).should == "123 + 45 = 168"
end
it "should not have a score calculation when starting score is zero" do
@level.score_calculation(0, 45).should == "45"
end
end
describe "tallying points" do
before(:each) do
@warrior = stub(:score => 0, :abilities => {})
@level.stubs(:warrior).returns(@warrior)
@level.floor.stubs(:other_units).returns([stub])
end
it "should add warrior score to profile" do
@warrior.stubs(:score).returns(30)
@level.tally_points
@profile.score.should == 30
end
it "should add warrior score to profile for epic mode" do
@profile.enable_epic_mode
@warrior.stubs(:score).returns(30)
@level.tally_points
@profile.current_epic_score.should == 30
end
it "should add level grade percent to profile for epic mode" do
@level.ace_score = 100
@profile.enable_epic_mode
@warrior.stubs(:score).returns(30)
@level.tally_points
@profile.current_epic_grades.should == { 1 => 0.3 }
end
it "should not add level grade if ace score is not set" do
@level.ace_score = nil
@profile.enable_epic_mode
@warrior.stubs(:score).returns(30)
@level.tally_points
@profile.current_epic_grades.should == {}
end
it "should apply warrior abilities to profile" do
@warrior.stubs(:abilities).returns({:foo => nil, :bar => nil})
@level.tally_points
@profile.abilities.to_set.should == [:foo, :bar].to_set
end
it "should apply time bonus to profile score" do
@level.time_bonus = 20
@level.tally_points
@profile.score.should == 20
end
it "should give 20% bonus when no other units left" do
@level.floor.stubs(:other_units).returns([])
@warrior.stubs(:score).returns(10)
@level.time_bonus = 10
@level.tally_points
@profile.score.should == 24
end
end
end