test-director.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <array>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5. #include "director.h"
  6. #include "galaxy.h"
  7. #include "gtest/gtest.h"
  8. /*
  9. How can I add logging, but not really add/use it?
  10. */
  11. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  12. namespace {
  13. TEST(director, director_debugging) {
  14. Director dir;
  15. std::vector<std::string> client;
  16. std::vector<std::string> server;
  17. dir.to_client = [&client](const std::string line) { client.push_back(line); };
  18. dir.to_server = [&server](const std::string line) { server.push_back(line); };
  19. // activating the game gets this over to dir.galaxy.username.
  20. dir.username = "test";
  21. // should I setup galaxy-N-test.yaml ? Probably yes.
  22. std::string lines[] = {
  23. "TradeWars Game Server Copyright (C) "
  24. "EIS",
  25. "Selection (? for menu): N",
  26. "Trade Wars 2002 Win32 module now loading.",
  27. " ==-- Trade Wars 2002 --==",
  28. "Enter your choice: ",
  29. "^Command [TL=00:00:00]:[2985] (?=Help)? :"};
  30. for (auto line : lines) {
  31. if (line[0] == '^') {
  32. dir.server_prompt(line.substr(1), line.substr(1));
  33. } else if (line[0] == '!') {
  34. // client input
  35. dir.client_input(line.substr(1));
  36. } else {
  37. dir.server_line(line, line);
  38. }
  39. }
  40. EXPECT_EQ(client[0],
  41. "\rTradeWars Proxy v2++ READY (~ or ESC to activate)\n\r");
  42. client.clear();
  43. dir.client_input("~");
  44. for (auto line : client) {
  45. GTEST_COUT << line << std::endl;
  46. }
  47. }
  48. TEST(director, director_galaxy_save) {
  49. Director dir;
  50. dir.galaxy.username = "test";
  51. dir.galaxy.game = 'Z';
  52. // This causes a YAML::Node Sequence
  53. dir.galaxy.meta["trade"][10963][0] = 1;
  54. dir.galaxy.save();
  55. EXPECT_TRUE(1) << "No exception, this is good!";
  56. }
  57. TEST(director, director_parsing_density) {
  58. Director dir;
  59. std::vector<std::string> client;
  60. std::vector<std::string> server;
  61. dir.to_client = [&client](const std::string line) { client.push_back(line); };
  62. dir.to_server = [&server](const std::string line) { server.push_back(line); };
  63. dir.username = "test";
  64. std::string lines[] = {
  65. "TradeWars Game Server Copyright (C) "
  66. "EIS",
  67. "Selection (? for menu): N",
  68. "^Command [TL=00:00:00]:[2985] (?=Help)? :",
  69. "!s",
  70. "Command [TL=00:00:00]:[926] (?=Help)? : S",
  71. "",
  72. "Long Range Scan",
  73. "^Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] ",
  74. "!d",
  75. "Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] D",
  76. "",
  77. " Relative Density Scan",
  78. "------------------------------------------------------------------------"
  79. "-----",
  80. "Sector 70 ==> 0 Warps : 6 NavHaz : 0% Anom "
  81. ": No",
  82. "Sector 441 ==> 100 Warps : 2 NavHaz : 0% Anom "
  83. ": No",
  84. "Sector ( 575) ==> 1000 Warps : 2 NavHaz : 15% Anom "
  85. ": Yes",
  86. "Sector 600 ==> 40 Warps : 6 NavHaz : 0% Anom "
  87. ": No",
  88. "Sector 629 ==> 1 Warps : 4 NavHaz : 0% Anom "
  89. ": No",
  90. "Sector 711 ==> 101 Warps : 6 NavHaz : 0% Anom "
  91. ": No",
  92. ""};
  93. for (auto line : lines) {
  94. if (line[0] == '^') {
  95. dir.server_prompt(line.substr(1), line.substr(1));
  96. } else if (line[0] == '!') {
  97. // client input
  98. dir.client_input(line.substr(1));
  99. } else {
  100. dir.server_line(line, line);
  101. }
  102. }
  103. // Check that each sector was correctly processed
  104. std::vector<int> sectors = {70, 441, 575, 600, 629, 711};
  105. YAML::Node sector_data;
  106. sector_data[70]["density"] = 0;
  107. sector_data[70]["warps"] = 6;
  108. sector_data[70]["navhaz"] = 0;
  109. sector_data[70]["anom"] = false;
  110. sector_data[70]["known"] = true;
  111. sector_data[441]["density"] = 100;
  112. sector_data[441]["warps"] = 2;
  113. sector_data[441]["navhaz"] = 0;
  114. sector_data[441]["anom"] = false;
  115. sector_data[441]["known"] = true;
  116. sector_data[575]["density"] = 1000;
  117. sector_data[575]["warps"] = 2;
  118. sector_data[575]["navhaz"] = 15;
  119. sector_data[575]["anom"] = true;
  120. sector_data[575]["known"] = false;
  121. sector_data[600]["density"] = 40;
  122. sector_data[600]["warps"] = 6;
  123. sector_data[600]["navhaz"] = 0;
  124. sector_data[600]["anom"] = false;
  125. sector_data[600]["known"] = true;
  126. sector_data[629]["density"] = 1;
  127. sector_data[629]["warps"] = 4;
  128. sector_data[629]["navhaz"] = 0;
  129. sector_data[629]["anom"] = false;
  130. sector_data[629]["known"] = true;
  131. sector_data[711]["density"] = 101;
  132. sector_data[711]["warps"] = 6;
  133. sector_data[711]["navhaz"] = 0;
  134. sector_data[711]["anom"] = false;
  135. sector_data[711]["known"] = true;
  136. for (auto sector : sectors) {
  137. // GTEST_COUT << "Testing Sector " << sector << std::endl;
  138. EXPECT_EQ(dir.galaxy.meta["density"][sector]["density"].as<int>(),
  139. sector_data[sector]["density"].as<int>()) << "Sector " << sector << " density";
  140. EXPECT_EQ(dir.galaxy.meta["density"][sector]["warps"].as<int>(),
  141. sector_data[sector]["warps"].as<int>()) << "Sector " << sector << " warps";
  142. EXPECT_EQ(dir.galaxy.meta["density"][sector]["navhaz"].as<int>(),
  143. sector_data[sector]["navhaz"].as<int>()) << "Sector " << sector << " navhaz";
  144. EXPECT_EQ(dir.galaxy.meta["density"][sector]["anom"].as<bool>(),
  145. sector_data[sector]["anom"].as<bool>()) << "Sector " << sector << " anom";
  146. EXPECT_EQ(dir.galaxy.meta["density"][sector]["known"].as<bool>(),
  147. sector_data[sector]["known"].as<bool>()) << "Sector " << sector << " known";
  148. }
  149. }
  150. } // namespace