Add PGN class for writing PGN files.
[kaya.git] / test / games / chess / test_chess_animator.rb
blob85d552296b7e0aaca3a376697a108f58567b7659
1 require 'test/unit'
2 require 'games/all'
3 require 'helpers/animation_test_helper'
5 class TestChessAnimator < Test::Unit::TestCase
6   include AnimationAssertions
7   
8   FakeItem = Struct.new(:name)
9   
10   def setup
11     @chess = Game.get(:chess)
12     @items = {
13       Point.new(3, 4) => FakeItem.new(@chess.piece.new(:white, :king)),
14       Point.new(3, 1) => FakeItem.new(@chess.piece.new(:black, :king)),
15       Point.new(7, 7) => FakeItem.new(@chess.piece.new(:black, :queen))
16     }
17     @board = FakeBoard.new(@items)
18     
19     @animator = @chess.animator.new(@board)
20     class << @animator
21       include StubbedAnimations
22     end
23     @state = @chess.state.new
24   end
25   
26   def test_null_warp
27     @state.board[Point.new(3, 4)] = @chess.piece.new(:white, :king)
28     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
29     @state.board[Point.new(7, 7)] = @chess.piece.new(:black, :queen)
30     
31     anim = @animator.warp(@state)
32     assert_animation(:group, anim) do |args|
33       assert_equal [], args
34     end
35   end
36   
37   def test_simple_warp
38     @state.board[Point.new(4, 3)] = @chess.piece.new(:white, :king)
39     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
40     @state.board[Point.new(7, 7)] = @chess.piece.new(:black, :queen)
41     
42     anim = @animator.warp(@state)
43     assert_animation(:group, anim) do |args|
44       assert_equal 2, args.size
45       
46       appear, disappear = args.sort
47       assert_animation :instant_appear, appear
48       assert_animation :instant_disappear, disappear
49     end
50   end
51   
52   def test_simple_noninstant_warp
53     @state.board[Point.new(4, 3)] = @chess.piece.new(:white, :king)
54     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
55     @state.board[Point.new(7, 7)] = @chess.piece.new(:black, :queen)
56     
57     anim = @animator.warp(@state, :instant => false)
58     assert_animation(:group, anim) do |args|
59       assert_equal 2, args.size
60       
61       appear, disappear = args.sort
62       assert_animation :appear, appear
63       assert_animation :disappear, disappear
64     end    
65   end
66   
67   def test_simple_forward
68     move = @state.move_factory.new(Point.new(7, 7), Point.new(5, 5))
69     @state.board[Point.new(3, 4)] = @chess.piece.new(:white, :king)
70     @state.board[Point.new(3, 1)] = @chess.piece.new(:black, :king)
71     @state.board[Point.new(5, 5)] = @chess.piece.new(:black, :queen)
72     
73     anim = @animator.forward(@state, move)
74     
75     assert_animation(:sequence, anim) do |args|
76       assert_equal 2, args.size
77       
78       warp, main = args.sort_by {|a| a.args.size }
79       
80       assert_animation(:group, warp) {|a| assert_equal [], a }
81       assert_animation(:group, main) do |args|
82         mov = args.find {|a| a.animation == :movement }
83         assert_animation(:movement, mov) do |args|
84           piece, src, dst = args
85           assert_equal FakeItem.new(@chess.piece.new(:black, :queen)), piece
86           assert_equal Point.new(7, 7), src
87           assert_equal Point.new(5, 5), dst
88         end
89       end
90     end
91   end
92 end