OSDN Git Service

Backported 87d145bd1f1a14a33f5f6fbc78b63a1952f1ca90 and 2df8c798aeb7f0e77735e893fd137...
[shogi-server/shogi-server.git] / test / TC_command.rb
index 63d88e4..f23062b 100644 (file)
@@ -14,6 +14,19 @@ class MockLeague
   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
@@ -38,12 +51,8 @@ class MockLeague
 
   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
@@ -51,6 +60,23 @@ class MockLeague
       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
 
 
@@ -117,6 +143,11 @@ class TestFactoryMethod < Test::Unit::TestCase
     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)
@@ -197,9 +228,38 @@ class TestFactoryMethod < Test::Unit::TestCase
     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
 
@@ -498,6 +558,23 @@ 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
@@ -721,8 +798,7 @@ end
 class BaseTestBuoyCommand < Test::Unit::TestCase
   def setup
     @p = MockPlayer.new
-    $p1 = nil
-    $p2 = nil
+    $league = MockLeague.new
 
     delete_buoy_yaml
     @buoy = ShogiServer::Buoy.new
@@ -779,8 +855,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
     rt = cmd.call
     assert :continue, rt
-    assert !$p1
-    assert !$p2
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
   end
 
@@ -793,8 +869,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
     rt = cmd.call
     assert :continue, rt
-    assert !$p1
-    assert !$p2
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
   end
 
@@ -803,8 +879,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
     rt = cmd.call
     assert :continue, rt
-    assert !$p1
-    assert !$p2
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert @buoy.is_new_game?("buoy_badmoves-1500-0")
   end
 
@@ -813,8 +889,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
     rt = cmd.call
     assert :continue, rt
-    assert !$p1
-    assert !$p2
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert @buoy.is_new_game?("buoy_badcounter-1500-0")
   end
 end
@@ -831,8 +907,8 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
     rt = cmd.call
     assert :continue, rt
-    assert !$p1
-    assert !$p2
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert @buoy.is_new_game?(buoy_game.game_name)
   end
 
@@ -842,8 +918,8 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
     rt = cmd.call
     assert :continue, rt
-    assert !$p1
-    assert !$p2
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert @buoy.is_new_game?(buoy_game.game_name)
   end
 
@@ -885,6 +961,26 @@ class TestGetBuoyCountCommand < BaseTestBuoyCommand
   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
 
 #
 #
@@ -901,6 +997,15 @@ class TestMonitorHandler1 < Test::Unit::TestCase
   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")
@@ -925,6 +1030,15 @@ class TestMonitorHandler2 < Test::Unit::TestCase
     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",