Skip to content

Instantly share code, notes, and snippets.

@dtrasbo
Created April 20, 2011 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtrasbo/814023f0698cbe4af63d to your computer and use it in GitHub Desktop.
Save dtrasbo/814023f0698cbe4af63d to your computer and use it in GitHub Desktop.
require 'test_helper'
module PongGame
class MovementTest < TestCase::Functional
focuses_on Player
# Test that the left player is initially rendered 120px from the left edge
# of the window and that the right player is rendered 120px from the right
# edge.
def test_stationary
window.cycle do
assert_rectangle do |r|
r.position_x = DISTANCE_FROM_EDGE
r.position_y = INITIAL_POSITION_Y
r.width = WIDTH
r.height = HEIGHT
r.color = COLOR
end
assert_rectangle do |r|
r.position_x = Window::WIDTH - DISTANCE_FROM_EDGE - WIDTH
r.position_y = INITIAL_POSITION_Y
r.width = WIDTH
r.height = HEIGHT
r.color = COLOR
end
end
end
# Test that both players will accelerate linearly from minimum to maximum
# velocity when their respective up/down buttons are held down.
def test_acceleration
[
[true, :-],
[false, :+]
].each do |up, operator|
window.players.each do |player|
values_for_direction = ->(up) { up ? [false, true] : [false] }
window.stubs(:button_down?).with(player.up_button).returns(*values_for_direction.(up))
window.stubs(:button_down?).with(player.down_button).returns(*values_for_direction.(!up))
end
assert_velocity_change(0..MAXIMUM_VELOCITY, operator)
reset_window
end
end
# Test that both players will decelerate linearly from maximum to minimum
# velocity when you let go of their respective up/down buttons.
def test_deceleration
[
[true, :-],
[false, :+]
].each do |up, operator|
window.players.each do |player|
window.stubs(:button_down?).with(player.up_button).returns(up)
window.stubs(:button_down?).with(player.down_button).returns(!up)
end
window.cycle(times: MAXIMUM_VELOCITY)
window.players.each do |player|
window.stubs(:button_down?).with(player.up_button).returns(false)
window.stubs(:button_down?).with(player.down_button).returns(false)
end
assert_velocity_change(MAXIMUM_VELOCITY..0, operator)
end
end
private
# Assert a linear acceleration/deceleration. Example:
#
# assert_velocity_change(1..15, PongGame::Window.new, ->(initial, offset) { initial - offset })
#
# Asserts an acceleration from 1 to 15. The lambda is used to calculate the
# vertical position. Here we assume an upwards acceleration on the screen,
# hence the subtraction.
def assert_velocity_change(range, operator)
range.inject do |offset, acceleration_or_deceleration|
position_y = INITIAL_POSITION_Y.send(operator, offset)
window.cycle do
window.players.each do |player|
assert_rectangle do |r|
r.position_x = player.position_x
r.position_y = position_y
r.width = WIDTH
r.height = HEIGHT
r.color = COLOR
end
end
end
if range.begin < range.end
offset += acceleration_or_deceleration
else
offset -= acceleration_or_deceleration
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment