OSDN Git Service

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