OSDN Git Service

test/TC_league.rb failed with Ruby 1.8.7. This issue has been fixed.
[shogi-server/shogi-server.git] / test / TC_command.rb
index 455fdd8..18f971b 100644 (file)
@@ -2,8 +2,8 @@ $:.unshift File.join(File.dirname(__FILE__), "..")
 $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'
@@ -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
 
 
@@ -210,6 +236,30 @@ class TestFactoryMethod < Test::Unit::TestCase
   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
 
@@ -748,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
@@ -783,7 +832,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 :continue, rt
+    assert_equal :continue, rt
     assert !@buoy.is_new_game?("buoy_hoge-1500-0")
     assert !$p1.out.empty?
     assert !$p2.out.empty?
@@ -795,7 +844,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 :continue, rt
+    assert_equal :continue, rt
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
     assert !$p1.out.empty?
     assert !$p2.out.empty?
@@ -805,9 +854,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 :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
 
@@ -819,9 +868,9 @@ 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_equal :continue, rt
+    assert $p1.out.empty?
+    assert $p2.out.empty?
     assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
   end
 
@@ -829,9 +878,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 :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
 
@@ -839,9 +888,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 :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
@@ -857,9 +906,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 :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
 
@@ -868,9 +917,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 :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
 
@@ -882,7 +931,7 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
 
     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
@@ -898,7 +947,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 :continue, rt
+    assert_equal :continue, rt
     assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
   end
 
@@ -907,7 +956,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 :continue, rt
+    assert_equal :continue, rt
     assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
   end
 end