| 1 | require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "helper"))
|
|---|
| 2 | require "starframe/sprite/animation"
|
|---|
| 3 |
|
|---|
| 4 | class TestStarFrameSpriteAnimationInstance < Test::Unit::TestCase
|
|---|
| 5 | def setup
|
|---|
| 6 | @animation = StarFrame::Sprite::Animation.new nil
|
|---|
| 7 | end
|
|---|
| 8 |
|
|---|
| 9 | def test_instance_variables
|
|---|
| 10 | animation = StarFrame::Sprite::Animation.new nil
|
|---|
| 11 | assert_equal 0, animation.instance_variable_get(:@frame)
|
|---|
| 12 | assert_equal 0, animation.instance_variable_get(:@max_frame)
|
|---|
| 13 | assert_equal true, animation.instance_variable_get(:@loop)
|
|---|
| 14 | assert_equal true, animation.instance_variable_get(:@running)
|
|---|
| 15 | end
|
|---|
| 16 |
|
|---|
| 17 | def test_instance_variables_overwrite_options
|
|---|
| 18 | overwrited = Class.new(StarFrame::Sprite::Animation)
|
|---|
| 19 | overwrited.instance_variable_set(:@options, {:loop => false, :start => false})
|
|---|
| 20 | overwrited.instance_variable_set(:@max_frame, 10)
|
|---|
| 21 | animation = overwrited.new nil
|
|---|
| 22 | assert_equal 0, animation.instance_variable_get(:@frame)
|
|---|
| 23 | assert_equal 10, animation.instance_variable_get(:@max_frame)
|
|---|
| 24 | assert_equal false, animation.instance_variable_get(:@loop)
|
|---|
| 25 | assert_equal false, animation.instance_variable_get(:@running)
|
|---|
| 26 | end
|
|---|
| 27 |
|
|---|
| 28 | def test_instance_frame_controller_next
|
|---|
| 29 | @animation.instance_variable_set :@max_frame, 3
|
|---|
| 30 | assert_equal true, @animation.next
|
|---|
| 31 | assert_equal 1, @animation.instance_variable_get(:@frame)
|
|---|
| 32 | assert_equal true, @animation.next
|
|---|
| 33 | assert_equal 2, @animation.instance_variable_get(:@frame)
|
|---|
| 34 | assert_equal false, @animation.next
|
|---|
| 35 | assert_equal 0, @animation.instance_variable_get(:@frame)
|
|---|
| 36 | end
|
|---|
| 37 | def test_instance_frame_controller_rewind
|
|---|
| 38 | @animation.instance_variable_set :@frame, 1
|
|---|
| 39 | @animation.rewind
|
|---|
| 40 | assert_equal 0, @animation.instance_variable_get(:@frame)
|
|---|
| 41 | end
|
|---|
| 42 |
|
|---|
| 43 | def test_instance_animation_controller_update
|
|---|
| 44 | @animation.instance_variable_set :@max_frame, 3
|
|---|
| 45 | @animation.update
|
|---|
| 46 | assert_equal 1, @animation.instance_variable_get(:@frame)
|
|---|
| 47 | end
|
|---|
| 48 | def test_instance_animation_controller_update_with_stop
|
|---|
| 49 | @animation.instance_variable_set :@running, false
|
|---|
| 50 | @animation.instance_variable_set :@max_frame, 3
|
|---|
| 51 | @animation.update
|
|---|
| 52 | assert_equal 0, @animation.instance_variable_get(:@frame)
|
|---|
| 53 | end
|
|---|
| 54 | def test_instance_animation_controller_update_with_loop
|
|---|
| 55 | @animation.instance_variable_set :@max_frame, 3
|
|---|
| 56 | 3.times{ @animation.update }
|
|---|
| 57 | assert_equal 0, @animation.instance_variable_get(:@frame)
|
|---|
| 58 | assert_equal true, @animation.instance_variable_get(:@running)
|
|---|
| 59 | end
|
|---|
| 60 | def test_instance_animation_controller_update_with_unloop
|
|---|
| 61 | @animation.instance_variable_set :@loop, false
|
|---|
| 62 | @animation.instance_variable_set :@max_frame, 3
|
|---|
| 63 | 3.times{ @animation.update }
|
|---|
| 64 | assert_equal 0, @animation.instance_variable_get(:@frame)
|
|---|
| 65 | assert_equal false, @animation.instance_variable_get(:@running)
|
|---|
| 66 | end
|
|---|
| 67 |
|
|---|
| 68 | def test_instance_animation_controller_start
|
|---|
| 69 | @animation.instance_variable_set :@running, false
|
|---|
| 70 | @animation.start
|
|---|
| 71 | assert_equal true, @animation.instance_variable_get(:@running)
|
|---|
| 72 | end
|
|---|
| 73 | def test_instance_animation_controller_stop
|
|---|
| 74 | @animation.instance_variable_set :@running, true
|
|---|
| 75 | @animation.stop
|
|---|
| 76 | assert_equal false, @animation.instance_variable_get(:@running)
|
|---|
| 77 | end
|
|---|
| 78 |
|
|---|
| 79 | def test_instance_animation_controller_restart
|
|---|
| 80 | @animation.instance_variable_set :@frame, 3
|
|---|
| 81 | @animation.instance_variable_set :@running, false
|
|---|
| 82 | @animation.restart
|
|---|
| 83 | assert_equal 0, @animation.instance_variable_get(:@frame)
|
|---|
| 84 | assert_equal true, @animation.instance_variable_get(:@running)
|
|---|
| 85 | end
|
|---|
| 86 | end
|
|---|
| 87 |
|
|---|
| 88 | class TestStarFrameSpriteAnimationPreparedInstance < Test::Unit::TestCase
|
|---|
| 89 | class AnimationSprite
|
|---|
| 90 | def initialize
|
|---|
| 91 | @called = []
|
|---|
| 92 | end
|
|---|
| 93 |
|
|---|
| 94 | attr_accessor :called
|
|---|
| 95 | def method_missing *args
|
|---|
| 96 | @called << args
|
|---|
| 97 | end
|
|---|
| 98 | end
|
|---|
| 99 |
|
|---|
| 100 | class PreparedAnimation < StarFrame::Sprite::Animation
|
|---|
| 101 | @animations = {
|
|---|
| 102 | 0 => [
|
|---|
| 103 | [:method1, 0],
|
|---|
| 104 | [:method2, 10],
|
|---|
| 105 | ],
|
|---|
| 106 | 1 => [
|
|---|
| 107 | [:method1, 1],
|
|---|
| 108 | [:method2, 11],
|
|---|
| 109 | ],
|
|---|
| 110 | 2 => [
|
|---|
| 111 | [:method3, 0],
|
|---|
| 112 | ],
|
|---|
| 113 | 3 => [
|
|---|
| 114 | [:method3, 2],
|
|---|
| 115 | ],
|
|---|
| 116 | }
|
|---|
| 117 | @max_frame = 4
|
|---|
| 118 | end
|
|---|
| 119 |
|
|---|
| 120 | def test_initialize_animations
|
|---|
| 121 | sprite1 = AnimationSprite.new
|
|---|
| 122 | animation1 = PreparedAnimation.new(sprite1)
|
|---|
| 123 |
|
|---|
| 124 | sprite2 = AnimationSprite.new
|
|---|
| 125 | animation2 = PreparedAnimation.new(sprite2)
|
|---|
| 126 |
|
|---|
| 127 | animation1.next
|
|---|
| 128 | assert_equal [[:method1, 0], [:method2, 10]], sprite1.called
|
|---|
| 129 | sprite1.called.clear
|
|---|
| 130 |
|
|---|
| 131 | animation1.next
|
|---|
| 132 | assert_equal [[:method1, 1], [:method2, 11]], sprite1.called
|
|---|
| 133 | sprite1.called.clear
|
|---|
| 134 |
|
|---|
| 135 | animation2.next
|
|---|
| 136 | assert_equal [[:method1, 0], [:method2, 10]], sprite2.called
|
|---|
| 137 | sprite2.called.clear
|
|---|
| 138 |
|
|---|
| 139 | animation1.next
|
|---|
| 140 | assert_equal [[:method3, 0]], sprite1.called
|
|---|
| 141 | sprite1.called.clear
|
|---|
| 142 |
|
|---|
| 143 | animation1.next
|
|---|
| 144 | assert_equal [[:method3, 2]], sprite1.called
|
|---|
| 145 | sprite1.called.clear
|
|---|
| 146 |
|
|---|
| 147 | animation1.next
|
|---|
| 148 | assert_equal [[:method1, 0], [:method2, 10]], sprite1.called
|
|---|
| 149 | sprite1.called.clear
|
|---|
| 150 | end
|
|---|
| 151 | end
|
|---|
| 152 |
|
|---|
| 153 | class TestStarFrameSpriteAnimationClassMethods < Test::Unit::TestCase
|
|---|
| 154 | class DefineSprite; end
|
|---|
| 155 |
|
|---|
| 156 | def test_define_method
|
|---|
| 157 | animation_class = Class.new(StarFrame::Sprite::Animation)
|
|---|
| 158 | animation_class.instance_variable_set :@sprite_class, DefineSprite
|
|---|
| 159 |
|
|---|
| 160 | before_instance_methods = DefineSprite.instance_methods
|
|---|
| 161 | before_method_count = StarFrame::Sprite::Animation::ClassMethods.__send__(:class_variable_get, :@@method_count)
|
|---|
| 162 |
|
|---|
| 163 | animation_class.__send__(:frame){ |c| return c }
|
|---|
| 164 |
|
|---|
| 165 | after_instance_methods = DefineSprite.instance_methods
|
|---|
| 166 | after_method_count = StarFrame::Sprite::Animation::ClassMethods.__send__(:class_variable_get, :@@method_count)
|
|---|
| 167 | method_name = "_starframe_sprite_animation_#{after_method_count}"
|
|---|
| 168 |
|
|---|
| 169 | assert_equal 1, (after_method_count-before_method_count)
|
|---|
| 170 | define_methods = after_instance_methods - before_instance_methods
|
|---|
| 171 | assert_equal [method_name], define_methods
|
|---|
| 172 |
|
|---|
| 173 | o = Object.new
|
|---|
| 174 | assert_equal o, DefineSprite.new.__send__(method_name, o)
|
|---|
| 175 | end
|
|---|
| 176 |
|
|---|
| 177 | def assertion_animations expected, max_frame, &block
|
|---|
| 178 | animation_class = Class.new(StarFrame::Sprite::Animation)
|
|---|
| 179 | animation_class.instance_variable_set :@sprite_class, DefineSprite
|
|---|
| 180 |
|
|---|
| 181 | before_method_count = StarFrame::Sprite::Animation::ClassMethods.__send__(:class_variable_get, :@@method_count)
|
|---|
| 182 |
|
|---|
| 183 | animation_class.class_eval &block
|
|---|
| 184 |
|
|---|
| 185 | counted_expected = {}
|
|---|
| 186 | expected.each do |frame, frame_animations|
|
|---|
| 187 | counted_expected[frame] = frame_animations.map do |count, c|
|
|---|
| 188 | [:"_starframe_sprite_animation_#{1+before_method_count+count}", c]
|
|---|
| 189 | end
|
|---|
| 190 | end
|
|---|
| 191 |
|
|---|
| 192 | assert_equal counted_expected, animation_class.animations
|
|---|
| 193 | assert_equal max_frame, animation_class.max_frame
|
|---|
| 194 | end
|
|---|
| 195 |
|
|---|
| 196 | def test_frames
|
|---|
| 197 | assertion_animations({
|
|---|
| 198 | 0 => [[0, 10]],
|
|---|
| 199 | 1 => [[0, 20]],
|
|---|
| 200 | }, 2) do
|
|---|
| 201 | frames [10, 20] do |c|
|
|---|
| 202 | end
|
|---|
| 203 | end
|
|---|
| 204 | end
|
|---|
| 205 | def test_frames_with_interval
|
|---|
| 206 | assertion_animations({
|
|---|
| 207 | 0 => [[0, 10]],
|
|---|
| 208 | 2 => [[0, 20]],
|
|---|
| 209 | }, 4) do
|
|---|
| 210 | frames([10, 20], 2){|c|}
|
|---|
| 211 | end
|
|---|
| 212 | end
|
|---|
| 213 |
|
|---|
| 214 | def test_frames_exception
|
|---|
| 215 | animation_class = Class.new(StarFrame::Sprite::Animation)
|
|---|
| 216 | animation_class.instance_variable_set :@sprite_class, DefineSprite
|
|---|
| 217 |
|
|---|
| 218 | assert_raise(RangeError){ animation_class.class_eval{ frames(0){|c|} } }
|
|---|
| 219 | assert_raise(RangeError){ animation_class.class_eval{ frames(-1){|c|} } }
|
|---|
| 220 | assert_raise(ArgumentError){ animation_class.class_eval{ frames(1, 0){|c|} } }
|
|---|
| 221 | assert_raise(ArgumentError){ animation_class.class_eval{ frames(1, -1){|c|} } }
|
|---|
| 222 | end
|
|---|
| 223 |
|
|---|
| 224 | def test_frame
|
|---|
| 225 | assertion_animations({
|
|---|
| 226 | 0 => [[0, 0]],
|
|---|
| 227 | }, 1) do
|
|---|
| 228 | frame{|c|}
|
|---|
| 229 | end
|
|---|
| 230 | end
|
|---|
| 231 | def test_frame_with_interval
|
|---|
| 232 | assertion_animations({
|
|---|
| 233 | 0 => [[0, 0]],
|
|---|
| 234 | }, 2) do
|
|---|
| 235 | frame(2){|c|}
|
|---|
| 236 | end
|
|---|
| 237 | end
|
|---|
| 238 |
|
|---|
| 239 | def test_wait
|
|---|
| 240 | assertion_animations({
|
|---|
| 241 | }, 10) do
|
|---|
| 242 | wait 10
|
|---|
| 243 | end
|
|---|
| 244 | end
|
|---|
| 245 |
|
|---|
| 246 | def test_default_serial
|
|---|
| 247 | assertion_animations({
|
|---|
| 248 | 0 => [[0, 1]],
|
|---|
| 249 | 1 => [[0, 2]],
|
|---|
| 250 | 2 => [[1, 3]],
|
|---|
| 251 | 3 => [[1, 4]],
|
|---|
| 252 | 4 => [[1, 5]],
|
|---|
| 253 | }, 5) do
|
|---|
| 254 | frames([1, 2]){|c|}
|
|---|
| 255 | frames([3, 4, 5]){|c|}
|
|---|
| 256 | end
|
|---|
| 257 | end
|
|---|
| 258 |
|
|---|
| 259 | def test_serial
|
|---|
| 260 | assertion_animations({
|
|---|
| 261 | 0 => [[0, 1]],
|
|---|
| 262 | 1 => [[0, 2]],
|
|---|
| 263 | 2 => [[1, 3]],
|
|---|
| 264 | 3 => [[1, 4]],
|
|---|
| 265 | 4 => [[1, 5]],
|
|---|
| 266 | }, 5) do
|
|---|
| 267 | serial do
|
|---|
| 268 | frames([1, 2]){|c|}
|
|---|
| 269 | frames([3, 4, 5]){|c|}
|
|---|
| 270 | end
|
|---|
| 271 | end
|
|---|
| 272 | end
|
|---|
| 273 |
|
|---|
| 274 | def test_parallel
|
|---|
| 275 | assertion_animations({
|
|---|
| 276 | 0 => [[0, 1], [1, 3]],
|
|---|
| 277 | 1 => [[0, 2], [1, 4]],
|
|---|
| 278 | 2 => [ [1, 5]],
|
|---|
| 279 | }, 3) do
|
|---|
| 280 | parallel do
|
|---|
| 281 | frames([1, 2]){|c|}
|
|---|
| 282 | frames([3, 4, 5]){|c|}
|
|---|
| 283 | end
|
|---|
| 284 | end
|
|---|
| 285 | end
|
|---|
| 286 | end
|
|---|
| 287 |
|
|---|
| 288 | class TestStarFrameSpriteAnimationClassMethodsTimeline < Test::Unit::TestCase
|
|---|
| 289 | def assert_entries_in_delta expecteds, actuals, delta
|
|---|
| 290 | assert_equal expecteds.size, actuals.size
|
|---|
| 291 |
|
|---|
| 292 | expecteds.zip actuals do |expected_float, actual_float|
|
|---|
| 293 | assert_in_delta expected_float, actual_float, delta
|
|---|
| 294 | end
|
|---|
| 295 | end
|
|---|
| 296 |
|
|---|
| 297 | def test_linear
|
|---|
| 298 | expecteds = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
|
|---|
| 299 | actuals = StarFrame::Sprite::Animation.__send__(:linear, 10)
|
|---|
| 300 | assert_entries_in_delta expecteds, actuals, 0.1
|
|---|
| 301 | end
|
|---|
| 302 |
|
|---|
| 303 | def test_accelerate
|
|---|
| 304 | expecteds = [0.0, 0.1, 0.4, 0.9, 1.6, 2.5, 3.6, 4.9, 6.4, 8.1]
|
|---|
| 305 | actuals = StarFrame::Sprite::Animation.__send__(:accelerate, 10)
|
|---|
| 306 | assert_entries_in_delta expecteds, actuals, 0.1
|
|---|
| 307 | end
|
|---|
| 308 |
|
|---|
| 309 | def test_decelerate
|
|---|
| 310 | expecteds = [0.0, 1.9, 3.6, 5.1, 6.4, 7.5, 8.4, 9.1, 9.6, 9.9]
|
|---|
| 311 | actuals = StarFrame::Sprite::Animation.__send__(:decelerate, 10)
|
|---|
| 312 | assert_entries_in_delta expecteds, actuals, 0.1
|
|---|
| 313 | end
|
|---|
| 314 | end
|
|---|