OSDN Git Service

util/statistics.rb: Updated to investigate floodgate-600-10 games.
[shogi-server/shogi-server.git] / test / TC_command.rb
index fb2e134..5e231fa 100644 (file)
@@ -2,8 +2,8 @@ $:.unshift File.join(File.dirname(__FILE__), "..")
 $topdir = File.expand_path File.dirname(__FILE__)
 require 'test/unit'
 require 'tempfile'
 $topdir = File.expand_path File.dirname(__FILE__)
 require 'test/unit'
 require 'tempfile'
-require 'mock_game'
-require 'mock_log_message'
+require 'test/mock_game'
+require 'test/mock_log_message'
 require 'test/mock_player'
 require 'shogi_server/login'
 require 'shogi_server/player'
 require 'test/mock_player'
 require 'shogi_server/login'
 require 'shogi_server/player'
@@ -14,6 +14,19 @@ class MockLeague
   def initialize
     @games = {}
     @games["dummy_game_id"] = MockGame.new
   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
   end
 
   def games
@@ -38,12 +51,8 @@ class MockLeague
 
   def get_player(status, game_id, sente, searcher)
     if sente == true
 
   def get_player(status, game_id, sente, searcher)
     if sente == true
-      $p1 = MockPlayer.new
-      $p1.name = "p1"
       return $p1
     elsif sente == false
       return $p1
     elsif sente == false
-      $p2 = MockPlayer.new
-      $p2.name = "p2"
       return $p2
     elsif sente == nil
       return nil
       return $p2
     elsif sente == nil
       return nil
@@ -51,6 +60,23 @@ class MockLeague
       return nil
     end
   end
       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
 
 
 end
 
 
@@ -202,6 +228,16 @@ class TestFactoryMethod < Test::Unit::TestCase
     assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
   end
 
     assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
   end
 
+  def test_fork_command
+    cmd = ShogiServer::Command.factory("%%FORK server-denou-14400-60+p1+p2+20130223185013 buoy_denou-14400-60", @p)
+    assert_instance_of(ShogiServer::ForkCommand, cmd)
+  end
+
+  def test_fork_command2
+    cmd = ShogiServer::Command.factory("%%FORK server-denou-14400-60+p1+p2+20130223185013", @p)
+    assert_instance_of(ShogiServer::ForkCommand, cmd)
+  end
+
   def test_void_command
     cmd = ShogiServer::Command.factory("%%%HOGE", @p)
     assert_instance_of(ShogiServer::VoidCommand, cmd)
   def test_void_command
     cmd = ShogiServer::Command.factory("%%%HOGE", @p)
     assert_instance_of(ShogiServer::VoidCommand, cmd)
@@ -211,29 +247,29 @@ class TestFactoryMethod < Test::Unit::TestCase
     cmd = ShogiServer::Command.factory("should_be_error", @p)
     assert_instance_of(ShogiServer::ErrorCommand, cmd)
     cmd.call
     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
+    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
   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
+    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
 
     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
+    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
 
     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
+    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
 
     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
+    assert_no_match /unknown command: LOGIN hoge foo/, cmd.msg
   end
 end
 
   end
 end
 
@@ -772,8 +808,7 @@ end
 class BaseTestBuoyCommand < 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
 
     delete_buoy_yaml
     @buoy = ShogiServer::Buoy.new
@@ -807,7 +842,7 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     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 @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")
     assert !$p1.out.empty?
     assert !$p2.out.empty?
@@ -819,7 +854,7 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     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 @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")
     assert !$p1.out.empty?
     assert !$p2.out.empty?
@@ -829,9 +864,9 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
     rt = cmd.call
     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
 
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
   end
 
@@ -843,9 +878,9 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
     rt = cmd.call
     
     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_duplicated-1500-0")
   end
 
@@ -853,9 +888,9 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     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 @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_badmoves-1500-0")
   end
 
@@ -863,9 +898,9 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     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 @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_badcounter-1500-0")
   end
 end
@@ -881,9 +916,9 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
     assert !@buoy.is_new_game?(buoy_game.game_name)
     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
     rt = cmd.call
     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)
   end
 
@@ -892,9 +927,9 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
     assert @buoy.is_new_game?(buoy_game.game_name)
     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
     rt = cmd.call
     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)
   end
 
@@ -906,7 +941,7 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
 
     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
     rt = cmd.call
 
     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_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
@@ -914,6 +949,28 @@ end
 
 #
 #
 
 #
 #
+class TestForkCommand < Test::Unit::TestCase
+  def setup
+    @player = MockPlayer.new
+  end
+
+  def test_new_buoy_game_name
+    src = "%%FORK server+denou-14400-60+p1+p2+20130223185013"
+    c = ShogiServer::ForkCommand.new src, @player, "server+denou-14400-60+p1+p2+20130223185013", nil, 13
+    c.decide_new_buoy_game_name
+    assert_equal "buoy_denou_13-14400-60", c.new_buoy_game
+  end
+
+  def test_new_buoy_game_name2
+    src = "%%FORK server+denou-14400-060+p1+p2+20130223185013"
+    c = ShogiServer::ForkCommand.new src, @player, "server+denou-14400-060+p1+p2+20130223185013", nil, 13
+    c.decide_new_buoy_game_name
+    assert_equal "buoy_denou_13-14400-060", c.new_buoy_game
+  end
+end
+
+#
+#
 class TestGetBuoyCountCommand < BaseTestBuoyCommand
   def test_call
     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
 class TestGetBuoyCountCommand < BaseTestBuoyCommand
   def test_call
     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
@@ -922,7 +979,7 @@ class TestGetBuoyCountCommand < BaseTestBuoyCommand
     assert !@buoy.is_new_game?(buoy_game.game_name)
     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
     rt = cmd.call
     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_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
   end
 
@@ -931,7 +988,7 @@ class TestGetBuoyCountCommand < BaseTestBuoyCommand
     assert @buoy.is_new_game?(buoy_game.game_name)
     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
     rt = cmd.call
     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
     assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
   end
 end
@@ -1026,4 +1083,3 @@ class TestMonitorHandler2 < Test::Unit::TestCase
                  @player.out.join)
   end
 end
                  @player.out.join)
   end
 end
-