1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 $topdir = File.expand_path File.dirname(__FILE__)
6 require 'mock_log_message'
7 require 'test/mock_player'
8 require 'shogi_server/login'
9 require 'shogi_server/player'
10 require 'shogi_server/command'
16 @games["dummy_game_id"] = MockGame.new
28 return [MockPlayer.new]
39 def get_player(status, game_id, sente, searcher)
57 class TestFactoryMethod < Test::Unit::TestCase
61 @p.name = "test_factory_method_player"
62 $league = MockLeague.new
65 def test_keep_alive_command
66 cmd = ShogiServer::Command.factory("", @p)
67 assert_instance_of(ShogiServer::KeepAliveCommand, cmd)
71 cmd = ShogiServer::Command.factory("+7776FU", @p)
72 assert_instance_of(ShogiServer::MoveCommand, cmd)
75 def test_special_command
76 cmd = ShogiServer::Command.factory("%TORYO", @p)
77 assert_instance_of(ShogiServer::SpecialCommand, cmd)
80 def test_special_command_timeout
81 cmd = ShogiServer::Command.factory(:timeout, @p)
82 assert_instance_of(ShogiServer::SpecialCommand, cmd)
85 def test_execption_command
86 cmd = ShogiServer::Command.factory(:exception, @p)
87 assert_instance_of(ShogiServer::ExceptionCommand, cmd)
90 def test_reject_command
91 cmd = ShogiServer::Command.factory("REJECT", @p)
92 assert_instance_of(ShogiServer::RejectCommand, cmd)
95 def test_agree_command
96 cmd = ShogiServer::Command.factory("AGREE", @p)
97 assert_instance_of(ShogiServer::AgreeCommand, cmd)
100 def test_show_command
101 cmd = ShogiServer::Command.factory("%%SHOW game_id", @p)
102 assert_instance_of(ShogiServer::ShowCommand, cmd)
105 def test_monitoron_command
106 cmd = ShogiServer::Command.factory("%%MONITORON game_id", @p)
107 assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
110 def test_monitor2on_command
111 cmd = ShogiServer::Command.factory("%%MONITOR2ON game_id", @p)
112 assert_instance_of(ShogiServer::Monitor2OnCommand, cmd)
115 def test_monitoroff_command
116 cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
117 assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
120 def test_monitor2off_command
121 cmd = ShogiServer::Command.factory("%%MONITOR2OFF game_id", @p)
122 assert_instance_of(ShogiServer::Monitor2OffCommand, cmd)
125 def test_help_command
126 cmd = ShogiServer::Command.factory("%%HELP", @p)
127 assert_instance_of(ShogiServer::HelpCommand, cmd)
130 def test_rating_command
131 cmd = ShogiServer::Command.factory("%%RATING", @p)
132 assert_instance_of(ShogiServer::RatingCommand, cmd)
135 def test_version_command
136 cmd = ShogiServer::Command.factory("%%VERSION", @p)
137 assert_instance_of(ShogiServer::VersionCommand, cmd)
140 def test_game_command
141 cmd = ShogiServer::Command.factory("%%GAME", @p)
142 assert_instance_of(ShogiServer::GameCommand, cmd)
145 def test_game_challenge_command_game
146 cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
147 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
150 def test_game_challenge_command_challenge
151 cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
152 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
155 def test_chat_command
156 cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
157 assert_instance_of(ShogiServer::ChatCommand, cmd)
160 def test_list_command
161 cmd = ShogiServer::Command.factory("%%LIST", @p)
162 assert_instance_of(ShogiServer::ListCommand, cmd)
166 cmd = ShogiServer::Command.factory("%%WHO", @p)
167 assert_instance_of(ShogiServer::WhoCommand, cmd)
170 def test_logout_command
171 cmd = ShogiServer::Command.factory("LOGOUT", @p)
172 assert_instance_of(ShogiServer::LogoutCommand, cmd)
175 def test_challenge_command
176 cmd = ShogiServer::Command.factory("CHALLENGE", @p)
177 assert_instance_of(ShogiServer::ChallengeCommand, cmd)
180 def test_space_command
181 cmd = ShogiServer::Command.factory(" ", @p)
182 assert_instance_of(ShogiServer::SpaceCommand, cmd)
185 def test_setbuoy_command
186 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU", @p)
187 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
190 def test_setbuoy_command_with_counter
191 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU 3", @p)
192 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
195 def test_deletebuoy_command
196 cmd = ShogiServer::Command.factory("%%DELETEBUOY buoy_test-1500-0", @p)
197 assert_instance_of(ShogiServer::DeleteBuoyCommand, cmd)
200 def test_getbuoycount_command
201 cmd = ShogiServer::Command.factory("%%GETBUOYCOUNT buoy_test-1500-0", @p)
202 assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
206 cmd = ShogiServer::Command.factory("should_be_error", @p)
207 assert_instance_of(ShogiServer::ErrorCommand, cmd)
213 class TestKeepAliveCommand < Test::Unit::TestCase
219 cmd = ShogiServer::KeepAliveCommand.new("", @p)
221 assert_equal(:continue, rc)
227 class TestMoveCommand < Test::Unit::TestCase
236 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
238 assert_equal(:continue, rc)
242 cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
244 assert_equal(:continue, rc)
245 assert_equal("'*comment", @game.log.first)
249 @game.finish_flag = true
250 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
251 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
253 assert_equal(:return, rc)
259 class TestSpecialComand < Test::Unit::TestCase
268 @game.finish_flag = true
269 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
271 assert_equal(:continue, rc)
274 def test_toryo_csa_protocol
275 @game.finish_flag = true
276 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
277 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
279 assert_equal(:return, rc)
283 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
285 assert_equal(:continue, rc)
288 def test_expired_game
289 @p.status = "agree_waiting"
290 @game.prepared_expire = true
291 assert(!@game.rejected)
292 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
294 assert_equal(:continue, rc)
295 assert(@game.rejected)
298 def test_expired_game_csa_protocol
299 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
300 @p.status = "agree_waiting"
301 @game.prepared_expire = true
302 assert(!@game.rejected)
303 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
305 assert_equal(:return, rc)
306 assert(@game.rejected)
310 @p.status = "should_be_ignored"
311 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
313 assert_equal(:continue, rc)
319 class TestExceptionCommand < Test::Unit::TestCase
325 cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
327 assert_equal(:return, rc)
333 class TestRejectCommand < Test::Unit::TestCase
342 @p.status = "agree_waiting"
343 assert(!@game.rejected)
344 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
347 assert_equal(:continue, rc)
348 assert(@game.rejected)
351 def test_call_csa_protocol
352 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
353 @p.status = "agree_waiting"
354 assert(!@game.rejected)
355 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
358 assert_equal(:return, rc)
359 assert(@game.rejected)
363 @p.status = "should_be_ignored"
364 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
367 assert_equal(:continue, rc)
368 assert(!@game.rejected)
374 class TestAgreeCommand < Test::Unit::TestCase
379 @p.status = "agree_waiting"
382 def test_not_start_yet
383 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
385 assert_equal(:continue, rc)
386 assert(!@game.started)
390 @game.is_startable_status = true
391 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
393 assert_equal(:continue, rc)
394 assert(@game.started)
398 @p.status = "should_be_ignored"
399 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
401 assert_equal(:continue, rc)
402 assert(!@game.started)
408 class TestShowCommand < Test::Unit::TestCase
416 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
419 assert_equal(:continue, rc)
422 def test_call_nil_game
423 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
426 assert_equal(:continue, rc)
432 class TestMonitorOnCommand < Test::Unit::TestCase
440 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
443 assert_equal(:continue, rc)
446 def test_call_read_logfile
448 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
450 assert_equal("##[MONITOR][dummy_game_id] dummy_game_show\n##[MONITOR][dummy_game_id] line1\n##[MONITOR][dummy_game_id] line2\n##[MONITOR][dummy_game_id] +OK\n", @p.out.join)
451 assert_equal(:continue, rc)
457 class TestMonitor2OnCommand < Test::Unit::TestCase
465 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
468 assert_equal(:continue, rc)
471 def test_call_read_logfile
472 $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
473 $tempfile.write "hoge\nfoo\n"
479 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
481 assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
482 assert_equal(:continue, rc)
489 class TestMonitorOffCommand < Test::Unit::TestCase
497 cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
500 assert_equal(:continue, rc)
506 class TestMonitor2OffCommand < Test::Unit::TestCase
514 cmd = ShogiServer::Monitor2OffCommand.new("%%MONITOR2OFF hoge", @p, nil)
517 assert_equal(:continue, rc)
523 class TestHelpCommand < Test::Unit::TestCase
531 cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
534 assert_equal(:continue, rc)
540 class TestRatingCommand < Test::Unit::TestCase
548 players = [MockPlayer.new]
549 cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
552 assert_equal(:continue, rc)
558 class TestVersionCommand < Test::Unit::TestCase
566 cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
569 assert_equal(:continue, rc)
575 class TestGameCommand < Test::Unit::TestCase
582 def test_call_connected
583 @p.status = "connected"
584 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
587 assert_equal(:continue, rc)
588 assert_equal("connected", @p.status)
591 def test_call_game_waiting
592 @p.status = "game_waiting"
593 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
596 assert_equal(:continue, rc)
597 assert_equal("connected", @p.status)
600 def test_call_agree_waiting
601 @p.status = "agree_waiting"
602 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
605 assert_equal(:continue, rc)
606 assert_equal("agree_waiting", @p.status)
612 class TestChatCommand < Test::Unit::TestCase
620 players = [["dummy_name", MockPlayer.new]]
621 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
624 assert_equal(:continue, rc)
627 def test_call_csa_protocol
628 players = [["dummy_name", MockPlayer.new]]
629 players.each do |name, p|
630 p.protocol = ShogiServer::LoginCSA::PROTOCOL
632 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
635 assert_equal(:continue, rc)
641 class TestListCommand < Test::Unit::TestCase
649 games = [["dummy_game_id", MockGame.new]]
650 cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
653 assert_equal(:continue, rc)
660 class TestWhoCommand < Test::Unit::TestCase
668 players = [["dummy_name", MockPlayer.new]]
669 cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
672 assert_equal(:continue, rc)
679 class TestLogoutCommand < Test::Unit::TestCase
687 cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
690 assert_equal(:return, rc)
697 class TestChallengeCommand < Test::Unit::TestCase
704 cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
707 assert_equal(:continue, rc)
713 class TestSpaceCommand < Test::Unit::TestCase
720 cmd = ShogiServer::SpaceCommand.new("", @p)
723 assert_equal(:continue, rc)
729 class TestErrorCommand < Test::Unit::TestCase
736 cmd = ShogiServer::ErrorCommand.new("", @p)
739 assert_equal(:continue, rc)
743 class BaseTestBuoyCommand < Test::Unit::TestCase
750 @buoy = ShogiServer::Buoy.new
758 file = File.join($topdir, "buoy.yaml")
759 File.delete file if File.exist? file
770 class TestSetBuoyCommand < BaseTestBuoyCommand
774 @p.name = "set_buoy_player"
778 assert @buoy.is_new_game?("buoy_hoge-1500-0")
779 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
782 assert !@buoy.is_new_game?("buoy_hoge-1500-0")
783 assert !$p1.out.empty?
784 assert !$p2.out.empty?
785 buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
786 assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
790 assert @buoy.is_new_game?("buoy_hoge-1500-0")
791 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
794 assert @buoy.is_new_game?("buoy_hoge-1500-0")
795 assert !$p1.out.empty?
796 assert !$p2.out.empty?
799 def test_call_error_not_buoy_game_name
800 assert @buoy.is_new_game?("buoy_hoge-1500-0")
801 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
806 assert @buoy.is_new_game?("buoy_hoge-1500-0")
809 def test_call_error_duplicated_game_name
810 assert @buoy.is_new_game?("buoy_duplicated-1500-0")
811 bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1)
813 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
815 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
820 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
823 def test_call_error_bad_moves
824 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
825 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
830 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
833 def test_call_error_bad_counter
834 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
835 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
840 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
847 class TestDeleteBuoyCommand < BaseTestBuoyCommand
849 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
850 assert @buoy.is_new_game?(buoy_game.game_name)
851 @buoy.add_game buoy_game
852 assert !@buoy.is_new_game?(buoy_game.game_name)
853 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
858 assert @buoy.is_new_game?(buoy_game.game_name)
861 def test_call_not_exist
862 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
863 assert @buoy.is_new_game?(buoy_game.game_name)
864 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
869 assert @buoy.is_new_game?(buoy_game.game_name)
872 def test_call_another_player
873 buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1)
874 assert @buoy.is_new_game?(buoy_game.game_name)
875 @buoy.add_game(buoy_game)
876 assert !@buoy.is_new_game?(buoy_game.game_name)
878 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
881 assert_equal "##[ERROR] you are not allowed to delete a buoy game that you did not set: buoy_anotherplayer-1500-0\n", @p.out.first
882 assert !@buoy.is_new_game?(buoy_game.game_name)
888 class TestGetBuoyCountCommand < BaseTestBuoyCommand
890 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
891 assert @buoy.is_new_game?(buoy_game.game_name)
892 @buoy.add_game buoy_game
893 assert !@buoy.is_new_game?(buoy_game.game_name)
894 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
897 assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
900 def test_call_not_exist
901 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
902 assert @buoy.is_new_game?(buoy_game.game_name)
903 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
906 assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
912 class TestMonitorHandler < Test::Unit::TestCase
913 def test_not_equal_players
914 @player1 = MockPlayer.new
915 @handler1 = ShogiServer::MonitorHandler1.new @player1
916 @player2 = MockPlayer.new
917 @handler2 = ShogiServer::MonitorHandler1.new @player2
919 assert_not_equal(@handler1, @handler2)
923 @player1 = MockPlayer.new
924 @handler1 = ShogiServer::MonitorHandler1.new @player1
925 @handler2 = ShogiServer::MonitorHandler1.new @player1
927 assert_equal(@handler1, @handler2)
933 class TestMonitorHandler1 < Test::Unit::TestCase
935 @player = MockPlayer.new
936 @handler = ShogiServer::MonitorHandler1.new @player
940 assert_equal(1, @handler.type)
944 assert_equal("MONITOR", @handler.header)
948 assert_equal @handler, @handler
949 assert_not_equal @handler, nil
953 assert_not_equal(@handler, ShogiServer::MonitorHandler2.new(@player))
957 @handler.write_safe("game_id", "hoge")
958 assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n",
965 class TestMonitorHandler2 < Test::Unit::TestCase
967 @player = MockPlayer.new
968 @handler = ShogiServer::MonitorHandler2.new @player
972 assert_equal(2, @handler.type)
976 assert_equal("MONITOR2", @handler.header)
980 assert_equal @handler, @handler
981 assert_not_equal @handler, nil
985 assert_not_equal(@handler, ShogiServer::MonitorHandler1.new(@player))
989 @handler.write_safe("game_id", "hoge")
990 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n",
995 @handler.write_safe("game_id", "hoge\nfoo")
996 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n",