OSDN Git Service

Merge branch '201312-usiToCsa'
[shogi-server/shogi-server.git] / test / TC_functional.rb
index c29c160..51aca58 100644 (file)
@@ -1,19 +1,22 @@
-require "baseclient"
+# -*- coding: windows-31j -*-
+$:.unshift File.join(File.dirname(__FILE__), "..")
+$topdir = File.expand_path File.dirname(__FILE__)
+require "test/baseclient"
 require "kconv"
 
 class TestClientAtmark < BaseClient
   # login with trip
-  def login
-    cmd "LOGIN testsente@p1 dummy x1"
-    cmd "%%GAME testClientAtmark-1500-0 +"
-    
-    cmd2 "LOGIN testgote@p2 dummy2 x1"
-    cmd2 "%%CHALLENGE testClientAtmark-1500-0 -"
+  def set_name
+    super
+    @game_name = "atmark"
+    @p1_name = "B@p1"
+    @p2_name = "W@p2"
   end
 
   def test_toryo
     result, result2 = handshake do
-      cmd  "%TORYO"
+      @p1.toryo
+      wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -22,16 +25,17 @@ class TestClientAtmark < BaseClient
     year  = now.strftime("%Y")
     month = now.strftime("%m")
     day   = now.strftime("%d")
-    path = File.join( File.dirname(__FILE__), "..", year, month, day, "*testClientAtmark-1500-0*")
+    path = File.join( File.dirname(__FILE__), "..", year, month, day, "*atmark-1500-0*")
     log_files = Dir.glob(path)
     assert(!log_files.empty?) 
-    log_content = File.open(log_files.sort.last).read
+    sleep 0.1
+    log_content = File.read(log_files.sort.last)
 
     # "$EVENT", "$START_TIME" and "'$END_TIME" are removed since they vary dinamically.
     should_be = <<-EOF
 V2
-N+testsente@p1
-N-testgote@p2
+N+atmark_B@p1
+N-atmark_W@p2
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
 P2 * -HI *  *  *  *  * -KA * 
 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
@@ -42,7 +46,7 @@ P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
 P8 * +KA *  *  *  *  * +HI * 
 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
 +
-'rating:testsente@p1+275876e34cf609db118f3d84b799a790:testgote@p2+c0c40e7a94eea7e2c238b75273087710
+'rating:atmark_B@p1+275876e34cf609db118f3d84b799a790:atmark_W@p2+275876e34cf609db118f3d84b799a790
 +2726FU
 T1
 -3334FU
@@ -58,7 +62,74 @@ T1
 'P8 * +KA *  *  *  *  * +HI * 
 'P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
 '+
-'summary:toryo:testsente@p1 lose:testgote@p2 win
+'summary:toryo:atmark_B@p1 lose:atmark_W@p2 win
+EOF
+
+    log_content.gsub!(/^\$.*?\n/m, "")
+    log_content.gsub!(/^'\$.*?\n/m, "")
+    assert_equal(should_be, log_content)
+  end
+end
+
+class TestHandicappedGame < BaseClient
+  # login with trip
+  def set_name
+    super
+    @game_name = "hc2p_hoge"
+    @p1_name = "B"
+    @p2_name = "W"
+  end
+
+  def test_toryo
+    result, result2 = handshake do
+      @p1.toryo
+      wait_finish
+    end
+    assert(/#LOSE/ =~ result)
+    assert(/#WIN/  =~ result2)
+
+    now = Time.now
+    year  = now.strftime("%Y")
+    month = now.strftime("%m")
+    day   = now.strftime("%d")
+    path = File.join( File.dirname(__FILE__), "..", year, month, day, "*hc2p_hoge-1500-0*")
+    log_files = Dir.glob(path)
+    assert(!log_files.empty?) 
+    sleep 0.1
+    log_content = File.read(log_files.sort.last)
+
+    # "$EVENT", "$START_TIME" and "'$END_TIME" are removed since they vary dinamically.
+    should_be = <<-EOF
+V2
+N+hc2p_hoge_B
+N-hc2p_hoge_W
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 *  *  *  *  *  *  *  *  * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+'rating:hc2p_hoge_B+275876e34cf609db118f3d84b799a790:hc2p_hoge_W+275876e34cf609db118f3d84b799a790
++2726FU
+T1
+-3334FU
+T1
+%TORYO
+'P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+'P2 * -HI *  *  *  *  * -KA * 
+'P3-FU-FU-FU-FU-FU-FU * -FU-FU
+'P4 *  *  *  *  *  * -FU *  * 
+'P5 *  *  *  *  *  *  *  *  * 
+'P6 *  *  *  *  *  *  * +FU * 
+'P7+FU+FU+FU+FU+FU+FU+FU * +FU
+'P8 *  *  *  *  *  *  *  *  * 
+'P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+'+
+'summary:toryo:hc2p_hoge_B lose:hc2p_hoge_W win
 EOF
 
     log_content.gsub!(/^\$.*?\n/m, "")
@@ -71,7 +142,8 @@ end
 class TestComment < BaseClient
   def test_toryo
     result, result2 = handshake do
-      cmd  "%TORYO"
+      @p1.toryo
+      wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -79,9 +151,10 @@ class TestComment < BaseClient
 
   def test_inline_comment
     result, result2 = handshake do
-      cmd "+2625FU,'comment"
-      cmd2 "-2233KA"
-      cmd  "%TORYO"
+      move "+2625FU,'comment"
+      move "-2233KA"
+      @p1.toryo
+      wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -89,9 +162,10 @@ class TestComment < BaseClient
 
   def test_inline_comment_ja_euc
     result, result2 = handshake do
-      cmd "+2625FU,'\93ú\96{\8cêEUC"
-      cmd2 "-2233KA"
-      cmd  "%TORYO"
+      move "+2625FU,'\93ú\96{\8cêEUC"
+      move "-2233KA"
+      @p1.toryo
+      wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -99,9 +173,10 @@ class TestComment < BaseClient
 
   def test_inline_comment_ja_utf8
     result, result2 = handshake do
-      cmd "+2625FU,'\93ú\96{\8cêUTF8".toutf8
-      cmd2 "-2233KA"
-      cmd  "%TORYO"
+      move "+2625FU,'\93ú\96{\8cêUTF8".toutf8
+      move "-2233KA"
+      @p1.toryo
+      wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -112,9 +187,9 @@ end
 class TestWhiteMovesBlack < BaseClient
   def test_white_moves_black
     result, result2 = handshake do
-      cmd  "+9796FU"
-      cmd2 "+1716FU"
-      sleep 0.5
+      move "+9796FU"
+      @p2.move "+1716FU"
+      wait_finish
     end
     assert(/#ILLEGAL_MOVE/ =~ result)
     assert(/#WIN/  =~ result)
@@ -124,56 +199,23 @@ class TestWhiteMovesBlack < BaseClient
 end
 
 
-class CSABaseClient < BaseClient
-  ##
-  # In CSA mode, the server decides sente or gote at random; and sockets are closed
-  # just after the game ends (i.e. %TORYO is sent)
-  # 
-  def handshake
-    login
-
-    sleep 0.5 # wait for game matching
-
-    str  = cmd  "AGREE"
-    str2 = cmd2 "AGREE"
-
-    if /Your_Turn:\+/ =~ str
-      @sente = "cmd"
-      @sente_socket = @socket1
-      @gote  = "cmd2"
-      @gote_socket  = @socket2
-    else
-      @sente = "cmd2"
-      @sente_socket = @socket2
-      @gote  = "cmd"
-      @gote_socket  = @socket1
-    end
-
-    yield if block_given?
-    
-    result  = read_nonblock(@sente_socket)
-    result2 = read_nonblock(@gote_socket)
-    [result, result2]
-  end
-
-  def sente_cmd(str)
-    eval "#{@sente} \"#{str}\""
-  end
-
-  def gote_cmd(str)
-    eval "#{@gote} \"#{str}\""
-  end
-end
+#
+# CSA test
+#
 
 class TestLoginCSAWithoutTripGoodGamename < CSABaseClient
-  def login
-    cmd  "LOGIN wo_trip_p1 testcase-1500-0"
-    cmd2 "LOGIN wo_trip_p2 testcase-1500-0"
+  def set_name
+    super
+    @game_name = "csawotrip"
+    @p1_name   = "p1"
+    @p2_name   = "p2"
   end
 
   def test_toryo
     result, result2 = handshake do
-      sente_cmd("%TORYO")
+      @p1.toryo
+      @p1.wait_finish
+      @p2.wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -181,15 +223,24 @@ class TestLoginCSAWithoutTripGoodGamename < CSABaseClient
 end
 
 class TestLoginCSAWithTripGoodGamename < CSABaseClient
-  def login
-    cmd  "LOGIN w_trip_p1 testcase-1500-0,atrip"
-    cmd2 "LOGIN w_trip_p2 testcase-1500-0,anothertrip"
+  def set_name
+    super
+    @game_name = "csawtrip"
+    @p1_name   = "p1"
+    @p2_name   = "p2"
+  end
+
+  def set_player
+    super
+    @p1.login_command += ",atrip"
+    @p2.login_command += ",anothertrip"
   end
 
   def test_toryo
     result, result2 = handshake do
-      sente_cmd "%TORYO"
-      sleep 0.5
+      @p1.toryo
+      @p1.wait_finish
+      @p2.wait_finish
     end
     assert(/#LOSE/ =~ result)
     assert(/#WIN/  =~ result2)
@@ -197,64 +248,88 @@ class TestLoginCSAWithTripGoodGamename < CSABaseClient
 end
 
 class TestChallenge < CSABaseClient
-  def login
-    cmd  "LOGIN w_trip_p1 testcase-1500-0,atrip"
-    cmd2 "LOGIN w_trip_p2 testcase-1500-0,anothertrip"
+  def set_name
+    super
+    @game_name = "challenge"
+    @p1_name   = "p1"
+    @p2_name   = "p2"
+  end
+
+  def set_player
+    super
+    @p1.login_command += ",atrip"
+    @p2.login_command += ",anothertrip"
   end
 
   def test_toryo
     result, result2 = handshake do
-      sente_cmd "CHALLENGE"
-      gote_cmd  "CHALLENGE"
+      @p1.puts "CHALLENGE"
+      @p1.wait(/CHALLENGE ACCEPTED/)
+      @p2.puts "CHALLENGE"
+      @p2.wait(/CHALLENGE ACCEPTED/)
     end
-    assert_match(/CHALLENGE ACCEPTED/, result)
-    assert_match(/CHALLENGE ACCEPTED/, result2)
+    assert(true)
   end
 end
 
+#
+# Test Floodgate
+#
+
 class TestFloodgateGame < BaseClient
-  def login
-    classname = self.class.name
-    gamename  = "floodgate-900-0"
-    cmd "LOGIN sente#{classname} dummy x1"
-    cmd "%%GAME #{gamename} *"
-    
-    cmd2 "LOGIN gote#{classname} dummy2 x1"
-    cmd2 "%%GAME #{gamename} *"
+  def set_name
+    super
+    @game_name = "floodgate"
+  end
+
+  def set_player
+    @p1 = SocketPlayer.new @game_name, @p1_name, "*"
+    @p2 = SocketPlayer.new @game_name, @p2_name, "*"
   end
 
   def test_game_wait
-    login
+    @p1.connect
+    @p2.connect
+    @p1.login
+    @p2.login
+    @p1.game
+    @p2.game
     assert(true)
+    logout12
   end
 end
 
 class TestFloodgateGameWrongTebam < BaseClient
-  def login
-    classname = self.class.name
-    gamename  = "floodgate-900-0"
-    cmd "LOGIN sente#{classname} dummy x1"
-    cmd("%%GAME #{gamename} +")
+  def set_name
+    super
+    @game_name = "floodgate"
   end
 
   def test_game_wait
-    login
-    sleep 1
-    reply = read_nonblock(@socket1)
-    assert_match(/##\[ERROR\] You are not allowed/m, reply)
+    @p1.connect
+    @p2.connect
+    @p1.login
+    @p2.login
+    @p1.game
+    @p1.wait %r!##\[ERROR\] You are not allowed!
+    assert true
+    logout12
   end
 end
 
+
+
+
 class TestDuplicatedMoves < BaseClient
   def test_defer
     result, result2 = handshake do
-      cmd  "+7776FU"
-      cmd  "+8786FU" # defer
-      cmd  "+9796FU" # defer
-      cmd2 "-7374FU"
-      cmd2 "-8384FU"
-      cmd2 "%TORYO" # defer
-      sleep 1
+      @p1.puts "+7776FU"
+      @p1.puts "+8786FU" # defer
+      @p1.puts "+9796FU" # defer
+      @p2.puts "-7374FU"
+      @p2.puts "-8384FU"
+      @p2.toryo
+      wait_finish
     end
     assert(/#WIN/  =~ result)
     assert(/#LOSE/ =~ result2)
@@ -262,12 +337,12 @@ class TestDuplicatedMoves < BaseClient
 
   def test_defer2
     result, result2 = handshake do
-      cmd  "+7776FU"
-      cmd  "+8786FU" # defer
-      cmd  "%TORYO" # defer
-      cmd2 "-7374FU"
-      cmd2 "-8384FU"
-      sleep 1
+      @p1.puts "+7776FU"
+      @p1.puts "+8786FU" # defer
+      @p1.puts "%TORYO" # defer
+      @p2.puts "-7374FU"
+      @p2.puts "-8384FU"
+      wait_finish
     end
     assert(/#LOSE/  =~ result)
     assert(/#WIN/ =~ result2)
@@ -275,24 +350,50 @@ class TestDuplicatedMoves < BaseClient
 
   def test_defer3
     result, result2 = handshake do
-      cmd  "+7776FU"
-      cmd  "+8786FU" # defer
-      cmd2 "-7374FU"
-      cmd2 "-8384FU"
-      cmd  "%TORYO" # defer
-      sleep 1
+      @p1.puts "+7776FU"
+      @p1.puts "+8786FU" # defer
+      @p2.puts "-7374FU"
+      @p2.puts "-8384FU"
+      @p1.toryo
+      wait_finish
     end
     assert(/#LOSE/  =~ result)
     assert(/#WIN/ =~ result2)
   end
 end
 
-class TestChatCommand < BaseClient
+class TestFunctionalChatCommand < BaseClient
   def test_chat
-    cmd "%%CHAT Hello"
-    sleep 1
-    str = read_nonblock(@socket2)
-    puts str   
-    assert("", str)
+    result, result2 = handshake do
+      @p1.puts"%%CHAT Hello"
+      @p1.wait %r!##\[CHAT\].*Hello!
+      @p2.wait %r!##\[CHAT\].*Hello!
+    end
+    assert true
+  end
+end
+
+
+
+
+class TestTwoSameMoves < CSABaseClient
+  def set_name
+    super
+    @game_name = "2moves"
+    @p1_name   = "p1"
+    @p2_name   = "p2"
+  end
+
+  def test_two_same_moves
+    result, result2 = handshake do
+      move  "+2726FU"
+      move "-8384FU"
+      @p2.puts "-8384FU" # ignored
+      sleep 0.1 # wait for finish of the command above
+      move "+2625FU"
+    end
+    assert(/#ILLEGAL_MOVE/ !~ result)
+    assert(/#ILLEGAL_MOVE/ !~ result2)
   end
 end
+