Add licence and installation instructions.
[kaya.git] / test / games / chess / test_chess_animator.rb
blobecc3ad390ef0737e890a52e8c66124afa3d9ac6a
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'test/unit'
9 require 'games/all'
10 require 'helpers/animation_test_helper'
12 class TestChessAnimator < Test::Unit::TestCase
13   include AnimationAssertions
14   
15   FakeItem = Struct.new(:name)
16   
17   def setup
18     @chess = Game.get(:chess)
19     @items = {
20       Point.new(3, 4) => FakeItem.new(@chess.piece.new(:white, :king)),
21       Point.new(3, 1) => FakeItem.new(@chess.piece.new(:black, :king)),
22       Point.new(7, 7) => FakeItem.new(@chess.piece.new(:black, :queen))
23     }
24     @board = FakeBoard.new(@items)
25     
26     @animator = @chess.animator.new(@board)
27     class << @animator
28       include StubbedAnimations
29     end
30     @state = @chess.state.new
31   end
32   
33   def test_null_warp
34     @state.board[Point.new(3, 4)] = @chess.piece.new(:white, :king)
35     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
36     @state.board[Point.new(7, 7)] = @chess.piece.new(:black, :queen)
37     
38     anim = @animator.warp(@state)
39     assert_animation(:group, anim) do |args|
40       assert_equal [], args
41     end
42   end
43   
44   def test_simple_warp
45     @state.board[Point.new(4, 3)] = @chess.piece.new(:white, :king)
46     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
47     @state.board[Point.new(7, 7)] = @chess.piece.new(:black, :queen)
48     
49     anim = @animator.warp(@state)
50     assert_animation(:group, anim) do |args|
51       assert_equal 2, args.size
52       
53       appear, disappear = args.sort
54       assert_animation :instant_appear, appear
55       assert_animation :instant_disappear, disappear
56     end
57   end
58   
59   def test_simple_noninstant_warp
60     @state.board[Point.new(4, 3)] = @chess.piece.new(:white, :king)
61     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
62     @state.board[Point.new(7, 7)] = @chess.piece.new(:black, :queen)
63     
64     anim = @animator.warp(@state, :instant => false)
65     assert_animation(:group, anim) do |args|
66       assert_equal 2, args.size
67       
68       appear, disappear = args.sort
69       assert_animation :appear, appear
70       assert_animation :disappear, disappear
71     end    
72   end
73   
74   def test_simple_forward
75     move = @state.move_factory.new(Point.new(7, 7), Point.new(5, 5))
76     @state.board[Point.new(3, 4)] = @chess.piece.new(:white, :king)
77     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
78     @state.board[Point.new(5, 5)] = @chess.piece.new(:black, :queen)
79     
80     anim = @animator.forward(@state, move)
81     
82     assert_animation(:sequence, anim) do |args|
83       assert_equal 2, args.size
84       
85       warp, main = args.sort_by {|a| a.args.size }
86       
87       assert_animation(:group, warp) {|a| assert_equal [], a }
88       assert_animation(:group, main) do |args|
89         mov = args.find {|a| a.animation == :movement }
90         assert_animation(:movement, mov) do |args|
91           piece, src, dst = args
92           assert_equal FakeItem.new(@chess.piece.new(:black, :queen)), piece
93           assert_equal Point.new(7, 7), src
94           assert_equal Point.new(5, 5), dst
95         end
96       end
97     end
98   end
99 end