OSDN Git Service

Added magic comment of this file encoding for Ruby 1.9.3.
[shogi-server/shogi-server.git] / test / TC_functional.rb
1 # -*- coding: windows-31j -*-
2 require "baseclient"
3 require "kconv"
4
5 class TestClientAtmark < BaseClient
6   # login with trip
7   def set_name
8     super
9     @game_name = "atmark"
10     @p1_name = "B@p1"
11     @p2_name = "W@p2"
12   end
13
14   def test_toryo
15     result, result2 = handshake do
16       @p1.toryo
17       wait_finish
18     end
19     assert(/#LOSE/ =~ result)
20     assert(/#WIN/  =~ result2)
21
22     now = Time.now
23     year  = now.strftime("%Y")
24     month = now.strftime("%m")
25     day   = now.strftime("%d")
26     path = File.join( File.dirname(__FILE__), "..", year, month, day, "*atmark-1500-0*")
27     log_files = Dir.glob(path)
28     assert(!log_files.empty?) 
29     sleep 0.1
30     log_content = File.read(log_files.sort.last)
31
32     # "$EVENT", "$START_TIME" and "'$END_TIME" are removed since they vary dinamically.
33     should_be = <<-EOF
34 V2
35 N+atmark_B@p1
36 N-atmark_W@p2
37 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
38 P2 * -HI *  *  *  *  * -KA * 
39 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
40 P4 *  *  *  *  *  *  *  *  * 
41 P5 *  *  *  *  *  *  *  *  * 
42 P6 *  *  *  *  *  *  *  *  * 
43 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
44 P8 * +KA *  *  *  *  * +HI * 
45 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
46 +
47 'rating:atmark_B@p1+275876e34cf609db118f3d84b799a790:atmark_W@p2+275876e34cf609db118f3d84b799a790
48 +2726FU
49 T1
50 -3334FU
51 T1
52 %TORYO
53 'P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
54 'P2 * -HI *  *  *  *  * -KA * 
55 'P3-FU-FU-FU-FU-FU-FU * -FU-FU
56 'P4 *  *  *  *  *  * -FU *  * 
57 'P5 *  *  *  *  *  *  *  *  * 
58 'P6 *  *  *  *  *  *  * +FU * 
59 'P7+FU+FU+FU+FU+FU+FU+FU * +FU
60 'P8 * +KA *  *  *  *  * +HI * 
61 'P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
62 '+
63 'summary:toryo:atmark_B@p1 lose:atmark_W@p2 win
64 EOF
65
66     log_content.gsub!(/^\$.*?\n/m, "")
67     log_content.gsub!(/^'\$.*?\n/m, "")
68     assert_equal(should_be, log_content)
69   end
70 end
71
72 class TestHandicappedGame < BaseClient
73   # login with trip
74   def set_name
75     super
76     @game_name = "hc2p_hoge"
77     @p1_name = "B"
78     @p2_name = "W"
79   end
80
81   def test_toryo
82     result, result2 = handshake do
83       @p1.toryo
84       wait_finish
85     end
86     assert(/#LOSE/ =~ result)
87     assert(/#WIN/  =~ result2)
88
89     now = Time.now
90     year  = now.strftime("%Y")
91     month = now.strftime("%m")
92     day   = now.strftime("%d")
93     path = File.join( File.dirname(__FILE__), "..", year, month, day, "*hc2p_hoge-1500-0*")
94     log_files = Dir.glob(path)
95     assert(!log_files.empty?) 
96     sleep 0.1
97     log_content = File.read(log_files.sort.last)
98
99     # "$EVENT", "$START_TIME" and "'$END_TIME" are removed since they vary dinamically.
100     should_be = <<-EOF
101 V2
102 N+hc2p_hoge_B
103 N-hc2p_hoge_W
104 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
105 P2 * -HI *  *  *  *  * -KA * 
106 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
107 P4 *  *  *  *  *  *  *  *  * 
108 P5 *  *  *  *  *  *  *  *  * 
109 P6 *  *  *  *  *  *  *  *  * 
110 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
111 P8 *  *  *  *  *  *  *  *  * 
112 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
113 +
114 'rating:hc2p_hoge_B+275876e34cf609db118f3d84b799a790:hc2p_hoge_W+275876e34cf609db118f3d84b799a790
115 +2726FU
116 T1
117 -3334FU
118 T1
119 %TORYO
120 'P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
121 'P2 * -HI *  *  *  *  * -KA * 
122 'P3-FU-FU-FU-FU-FU-FU * -FU-FU
123 'P4 *  *  *  *  *  * -FU *  * 
124 'P5 *  *  *  *  *  *  *  *  * 
125 'P6 *  *  *  *  *  *  * +FU * 
126 'P7+FU+FU+FU+FU+FU+FU+FU * +FU
127 'P8 *  *  *  *  *  *  *  *  * 
128 'P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
129 '+
130 'summary:toryo:hc2p_hoge_B lose:hc2p_hoge_W win
131 EOF
132
133     log_content.gsub!(/^\$.*?\n/m, "")
134     log_content.gsub!(/^'\$.*?\n/m, "")
135     assert_equal(should_be, log_content)
136   end
137 end
138
139
140 class TestComment < BaseClient
141   def test_toryo
142     result, result2 = handshake do
143       @p1.toryo
144       wait_finish
145     end
146     assert(/#LOSE/ =~ result)
147     assert(/#WIN/  =~ result2)
148   end
149
150   def test_inline_comment
151     result, result2 = handshake do
152       move "+2625FU,'comment"
153       move "-2233KA"
154       @p1.toryo
155       wait_finish
156     end
157     assert(/#LOSE/ =~ result)
158     assert(/#WIN/  =~ result2)
159   end
160
161   def test_inline_comment_ja_euc
162     result, result2 = handshake do
163       move "+2625FU,'\93ú\96{\8cêEUC"
164       move "-2233KA"
165       @p1.toryo
166       wait_finish
167     end
168     assert(/#LOSE/ =~ result)
169     assert(/#WIN/  =~ result2)
170   end
171
172   def test_inline_comment_ja_utf8
173     result, result2 = handshake do
174       move "+2625FU,'\93ú\96{\8cêUTF8".toutf8
175       move "-2233KA"
176       @p1.toryo
177       wait_finish
178     end
179     assert(/#LOSE/ =~ result)
180     assert(/#WIN/  =~ result2)
181   end
182 end
183
184
185 class TestWhiteMovesBlack < BaseClient
186   def test_white_moves_black
187     result, result2 = handshake do
188       move "+9796FU"
189       @p2.move "+1716FU"
190       wait_finish
191     end
192     assert(/#ILLEGAL_MOVE/ =~ result)
193     assert(/#WIN/  =~ result)
194     assert(/#ILLEGAL_MOVE/ =~ result2)
195     assert(/#LOSE/ =~ result2)
196   end
197 end
198
199
200 #
201 # CSA test
202 #
203
204 class TestLoginCSAWithoutTripGoodGamename < CSABaseClient
205   def set_name
206     super
207     @game_name = "csawotrip"
208     @p1_name   = "p1"
209     @p2_name   = "p2"
210   end
211
212   def test_toryo
213     result, result2 = handshake do
214       @p1.toryo
215       @p1.wait_finish
216       @p2.wait_finish
217     end
218     assert(/#LOSE/ =~ result)
219     assert(/#WIN/  =~ result2)
220   end
221 end
222
223 class TestLoginCSAWithTripGoodGamename < CSABaseClient
224   def set_name
225     super
226     @game_name = "csawtrip"
227     @p1_name   = "p1"
228     @p2_name   = "p2"
229   end
230
231   def set_player
232     super
233     @p1.login_command += ",atrip"
234     @p2.login_command += ",anothertrip"
235   end
236
237   def test_toryo
238     result, result2 = handshake do
239       @p1.toryo
240       @p1.wait_finish
241       @p2.wait_finish
242     end
243     assert(/#LOSE/ =~ result)
244     assert(/#WIN/  =~ result2)
245   end
246 end
247
248 class TestChallenge < CSABaseClient
249   def set_name
250     super
251     @game_name = "challenge"
252     @p1_name   = "p1"
253     @p2_name   = "p2"
254   end
255
256   def set_player
257     super
258     @p1.login_command += ",atrip"
259     @p2.login_command += ",anothertrip"
260   end
261
262   def test_toryo
263     result, result2 = handshake do
264       @p1.puts "CHALLENGE"
265       @p1.wait(/CHALLENGE ACCEPTED/)
266       @p2.puts "CHALLENGE"
267       @p2.wait(/CHALLENGE ACCEPTED/)
268     end
269     assert(true)
270   end
271 end
272
273 #
274 # Test Floodgate
275 #
276
277 class TestFloodgateGame < BaseClient
278   def set_name
279     super
280     @game_name = "floodgate"
281   end
282
283   def set_player
284     @p1 = SocketPlayer.new @game_name, @p1_name, "*"
285     @p2 = SocketPlayer.new @game_name, @p2_name, "*"
286   end
287
288   def test_game_wait
289     @p1.connect
290     @p2.connect
291     @p1.login
292     @p2.login
293     @p1.game
294     @p2.game
295     assert(true)
296     logout12
297   end
298 end
299
300 class TestFloodgateGameWrongTebam < BaseClient
301   def set_name
302     super
303     @game_name = "floodgate"
304   end
305
306   def test_game_wait
307     @p1.connect
308     @p2.connect
309     @p1.login
310     @p2.login
311     @p1.game
312     @p1.wait %r!##\[ERROR\] You are not allowed!
313     assert true
314     logout12
315   end
316 end
317
318
319
320
321 class TestDuplicatedMoves < BaseClient
322   def test_defer
323     result, result2 = handshake do
324       @p1.puts "+7776FU"
325       @p1.puts "+8786FU" # defer
326       @p1.puts "+9796FU" # defer
327       @p2.puts "-7374FU"
328       @p2.puts "-8384FU"
329       @p2.toryo
330       wait_finish
331     end
332     assert(/#WIN/  =~ result)
333     assert(/#LOSE/ =~ result2)
334   end
335
336   def test_defer2
337     result, result2 = handshake do
338       @p1.puts "+7776FU"
339       @p1.puts "+8786FU" # defer
340       @p1.puts "%TORYO" # defer
341       @p2.puts "-7374FU"
342       @p2.puts "-8384FU"
343       wait_finish
344     end
345     assert(/#LOSE/  =~ result)
346     assert(/#WIN/ =~ result2)
347   end
348
349   def test_defer3
350     result, result2 = handshake do
351       @p1.puts "+7776FU"
352       @p1.puts "+8786FU" # defer
353       @p2.puts "-7374FU"
354       @p2.puts "-8384FU"
355       @p1.toryo
356       wait_finish
357     end
358     assert(/#LOSE/  =~ result)
359     assert(/#WIN/ =~ result2)
360   end
361 end
362
363 class TestFunctionalChatCommand < BaseClient
364   def test_chat
365     result, result2 = handshake do
366       @p1.puts"%%CHAT Hello"
367       @p1.wait %r!##\[CHAT\].*Hello!
368       @p2.wait %r!##\[CHAT\].*Hello!
369     end
370     assert true
371   end
372 end
373
374
375
376
377 class TestTwoSameMoves < CSABaseClient
378   def set_name
379     super
380     @game_name = "2moves"
381     @p1_name   = "p1"
382     @p2_name   = "p2"
383   end
384
385   def test_two_same_moves
386     result, result2 = handshake do
387       move  "+2726FU"
388       move "-8384FU"
389       @p2.puts "-8384FU" # ignored
390       sleep 0.1 # wait for finish of the command above
391       move "+2625FU"
392     end
393     assert(/#ILLEGAL_MOVE/ !~ result)
394     assert(/#ILLEGAL_MOVE/ !~ result2)
395   end
396 end
397