OSDN Git Service

* [shogi-server]
[shogi-server/shogi-server.git] / test / TC_command.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'shogi_server/login'
4 require 'shogi_server/player'
5 require 'shogi_server/command'
6
7 def log_warning(str)
8   $stderr.puts str
9 end
10
11 def log_error(str)
12   $stderr.puts str
13 end
14
15 class MockPlayer < ShogiServer::BasicPlayer
16   attr_reader :out
17   attr_accessor :game, :status, :protocol
18   attr_accessor :game_name
19
20   def initialize
21     @out      = []
22     @game     = nil
23     @status   = nil
24     @protocol = nil
25     @game_name = "dummy_game_name"
26   end
27
28   def write_safe(str)
29     @out << str
30   end
31 end
32
33 class MockGame
34   attr_accessor :finish_flag
35   attr_reader :log
36   attr_accessor :prepared_expire
37   attr_accessor :rejected
38   attr_accessor :is_startable_status
39   attr_accessor :started
40   attr_accessor :game_id
41
42   def initialize
43     @finish_flag     = false
44     @log             = []
45     @prepared_expire = false
46     @rejected        = false
47     @is_startable_status = false
48     @started             = false
49     @game_id         = "dummy_game_id"
50     @monitoron_called = false
51     @monitoroff_called = false
52   end
53
54   def handle_one_move(move, player)
55     return @finish_flag
56   end
57
58   def log_game(str)
59     @log << str
60   end
61
62   def prepared_expire?
63     return @prepared_expire
64   end
65
66   def reject(str)
67     @rejected = true
68   end
69
70   def is_startable_status?
71     return @is_startable_status
72   end
73
74   def start
75     @started = true
76   end
77
78   def show
79     return "dummy_game_show"
80   end
81
82   def monitoron(player)
83     @monitoron_called = true
84   end
85
86   def monitoroff(player)
87     @monitoroff_called = true
88   end
89 end
90
91 class MockLeague
92   def initialize
93     @games = {}
94     @games["dummy_game_id"] = MockGame.new
95   end
96
97   def games
98     return @games
99   end
100
101   def rated_players
102     return []
103   end
104
105   def players
106     return [MockPlayer.new]
107   end
108 end
109
110
111 class TestFactoryMethod < Test::Unit::TestCase 
112
113   def setup
114     @p = MockPlayer.new
115     $league = MockLeague.new
116   end
117
118   def test_keep_alive_command
119     cmd = ShogiServer::Command.factory("", @p)
120     assert_instance_of(ShogiServer::KeepAliveCommand, cmd)
121   end
122
123   def test_move_command
124     cmd = ShogiServer::Command.factory("+7776FU", @p)
125     assert_instance_of(ShogiServer::MoveCommand, cmd)
126   end
127
128   def test_special_command
129     cmd = ShogiServer::Command.factory("%TORYO", @p)
130     assert_instance_of(ShogiServer::SpecialCommand, cmd)
131   end
132
133   def test_special_command_timeout
134     cmd = ShogiServer::Command.factory(:timeout, @p)
135     assert_instance_of(ShogiServer::SpecialCommand, cmd)
136   end
137
138   def test_execption_command
139     cmd = ShogiServer::Command.factory(:exception, @p)
140     assert_instance_of(ShogiServer::ExceptionCommand, cmd)
141   end
142
143   def test_reject_command
144     cmd = ShogiServer::Command.factory("REJECT", @p)
145     assert_instance_of(ShogiServer::RejectCommand, cmd)
146   end
147
148   def test_agree_command
149     cmd = ShogiServer::Command.factory("AGREE", @p)
150     assert_instance_of(ShogiServer::AgreeCommand, cmd)
151   end
152
153   def test_show_command
154     cmd = ShogiServer::Command.factory("%%SHOW game_id", @p)
155     assert_instance_of(ShogiServer::ShowCommand, cmd)
156   end
157
158   def test_monitoron_command
159     cmd = ShogiServer::Command.factory("%%MONITORON game_id", @p)
160     assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
161   end
162
163   def test_monitoroff_command
164     cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
165     assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
166   end
167
168   def test_help_command
169     cmd = ShogiServer::Command.factory("%%HELP", @p)
170     assert_instance_of(ShogiServer::HelpCommand, cmd)
171   end
172
173   def test_rating_command
174     cmd = ShogiServer::Command.factory("%%RATING", @p)
175     assert_instance_of(ShogiServer::RatingCommand, cmd)
176   end
177
178   def test_version_command
179     cmd = ShogiServer::Command.factory("%%VERSION", @p)
180     assert_instance_of(ShogiServer::VersionCommand, cmd)
181   end
182
183   def test_game_command
184     cmd = ShogiServer::Command.factory("%%GAME", @p)
185     assert_instance_of(ShogiServer::GameCommand, cmd)
186   end
187
188   def test_game_challenge_command_game
189     cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
190     assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
191   end
192
193   def test_game_challenge_command_challenge
194     cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
195     assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
196   end
197
198   def test_chat_command
199     cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
200     assert_instance_of(ShogiServer::ChatCommand, cmd)
201   end
202
203   def test_list_command
204     cmd = ShogiServer::Command.factory("%%LIST", @p)
205     assert_instance_of(ShogiServer::ListCommand, cmd)
206   end
207
208   def test_who_command
209     cmd = ShogiServer::Command.factory("%%WHO", @p)
210     assert_instance_of(ShogiServer::WhoCommand, cmd)
211   end
212
213   def test_logout_command
214     cmd = ShogiServer::Command.factory("LOGOUT", @p)
215     assert_instance_of(ShogiServer::LogoutCommand, cmd)
216   end
217
218   def test_challenge_command
219     cmd = ShogiServer::Command.factory("CHALLENGE", @p)
220     assert_instance_of(ShogiServer::ChallengeCommand, cmd)
221   end
222
223   def test_space_command
224     cmd = ShogiServer::Command.factory(" ", @p)
225     assert_instance_of(ShogiServer::SpaceCommand, cmd)
226   end
227
228   def test_error
229     cmd = ShogiServer::Command.factory("should_be_error", @p)
230     assert_instance_of(ShogiServer::ErrorCommand, cmd)
231   end
232 end
233
234 #
235 #
236 class TestKeepAliveCommand < Test::Unit::TestCase 
237   def setup
238     @p = MockPlayer.new
239   end
240
241   def test_call
242     cmd = ShogiServer::KeepAliveCommand.new("", @p)
243     rc = cmd.call
244     assert_equal(:continue, rc)
245   end
246 end
247
248 #
249 #
250 class TestMoveCommand < Test::Unit::TestCase
251   def setup
252     @p = MockPlayer.new
253     @game = MockGame.new
254     @p.game = @game
255     @p.status = "game"
256   end
257
258   def test_call
259     cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
260     rc = cmd.call
261     assert_equal(:continue, rc)
262   end
263
264   def test_comment
265     cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
266     rc = cmd.call
267     assert_equal(:continue, rc)
268     assert_equal("'*comment", @game.log.first)
269   end
270
271   def test_x1_return
272     @game.finish_flag = true
273     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
274     cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
275     rc = cmd.call
276     assert_equal(:return, rc)
277   end
278 end
279
280 #
281 #
282 class TestSpecialComand < Test::Unit::TestCase
283   def setup
284     @p = MockPlayer.new
285     @game = MockGame.new
286     @p.game = @game
287     @p.status = "game"
288   end
289
290   def test_toryo
291     @game.finish_flag = true
292     cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
293     rc = cmd.call
294     assert_equal(:continue, rc)
295   end
296
297   def test_toryo_csa_protocol
298     @game.finish_flag = true
299     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
300     cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
301     rc = cmd.call
302     assert_equal(:return, rc)
303   end
304
305   def test_timeout
306     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
307     rc = cmd.call
308     assert_equal(:continue, rc)
309   end
310
311   def test_expired_game
312     @p.status = "agree_waiting"
313     @game.prepared_expire = true
314     assert(!@game.rejected)
315     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
316     rc = cmd.call
317     assert_equal(:continue, rc)
318     assert(@game.rejected)
319   end
320
321   def test_expired_game_csa_protocol
322     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
323     @p.status = "agree_waiting"
324     @game.prepared_expire = true
325     assert(!@game.rejected)
326     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
327     rc = cmd.call
328     assert_equal(:return, rc)
329     assert(@game.rejected)
330   end
331
332   def test_error
333     @p.status = "should_be_ignored"
334     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
335     rc = cmd.call
336     assert_equal(:continue, rc)
337   end
338 end
339
340 #
341 #
342 class TestExceptionCommand < Test::Unit::TestCase 
343   def setup
344     @p = MockPlayer.new
345   end
346
347   def test_call
348     cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
349     rc = cmd.call
350     assert_equal(:return, rc)
351   end
352 end
353
354 #
355 #
356 class TestRejectCommand < Test::Unit::TestCase 
357   def setup
358     @p = MockPlayer.new
359     @game = MockGame.new
360     @p.game = @game
361     @p.status = "game"
362   end
363
364   def test_call
365     @p.status = "agree_waiting"
366     assert(!@game.rejected)
367     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
368     rc = cmd.call
369
370     assert_equal(:continue, rc)
371     assert(@game.rejected)
372   end
373
374   def test_call_csa_protocol
375     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
376     @p.status = "agree_waiting"
377     assert(!@game.rejected)
378     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
379     rc = cmd.call
380
381     assert_equal(:return, rc)
382     assert(@game.rejected)
383   end
384
385   def test_error
386     @p.status = "should_be_ignored"
387     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
388     rc = cmd.call
389
390     assert_equal(:continue, rc)
391     assert(!@game.rejected)
392   end
393 end
394
395 #
396 #
397 class TestAgreeCommand < Test::Unit::TestCase 
398   def setup
399     @p = MockPlayer.new
400     @game = MockGame.new
401     @p.game = @game
402     @p.status = "agree_waiting"
403   end
404
405   def test_not_start_yet
406     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
407     rc = cmd.call
408     assert_equal(:continue, rc)
409     assert(!@game.started)
410   end
411
412   def test_start
413     @game.is_startable_status = true
414     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
415     rc = cmd.call
416     assert_equal(:continue, rc)
417     assert(@game.started)
418   end
419
420   def test_error
421     @p.status = "should_be_ignored"
422     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
423     rc = cmd.call
424     assert_equal(:continue, rc)
425     assert(!@game.started)
426   end
427 end
428
429 #
430 #
431 class TestShowCommand < Test::Unit::TestCase 
432   def setup
433     @p = MockPlayer.new
434     @game = MockGame.new
435     @p.game = @game
436   end
437
438   def test_call
439     cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
440     rc = cmd.call
441
442     assert_equal(:continue, rc)
443   end
444
445   def test_call_nil_game
446     cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
447     rc = cmd.call
448
449     assert_equal(:continue, rc)
450   end
451 end
452
453 #
454 #
455 class TestMonitorOnCommand < Test::Unit::TestCase 
456   def setup
457     @p = MockPlayer.new
458     @game = MockGame.new
459     @p.game = @game
460   end
461
462   def test_call
463     cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
464     rc = cmd.call
465
466     assert_equal(:continue, rc)
467   end
468 end
469
470 #
471 #
472 class TestMonitorOffCommand < Test::Unit::TestCase 
473   def setup
474     @p = MockPlayer.new
475     @game = MockGame.new
476     @p.game = @game
477   end
478
479   def test_call
480     cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
481     rc = cmd.call
482
483     assert_equal(:continue, rc)
484   end
485 end
486
487 #
488 #
489 class TestHelpCommand < Test::Unit::TestCase 
490   def setup
491     @p = MockPlayer.new
492     @game = MockGame.new
493     @p.game = @game
494   end
495
496   def test_call
497     cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
498     rc = cmd.call
499
500     assert_equal(:continue, rc)
501   end
502 end
503
504 #
505 #
506 class TestRatingCommand < Test::Unit::TestCase 
507   def setup
508     @p = MockPlayer.new
509     @game = MockGame.new
510     @p.game = @game
511   end
512
513   def test_call
514     players = [MockPlayer.new]
515     cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
516     rc = cmd.call
517
518     assert_equal(:continue, rc)
519   end
520 end
521
522 #
523 #
524 class TestVersionCommand < Test::Unit::TestCase 
525   def setup
526     @p = MockPlayer.new
527     @game = MockGame.new
528     @p.game = @game
529   end
530
531   def test_call
532     cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
533     rc = cmd.call
534
535     assert_equal(:continue, rc)
536   end
537 end
538
539 #
540 #
541 class TestGameCommand < Test::Unit::TestCase 
542   def setup
543     @p = MockPlayer.new
544     @game = MockGame.new
545     @p.game = @game
546   end
547
548   def test_call_connected
549     @p.status = "connected"
550     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
551     rc = cmd.call
552
553     assert_equal(:continue, rc)
554     assert_equal("connected", @p.status)
555   end
556
557   def test_call_game_waiting
558     @p.status = "game_waiting"
559     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
560     rc = cmd.call
561
562     assert_equal(:continue, rc)
563     assert_equal("connected", @p.status)
564   end
565
566   def test_call_agree_waiting
567     @p.status = "agree_waiting"
568     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
569     rc = cmd.call
570
571     assert_equal(:continue, rc)
572     assert_equal("agree_waiting", @p.status)
573   end
574 end
575
576 #
577 #
578 class TestChatCommand < Test::Unit::TestCase 
579   def setup
580     @p = MockPlayer.new
581     @game = MockGame.new
582     @p.game = @game
583   end
584
585   def test_call
586     players = [["dummy_name", MockPlayer.new]]
587     cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
588     rc = cmd.call
589
590     assert_equal(:continue, rc)
591   end
592
593   def test_call_csa_protocol
594     players = [["dummy_name", MockPlayer.new]]
595     players.each do |name, p|
596       p.protocol = ShogiServer::LoginCSA::PROTOCOL
597     end
598     cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
599     rc = cmd.call
600
601     assert_equal(:continue, rc)
602   end
603 end
604
605 #
606 #
607 class TestListCommand < Test::Unit::TestCase 
608   def setup
609     @p = MockPlayer.new
610     @game = MockGame.new
611     @p.game = @game
612   end
613
614   def test_call
615     games = [["dummy_game_id", MockGame.new]]
616     cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
617     rc = cmd.call
618
619     assert_equal(:continue, rc)
620   end
621
622 end
623
624 #
625 #
626 class TestWhoCommand < Test::Unit::TestCase 
627   def setup
628     @p = MockPlayer.new
629     @game = MockGame.new
630     @p.game = @game
631   end
632
633   def test_call
634     players = [["dummy_name", MockPlayer.new]]
635     cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
636     rc = cmd.call
637
638     assert_equal(:continue, rc)
639   end
640
641 end
642
643 #
644 #
645 class TestLogoutCommand < Test::Unit::TestCase 
646   def setup
647     @p = MockPlayer.new
648     @game = MockGame.new
649     @p.game = @game
650   end
651
652   def test_call
653     cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
654     rc = cmd.call
655
656     assert_equal(:return, rc)
657   end
658
659 end
660
661 #
662 #
663 class TestChallengeCommand < Test::Unit::TestCase 
664   def setup
665     @p = MockPlayer.new
666     @game = MockGame.new
667   end
668
669   def test_call
670     cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
671     rc = cmd.call
672
673     assert_equal(:continue, rc)
674   end
675 end
676
677 #
678 #
679 class TestSpaceCommand < Test::Unit::TestCase 
680   def setup
681     @p = MockPlayer.new
682     @game = MockGame.new
683   end
684
685   def test_call
686     cmd = ShogiServer::SpaceCommand.new("", @p)
687     rc = cmd.call
688
689     assert_equal(:continue, rc)
690   end
691 end
692
693 #
694 #
695 class TestErrorCommand < Test::Unit::TestCase 
696   def setup
697     @p = MockPlayer.new
698     @game = MockGame.new
699   end
700
701   def test_call
702     cmd = ShogiServer::ErrorCommand.new("", @p)
703     rc = cmd.call
704
705     assert_equal(:continue, rc)
706   end
707 end
708
709