1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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.
10 require 'helpers/validation_helper'
12 class TestShogiSerializer < Test::Unit::TestCase
13 include ValidationHelper
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)
24 def test_simple_serialization
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)
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)))
37 def test_compact_serialization
39 assert_equal 'P-7f', serialize(@compact, 2, 6, 2, 5)
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)
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)
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)))
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)
64 @state.board[Point.new(1, 0)] = @game.piece.new(:black, :horse)
65 assert_equal 'R9ax8a', serialize(@compact, 0, 0, 1, 0, :promote => false)
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)
73 assert_deserialize('P-7f', 2, 6, 2, 5)
74 assert_deserialize('7g7f', 2, 6, 2, 5)
76 assert_deserialize('P-7d', 2, 2, 2, 3)
78 assert_deserialize('P-7e', 2, 5, 2, 4)
80 assert_deserialize('Px7e', 2, 3, 2, 4)
83 def test_deserialize_promote
85 @state.board[Point.new(6, 3)] = @game.piece.new(:black, :horse)
87 assert_deserialize('Nx2b+', 6, 3, 7, 1, :promote => true)
90 def test_deserialize_promoted
92 @state.board[Point.new(4, 4)] = @game.piece.new(:black, Promoted.new(:lance))
94 assert_deserialize('+L-4e', 4, 4, 5, 4)
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