OSDN Git Service

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