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_help_command
121 cmd = ShogiServer::Command.factory("%%HELP", @p)
122 assert_instance_of(ShogiServer::HelpCommand, cmd)
125 def test_rating_command
126 cmd = ShogiServer::Command.factory("%%RATING", @p)
127 assert_instance_of(ShogiServer::RatingCommand, cmd)
130 def test_version_command
131 cmd = ShogiServer::Command.factory("%%VERSION", @p)
132 assert_instance_of(ShogiServer::VersionCommand, cmd)
135 def test_game_command
136 cmd = ShogiServer::Command.factory("%%GAME", @p)
137 assert_instance_of(ShogiServer::GameCommand, cmd)
140 def test_game_challenge_command_game
141 cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
142 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
145 def test_game_challenge_command_challenge
146 cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
147 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
150 def test_chat_command
151 cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
152 assert_instance_of(ShogiServer::ChatCommand, cmd)
155 def test_list_command
156 cmd = ShogiServer::Command.factory("%%LIST", @p)
157 assert_instance_of(ShogiServer::ListCommand, cmd)
161 cmd = ShogiServer::Command.factory("%%WHO", @p)
162 assert_instance_of(ShogiServer::WhoCommand, cmd)
165 def test_logout_command
166 cmd = ShogiServer::Command.factory("LOGOUT", @p)
167 assert_instance_of(ShogiServer::LogoutCommand, cmd)
170 def test_challenge_command
171 cmd = ShogiServer::Command.factory("CHALLENGE", @p)
172 assert_instance_of(ShogiServer::ChallengeCommand, cmd)
175 def test_space_command
176 cmd = ShogiServer::Command.factory(" ", @p)
177 assert_instance_of(ShogiServer::SpaceCommand, cmd)
180 def test_setbuoy_command
181 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU", @p)
182 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
185 def test_setbuoy_command_with_counter
186 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU 3", @p)
187 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
190 def test_deletebuoy_command
191 cmd = ShogiServer::Command.factory("%%DELETEBUOY buoy_test-1500-0", @p)
192 assert_instance_of(ShogiServer::DeleteBuoyCommand, cmd)
195 def test_getbuoycount_command
196 cmd = ShogiServer::Command.factory("%%GETBUOYCOUNT buoy_test-1500-0", @p)
197 assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
201 cmd = ShogiServer::Command.factory("should_be_error", @p)
202 assert_instance_of(ShogiServer::ErrorCommand, cmd)
208 class TestKeepAliveCommand < Test::Unit::TestCase
214 cmd = ShogiServer::KeepAliveCommand.new("", @p)
216 assert_equal(:continue, rc)
222 class TestMoveCommand < Test::Unit::TestCase
231 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
233 assert_equal(:continue, rc)
237 cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
239 assert_equal(:continue, rc)
240 assert_equal("'*comment", @game.log.first)
244 @game.finish_flag = true
245 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
246 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
248 assert_equal(:return, rc)
254 class TestSpecialComand < Test::Unit::TestCase
263 @game.finish_flag = true
264 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
266 assert_equal(:continue, rc)
269 def test_toryo_csa_protocol
270 @game.finish_flag = true
271 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
272 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
274 assert_equal(:return, rc)
278 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
280 assert_equal(:continue, rc)
283 def test_expired_game
284 @p.status = "agree_waiting"
285 @game.prepared_expire = true
286 assert(!@game.rejected)
287 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
289 assert_equal(:continue, rc)
290 assert(@game.rejected)
293 def test_expired_game_csa_protocol
294 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
295 @p.status = "agree_waiting"
296 @game.prepared_expire = true
297 assert(!@game.rejected)
298 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
300 assert_equal(:return, rc)
301 assert(@game.rejected)
305 @p.status = "should_be_ignored"
306 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
308 assert_equal(:continue, rc)
314 class TestExceptionCommand < Test::Unit::TestCase
320 cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
322 assert_equal(:return, rc)
328 class TestRejectCommand < Test::Unit::TestCase
337 @p.status = "agree_waiting"
338 assert(!@game.rejected)
339 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
342 assert_equal(:continue, rc)
343 assert(@game.rejected)
346 def test_call_csa_protocol
347 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
348 @p.status = "agree_waiting"
349 assert(!@game.rejected)
350 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
353 assert_equal(:return, rc)
354 assert(@game.rejected)
358 @p.status = "should_be_ignored"
359 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
362 assert_equal(:continue, rc)
363 assert(!@game.rejected)
369 class TestAgreeCommand < Test::Unit::TestCase
374 @p.status = "agree_waiting"
377 def test_not_start_yet
378 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
380 assert_equal(:continue, rc)
381 assert(!@game.started)
385 @game.is_startable_status = true
386 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
388 assert_equal(:continue, rc)
389 assert(@game.started)
393 @p.status = "should_be_ignored"
394 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
396 assert_equal(:continue, rc)
397 assert(!@game.started)
403 class TestShowCommand < Test::Unit::TestCase
411 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
414 assert_equal(:continue, rc)
417 def test_call_nil_game
418 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
421 assert_equal(:continue, rc)
427 class TestMonitorOnCommand < Test::Unit::TestCase
435 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
438 assert_equal(:continue, rc)
441 def test_call_read_logfile
443 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
445 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)
446 assert_equal(:continue, rc)
452 class TestMonitor2OnCommand < Test::Unit::TestCase
460 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
463 assert_equal(:continue, rc)
466 def test_call_read_logfile
467 $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
468 $tempfile.write "hoge\nfoo\n"
474 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
476 assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
477 assert_equal(:continue, rc)
484 class TestMonitorOffCommand < Test::Unit::TestCase
492 cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
495 assert_equal(:continue, rc)
501 class TestHelpCommand < Test::Unit::TestCase
509 cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
512 assert_equal(:continue, rc)
518 class TestRatingCommand < Test::Unit::TestCase
526 players = [MockPlayer.new]
527 cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
530 assert_equal(:continue, rc)
536 class TestVersionCommand < Test::Unit::TestCase
544 cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
547 assert_equal(:continue, rc)
553 class TestGameCommand < Test::Unit::TestCase
560 def test_call_connected
561 @p.status = "connected"
562 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
565 assert_equal(:continue, rc)
566 assert_equal("connected", @p.status)
569 def test_call_game_waiting
570 @p.status = "game_waiting"
571 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
574 assert_equal(:continue, rc)
575 assert_equal("connected", @p.status)
578 def test_call_agree_waiting
579 @p.status = "agree_waiting"
580 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
583 assert_equal(:continue, rc)
584 assert_equal("agree_waiting", @p.status)
590 class TestChatCommand < Test::Unit::TestCase
598 players = [["dummy_name", MockPlayer.new]]
599 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
602 assert_equal(:continue, rc)
605 def test_call_csa_protocol
606 players = [["dummy_name", MockPlayer.new]]
607 players.each do |name, p|
608 p.protocol = ShogiServer::LoginCSA::PROTOCOL
610 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
613 assert_equal(:continue, rc)
619 class TestListCommand < Test::Unit::TestCase
627 games = [["dummy_game_id", MockGame.new]]
628 cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
631 assert_equal(:continue, rc)
638 class TestWhoCommand < Test::Unit::TestCase
646 players = [["dummy_name", MockPlayer.new]]
647 cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
650 assert_equal(:continue, rc)
657 class TestLogoutCommand < Test::Unit::TestCase
665 cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
668 assert_equal(:return, rc)
675 class TestChallengeCommand < Test::Unit::TestCase
682 cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
685 assert_equal(:continue, rc)
691 class TestSpaceCommand < Test::Unit::TestCase
698 cmd = ShogiServer::SpaceCommand.new("", @p)
701 assert_equal(:continue, rc)
707 class TestErrorCommand < Test::Unit::TestCase
714 cmd = ShogiServer::ErrorCommand.new("", @p)
717 assert_equal(:continue, rc)
721 class BaseTestBuoyCommand < Test::Unit::TestCase
728 @buoy = ShogiServer::Buoy.new
736 file = File.join($topdir, "buoy.yaml")
737 File.delete file if File.exist? file
748 class TestSetBuoyCommand < BaseTestBuoyCommand
752 @p.name = "set_buoy_player"
756 assert @buoy.is_new_game?("buoy_hoge-1500-0")
757 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
760 assert !@buoy.is_new_game?("buoy_hoge-1500-0")
761 assert !$p1.out.empty?
762 assert !$p2.out.empty?
763 buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
764 assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
768 assert @buoy.is_new_game?("buoy_hoge-1500-0")
769 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
772 assert @buoy.is_new_game?("buoy_hoge-1500-0")
773 assert !$p1.out.empty?
774 assert !$p2.out.empty?
777 def test_call_error_not_buoy_game_name
778 assert @buoy.is_new_game?("buoy_hoge-1500-0")
779 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
784 assert @buoy.is_new_game?("buoy_hoge-1500-0")
787 def test_call_error_duplicated_game_name
788 assert @buoy.is_new_game?("buoy_duplicated-1500-0")
789 bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1)
791 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
793 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
798 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
801 def test_call_error_bad_moves
802 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
803 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
808 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
811 def test_call_error_bad_counter
812 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
813 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
818 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
825 class TestDeleteBuoyCommand < BaseTestBuoyCommand
827 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
828 assert @buoy.is_new_game?(buoy_game.game_name)
829 @buoy.add_game buoy_game
830 assert !@buoy.is_new_game?(buoy_game.game_name)
831 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
836 assert @buoy.is_new_game?(buoy_game.game_name)
839 def test_call_not_exist
840 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
841 assert @buoy.is_new_game?(buoy_game.game_name)
842 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
847 assert @buoy.is_new_game?(buoy_game.game_name)
850 def test_call_another_player
851 buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1)
852 assert @buoy.is_new_game?(buoy_game.game_name)
853 @buoy.add_game(buoy_game)
854 assert !@buoy.is_new_game?(buoy_game.game_name)
856 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
859 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
860 assert !@buoy.is_new_game?(buoy_game.game_name)
866 class TestGetBuoyCountCommand < BaseTestBuoyCommand
868 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
869 assert @buoy.is_new_game?(buoy_game.game_name)
870 @buoy.add_game buoy_game
871 assert !@buoy.is_new_game?(buoy_game.game_name)
872 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
875 assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
878 def test_call_not_exist
879 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
880 assert @buoy.is_new_game?(buoy_game.game_name)
881 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
884 assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
890 class TestMonitorHandler < Test::Unit::TestCase
892 @player1 = MockPlayer.new
893 @handler1 = ShogiServer::MonitorHandler1.new @player1
894 @player2 = MockPlayer.new
895 @handler2 = ShogiServer::MonitorHandler1.new @player2
897 assert_not_equal(@handler1, @handler2)
901 @player1 = MockPlayer.new
902 @handler1 = ShogiServer::MonitorHandler1.new @player1
903 @handler2 = ShogiServer::MonitorHandler1.new @player1
905 assert_equal(@handler1, @handler2)
911 class TestMonitorHandler1 < Test::Unit::TestCase
913 @player = MockPlayer.new
914 @handler = ShogiServer::MonitorHandler1.new @player
918 assert_equal(1, @handler.type)
922 assert_equal("MONITOR", @handler.header)
926 @handler.write_safe("game_id", "hoge")
927 assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n",
934 class TestMonitorHandler2 < Test::Unit::TestCase
936 @player = MockPlayer.new
937 @handler = ShogiServer::MonitorHandler2.new @player
941 assert_equal(2, @handler.type)
945 assert_equal("MONITOR2", @handler.header)
949 @handler.write_safe("game_id", "hoge")
950 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n",
955 @handler.write_safe("game_id", "hoge\nfoo")
956 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n",