$:.unshift File.join(File.dirname(__FILE__), "..")
$topdir = File.expand_path File.dirname(__FILE__)
require 'test/unit'
-require 'mock_game'
-require 'mock_log_message'
+require 'tempfile'
+require 'test/mock_game'
+require 'test/mock_log_message'
require 'test/mock_player'
require 'shogi_server/login'
require 'shogi_server/player'
def initialize
@games = {}
@games["dummy_game_id"] = MockGame.new
+
+ reset_players
+ end
+
+ def reset_players
+ $p1 = MockPlayer.new
+ $p1.name = "p1"
+ $p1.status = "game_waiting"
+ $p1.sente = true
+ $p2 = MockPlayer.new
+ $p2.name = "p2"
+ $p2.status = "game_waiting"
+ $p2.sente = false
end
def games
def get_player(status, game_id, sente, searcher)
if sente == true
- $p1 = MockPlayer.new
- $p1.name = "p1"
return $p1
elsif sente == false
- $p2 = MockPlayer.new
- $p2.name = "p2"
return $p2
elsif sente == nil
return nil
return nil
end
end
+
+ def find_all_players
+ [$p1,$p2].each {|pp| yield pp}
+ end
+
+ def find_rival(player, game_name)
+ case player.sente
+ when nil # no preference
+ return get_player("game_waiting", game_name, nil, player)
+ when true # rival must be gote
+ return get_player("game_waiting", game_name, false, player)
+ when false # rival must be sente
+ return get_player("game_waiting", game_name, true, player)
+ else
+ return :continue
+ end
+ end
end
assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
end
+ def test_monitor2on_command
+ cmd = ShogiServer::Command.factory("%%MONITOR2ON game_id", @p)
+ assert_instance_of(ShogiServer::Monitor2OnCommand, cmd)
+ end
+
def test_monitoroff_command
cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
end
+ def test_monitor2off_command
+ cmd = ShogiServer::Command.factory("%%MONITOR2OFF game_id", @p)
+ assert_instance_of(ShogiServer::Monitor2OffCommand, cmd)
+ end
+
def test_help_command
cmd = ShogiServer::Command.factory("%%HELP", @p)
assert_instance_of(ShogiServer::HelpCommand, cmd)
assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
end
+ def test_void_command
+ cmd = ShogiServer::Command.factory("%%%HOGE", @p)
+ assert_instance_of(ShogiServer::VoidCommand, cmd)
+ end
+
def test_error
cmd = ShogiServer::Command.factory("should_be_error", @p)
assert_instance_of(ShogiServer::ErrorCommand, cmd)
+ cmd.call
+ assert_match /unknown command should_be_error/, cmd.msg
+ end
+
+ def test_error_login
+ cmd = ShogiServer::Command.factory("LOGIN hoge foo", @p)
+ assert_instance_of(ShogiServer::ErrorCommand, cmd)
+ cmd.call
+ assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
+
+ cmd = ShogiServer::Command.factory("LOGin hoge foo", @p)
+ assert_instance_of(ShogiServer::ErrorCommand, cmd)
+ cmd.call
+ assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
+
+ cmd = ShogiServer::Command.factory("LOGIN hoge foo", @p)
+ assert_instance_of(ShogiServer::ErrorCommand, cmd)
+ cmd.call
+ assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
+
+ cmd = ShogiServer::Command.factory("LOGINhoge foo", @p)
+ assert_instance_of(ShogiServer::ErrorCommand, cmd)
+ cmd.call
+ assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
end
end
assert_equal(:continue, rc)
end
+
+ def test_call_read_logfile
+ game = MockGame.new
+ cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
+ rc = cmd.call
+ 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)
+ assert_equal(:continue, rc)
+ end
+end
+
+#
+#
+class TestMonitor2OnCommand < Test::Unit::TestCase
+ def setup
+ @p = MockPlayer.new
+ @game = MockGame.new
+ @p.game = @game
+ end
+
+ def test_call
+ cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
+ rc = cmd.call
+
+ assert_equal(:continue, rc)
+ end
+
+ def test_call_read_logfile
+ $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
+ $tempfile.write "hoge\nfoo\n"
+ $tempfile.close
+ game = MockGame.new
+ def game.logfile
+ $tempfile.path
+ end
+ cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
+ rc = cmd.call
+ assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
+ assert_equal(:continue, rc)
+ $tempfile = nil
+ end
end
#
#
#
+class TestMonitor2OffCommand < Test::Unit::TestCase
+ def setup
+ @p = MockPlayer.new
+ @game = MockGame.new
+ @p.game = @game
+ end
+
+ def test_call
+ cmd = ShogiServer::Monitor2OffCommand.new("%%MONITOR2OFF hoge", @p, nil)
+ rc = cmd.call
+
+ assert_equal(:continue, rc)
+ end
+end
+
+#
+#
class TestHelpCommand < Test::Unit::TestCase
def setup
@p = MockPlayer.new
class BaseTestBuoyCommand < Test::Unit::TestCase
def setup
@p = MockPlayer.new
- $p1 = nil
- $p2 = nil
+ $league = MockLeague.new
delete_buoy_yaml
@buoy = ShogiServer::Buoy.new
assert @buoy.is_new_game?("buoy_hoge-1500-0")
cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
rt = cmd.call
- assert :continue, rt
+ assert_equal :continue, rt
assert !@buoy.is_new_game?("buoy_hoge-1500-0")
assert !$p1.out.empty?
assert !$p2.out.empty?
assert @buoy.is_new_game?("buoy_hoge-1500-0")
cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
rt = cmd.call
- assert :continue, rt
+ assert_equal :continue, rt
assert @buoy.is_new_game?("buoy_hoge-1500-0")
assert !$p1.out.empty?
assert !$p2.out.empty?
assert @buoy.is_new_game?("buoy_hoge-1500-0")
cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
rt = cmd.call
- assert :continue, rt
- assert !$p1
- assert !$p2
+ assert_equal :continue, rt
+ assert $p1.out.empty?
+ assert $p2.out.empty?
assert @buoy.is_new_game?("buoy_hoge-1500-0")
end
cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
rt = cmd.call
- assert :continue, rt
- assert !$p1
- assert !$p2
+ assert_equal :continue, rt
+ assert $p1.out.empty?
+ assert $p2.out.empty?
assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
end
assert @buoy.is_new_game?("buoy_badmoves-1500-0")
cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
rt = cmd.call
- assert :continue, rt
- assert !$p1
- assert !$p2
+ assert_equal :continue, rt
+ assert $p1.out.empty?
+ assert $p2.out.empty?
assert @buoy.is_new_game?("buoy_badmoves-1500-0")
end
assert @buoy.is_new_game?("buoy_badcounter-1500-0")
cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
rt = cmd.call
- assert :continue, rt
- assert !$p1
- assert !$p2
+ assert_equal :continue, rt
+ assert $p1.out.empty?
+ assert $p2.out.empty?
assert @buoy.is_new_game?("buoy_badcounter-1500-0")
end
end
assert !@buoy.is_new_game?(buoy_game.game_name)
cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
rt = cmd.call
- assert :continue, rt
- assert !$p1
- assert !$p2
+ assert_equal :continue, rt
+ assert $p1.out.empty?
+ assert $p2.out.empty?
assert @buoy.is_new_game?(buoy_game.game_name)
end
assert @buoy.is_new_game?(buoy_game.game_name)
cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
rt = cmd.call
- assert :continue, rt
- assert !$p1
- assert !$p2
+ assert_equal :continue, rt
+ assert $p1.out.empty?
+ assert $p2.out.empty?
assert @buoy.is_new_game?(buoy_game.game_name)
end
cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
rt = cmd.call
- assert :continue, rt
+ assert_equal :continue, rt
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
assert !@buoy.is_new_game?(buoy_game.game_name)
end
assert !@buoy.is_new_game?(buoy_game.game_name)
cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
rt = cmd.call
- assert :continue, rt
+ assert_equal :continue, rt
assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
end
assert @buoy.is_new_game?(buoy_game.game_name)
cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
rt = cmd.call
- assert :continue, rt
+ assert_equal :continue, rt
assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
end
end
+#
+#
+class TestMonitorHandler < Test::Unit::TestCase
+ def test_not_equal_players
+ @player1 = MockPlayer.new
+ @handler1 = ShogiServer::MonitorHandler1.new @player1
+ @player2 = MockPlayer.new
+ @handler2 = ShogiServer::MonitorHandler1.new @player2
+
+ assert_not_equal(@handler1, @handler2)
+ end
+
+ def test_equal
+ @player1 = MockPlayer.new
+ @handler1 = ShogiServer::MonitorHandler1.new @player1
+ @handler2 = ShogiServer::MonitorHandler1.new @player1
+
+ assert_equal(@handler1, @handler2)
+ end
+end
+
+#
+#
+class TestMonitorHandler1 < Test::Unit::TestCase
+ def setup
+ @player = MockPlayer.new
+ @handler = ShogiServer::MonitorHandler1.new @player
+ end
+
+ def test_type
+ assert_equal(1, @handler.type)
+ end
+
+ def test_header
+ assert_equal("MONITOR", @handler.header)
+ end
+
+ def test_equal
+ assert_equal @handler, @handler
+ assert_not_equal @handler, nil
+ end
+
+ def test_not_equal
+ assert_not_equal(@handler, ShogiServer::MonitorHandler2.new(@player))
+ end
+
+ def test_write_safe
+ @handler.write_safe("game_id", "hoge")
+ assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n",
+ @player.out.join)
+ end
+end
+
+#
+#
+class TestMonitorHandler2 < Test::Unit::TestCase
+ def setup
+ @player = MockPlayer.new
+ @handler = ShogiServer::MonitorHandler2.new @player
+ end
+
+ def test_type
+ assert_equal(2, @handler.type)
+ end
+
+ def test_header
+ assert_equal("MONITOR2", @handler.header)
+ end
+
+ def test_equal
+ assert_equal @handler, @handler
+ assert_not_equal @handler, nil
+ end
+
+ def test_not_equal
+ assert_not_equal(@handler, ShogiServer::MonitorHandler1.new(@player))
+ end
+
+ def test_write_safe
+ @handler.write_safe("game_id", "hoge")
+ assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n",
+ @player.out.join)
+ end
+
+ def test_write_safe2
+ @handler.write_safe("game_id", "hoge\nfoo")
+ assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n",
+ @player.out.join)
+ end
+end