root/lang/ruby/starframe/test/starframe/test_sprite.rb @ 27982

Revision 27982, 16.3 kB (checked in by isaisstillalive, 5 years ago)
  • テスト内でクラス自体をいじると他のテストに影響が起こりそうなので削除
Line 
1require File.expand_path(File.join(File.dirname(__FILE__), "..", "helper"))
2require "starframe/sprite"
3
4class TestStarFrameSprite < Test::Unit::TestCase
5  class NoTextureSprite < StarFrame::Sprite
6  end
7  class TexturedSprite < StarFrame::Sprite
8    @texture = StarRuby::Texture.load("texture")
9  end
10  class TexturedSubSprite < TexturedSprite
11    @texture = StarRuby::Texture.load("texture_sub")
12  end
13  class TexturedSubNotChangedTextureSprite < TexturedSprite
14  end
15 
16  class CenteringTexturedSprite < StarFrame::Sprite
17    @texture  = StarRuby::Texture.load("options_texture")
18    @center_x = 2
19    @center_y = 4
20  end
21  class NegativeCenteringTexturedSprite < StarFrame::Sprite
22    @texture  = StarRuby::Texture.load("options_texture")
23    @center_x = -2
24    @center_y = -4
25  end
26 
27  class MovableSprite < StarFrame::Sprite
28    @texture = StarRuby::Texture.load("movable")
29    def init options
30      @movement_x = 10
31      @movement_y = 12
32      @angular_velocity = 14
33    end
34  end
35 
36  class TestSprite < StarFrame::Sprite
37    @texture = StarRuby::Texture.load("test")
38  end
39  class TallSprite < StarFrame::Sprite
40    @texture = StarRuby::Texture.new(10, 20)
41  end
42  class FatSprite < StarFrame::Sprite
43    @texture = StarRuby::Texture.new(20, 10)
44  end
45 
46  def setup
47    @sprite = TestSprite.new
48   
49    @screen = StarRuby::Texture.new(100, 100)
50  end
51 
52  def test_notexture_sprite
53    assert_nil NoTextureSprite.instance_variable_get(:@texture)
54    assert_equal 0,      NoTextureSprite.center_x
55    assert_equal 0,      NoTextureSprite.center_y
56    assert_equal [0, 0], NoTextureSprite.center
57  end
58  def test_textured_sprite
59    assert_kind_of StarRuby::Texture, TexturedSprite.instance_variable_get(:@texture)
60    assert_equal "texture",           TexturedSprite.instance_variable_get(:@texture).path
61    assert_equal 0,      TexturedSprite.center_x
62    assert_equal 0,      TexturedSprite.center_y
63    assert_equal [0, 0], TexturedSprite.center
64    assert_not_equal TexturedSubSprite.instance_variable_get(:@texture),       TexturedSprite.instance_variable_get(:@texture)
65    assert_not_equal CenteringTexturedSprite.instance_variable_get(:@texture), TexturedSprite.instance_variable_get(:@texture)
66    assert_not_equal TestSprite.instance_variable_get(:@texture),              TexturedSprite.instance_variable_get(:@texture)
67  end
68  def test_textured_sprite_subclass
69    assert_kind_of StarRuby::Texture, TexturedSubSprite.instance_variable_get(:@texture)
70    assert_equal "texture_sub",       TexturedSubSprite.instance_variable_get(:@texture).path
71    assert_equal 0,      TexturedSubSprite.center_x
72    assert_equal 0,      TexturedSubSprite.center_y
73    assert_equal [0, 0], TexturedSubSprite.center
74    assert_not_equal TexturedSprite.instance_variable_get(:@texture),          TexturedSubSprite.instance_variable_get(:@texture)
75    assert_not_equal CenteringTexturedSprite.instance_variable_get(:@texture), TexturedSubSprite.instance_variable_get(:@texture)
76    assert_not_equal TestSprite.instance_variable_get(:@texture),              TexturedSubSprite.instance_variable_get(:@texture)
77  end
78  def test_textured_sprite_subclass_not_changed_texture
79    assert_kind_of StarRuby::Texture, TexturedSubNotChangedTextureSprite.instance_variable_get(:@texture)
80    assert_equal "texture",           TexturedSubNotChangedTextureSprite.instance_variable_get(:@texture).path
81    assert_equal 0,      TexturedSubSprite.center_x
82    assert_equal 0,      TexturedSubSprite.center_y
83    assert_equal [0, 0], TexturedSubSprite.center
84    assert_equal     TexturedSprite.instance_variable_get(:@texture),          TexturedSubNotChangedTextureSprite.instance_variable_get(:@texture)
85    assert_not_equal CenteringTexturedSprite.instance_variable_get(:@texture), TexturedSubNotChangedTextureSprite.instance_variable_get(:@texture)
86    assert_not_equal TestSprite.instance_variable_get(:@texture),              TexturedSubNotChangedTextureSprite.instance_variable_get(:@texture)
87  end
88  def test_centering_textured_sprite
89    assert_kind_of StarRuby::Texture, CenteringTexturedSprite.instance_variable_get(:@texture)
90    assert_equal "options_texture",   CenteringTexturedSprite.instance_variable_get(:@texture).path
91    assert_equal 2,      CenteringTexturedSprite.center_x
92    assert_equal 4,      CenteringTexturedSprite.center_y
93    assert_equal [2, 4], CenteringTexturedSprite.center
94    assert_not_equal TexturedSprite.instance_variable_get(:@texture),    CenteringTexturedSprite.instance_variable_get(:@texture)
95    assert_not_equal TexturedSubSprite.instance_variable_get(:@texture), CenteringTexturedSprite.instance_variable_get(:@texture)
96    assert_not_equal TestSprite.instance_variable_get(:@texture),        CenteringTexturedSprite.instance_variable_get(:@texture)
97  end
98 
99  def test_notexture_sprite_instance
100    sprite = NoTextureSprite.new
101    assert_equal NoTextureSprite.instance_variable_get(:@texture),  sprite.instance_variable_get(:@texture)
102    assert_equal NoTextureSprite.center_x, sprite.center_x
103    assert_equal NoTextureSprite.center_y, sprite.center_y
104    assert_equal NoTextureSprite.center,   sprite.center
105    assert_equal({}, sprite.instance_variable_get(:@options))
106  end
107  def test_textured_sprite_instance
108    sprite = TexturedSprite.new
109    assert_equal TexturedSprite.instance_variable_get(:@texture),  sprite.instance_variable_get(:@texture)
110    assert_equal TexturedSprite.center_x, sprite.center_x
111    assert_equal TexturedSprite.center_y, sprite.center_y
112    assert_equal TexturedSprite.center,   sprite.center
113    assert_equal({}, sprite.instance_variable_get(:@options))
114  end
115  def test_textured_sprite_subclass_instance
116    sprite = TexturedSubSprite.new
117    assert_equal TexturedSubSprite.instance_variable_get(:@texture),  sprite.instance_variable_get(:@texture)
118    assert_equal TexturedSubSprite.center_x, sprite.center_x
119    assert_equal TexturedSubSprite.center_y, sprite.center_y
120    assert_equal TexturedSubSprite.center,   sprite.center
121    assert_equal({}, sprite.instance_variable_get(:@options))
122  end
123  def test_centering_textured_sprite_instance
124    sprite = CenteringTexturedSprite.new
125    assert_equal CenteringTexturedSprite.instance_variable_get(:@texture),  sprite.instance_variable_get(:@texture)
126    assert_equal CenteringTexturedSprite.center_x, sprite.center_x
127    assert_equal CenteringTexturedSprite.center_y, sprite.center_y
128    assert_equal CenteringTexturedSprite.center,   sprite.center
129    assert_equal({:center_x => 2, :center_y => 4}, sprite.instance_variable_get(:@options))
130  end
131 
132  def test_initialize
133    sprite = TestSprite.new
134    assert_equal 0,    sprite.x
135    assert_equal 0,    sprite.y
136    assert_equal 0,    sprite.angle
137    assert_equal 0xFF, sprite.alpha
138  end
139  def test_initialize_with_position
140    sprite = TestSprite.new(1, 2)
141    assert_equal 1,    sprite.x
142    assert_equal 2,    sprite.y
143    assert_equal 0,    sprite.angle
144    assert_equal 0xFF, sprite.alpha
145  end
146  def test_initialize_with_options
147    sprite = TestSprite.new(1, 2, :angle => 3, :alpha => 4)
148    assert_equal 1, sprite.x
149    assert_equal 2, sprite.y
150    assert_equal 3, sprite.angle
151    assert_equal 4, sprite.alpha
152  end
153 
154  def test_position_reader
155    sprite = MovableSprite.new(1, 2)
156    assert_kind_of Array, sprite.position
157    assert_equal [1, 2],  sprite.position
158    assert_equal 1, sprite.x
159    assert_equal 2, sprite.y
160  end
161  def test_position_writer
162    sprite = MovableSprite.new(1, 2)
163    sprite.position = 2, 3
164    assert_equal 2, sprite.x
165    assert_equal 3, sprite.y
166  end
167 
168  def test_movement_reader
169    sprite = MovableSprite.new
170    assert_kind_of Array,  sprite.movement
171    assert_equal [10, 12], sprite.movement
172    assert_equal 10, sprite.movement_x
173    assert_equal 12, sprite.movement_y
174  end
175  def test_movement_writer
176    sprite = MovableSprite.new(1, 2)
177    sprite.movement = 20, 22
178    assert_equal 20, sprite.movement_x
179    assert_equal 22, sprite.movement_y
180  end
181 
182  def test_angular_velocity
183    sprite = MovableSprite.new
184    assert_equal 14, sprite.angular_velocity
185  end
186 
187  def test_movement_default
188    sprite = TestSprite.new
189    assert_equal 0, sprite.movement_x
190    assert_equal 0, sprite.movement_y
191    assert_equal 0, sprite.angular_velocity
192  end
193 
194  def test_render_options
195    {
196      :alpha => 0xFF,
197      :angle => 0,
198      :tone_red => 0,
199      :tone_green => 0,
200      :tone_blue => 0,
201      :src_x => 0,
202      :src_y => 0,
203      :src_height => 1,
204      :src_width  => 1,
205      :scale_x => 1,
206      :scale_y => 1,
207      :saturation => 0xFF,
208      :blend_type => ":alpha",
209    }.each do |key, default|
210      assert_equal eval("#{default}"), eval("@sprite.#{key}"),              key
211      assert_equal nil,                @sprite.instance_variable_get(:@options)[key],                key
212      assert_equal 10,                 eval("@sprite.#{key} = 10"),         key
213      assert_equal 10,                 eval("@sprite.#{key}"),              key
214      assert_equal 10,                 @sprite.instance_variable_get(:@options)[key],                key
215      assert_equal eval("#{default}"), eval("@sprite.#{key} = #{default}"), key
216      assert_equal eval("#{default}"), eval("@sprite.#{key}"),              key
217      assert_equal nil,                @sprite.instance_variable_get(:@options)[key],                key
218    end
219  end
220  def test_tone
221    @sprite.tone_red   = 1
222    @sprite.tone_green = 2
223    @sprite.tone_blue  = 3
224   
225    @sprite.tone = 4
226    assert_equal 4, @sprite.tone_red
227    assert_equal 4, @sprite.tone_green
228    assert_equal 4, @sprite.tone_blue
229    assert_equal 4, @sprite.instance_variable_get(:@options)[:tone_red]
230    assert_equal 4, @sprite.instance_variable_get(:@options)[:tone_green]
231    assert_equal 4, @sprite.instance_variable_get(:@options)[:tone_blue]
232   
233    @sprite.tone = 0
234    assert_equal 0, @sprite.tone_red
235    assert_equal 0, @sprite.tone_green
236    assert_equal 0, @sprite.tone_blue
237    assert_equal nil, @sprite.instance_variable_get(:@options)[:tone_red]
238    assert_equal nil, @sprite.instance_variable_get(:@options)[:tone_green]
239    assert_equal nil, @sprite.instance_variable_get(:@options)[:tone_blue]
240  end
241  def test_scale
242    @sprite.scale_x = 2
243    @sprite.scale_y = 3
244   
245    @sprite.scale = 4
246    assert_equal 4, @sprite.scale_x
247    assert_equal 4, @sprite.scale_y
248    assert_equal 4, @sprite.instance_variable_get(:@options)[:scale_x]
249    assert_equal 4, @sprite.instance_variable_get(:@options)[:scale_y]
250   
251    @sprite.scale = 1
252    assert_equal 1, @sprite.scale_x
253    assert_equal 1, @sprite.scale_y
254    assert_equal nil, @sprite.instance_variable_get(:@options)[:scale_x]
255    assert_equal nil, @sprite.instance_variable_get(:@options)[:scale_y]
256  end
257 
258  def test_scale_width_to
259    sprite = TallSprite.new
260   
261    sprite.scale_width_to 30
262    assert_equal 3, sprite.scale_x
263    assert_equal 1, sprite.scale_y
264   
265    sprite.scale_width_to 2
266    assert_equal 0.2, sprite.scale_x
267    assert_equal 1,   sprite.scale_y
268  end
269  def test_scale_height_to
270    sprite = TallSprite.new
271   
272    sprite.scale_height_to 30
273    assert_equal 1,   sprite.scale_x
274    assert_equal 1.5, sprite.scale_y
275   
276    sprite.scale_height_to 2
277    assert_equal 1,   sprite.scale_x
278    assert_equal 0.1, sprite.scale_y
279  end
280  def test_scale_to_fit_on
281    [
282      [TallSprite, 2],
283      [FatSprite,  1.5],
284    ].each do |sprite_class, scale|
285      sprite = sprite_class.new
286      sprite.scale_to_fit_on 30
287      assert_equal 1.5, sprite.scale_x
288      assert_equal 1.5, sprite.scale_y
289     
290      sprite.scale_to_fit_on 30, 40
291      assert_equal scale, sprite.scale_x
292      assert_equal scale, sprite.scale_y
293    end
294  end
295  def test_scale_to_crop_on
296    [
297      [TallSprite, 3],
298      [FatSprite,  4],
299    ].each do |sprite_class, scale|
300      sprite = sprite_class.new
301      sprite.scale_to_crop_on 30
302      assert_equal 3, sprite.scale_x
303      assert_equal 3, sprite.scale_y
304     
305      sprite.scale_to_crop_on 30, 40
306      assert_equal scale, sprite.scale_x
307      assert_equal scale, sprite.scale_y
308    end
309  end
310 
311  def test_to_texture
312    assert_equal @sprite.instance_variable_get(:@texture), @sprite.to_texture
313  end
314 
315  def test_render_to
316    @sprite.x = 0
317    @sprite.y = 1
318    @sprite.render_to @screen
319   
320    assert_equal :render_texture, @screen.last_method
321    assert_equal @sprite.instance_variable_get(:@texture), @screen.last_method_args[0]
322    assert_equal 0, @screen.last_method_args[1]
323    assert_equal 1, @screen.last_method_args[2]
324    assert_equal 0, @screen.last_method_args[3].size
325    assert_equal({}, @screen.last_method_args[3])
326  end
327  def test_render_to_options
328    @sprite.x = 0
329    @sprite.y = 1
330    @sprite.alpha = 2
331    @sprite.angle = 3
332    @sprite.src_x = 4
333    @sprite.src_y = 5
334    @sprite.render_to @screen
335   
336    assert_equal :render_texture, @screen.last_method
337    assert_equal @sprite.instance_variable_get(:@texture), @screen.last_method_args[0]
338    assert_equal 0, @screen.last_method_args[1]
339    assert_equal 1, @screen.last_method_args[2]
340    assert_equal 4, @screen.last_method_args[3].size
341    assert_equal 2, @screen.last_method_args[3][:alpha]
342    assert_equal 3, @screen.last_method_args[3][:angle]
343    assert_equal 4, @screen.last_method_args[3][:src_x]
344    assert_equal 5, @screen.last_method_args[3][:src_y]
345  end
346  def test_render_to_centering_texture
347    sprite = CenteringTexturedSprite.new
348    sprite.render_to @screen
349    assert_equal :render_texture, @screen.last_method
350    assert_equal sprite.instance_variable_get(:@texture),  @screen.last_method_args[0]
351    assert_equal -2,      @screen.last_method_args[1]
352    assert_equal -4,      @screen.last_method_args[2]
353    assert_equal 2,       @screen.last_method_args[3].size
354    assert_equal 2,       @screen.last_method_args[3][:center_x]
355    assert_equal 4,       @screen.last_method_args[3][:center_y]
356  end
357  def test_render_to_negative_centering_texture
358    sprite = NegativeCenteringTexturedSprite.new
359    sprite.render_to @screen
360    assert_equal :render_texture, @screen.last_method
361    assert_equal sprite.instance_variable_get(:@texture),  @screen.last_method_args[0]
362    assert_equal 2,       @screen.last_method_args[1]
363    assert_equal 4,       @screen.last_method_args[2]
364    assert_equal 2,       @screen.last_method_args[3].size
365    assert_equal -2,      @screen.last_method_args[3][:center_x]
366    assert_equal -4,      @screen.last_method_args[3][:center_y]
367  end
368 
369 
370  def test_call
371    sprite = MovableSprite.new(1, 2)
372    sprite.call
373    assert_equal [11, 14],  sprite.position
374    assert_equal 14, sprite.angle
375    assert_equal 14, sprite.instance_variable_get(:@options)[:angle]
376  end
377  def test_move
378    sprite = MovableSprite.new(1, 2)
379    sprite.move
380    assert_equal [11, 14],  sprite.position
381    assert_equal 14, sprite.angle
382    assert_equal 14, sprite.instance_variable_get(:@options)[:angle]
383  end
384  def test_move_minute
385    sprite = MovableSprite.new(1, 2)
386    sprite.move_minute 0.6
387    assert_equal [7, 9.2],  sprite.position
388    assert_equal 8.4, sprite.angle
389    assert_equal 8.4, sprite.instance_variable_get(:@options)[:angle]
390  end
391  def test_futures
392    sprite = MovableSprite.new(1, 2)
393    assert_equal [7, 9.2],  sprite.future_position(0.6)
394    assert_equal 8.4, sprite.future_angle(0.6)
395  end
396 
397 
398  def test_squared_distance
399    sprite1 = TestSprite.new(0, 0)
400    sprite2 = TestSprite.new(3, 4)
401    assert_equal 25, sprite1.squared_distance(sprite2)
402   
403    sprite1 = TestSprite.new(0, 0)
404    sprite2 = TestSprite.new(2, 2)
405    assert_equal 8, sprite1.squared_distance(sprite2)
406  end
407  def test_distance
408    sprite1 = TestSprite.new(0, 0)
409    sprite2 = TestSprite.new(3, 4)
410    assert_equal 5, sprite1.distance(sprite2)
411   
412    sprite1 = TestSprite.new(0, 0)
413    sprite2 = TestSprite.new(2, 2)
414    assert_in_delta 2.828, 0.00, sprite1.distance(sprite2)
415  end
416 
417  def bench
418    require "benchmark"
419    sprite1 = TestSprite.new(0, 0)
420    sprite2 = TestSprite.new(2, 2)
421    Benchmark.bmbm do |bm|
422      bm.report{ 10000.times{ sprite1.distance(sprite2) } }
423      bm.report{ 10000.times{ sprite1.distance2(sprite2) } }
424      bm.report{ 10000.times{ sprite1.squared_distance(sprite2) } }
425    end
426  end
427end
Note: See TracBrowser for help on using the browser.