Implement ActionList::Entry#clear.
[kaya.git] / test / plugins / shogi / test_shogi_serializer.rb
blob495e4e0fd886d0c3ab5f7b9286e8f679d8680442
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/validation_helper'
12 class TestShogiSerializer < Test::Unit::TestCase
13   include ValidationHelper
15   def setup
16     @game = Game.get(:shogi)
17     @simple = @game.serializer.new(:simple)
18     @compact = @game.serializer.new(:compact)
19     @dec = @game.serializer.new(:decorated)
20     @state = @game.state.new
21     @validate = @game.validator.new(@state)
22   end
24   def test_simple_serialization
25     @state.setup
26     assert_equal '7g7f', serialize(@simple, 2, 6, 2, 5)
27     @state.board[Point.new(2, 3)] = @game.piece.new(:black, :pawn)
28     @state.board[Point.new(2, 6)] = nil
29     assert_equal '7d7c+', serialize(@simple, 2, 3, 2, 2, :promote => true)
30     
31     piece = @game.piece.new(:black, :lance)
32     @state.pool(:black).add(piece)
33     assert_equal 'L*5e', serialize(@simple, 
34       @game.move.drop(piece, Point.new(4, 4)))
35   end
36   
37   def test_compact_serialization
38     @state.setup
39     assert_equal 'P-7f', serialize(@compact, 2, 6, 2, 5)
40     
41     @state.board[Point.new(4, 4)] = @game.piece.new(:black, :rook)
42     assert_equal 'R-1e', serialize(@compact, 4, 4, 8, 4)
43     assert_equal 'Rx5c+', serialize(@compact, 4, 4, 4, 2, :promote => true)
44     
45     @state.board[Point.new(4, 4)] = @game.piece.new(:black, Promoted.new(:silver))
46     assert_equal '+S-6e', serialize(@compact, 4, 4, 3, 4)
47     
48     @state.board[Point.new(4, 4)] = nil
49     piece = @game.piece.new(:black, :gold)
50     @state.pool(:black).add(piece)
51     assert_equal 'G*5e', serialize(@compact, 
52       @game.move.drop(piece, Point.new(4, 4)))
53   end
54   
55   def test_compact_serialization_ambiguous
56     @state.board[Point.new(0, 0)] = @game.piece.new(:black, :rook)
57     @state.board[Point.new(8, 0)] = @game.piece.new(:black, :rook)
58     @state.board[Point.new(0, 8)] = @game.piece.new(:black, :rook)
59     @state.board[Point.new(8, 8)] = @game.piece.new(:black, :king)
60     @state.board[Point.new(4, 4)] = @game.piece.new(:white, :king)
61     assert_equal 'R9a-8a=', serialize(@compact, 0, 0, 1, 0, :promote => false)
62     assert_equal 'R9a-9b+', serialize(@compact, 0, 0, 0, 1, :promote => true)
63     
64     @state.board[Point.new(1, 0)] = @game.piece.new(:black, :horse)
65     assert_equal 'R9ax8a', serialize(@compact, 0, 0, 1, 0, :promote => false)
66     
67     @state.board[Point.new(0, 0)] = @game.piece.new(:black, Promoted.new(:rook))
68     assert_equal '+R9ax8a+', serialize(@compact, 0, 0, 1, 0, :promote => true)
69   end
70   
71   def test_deserialize
72     @state.setup
73     assert_deserialize('P-7f', 2, 6, 2, 5)
74     assert_deserialize('7g7f', 2, 6, 2, 5)
75     execute 2, 6, 2, 5
76     assert_deserialize('P-7d', 2, 2, 2, 3)
77     execute 2, 2, 2, 3
78     assert_deserialize('P-7e', 2, 5, 2, 4)
79     execute 2, 5, 2, 4
80     assert_deserialize('Px7e', 2, 3, 2, 4)
81   end
82   
83   def test_deserialize_promote
84     @state.setup
85     @state.board[Point.new(6, 3)] = @game.piece.new(:black, :horse)
86     
87     assert_deserialize('Nx2b+', 6, 3, 7, 1, :promote => true)
88   end
89   
90   def test_deserialize_promoted
91     @state.setup
92     @state.board[Point.new(4, 4)] = @game.piece.new(:black, Promoted.new(:lance))
93     
94     assert_deserialize('+L-4e', 4, 4, 5, 4)
95   end
96   
97   private
98   
99   def assert_deserialize(text, *args)
100     move = unpack_move(*args)
101     @game.validator.new(@state)[move]
102     move2 = @compact.deserialize(text, @state)
103     assert_equal move, move2
104   end