Browse Source

Added director tests.

Steve Thielemann 3 years ago
parent
commit
e7949ee5fe
4 changed files with 109 additions and 2 deletions
  1. 6 2
      CMakeLists.txt
  2. 46 0
      log-parser.py
  3. 56 0
      test-director.cpp
  4. 1 0
      test-galaxy.cpp

+ 6 - 2
CMakeLists.txt

@@ -58,13 +58,17 @@ option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
 ### TESTS
 add_executable(test-galaxy test-galaxy.cpp galaxy.cpp utils.cpp buysell.cpp)
 add_dependencies(test-galaxy gtest)
-## target_link_libraries(test-galaxy gtest_main)
 target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES} yaml-cpp)
 
+add_executable(test-director test-director.cpp galaxy.cpp utils.cpp buysell.cpp director.cpp dispatchers.cpp boxes.cpp scripts.cpp)
+add_dependencies(test-director gtest)
+target_link_libraries(test-director gtest_main ${Boost_LIBRARIES} yaml-cpp)
+
 enable_testing()
 add_test(NAME test-galaxy
   COMMAND test-galaxy)
-
+add_test(NAME test-director
+  COMMAND test-director)
 
 # dispatchers.cpp 
 ADD_EXECUTABLE( twproxy twproxy.cpp utils.cpp session.cpp boxes.cpp director.cpp galaxy.cpp dispatchers.cpp scripts.cpp buysell.cpp )

+ 46 - 0
log-parser.py

@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+import sys
+
+opening = False
+
+print("std::string lines[] = {")
+
+def printer(line):
+  global opening
+
+  if opening:
+    if "[Pause]" in line:
+      opening = False
+    else:
+      return
+  out = repr(line)
+  out.replace(r"\\\\", r"\\")
+  out = out[1:-1]
+  # print("[", repr(out), "]")
+  if out.startswith("^\\r#"):
+    return
+  print('"', out, '",', sep="")
+  if "Trade Wars 2002 Win32 module now" in line:
+    opening = True
+
+filename = sys.argv[1]
+with open(filename) as fp:
+  for line in fp:
+    if " SL: " in line:
+      _, part = line.strip().split(" SL: ")
+      part = part[1:-1]
+      # print(repr(part))
+      printer(part)
+    if " SP: " in line:
+      _, part = line.strip().split(" SP: ")
+      part = part[1:-1]
+      # print("^", repr(part))
+      printer("^" + part)
+    if " CI: " in line:
+      _, part = line.strip().split(" CI: ")
+      part = part[1:-1]
+      # print("!", repr(part))
+      printer("!" + part)
+
+print("};")

+ 56 - 0
test-director.cpp

@@ -0,0 +1,56 @@
+
+#include <array>
+#include <map>
+#include <string>
+#include <vector>
+
+#include "director.h"
+#include "galaxy.h"
+#include "gtest/gtest.h"
+
+/*
+
+How can I add logging, but not really add/use it?
+
+*/
+
+#define GTEST_COUT std::cerr << "[          ] [ INFO ]"
+
+namespace {
+
+TEST(director, director_debugging) {
+  Director dir;
+  std::vector<std::string> client;
+  std::vector<std::string> server;
+  dir.to_client = [&client](const std::string line) { client.push_back(line); };
+  dir.to_server = [&server](const std::string line) { server.push_back(line); };
+
+  dir.username = "test";
+  // should I setup galaxy-N-test.yaml ?  Probably yes.
+  std::string lines[] = {
+      "TradeWars Game Server                                     Copyright (C) "
+      "EIS",
+      "Selection (? for menu): N",
+      "Trade Wars 2002 Win32 module now loading.",
+      "   ==-- Trade Wars 2002 --==",
+      "Enter your choice: ",
+      "^Command [TL=00:00:00]:[2985] (?=Help)? :"};
+
+  for (auto line : lines) {
+    if (line[0] == '^') {
+      dir.server_prompt(line.substr(1), line.substr(1));
+    } else {
+      dir.server_line(line, line);
+    }
+  }
+  EXPECT_EQ(client[0],
+            "\rTradeWars Proxy v2++ READY (~ or ESC to activate)\n\r");
+  client.clear();
+  dir.client_input("~");
+
+  for (auto line : client) {
+    GTEST_COUT << line << std::endl;
+  }
+}
+
+}  // namespace

+ 1 - 0
test-galaxy.cpp

@@ -6,6 +6,7 @@
 
 #include "galaxy.h"
 #include "gtest/gtest.h"
+#include "director.h"
 
 /*