OSDN Git Service

Fix #36821: Allow to customize maximum lenght of a login indentifier
[shogi-server/shogi-server.git] / test / TC_floodgate.rb
index 8831e54..c169b26 100644 (file)
@@ -4,26 +4,17 @@ require 'shogi_server'
 require 'shogi_server/player'
 require 'shogi_server/pairing'
 require 'shogi_server/league/floodgate'
+require 'test/mock_log_message'
 
-class MockLogger
-  def debug(str)
-  end
-  def info(str)
-    #puts str
-  end
-  def warn(str)
-  end
-  def error(str)
-  end
-end
+$topdir = File.expand_path File.dirname(__FILE__)
 
-$logger = MockLogger.new
-def log_message(msg)
-  $logger.info(msg)
-end
-
-def log_warning(msg)
-  $logger.warn(msg)
+class SimplePlayer < ShogiServer::BasicPlayer
+  attr_accessor :status
+  def initialize
+    super
+    @status = "game_waiting"
+    @game_name = "floodgate-900-0"
+  end
 end
 
 class TestFloodgate < Test::Unit::TestCase
@@ -41,6 +32,46 @@ class TestFloodgate < Test::Unit::TestCase
     assert(!ShogiServer::League::Floodgate.game_name?("floodgat-900-0"))
   end
 
+  def test_instance_game_name
+    fg = ShogiServer::League::Floodgate.new(nil, {:game_name => "floodgate-900-0"})
+    assert(fg.game_name?("floodgate-900-0"))
+    assert(!fg.game_name?("floodgate-3600-0"))
+    fg = ShogiServer::League::Floodgate.new(nil, {:game_name => "floodgate-3600-0"})
+    assert(!fg.game_name?("floodgate-900-0"))
+    assert(fg.game_name?("floodgate-3600-0"))
+  end
+
+  def test_select_players
+    league = ShogiServer::League.new(File.dirname(__FILE__))
+    league.event = "test"
+    league.setup_players_database
+
+    a = SimplePlayer.new
+    a.win  = 1
+    a.loss = 2
+    a.rate = 0
+    a.name = "a"
+    a.player_id = "a+123"
+    b = SimplePlayer.new
+    b.win  = 10
+    b.loss = 20
+    b.rate = 1500
+    b.name = "b"
+    b.player_id = "b+456"
+    c = SimplePlayer.new
+    c.win  = 100
+    c.loss = 200
+    c.rate = 1000
+    c.name = "c"
+
+    league.add a
+    league.add b
+    league.add c
+
+    fg = ShogiServer::League::Floodgate.new(league, {:game_name => "floodgate-900-0"})
+
+    assert_equal([a,b], fg.select_players)
+  end
 end
 
 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
@@ -97,7 +128,8 @@ class TestMakeEven < Test::Unit::TestCase
  def test_match_odd
     players = [@a, @b, @c]
     @pairing.match(players)
-    assert_equal([@a, @b], players)
+    assert_equal(2, players.size)
+    assert(players[0] != players[1])
   end
 end
 
@@ -146,7 +178,10 @@ class TestRandomize < Test::Unit::TestCase
   def test_match
     players = [@a, @b, @c]
     @pairing.match(players)
-    assert_equal([@b,@a,@c], players)
+    assert_equal(3, players.size)
+    assert(players.include? @a)
+    assert(players.include? @b)
+    assert(players.include? @c)
   end
 end
 
@@ -268,50 +303,66 @@ class TestSwissPairing < Test::Unit::TestCase
     @a = ShogiServer::BasicPlayer.new
     @a.player_id = "a"
     @a.rate = 0
+    @a.game_name = "floodgate-900-0"
     @b = ShogiServer::BasicPlayer.new
     @b.player_id = "b"
     @b.rate = 1000
+    @b.game_name = "floodgate-900-0"
     @c = ShogiServer::BasicPlayer.new
     @c.player_id = "c"
     @c.rate = 1500
+    @c.game_name = "floodgate-900-0"
     @d = ShogiServer::BasicPlayer.new
     @d.player_id = "d"
     @d.rate = 2000
+    @d.game_name = "floodgate-900-0"
 
     @players = [@a, @b, @c, @d]
 
-    @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
-    @history = ShogiServer::League::Floodgate::History.new @file
+    @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
+    @history = ShogiServer::League::Floodgate::History.factory @file
 
-    @swiss = ShogiServer::Swiss.new @history
+    @swiss = ShogiServer::Swiss.new
   end
 
   def teardown
     @file.delete if @file.exist?
   end
 
+  def test_none
+    players = []
+    @swiss.match players
+    assert(players.empty?)
+  end
+
   def test_all_win
-    def @history.last_win?(player_id)
-      true
+    ShogiServer::League::Floodgate::History.class_eval do
+      def last_win?(player_id)
+        true
+      end
     end
     @swiss.match @players
     assert_equal([@d, @c, @b, @a], @players)
   end
 
   def test_all_lose
-    def @history.last_win?(player_id)
-      false
+    ShogiServer::League::Floodgate::History.class_eval do
+      def last_win?(player_id)
+        false
+      end
     end
     @swiss.match @players
     assert_equal([@d, @c, @b, @a], @players)
   end
 
   def test_one_win
-    def @history.last_win?(player_id)
-      if player_id == "a"
-        true
-      else
-        false
+    ShogiServer::League::Floodgate::History.class_eval do
+      def last_win?(player_id)
+        if player_id == "a"
+          true
+        else
+          false
+        end
       end
     end
     @swiss.match @players
@@ -319,11 +370,13 @@ class TestSwissPairing < Test::Unit::TestCase
   end
 
   def test_two_win
-    def @history.last_win?(player_id)
-      if player_id == "a" || player_id == "d"
-        true
-      else
-        false
+    ShogiServer::League::Floodgate::History.class_eval do
+      def last_win?(player_id)
+        if player_id == "a" || player_id == "d"
+          true
+        else
+          false
+        end
       end
     end
     @swiss.match @players
@@ -386,6 +439,22 @@ class TestFloodgateHistory < Test::Unit::TestCase
     assert !@history.last_win?("foo")
     assert !@history.last_lose?("hoge")
     assert @history.last_lose?("foo")
+
+    assert_equal("foo", @history.last_opponent("hoge"))
+    assert_equal("hoge", @history.last_opponent("foo"))
+
+    games = @history.win_games("hoge")
+    assert_equal(1, games.size )
+    assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
+    games = @history.win_games("foo")
+    assert_equal(1, games.size )
+    assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
+    games = @history.loss_games("hoge")
+    assert_equal(1, games.size )
+    assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
+    games = @history.loss_games("foo")
+    assert_equal(1, games.size )
+    assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
   end
 end