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)
205 def test_void_command
206 cmd = ShogiServer::Command.factory("%%%HOGE", @p)
207 assert_instance_of(ShogiServer::VoidCommand, cmd)
211 cmd = ShogiServer::Command.factory("should_be_error", @p)
212 assert_instance_of(ShogiServer::ErrorCommand, cmd)
218 class TestKeepAliveCommand < Test::Unit::TestCase
224 cmd = ShogiServer::KeepAliveCommand.new("", @p)
226 assert_equal(:continue, rc)
232 class TestMoveCommand < Test::Unit::TestCase
241 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
243 assert_equal(:continue, rc)
247 cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
249 assert_equal(:continue, rc)
250 assert_equal("'*comment", @game.log.first)
254 @game.finish_flag = true
255 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
256 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
258 assert_equal(:return, rc)
264 class TestSpecialComand < Test::Unit::TestCase
273 @game.finish_flag = true
274 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
276 assert_equal(:continue, rc)
279 def test_toryo_csa_protocol
280 @game.finish_flag = true
281 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
282 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
284 assert_equal(:return, rc)
288 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
290 assert_equal(:continue, rc)
293 def test_expired_game
294 @p.status = "agree_waiting"
295 @game.prepared_expire = true
296 assert(!@game.rejected)
297 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
299 assert_equal(:continue, rc)
300 assert(@game.rejected)
303 def test_expired_game_csa_protocol
304 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
305 @p.status = "agree_waiting"
306 @game.prepared_expire = true
307 assert(!@game.rejected)
308 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
310 assert_equal(:return, rc)
311 assert(@game.rejected)
315 @p.status = "should_be_ignored"
316 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
318 assert_equal(:continue, rc)
324 class TestExceptionCommand < Test::Unit::TestCase
330 cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
332 assert_equal(:return, rc)
338 class TestRejectCommand < Test::Unit::TestCase
347 @p.status = "agree_waiting"
348 assert(!@game.rejected)
349 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
352 assert_equal(:continue, rc)
353 assert(@game.rejected)
356 def test_call_csa_protocol
357 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
358 @p.status = "agree_waiting"
359 assert(!@game.rejected)
360 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
363 assert_equal(:return, rc)
364 assert(@game.rejected)
368 @p.status = "should_be_ignored"
369 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
372 assert_equal(:continue, rc)
373 assert(!@game.rejected)
379 class TestAgreeCommand < Test::Unit::TestCase
384 @p.status = "agree_waiting"
387 def test_not_start_yet
388 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
390 assert_equal(:continue, rc)
391 assert(!@game.started)
395 @game.is_startable_status = true
396 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
398 assert_equal(:continue, rc)
399 assert(@game.started)
403 @p.status = "should_be_ignored"
404 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
406 assert_equal(:continue, rc)
407 assert(!@game.started)
413 class TestShowCommand < Test::Unit::TestCase
421 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
424 assert_equal(:continue, rc)
427 def test_call_nil_game
428 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
431 assert_equal(:continue, rc)
437 class TestMonitorOnCommand < Test::Unit::TestCase
445 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
448 assert_equal(:continue, rc)
451 def test_call_read_logfile
453 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
455 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)
456 assert_equal(:continue, rc)
462 class TestMonitor2OnCommand < Test::Unit::TestCase
470 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
473 assert_equal(:continue, rc)
476 def test_call_read_logfile
477 $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
478 $tempfile.write "hoge\nfoo\n"
484 cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
486 assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
487 assert_equal(:continue, rc)
494 class TestMonitorOffCommand < Test::Unit::TestCase
502 cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
505 assert_equal(:continue, rc)
511 class TestMonitor2OffCommand < Test::Unit::TestCase
519 cmd = ShogiServer::Monitor2OffCommand.new("%%MONITOR2OFF hoge", @p, nil)
522 assert_equal(:continue, rc)
528 class TestHelpCommand < Test::Unit::TestCase
536 cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
539 assert_equal(:continue, rc)
545 class TestRatingCommand < Test::Unit::TestCase
553 players = [MockPlayer.new]
554 cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
557 assert_equal(:continue, rc)
563 class TestVersionCommand < Test::Unit::TestCase
571 cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
574 assert_equal(:continue, rc)
580 class TestGameCommand < Test::Unit::TestCase
587 def test_call_connected
588 @p.status = "connected"
589 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
592 assert_equal(:continue, rc)
593 assert_equal("connected", @p.status)
596 def test_call_game_waiting
597 @p.status = "game_waiting"
598 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
601 assert_equal(:continue, rc)
602 assert_equal("connected", @p.status)
605 def test_call_agree_waiting
606 @p.status = "agree_waiting"
607 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
610 assert_equal(:continue, rc)
611 assert_equal("agree_waiting", @p.status)
617 class TestChatCommand < Test::Unit::TestCase
625 players = [["dummy_name", MockPlayer.new]]
626 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
629 assert_equal(:continue, rc)
632 def test_call_csa_protocol
633 players = [["dummy_name", MockPlayer.new]]
634 players.each do |name, p|
635 p.protocol = ShogiServer::LoginCSA::PROTOCOL
637 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
640 assert_equal(:continue, rc)
646 class TestListCommand < Test::Unit::TestCase
654 games = [["dummy_game_id", MockGame.new]]
655 cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
658 assert_equal(:continue, rc)
665 class TestWhoCommand < Test::Unit::TestCase
673 players = [["dummy_name", MockPlayer.new]]
674 cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
677 assert_equal(:continue, rc)
684 class TestLogoutCommand < Test::Unit::TestCase
692 cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
695 assert_equal(:return, rc)
702 class TestChallengeCommand < Test::Unit::TestCase
709 cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
712 assert_equal(:continue, rc)
718 class TestSpaceCommand < Test::Unit::TestCase
725 cmd = ShogiServer::SpaceCommand.new("", @p)
728 assert_equal(:continue, rc)
734 class TestErrorCommand < Test::Unit::TestCase
741 cmd = ShogiServer::ErrorCommand.new("", @p)
744 assert_equal(:continue, rc)
748 class BaseTestBuoyCommand < Test::Unit::TestCase
755 @buoy = ShogiServer::Buoy.new
763 file = File.join($topdir, "buoy.yaml")
764 File.delete file if File.exist? file
775 class TestSetBuoyCommand < BaseTestBuoyCommand
779 @p.name = "set_buoy_player"
783 assert @buoy.is_new_game?("buoy_hoge-1500-0")
784 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
787 assert !@buoy.is_new_game?("buoy_hoge-1500-0")
788 assert !$p1.out.empty?
789 assert !$p2.out.empty?
790 buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
791 assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
795 assert @buoy.is_new_game?("buoy_hoge-1500-0")
796 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
799 assert @buoy.is_new_game?("buoy_hoge-1500-0")
800 assert !$p1.out.empty?
801 assert !$p2.out.empty?
804 def test_call_error_not_buoy_game_name
805 assert @buoy.is_new_game?("buoy_hoge-1500-0")
806 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
811 assert @buoy.is_new_game?("buoy_hoge-1500-0")
814 def test_call_error_duplicated_game_name
815 assert @buoy.is_new_game?("buoy_duplicated-1500-0")
816 bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1)
818 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
820 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
825 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
828 def test_call_error_bad_moves
829 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
830 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
835 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
838 def test_call_error_bad_counter
839 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
840 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
845 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
852 class TestDeleteBuoyCommand < BaseTestBuoyCommand
854 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
855 assert @buoy.is_new_game?(buoy_game.game_name)
856 @buoy.add_game buoy_game
857 assert !@buoy.is_new_game?(buoy_game.game_name)
858 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
863 assert @buoy.is_new_game?(buoy_game.game_name)
866 def test_call_not_exist
867 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
868 assert @buoy.is_new_game?(buoy_game.game_name)
869 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
874 assert @buoy.is_new_game?(buoy_game.game_name)
877 def test_call_another_player
878 buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1)
879 assert @buoy.is_new_game?(buoy_game.game_name)
880 @buoy.add_game(buoy_game)
881 assert !@buoy.is_new_game?(buoy_game.game_name)
883 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
886 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
887 assert !@buoy.is_new_game?(buoy_game.game_name)
893 class TestGetBuoyCountCommand < BaseTestBuoyCommand
895 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
896 assert @buoy.is_new_game?(buoy_game.game_name)
897 @buoy.add_game buoy_game
898 assert !@buoy.is_new_game?(buoy_game.game_name)
899 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
902 assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
905 def test_call_not_exist
906 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
907 assert @buoy.is_new_game?(buoy_game.game_name)
908 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
911 assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
917 class TestMonitorHandler < Test::Unit::TestCase
918 def test_not_equal_players
919 @player1 = MockPlayer.new
920 @handler1 = ShogiServer::MonitorHandler1.new @player1
921 @player2 = MockPlayer.new
922 @handler2 = ShogiServer::MonitorHandler1.new @player2
924 assert_not_equal(@handler1, @handler2)
928 @player1 = MockPlayer.new
929 @handler1 = ShogiServer::MonitorHandler1.new @player1
930 @handler2 = ShogiServer::MonitorHandler1.new @player1
932 assert_equal(@handler1, @handler2)
938 class TestMonitorHandler1 < Test::Unit::TestCase
940 @player = MockPlayer.new
941 @handler = ShogiServer::MonitorHandler1.new @player
945 assert_equal(1, @handler.type)
949 assert_equal("MONITOR", @handler.header)
953 assert_equal @handler, @handler
954 assert_not_equal @handler, nil
958 assert_not_equal(@handler, ShogiServer::MonitorHandler2.new(@player))
962 @handler.write_safe("game_id", "hoge")
963 assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n",
970 class TestMonitorHandler2 < Test::Unit::TestCase
972 @player = MockPlayer.new
973 @handler = ShogiServer::MonitorHandler2.new @player
977 assert_equal(2, @handler.type)
981 assert_equal("MONITOR2", @handler.header)
985 assert_equal @handler, @handler
986 assert_not_equal @handler, nil
990 assert_not_equal(@handler, ShogiServer::MonitorHandler1.new(@player))
994 @handler.write_safe("game_id", "hoge")
995 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n",
1000 @handler.write_safe("game_id", "hoge\nfoo")
1001 assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n",