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
24 $p1.status = "game_waiting"
28 $p2.status = "game_waiting"
41 return [MockPlayer.new]
52 def get_player(status, game_id, sente, searcher)
65 [$p1,$p2].each {|pp| yield pp}
68 def find_rival(player, game_name)
70 when nil # no preference
71 return get_player("game_waiting", game_name, nil, player)
72 when true # rival must be gote
73 return get_player("game_waiting", game_name, false, player)
74 when false # rival must be sente
75 return get_player("game_waiting", game_name, true, player)
83 class TestFactoryMethod < Test::Unit::TestCase
87 @p.name = "test_factory_method_player"
88 $league = MockLeague.new
91 def test_keep_alive_command
92 cmd = ShogiServer::Command.factory("", @p)
93 assert_instance_of(ShogiServer::KeepAliveCommand, cmd)
97 cmd = ShogiServer::Command.factory("+7776FU", @p)
98 assert_instance_of(ShogiServer::MoveCommand, cmd)
101 def test_special_command
102 cmd = ShogiServer::Command.factory("%TORYO", @p)
103 assert_instance_of(ShogiServer::SpecialCommand, cmd)
106 def test_special_command_timeout
107 cmd = ShogiServer::Command.factory(:timeout, @p)
108 assert_instance_of(ShogiServer::SpecialCommand, cmd)
111 def test_execption_command
112 cmd = ShogiServer::Command.factory(:exception, @p)
113 assert_instance_of(ShogiServer::ExceptionCommand, cmd)
116 def test_reject_command
117 cmd = ShogiServer::Command.factory("REJECT", @p)
118 assert_instance_of(ShogiServer::RejectCommand, cmd)
121 def test_agree_command
122 cmd = ShogiServer::Command.factory("AGREE", @p)
123 assert_instance_of(ShogiServer::AgreeCommand, cmd)
126 def test_show_command
127 cmd = ShogiServer::Command.factory("%%SHOW game_id", @p)
128 assert_instance_of(ShogiServer::ShowCommand, cmd)
131 def test_monitoron_command
132 cmd = ShogiServer::Command.factory("%%MONITORON game_id", @p)
133 assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
136 def test_monitor2on_command
137 cmd = ShogiServer::Command.factory("%%MONITOR2ON game_id", @p)
138 assert_instance_of(ShogiServer::Monitor2OnCommand, cmd)
141 def test_monitoroff_command
142 cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
143 assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
146 def test_monitor2off_command
147 cmd = ShogiServer::Command.factory("%%MONITOR2OFF game_id", @p)
148 assert_instance_of(ShogiServer::Monitor2OffCommand, cmd)
151 def test_help_command
152 cmd = ShogiServer::Command.factory("%%HELP", @p)
153 assert_instance_of(ShogiServer::HelpCommand, cmd)
156 def test_rating_command
157 cmd = ShogiServer::Command.factory("%%RATING", @p)
158 assert_instance_of(ShogiServer::RatingCommand, cmd)
161 def test_version_command
162 cmd = ShogiServer::Command.factory("%%VERSION", @p)
163 assert_instance_of(ShogiServer::VersionCommand, cmd)
166 def test_game_command
167 cmd = ShogiServer::Command.factory("%%GAME", @p)
168 assert_instance_of(ShogiServer::GameCommand, cmd)
171 def test_game_challenge_command_game
172 cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
173 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
176 def test_game_challenge_command_challenge
177 cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
178 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
181 def test_chat_command
182 cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
183 assert_instance_of(ShogiServer::ChatCommand, cmd)
186 def test_list_command
187 cmd = ShogiServer::Command.factory("%%LIST", @p)
188 assert_instance_of(ShogiServer::ListCommand, cmd)
192 cmd = ShogiServer::Command.factory("%%WHO", @p)
193 assert_instance_of(ShogiServer::WhoCommand, cmd)
196 def test_logout_command
197 cmd = ShogiServer::Command.factory("LOGOUT", @p)
198 assert_instance_of(ShogiServer::LogoutCommand, cmd)
201 def test_challenge_command
202 cmd = ShogiServer::Command.factory("CHALLENGE", @p)
203 assert_instance_of(ShogiServer::ChallengeCommand, cmd)
206 def test_space_command
207 cmd = ShogiServer::Command.factory(" ", @p)
208 assert_instance_of(ShogiServer::SpaceCommand, cmd)
211 def test_setbuoy_command
212 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU", @p)
213 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
216 def test_setbuoy_command_with_counter
217 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU 3", @p)
218 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
221 def test_deletebuoy_command
222 cmd = ShogiServer::Command.factory("%%DELETEBUOY buoy_test-1500-0", @p)
223 assert_instance_of(ShogiServer::DeleteBuoyCommand, cmd)
226 def test_getbuoycount_command
227 cmd = ShogiServer::Command.factory("%%GETBUOYCOUNT buoy_test-1500-0", @p)
228 assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
231 def test_void_command
232 cmd = ShogiServer::Command.factory("%%%HOGE", @p)
233 assert_instance_of(ShogiServer::VoidCommand, cmd)
237 cmd = ShogiServer::Command.factory("should_be_error", @p)
238 assert_instance_of(ShogiServer::ErrorCommand, cmd)
240 assert_match /unknown command should_be_error/, cmd.msg
244 cmd = ShogiServer::Command.factory("LOGIN hoge foo", @p)
245 assert_instance_of(ShogiServer::ErrorCommand, cmd)
247 assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
249 cmd = ShogiServer::Command.factory("LOGin hoge foo", @p)
250 assert_instance_of(ShogiServer::ErrorCommand, cmd)
252 assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
254 cmd = ShogiServer::Command.factory("LOGIN hoge foo", @p)
255 assert_instance_of(ShogiServer::ErrorCommand, cmd)
257 assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
259 cmd = ShogiServer::Command.factory("LOGINhoge foo", @p)
260 assert_instance_of(ShogiServer::ErrorCommand, cmd)
262 assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
268 class TestKeepAliveCommand < Test::Unit::TestCase
274 cmd = ShogiServer::KeepAliveCommand.new("", @p)
276 assert_equal(:continue, rc)
282 class TestMoveCommand < Test::Unit::TestCase
291 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
293 assert_equal(:continue, rc)
297 cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
299 assert_equal(:continue, rc)
300 assert_equal("'*comment", @game.log.first)
304 @game.finish_flag = true
305 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
306 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
308 assert_equal(:return, rc)
314 class TestSpecialComand < Test::Unit::TestCase
323 @game.finish_flag = true
324 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
326 assert_equal(:continue, rc)
329 def test_toryo_csa_protocol
330 @game.finish_flag = true
331 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
332 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
334 assert_equal(:return, rc)
338 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
340 assert_equal(:continue, rc)
343 def test_expired_game
344 @p.status = "agree_waiting"
345 @game.prepared_expire = true
346 assert(!@game.rejected)
347 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
349 assert_equal(:continue, rc)
350 assert(@game.rejected)
353 def test_expired_game_csa_protocol
354 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
355 @p.status = "agree_waiting"
356 @game.prepared_expire = true
357 assert(!@game.rejected)
358 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
360 assert_equal(:return, rc)
361 assert(@game.rejected)
365 @p.status = "should_be_ignored"
366 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
368 assert_equal(:continue, rc)
374 class TestExceptionCommand < Test::Unit::TestCase
380 cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
382 assert_equal(:return, rc)
388 class TestRejectCommand < Test::Unit::TestCase
397 @p.status = "agree_waiting"
398 assert(!@game.rejected)
399 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
402 assert_equal(:continue, rc)
403 assert(@game.rejected)
406 def test_call_csa_protocol
407 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
408 @p.status = "agree_waiting"
409 assert(!@game.rejected)
410 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
413 assert_equal(:return, rc)
414 assert(@game.rejected)
418 @p.status = "should_be_ignored"
419 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
422 assert_equal(:continue, rc)
423 assert(!@game.rejected)
429 class TestAgreeCommand < Test::Unit::TestCase
434 @p.status = "agree_waiting"
437 def test_not_start_yet
438 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
440 assert_equal(:continue, rc)
441 assert(!@game.started)
445 @game.is_startable_status = true
446 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
448 assert_equal(:continue, rc)
449 assert(@game.started)
453 @p.status = "should_be_ignored"
454 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
456 assert_equal(:continue, rc)
457 assert(!@game.started)
463 class TestShowCommand < Test::Unit::TestCase
471 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
474 assert_equal(:continue, rc)
477 def test_call_nil_game
478 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
481 assert_equal(:continue, rc)
487 class TestMonitorOnCommand < Test::Unit::TestCase
495 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
498 assert_equal(:continue, rc)
501 def test_call_read_logfile
503 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
505 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)
506 assert_equal(:continue, rc)
512 class TestMonitor2OnCommand < Test::Unit::TestCase
520 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
523 assert_equal(:continue, rc)
526 def test_call_read_logfile
527 $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
528 $tempfile.write "hoge\nfoo\n"
534 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
536 assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
537 assert_equal(:continue, rc)
544 class TestMonitorOffCommand < Test::Unit::TestCase
552 cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
555 assert_equal(:continue, rc)
561 class TestMonitor2OffCommand < Test::Unit::TestCase
569 cmd = ShogiServer::Monitor2OffCommand.new("%%MONITOR2OFF hoge", @p, nil)
572 assert_equal(:continue, rc)
578 class TestHelpCommand < Test::Unit::TestCase
586 cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
589 assert_equal(:continue, rc)
595 class TestRatingCommand < Test::Unit::TestCase
603 players = [MockPlayer.new]
604 cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
607 assert_equal(:continue, rc)
613 class TestVersionCommand < Test::Unit::TestCase
621 cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
624 assert_equal(:continue, rc)
630 class TestGameCommand < Test::Unit::TestCase
637 def test_call_connected
638 @p.status = "connected"
639 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
642 assert_equal(:continue, rc)
643 assert_equal("connected", @p.status)
646 def test_call_game_waiting
647 @p.status = "game_waiting"
648 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
651 assert_equal(:continue, rc)
652 assert_equal("connected", @p.status)
655 def test_call_agree_waiting
656 @p.status = "agree_waiting"
657 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
660 assert_equal(:continue, rc)
661 assert_equal("agree_waiting", @p.status)
667 class TestChatCommand < Test::Unit::TestCase
675 players = [["dummy_name", MockPlayer.new]]
676 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
679 assert_equal(:continue, rc)
682 def test_call_csa_protocol
683 players = [["dummy_name", MockPlayer.new]]
684 players.each do |name, p|
685 p.protocol = ShogiServer::LoginCSA::PROTOCOL
687 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
690 assert_equal(:continue, rc)
696 class TestListCommand < Test::Unit::TestCase
704 games = [["dummy_game_id", MockGame.new]]
705 cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
708 assert_equal(:continue, rc)
715 class TestWhoCommand < Test::Unit::TestCase
723 players = [["dummy_name", MockPlayer.new]]
724 cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
727 assert_equal(:continue, rc)
734 class TestLogoutCommand < Test::Unit::TestCase
742 cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
745 assert_equal(:return, rc)
752 class TestChallengeCommand < Test::Unit::TestCase
759 cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
762 assert_equal(:continue, rc)
768 class TestSpaceCommand < Test::Unit::TestCase
775 cmd = ShogiServer::SpaceCommand.new("", @p)
778 assert_equal(:continue, rc)
784 class TestErrorCommand < Test::Unit::TestCase
791 cmd = ShogiServer::ErrorCommand.new("", @p)
794 assert_equal(:continue, rc)
798 class BaseTestBuoyCommand < Test::Unit::TestCase
801 $league = MockLeague.new
804 @buoy = ShogiServer::Buoy.new
812 file = File.join($topdir, "buoy.yaml")
813 File.delete file if File.exist? file
824 class TestSetBuoyCommand < BaseTestBuoyCommand
828 @p.name = "set_buoy_player"
832 assert @buoy.is_new_game?("buoy_hoge-1500-0")
833 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
836 assert !@buoy.is_new_game?("buoy_hoge-1500-0")
837 assert !$p1.out.empty?
838 assert !$p2.out.empty?
839 buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
840 assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
844 assert @buoy.is_new_game?("buoy_hoge-1500-0")
845 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
848 assert @buoy.is_new_game?("buoy_hoge-1500-0")
849 assert !$p1.out.empty?
850 assert !$p2.out.empty?
853 def test_call_error_not_buoy_game_name
854 assert @buoy.is_new_game?("buoy_hoge-1500-0")
855 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
858 assert $p1.out.empty?
859 assert $p2.out.empty?
860 assert @buoy.is_new_game?("buoy_hoge-1500-0")
863 def test_call_error_duplicated_game_name
864 assert @buoy.is_new_game?("buoy_duplicated-1500-0")
865 bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1)
867 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
869 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
872 assert $p1.out.empty?
873 assert $p2.out.empty?
874 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
877 def test_call_error_bad_moves
878 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
879 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
882 assert $p1.out.empty?
883 assert $p2.out.empty?
884 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
887 def test_call_error_bad_counter
888 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
889 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
892 assert $p1.out.empty?
893 assert $p2.out.empty?
894 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
901 class TestDeleteBuoyCommand < BaseTestBuoyCommand
903 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
904 assert @buoy.is_new_game?(buoy_game.game_name)
905 @buoy.add_game buoy_game
906 assert !@buoy.is_new_game?(buoy_game.game_name)
907 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
910 assert $p1.out.empty?
911 assert $p2.out.empty?
912 assert @buoy.is_new_game?(buoy_game.game_name)
915 def test_call_not_exist
916 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
917 assert @buoy.is_new_game?(buoy_game.game_name)
918 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
921 assert $p1.out.empty?
922 assert $p2.out.empty?
923 assert @buoy.is_new_game?(buoy_game.game_name)
926 def test_call_another_player
927 buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1)
928 assert @buoy.is_new_game?(buoy_game.game_name)
929 @buoy.add_game(buoy_game)
930 assert !@buoy.is_new_game?(buoy_game.game_name)
932 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
935 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
936 assert !@buoy.is_new_game?(buoy_game.game_name)
942 class TestGetBuoyCountCommand < BaseTestBuoyCommand
944 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
945 assert @buoy.is_new_game?(buoy_game.game_name)
946 @buoy.add_game buoy_game
947 assert !@buoy.is_new_game?(buoy_game.game_name)
948 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
951 assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
954 def test_call_not_exist
955 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
956 assert @buoy.is_new_game?(buoy_game.game_name)
957 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
960 assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
966 class TestMonitorHandler < Test::Unit::TestCase
967 def test_not_equal_players
968 @player1 = MockPlayer.new
969 @handler1 = ShogiServer::MonitorHandler1.new @player1
970 @player2 = MockPlayer.new
971 @handler2 = ShogiServer::MonitorHandler1.new @player2
973 assert_not_equal(@handler1, @handler2)
977 @player1 = MockPlayer.new
978 @handler1 = ShogiServer::MonitorHandler1.new @player1
979 @handler2 = ShogiServer::MonitorHandler1.new @player1
981 assert_equal(@handler1, @handler2)
987 class TestMonitorHandler1 < Test::Unit::TestCase
989 @player = MockPlayer.new
990 @handler = ShogiServer::MonitorHandler1.new @player
994 assert_equal(1, @handler.type)
998 assert_equal("MONITOR", @handler.header)
1002 assert_equal @handler, @handler
1003 assert_not_equal @handler, nil
1007 assert_not_equal(@handler, ShogiServer::MonitorHandler2.new(@player))
1011 @handler.write_safe("game_id", "hoge")
1012 assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n",
1019 class TestMonitorHandler2 < Test::Unit::TestCase
1021 @player = MockPlayer.new
1022 @handler = ShogiServer::MonitorHandler2.new @player
1026 assert_equal(2, @handler.type)
1030 assert_equal("MONITOR2", @handler.header)
1034 assert_equal @handler, @handler
1035 assert_not_equal @handler, nil
1039 assert_not_equal(@handler, ShogiServer::MonitorHandler1.new(@player))
1043 @handler.write_safe("game_id", "hoge")
1044 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n",
1048 def test_write_safe2
1049 @handler.write_safe("game_id", "hoge\nfoo")
1050 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n",