OSDN Git Service

Merge branch 'logger'
[shogi-server/shogi-server.git] / test / TC_functional.rb
index 41558a1..44b5240 100644 (file)
@@ -3,20 +3,68 @@ require "kconv"
 
 class TestClientAtmark < BaseClient
   # login with trip
-  def login
-    cmd "LOGIN testsente@p1 dummy x1"
-    cmd "%%GAME testcase-1500-0 +"
-    
-    cmd2 "LOGIN testgote@p2 dummy2 x1"
-    cmd2 "%%CHALLENGE testcase-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)
+
+    now = Time.now
+    year  = now.strftime("%Y")
+    month = now.strftime("%m")
+    day   = now.strftime("%d")
+    path = File.join( File.dirname(__FILE__), "..", year, month, day, "*atmark-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+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
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+'rating:atmark_B@p1+275876e34cf609db118f3d84b799a790:atmark_W@p2+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 * +KA *  *  *  *  * +HI * 
+'P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+'+
+'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
 
@@ -24,7 +72,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)
@@ -32,9 +81,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)
@@ -42,9 +92,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)
@@ -52,9 +103,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)
@@ -65,9 +117,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)
@@ -77,56 +129,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)
@@ -134,15 +153,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)
@@ -150,64 +178,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)
@@ -215,12 +267,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)
@@ -228,15 +280,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 TestFunctionalChatCommand < BaseClient
+  def test_chat
+    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
+