1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 $topdir = File.expand_path File.dirname(__FILE__)
5 require 'mock_log_message'
6 require 'test/mock_player'
7 require 'shogi_server/login'
8 require 'shogi_server/player'
9 require 'shogi_server/command'
15 @games["dummy_game_id"] = MockGame.new
27 return [MockPlayer.new]
38 def get_player(status, game_id, sente, searcher)
56 class TestFactoryMethod < Test::Unit::TestCase
60 @p.name = "test_factory_method_player"
61 $league = MockLeague.new
64 def test_keep_alive_command
65 cmd = ShogiServer::Command.factory("", @p)
66 assert_instance_of(ShogiServer::KeepAliveCommand, cmd)
70 cmd = ShogiServer::Command.factory("+7776FU", @p)
71 assert_instance_of(ShogiServer::MoveCommand, cmd)
74 def test_special_command
75 cmd = ShogiServer::Command.factory("%TORYO", @p)
76 assert_instance_of(ShogiServer::SpecialCommand, cmd)
79 def test_special_command_timeout
80 cmd = ShogiServer::Command.factory(:timeout, @p)
81 assert_instance_of(ShogiServer::SpecialCommand, cmd)
84 def test_execption_command
85 cmd = ShogiServer::Command.factory(:exception, @p)
86 assert_instance_of(ShogiServer::ExceptionCommand, cmd)
89 def test_reject_command
90 cmd = ShogiServer::Command.factory("REJECT", @p)
91 assert_instance_of(ShogiServer::RejectCommand, cmd)
94 def test_agree_command
95 cmd = ShogiServer::Command.factory("AGREE", @p)
96 assert_instance_of(ShogiServer::AgreeCommand, cmd)
100 cmd = ShogiServer::Command.factory("%%SHOW game_id", @p)
101 assert_instance_of(ShogiServer::ShowCommand, cmd)
104 def test_monitoron_command
105 cmd = ShogiServer::Command.factory("%%MONITORON game_id", @p)
106 assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
109 def test_monitoroff_command
110 cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
111 assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
114 def test_help_command
115 cmd = ShogiServer::Command.factory("%%HELP", @p)
116 assert_instance_of(ShogiServer::HelpCommand, cmd)
119 def test_rating_command
120 cmd = ShogiServer::Command.factory("%%RATING", @p)
121 assert_instance_of(ShogiServer::RatingCommand, cmd)
124 def test_version_command
125 cmd = ShogiServer::Command.factory("%%VERSION", @p)
126 assert_instance_of(ShogiServer::VersionCommand, cmd)
129 def test_game_command
130 cmd = ShogiServer::Command.factory("%%GAME", @p)
131 assert_instance_of(ShogiServer::GameCommand, cmd)
134 def test_game_challenge_command_game
135 cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
136 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
139 def test_game_challenge_command_challenge
140 cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
141 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
144 def test_chat_command
145 cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
146 assert_instance_of(ShogiServer::ChatCommand, cmd)
149 def test_list_command
150 cmd = ShogiServer::Command.factory("%%LIST", @p)
151 assert_instance_of(ShogiServer::ListCommand, cmd)
155 cmd = ShogiServer::Command.factory("%%WHO", @p)
156 assert_instance_of(ShogiServer::WhoCommand, cmd)
159 def test_logout_command
160 cmd = ShogiServer::Command.factory("LOGOUT", @p)
161 assert_instance_of(ShogiServer::LogoutCommand, cmd)
164 def test_challenge_command
165 cmd = ShogiServer::Command.factory("CHALLENGE", @p)
166 assert_instance_of(ShogiServer::ChallengeCommand, cmd)
169 def test_space_command
170 cmd = ShogiServer::Command.factory(" ", @p)
171 assert_instance_of(ShogiServer::SpaceCommand, cmd)
174 def test_setbuoy_command
175 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU", @p)
176 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
179 def test_setbuoy_command_with_counter
180 cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU 3", @p)
181 assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
184 def test_deletebuoy_command
185 cmd = ShogiServer::Command.factory("%%DELETEBUOY buoy_test-1500-0", @p)
186 assert_instance_of(ShogiServer::DeleteBuoyCommand, cmd)
189 def test_getbuoycount_command
190 cmd = ShogiServer::Command.factory("%%GETBUOYCOUNT buoy_test-1500-0", @p)
191 assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
195 cmd = ShogiServer::Command.factory("should_be_error", @p)
196 assert_instance_of(ShogiServer::ErrorCommand, cmd)
202 class TestKeepAliveCommand < Test::Unit::TestCase
208 cmd = ShogiServer::KeepAliveCommand.new("", @p)
210 assert_equal(:continue, rc)
216 class TestMoveCommand < Test::Unit::TestCase
225 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
227 assert_equal(:continue, rc)
231 cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
233 assert_equal(:continue, rc)
234 assert_equal("'*comment", @game.log.first)
238 @game.finish_flag = true
239 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
240 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
242 assert_equal(:return, rc)
248 class TestSpecialComand < Test::Unit::TestCase
257 @game.finish_flag = true
258 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
260 assert_equal(:continue, rc)
263 def test_toryo_csa_protocol
264 @game.finish_flag = true
265 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
266 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
268 assert_equal(:return, rc)
272 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
274 assert_equal(:continue, rc)
277 def test_expired_game
278 @p.status = "agree_waiting"
279 @game.prepared_expire = true
280 assert(!@game.rejected)
281 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
283 assert_equal(:continue, rc)
284 assert(@game.rejected)
287 def test_expired_game_csa_protocol
288 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
289 @p.status = "agree_waiting"
290 @game.prepared_expire = true
291 assert(!@game.rejected)
292 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
294 assert_equal(:return, rc)
295 assert(@game.rejected)
299 @p.status = "should_be_ignored"
300 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
302 assert_equal(:continue, rc)
308 class TestExceptionCommand < Test::Unit::TestCase
314 cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
316 assert_equal(:return, rc)
322 class TestRejectCommand < Test::Unit::TestCase
331 @p.status = "agree_waiting"
332 assert(!@game.rejected)
333 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
336 assert_equal(:continue, rc)
337 assert(@game.rejected)
340 def test_call_csa_protocol
341 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
342 @p.status = "agree_waiting"
343 assert(!@game.rejected)
344 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
347 assert_equal(:return, rc)
348 assert(@game.rejected)
352 @p.status = "should_be_ignored"
353 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
356 assert_equal(:continue, rc)
357 assert(!@game.rejected)
363 class TestAgreeCommand < Test::Unit::TestCase
368 @p.status = "agree_waiting"
371 def test_not_start_yet
372 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
374 assert_equal(:continue, rc)
375 assert(!@game.started)
379 @game.is_startable_status = true
380 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
382 assert_equal(:continue, rc)
383 assert(@game.started)
387 @p.status = "should_be_ignored"
388 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
390 assert_equal(:continue, rc)
391 assert(!@game.started)
397 class TestShowCommand < Test::Unit::TestCase
405 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
408 assert_equal(:continue, rc)
411 def test_call_nil_game
412 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
415 assert_equal(:continue, rc)
421 class TestMonitorOnCommand < Test::Unit::TestCase
429 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
432 assert_equal(:continue, rc)
438 class TestMonitorOffCommand < Test::Unit::TestCase
446 cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
449 assert_equal(:continue, rc)
455 class TestHelpCommand < Test::Unit::TestCase
463 cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
466 assert_equal(:continue, rc)
472 class TestRatingCommand < Test::Unit::TestCase
480 players = [MockPlayer.new]
481 cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
484 assert_equal(:continue, rc)
490 class TestVersionCommand < Test::Unit::TestCase
498 cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
501 assert_equal(:continue, rc)
507 class TestGameCommand < Test::Unit::TestCase
514 def test_call_connected
515 @p.status = "connected"
516 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
519 assert_equal(:continue, rc)
520 assert_equal("connected", @p.status)
523 def test_call_game_waiting
524 @p.status = "game_waiting"
525 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
528 assert_equal(:continue, rc)
529 assert_equal("connected", @p.status)
532 def test_call_agree_waiting
533 @p.status = "agree_waiting"
534 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
537 assert_equal(:continue, rc)
538 assert_equal("agree_waiting", @p.status)
544 class TestChatCommand < Test::Unit::TestCase
552 players = [["dummy_name", MockPlayer.new]]
553 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
556 assert_equal(:continue, rc)
559 def test_call_csa_protocol
560 players = [["dummy_name", MockPlayer.new]]
561 players.each do |name, p|
562 p.protocol = ShogiServer::LoginCSA::PROTOCOL
564 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
567 assert_equal(:continue, rc)
573 class TestListCommand < Test::Unit::TestCase
581 games = [["dummy_game_id", MockGame.new]]
582 cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
585 assert_equal(:continue, rc)
592 class TestWhoCommand < Test::Unit::TestCase
600 players = [["dummy_name", MockPlayer.new]]
601 cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
604 assert_equal(:continue, rc)
611 class TestLogoutCommand < Test::Unit::TestCase
619 cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
622 assert_equal(:return, rc)
629 class TestChallengeCommand < Test::Unit::TestCase
636 cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
639 assert_equal(:continue, rc)
645 class TestSpaceCommand < Test::Unit::TestCase
652 cmd = ShogiServer::SpaceCommand.new("", @p)
655 assert_equal(:continue, rc)
661 class TestErrorCommand < Test::Unit::TestCase
668 cmd = ShogiServer::ErrorCommand.new("", @p)
671 assert_equal(:continue, rc)
675 class BaseTestBuoyCommand < Test::Unit::TestCase
682 @buoy = ShogiServer::Buoy.new
690 file = File.join($topdir, "buoy.yaml")
691 File.delete file if File.exist? file
702 class TestSetBuoyCommand < BaseTestBuoyCommand
706 @p.name = "set_buoy_player"
710 assert @buoy.is_new_game?("buoy_hoge-1500-0")
711 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
714 assert !@buoy.is_new_game?("buoy_hoge-1500-0")
715 assert !$p1.out.empty?
716 assert !$p2.out.empty?
717 buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
718 assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
722 assert @buoy.is_new_game?("buoy_hoge-1500-0")
723 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
726 assert @buoy.is_new_game?("buoy_hoge-1500-0")
727 assert !$p1.out.empty?
728 assert !$p2.out.empty?
731 def test_call_error_not_buoy_game_name
732 assert @buoy.is_new_game?("buoy_hoge-1500-0")
733 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
738 assert @buoy.is_new_game?("buoy_hoge-1500-0")
741 def test_call_error_duplicated_game_name
742 assert @buoy.is_new_game?("buoy_duplicated-1500-0")
743 bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1)
745 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
747 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
752 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
755 def test_call_error_bad_moves
756 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
757 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
762 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
765 def test_call_error_bad_counter
766 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
767 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
772 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
779 class TestDeleteBuoyCommand < BaseTestBuoyCommand
781 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
782 assert @buoy.is_new_game?(buoy_game.game_name)
783 @buoy.add_game buoy_game
784 assert !@buoy.is_new_game?(buoy_game.game_name)
785 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
790 assert @buoy.is_new_game?(buoy_game.game_name)
793 def test_call_not_exist
794 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
795 assert @buoy.is_new_game?(buoy_game.game_name)
796 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
801 assert @buoy.is_new_game?(buoy_game.game_name)
804 def test_call_another_player
805 buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1)
806 assert @buoy.is_new_game?(buoy_game.game_name)
807 @buoy.add_game(buoy_game)
808 assert !@buoy.is_new_game?(buoy_game.game_name)
810 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
813 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
814 assert !@buoy.is_new_game?(buoy_game.game_name)
820 class TestGetBuoyCountCommand < BaseTestBuoyCommand
822 buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
823 assert @buoy.is_new_game?(buoy_game.game_name)
824 @buoy.add_game buoy_game
825 assert !@buoy.is_new_game?(buoy_game.game_name)
826 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
829 assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
832 def test_call_not_exist
833 buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
834 assert @buoy.is_new_game?(buoy_game.game_name)
835 cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
838 assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out