Merge games and plugins.
[kaya.git] / test / games / chess / test_pgn.rb
blob59bf0fc15e2ecf290ad44420fde9c0da91264bc8
1 require 'test/unit'
2 require 'games/all'
3 require 'history'
4 require 'helpers/validation_helper'
6 class TestPGN < Test::Unit::TestCase
7   include ValidationHelper
8   
9   PGN1 = <<-END_OF_PGN
10 [Result "0-1"]
12 1.e4 e5 0-1
13   END_OF_PGN
14   PGN2 = <<-END_OF_PGN
15 [Event "Oktoberfest"]
16 [White "Doe, John"]
17 [Black "Smith, Bob"]
18 [Result "1-0"]
20 1.Nf3 1-0
21   END_OF_PGN
22   PGN3 = <<-END_OF_PGN
23 [Event "Blindfold"]
24 [Site "Hollywood (USA)"]
25 [Date "1954.??.??"]
26 [EventDate "?"]
27 [Round "?"]
28 [Result "0-1"]
29 [White "Heinz Steiner"]
30 [Black "Arthur Bisguier"]
31 [ECO "B70"]
32 [WhiteElo "?"]
33 [BlackElo "?"]
34 [PlyCount "124"]
36 1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 g6 6.Bg5 Bg7 7.Qd2
37 Nc6 8.Nb3 h6 9.Bh4 g5 10.Bg3 Nh5 11.Be2 Nxg3 12.hxg3 a5 13.a4
38 Be6 14.f4 Bxb3 15.cxb3 Nd4 16.Bc4 Rc8 17.Rd1 Qb6 18.Qf2 Qc5
39 19.fxg5 Ne6 20.g6 Qxf2+ 21.Kxf2 Ng5 22.gxf7+ Nxf7 23.Ke2 Ng5
40 24.Ke3 Rc5 25.Rd5 Ne6 26.Rf1 Be5 27.g4 Rf8 28.Rf5 Rf6 29.Rd1
41 Rg6 30.Rh1 Kd7 31.Nd5 Nc7 32.Nb6+ Kc6 33.Nd5 Kd7 34.Rf7 Nxd5+
42 35.exd5 Bf6 36.Kf3 Ke8 37.Rh7 Bg7 38.Bd3 Kf7 39.Bxg6+ Kxg6
43 40.Rxg7+ Kxg7 41.Re1 Kf7 42.Ke4 Rc2 43.g3 Rxb2 44.Re3 e6
44 45.d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ess)
74     @state = @game.state.new
75     @state.setup
76     @history = History.new(@state)
77     @pgn = @game.game_writer
78   end
79   
80   def test_pgn_black_wins
81     add_move 4, 6, 4, 4
82     add_move 4, 1, 4, 3
83     info = { :result => :black }
85     assert_equal PGN1, @pgn.write(info, @history)
86   end
87   
88   def test_pgn_white_wins
89     add_move 6, 7, 5, 5
90     info = { :result => :white,
91              :event => 'Oktoberfest',
92              :players => { :white => 'Doe, John',
93                            :black => 'Smith, Bob' } }
95     assert_equal PGN2, @pgn.write(info, @history)
96   end
97   
98   def test_pgn_read_tags
99     info = {}
100     @pgn.read(PGN2, info)
101     
102     assert_equal "Oktoberfest", info[:event]
103     assert_equal "Doe, John", info[:players][:white]
104     assert_equal "Smith, Bob", info[:players][:black]
105     assert_equal "1-0", info[:result]
106   end
107   
108   def test_pgn_read_moves
109     info = {}
110     @history = @pgn.read(PGN3, info)
111     
112     assert_move 1,   4, 6, 4, 4
113     assert_move 2,   2, 1, 2, 3
114     assert_move 3,   6, 7, 5, 5
115     assert_move 12,  5, 0, 6, 1
116   end
117   
118   def test_pgn_read_write
119 #     info = {}
120 #     @history = @pgn.read(PGN4, info)
121 #     text = @pgn.write(info, @history)
122 #     assert_equal PGN4, text
123     # TODO reenable when suffixes work
124   end
125   
126   private
127   
128   def add_move(*args)
129     move = unpack_move(*args)
130     validate = @game.validator.new(@history.state)
131     assert validate[move]
132     state = @history.state.dup
133     state.perform! move
134     @history.add_move(state, move)
135   end
136   
137   def assert_move(index, *args)
138     move = unpack_move(*args)
139     assert_equal move.src, @history[index].move.src
140     assert_equal move.dst, @history[index].move.dst
141   end