1 $:.unshift File.join(File.dirname(__FILE__), "..")
3 require 'shogi_server/login'
4 require 'shogi_server/player'
5 require 'shogi_server/command'
15 class MockPlayer < ShogiServer::BasicPlayer
17 attr_accessor :game, :status, :protocol
18 attr_accessor :game_name
25 @game_name = "dummy_game_name"
34 attr_accessor :finish_flag
36 attr_accessor :prepared_expire
37 attr_accessor :rejected
38 attr_accessor :is_startable_status
39 attr_accessor :started
40 attr_accessor :game_id
45 @prepared_expire = false
47 @is_startable_status = false
49 @game_id = "dummy_game_id"
50 @monitoron_called = false
51 @monitoroff_called = false
54 def handle_one_move(move, player)
63 return @prepared_expire
70 def is_startable_status?
71 return @is_startable_status
79 return "dummy_game_show"
83 @monitoron_called = true
86 def monitoroff(player)
87 @monitoroff_called = true
94 @games["dummy_game_id"] = MockGame.new
106 return [MockPlayer.new]
111 class TestFactoryMethod < Test::Unit::TestCase
115 $league = MockLeague.new
118 def test_keep_alive_command
119 cmd = ShogiServer::Command.factory("", @p)
120 assert_instance_of(ShogiServer::KeepAliveCommand, cmd)
123 def test_move_command
124 cmd = ShogiServer::Command.factory("+7776FU", @p)
125 assert_instance_of(ShogiServer::MoveCommand, cmd)
128 def test_special_command
129 cmd = ShogiServer::Command.factory("%TORYO", @p)
130 assert_instance_of(ShogiServer::SpecialCommand, cmd)
133 def test_special_command_timeout
134 cmd = ShogiServer::Command.factory(:timeout, @p)
135 assert_instance_of(ShogiServer::SpecialCommand, cmd)
138 def test_execption_command
139 cmd = ShogiServer::Command.factory(:exception, @p)
140 assert_instance_of(ShogiServer::ExceptionCommand, cmd)
143 def test_reject_command
144 cmd = ShogiServer::Command.factory("REJECT", @p)
145 assert_instance_of(ShogiServer::RejectCommand, cmd)
148 def test_agree_command
149 cmd = ShogiServer::Command.factory("AGREE", @p)
150 assert_instance_of(ShogiServer::AgreeCommand, cmd)
153 def test_show_command
154 cmd = ShogiServer::Command.factory("%%SHOW game_id", @p)
155 assert_instance_of(ShogiServer::ShowCommand, cmd)
158 def test_monitoron_command
159 cmd = ShogiServer::Command.factory("%%MONITORON game_id", @p)
160 assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
163 def test_monitoroff_command
164 cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
165 assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
168 def test_help_command
169 cmd = ShogiServer::Command.factory("%%HELP", @p)
170 assert_instance_of(ShogiServer::HelpCommand, cmd)
173 def test_rating_command
174 cmd = ShogiServer::Command.factory("%%RATING", @p)
175 assert_instance_of(ShogiServer::RatingCommand, cmd)
178 def test_version_command
179 cmd = ShogiServer::Command.factory("%%VERSION", @p)
180 assert_instance_of(ShogiServer::VersionCommand, cmd)
183 def test_game_command
184 cmd = ShogiServer::Command.factory("%%GAME", @p)
185 assert_instance_of(ShogiServer::GameCommand, cmd)
188 def test_game_challenge_command_game
189 cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
190 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
193 def test_game_challenge_command_challenge
194 cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
195 assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
198 def test_chat_command
199 cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
200 assert_instance_of(ShogiServer::ChatCommand, cmd)
203 def test_list_command
204 cmd = ShogiServer::Command.factory("%%LIST", @p)
205 assert_instance_of(ShogiServer::ListCommand, cmd)
209 cmd = ShogiServer::Command.factory("%%WHO", @p)
210 assert_instance_of(ShogiServer::WhoCommand, cmd)
213 def test_logout_command
214 cmd = ShogiServer::Command.factory("LOGOUT", @p)
215 assert_instance_of(ShogiServer::LogoutCommand, cmd)
218 def test_challenge_command
219 cmd = ShogiServer::Command.factory("CHALLENGE", @p)
220 assert_instance_of(ShogiServer::ChallengeCommand, cmd)
223 def test_space_command
224 cmd = ShogiServer::Command.factory(" ", @p)
225 assert_instance_of(ShogiServer::SpaceCommand, cmd)
229 cmd = ShogiServer::Command.factory("should_be_error", @p)
230 assert_instance_of(ShogiServer::ErrorCommand, cmd)
236 class TestKeepAliveCommand < Test::Unit::TestCase
242 cmd = ShogiServer::KeepAliveCommand.new("", @p)
244 assert_equal(:continue, rc)
250 class TestMoveCommand < Test::Unit::TestCase
259 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
261 assert_equal(:continue, rc)
265 cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
267 assert_equal(:continue, rc)
268 assert_equal("'*comment", @game.log.first)
272 @game.finish_flag = true
273 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
274 cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
276 assert_equal(:return, rc)
282 class TestSpecialComand < Test::Unit::TestCase
291 @game.finish_flag = true
292 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
294 assert_equal(:continue, rc)
297 def test_toryo_csa_protocol
298 @game.finish_flag = true
299 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
300 cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
302 assert_equal(:return, rc)
306 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
308 assert_equal(:continue, rc)
311 def test_expired_game
312 @p.status = "agree_waiting"
313 @game.prepared_expire = true
314 assert(!@game.rejected)
315 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
317 assert_equal(:continue, rc)
318 assert(@game.rejected)
321 def test_expired_game_csa_protocol
322 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
323 @p.status = "agree_waiting"
324 @game.prepared_expire = true
325 assert(!@game.rejected)
326 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
328 assert_equal(:return, rc)
329 assert(@game.rejected)
333 @p.status = "should_be_ignored"
334 cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
336 assert_equal(:continue, rc)
342 class TestExceptionCommand < Test::Unit::TestCase
348 cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
350 assert_equal(:return, rc)
356 class TestRejectCommand < Test::Unit::TestCase
365 @p.status = "agree_waiting"
366 assert(!@game.rejected)
367 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
370 assert_equal(:continue, rc)
371 assert(@game.rejected)
374 def test_call_csa_protocol
375 @p.protocol = ShogiServer::LoginCSA::PROTOCOL
376 @p.status = "agree_waiting"
377 assert(!@game.rejected)
378 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
381 assert_equal(:return, rc)
382 assert(@game.rejected)
386 @p.status = "should_be_ignored"
387 cmd = ShogiServer::RejectCommand.new("REJECT", @p)
390 assert_equal(:continue, rc)
391 assert(!@game.rejected)
397 class TestAgreeCommand < Test::Unit::TestCase
402 @p.status = "agree_waiting"
405 def test_not_start_yet
406 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
408 assert_equal(:continue, rc)
409 assert(!@game.started)
413 @game.is_startable_status = true
414 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
416 assert_equal(:continue, rc)
417 assert(@game.started)
421 @p.status = "should_be_ignored"
422 cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
424 assert_equal(:continue, rc)
425 assert(!@game.started)
431 class TestShowCommand < Test::Unit::TestCase
439 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
442 assert_equal(:continue, rc)
445 def test_call_nil_game
446 cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
449 assert_equal(:continue, rc)
455 class TestMonitorOnCommand < Test::Unit::TestCase
463 cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
466 assert_equal(:continue, rc)
472 class TestMonitorOffCommand < Test::Unit::TestCase
480 cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
483 assert_equal(:continue, rc)
489 class TestHelpCommand < Test::Unit::TestCase
497 cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
500 assert_equal(:continue, rc)
506 class TestRatingCommand < Test::Unit::TestCase
514 players = [MockPlayer.new]
515 cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
518 assert_equal(:continue, rc)
524 class TestVersionCommand < Test::Unit::TestCase
532 cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
535 assert_equal(:continue, rc)
541 class TestGameCommand < Test::Unit::TestCase
548 def test_call_connected
549 @p.status = "connected"
550 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
553 assert_equal(:continue, rc)
554 assert_equal("connected", @p.status)
557 def test_call_game_waiting
558 @p.status = "game_waiting"
559 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
562 assert_equal(:continue, rc)
563 assert_equal("connected", @p.status)
566 def test_call_agree_waiting
567 @p.status = "agree_waiting"
568 cmd = ShogiServer::GameCommand.new("%%GAME", @p)
571 assert_equal(:continue, rc)
572 assert_equal("agree_waiting", @p.status)
578 class TestChatCommand < Test::Unit::TestCase
586 players = [["dummy_name", MockPlayer.new]]
587 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
590 assert_equal(:continue, rc)
593 def test_call_csa_protocol
594 players = [["dummy_name", MockPlayer.new]]
595 players.each do |name, p|
596 p.protocol = ShogiServer::LoginCSA::PROTOCOL
598 cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
601 assert_equal(:continue, rc)
607 class TestListCommand < Test::Unit::TestCase
615 games = [["dummy_game_id", MockGame.new]]
616 cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
619 assert_equal(:continue, rc)
626 class TestWhoCommand < Test::Unit::TestCase
634 players = [["dummy_name", MockPlayer.new]]
635 cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
638 assert_equal(:continue, rc)
645 class TestLogoutCommand < Test::Unit::TestCase
653 cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
656 assert_equal(:return, rc)
663 class TestChallengeCommand < Test::Unit::TestCase
670 cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
673 assert_equal(:continue, rc)
679 class TestSpaceCommand < Test::Unit::TestCase
686 cmd = ShogiServer::SpaceCommand.new("", @p)
689 assert_equal(:continue, rc)
695 class TestErrorCommand < Test::Unit::TestCase
702 cmd = ShogiServer::ErrorCommand.new("", @p)
705 assert_equal(:continue, rc)