OSDN Git Service

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