catch-parser.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "oopless-parser.hpp"
  2. #include "catch.hpp"
  3. #include <iostream>
  4. std::vector<TFace> getfaces(const std::string &test) {
  5. using namespace std;
  6. stringstream ss;
  7. ss << test;
  8. return parse_face<stringstream>(ss);
  9. }
  10. TEST_CASE ("face parsing case: faces with 3 indices", "[Parser]" ) {
  11. std::string sample = " 0.000000 0.000000 1.000000 1.000000 0.000000 6 0 7 0xff727278 common/materials/stone/stone";
  12. struct TFace f = getfaces(sample).front();
  13. REQUIRE (f.m_Indices.size() == 3);
  14. }
  15. TEST_CASE ("face parsing case: faces with 4 indices", "[Parser]") {
  16. std::string sample = " 0.000000 0.000000 1.000000 1.000000 0.000000 3 1 0 4 0xffffffff common/materials/metal/aluminum";
  17. struct TFace f = getfaces(sample).front();
  18. REQUIRE (f.m_Indices.size() == 4);
  19. }
  20. TEST_CASE ("face parsing case: faces with 5 indices", "[Parser]") {
  21. std::string sample = " 0.000000 0.000000 1.000000 1.000000 0.000000 2 7 5 7 4 0xff727278 common/materials/stone/stone";
  22. struct TFace f = getfaces(sample).front();
  23. REQUIRE (f.m_Indices.size() == 5);
  24. }
  25. TEST_CASE ("face parsing case: faces with 9 indices", "[Parser]") {
  26. std::string sample = " 0.000000 0.000000 1.000000 1.000000 0.000000 2 7 5 7 4 7 5 7 4 0xff727278 common/materials/stone/stone";
  27. struct TFace f = getfaces(sample).front();
  28. REQUIRE (f.m_Indices.size() == 9);
  29. }
  30. TEST_CASE("face parsing case: faces with no material", "[Parser]") {
  31. std::string sample =
  32. " 0.000000 0.000000 1.000000 1.000000 0.000000 1 0 4 5 0x00000000 ";
  33. std::string answer; //empty string
  34. struct TFace f = getfaces(sample).front();
  35. REQUIRE (f.m_Material == answer);
  36. }
  37. TEST_CASE("face parsing case: face's material name is preserved", "[Parser]") {
  38. std::string sample =
  39. " 0.000000 0.000000 1.000000 1.000000 0.000000 3 1 0 4 0xffffffff common/materials/metal/aluminum";
  40. std::string answer = "common/materials/metal/aluminum";
  41. struct TFace f = getfaces(sample).front();
  42. REQUIRE (f.m_Material == answer);
  43. }
  44. TEST_CASE("face parsing case: face's color code is preserved", "[Parser]") {
  45. std::string sample =
  46. " 0.000000 0.000000 1.000000 1.000000 0.000000 2 4 1 5 0xffa5a5a5 common/materials/metal/p_metal3";
  47. std::string answer = "0xffa5a5a5";
  48. struct TFace f = getfaces(sample).front();
  49. REQUIRE (f.hex == answer);
  50. }
  51. TEST_CASE("brush parser case: returns populated Vertices and Faces list", "[Parser]") {
  52. std::string sample = " vertices\n\
  53. -23.000000 -20.000000 15.000000\n\
  54. 9.000000 -20.000000 15.000000\n\
  55. 9.000000 -20.000000 -17.000000\n\
  56. -15.000000 -24.000000 -9.000000\n\
  57. -23.000000 -20.000000 -17.000000\n\
  58. 1.000000 -24.000000 7.000000\n\
  59. 1.000000 -24.000000 -9.000000\n\
  60. -15.000000 -24.000000 7.000000\n\
  61. faces\n\
  62. 0.000000 0.000000 1.000000 1.000000 0.000000 0 1 2 4 0xff888888 common/materials/metal/p_metal3\n\
  63. 0.000000 0.000000 1.000000 1.000000 0.000000 3 4 2 6 0xff888888 common/materials/metal/p_metal3\n\
  64. 0.000000 0.000000 1.000000 1.000000 0.000000 2 1 5 6 0xff888888 common/materials/metal/p_metal3\n\
  65. 0.000000 0.000000 1.000000 1.000000 0.000000 0 4 3 7 0xff888888 common/materials/metal/p_metal3\n\
  66. 0.000000 0.000000 1.000000 1.000000 131.810318 5 1 0 7 0xff888888 common/materials/metal/p_metal3\n\
  67. 0.000000 0.000000 1.000000 1.000000 0.000000 3 6 5 7 0xff888888 common/materials/metal/p_metal3";
  68. std::stringstream ss;
  69. ss << sample;
  70. struct TBrush b = parse_brush<std::stringstream>(ss);
  71. REQUIRE ((b.m_Vertices.size() == 8 && b.m_Faces.size() == 6));
  72. }
  73. TEST_CASE("brush parser case: returns empty when given empty input", "[Parser]") {
  74. std::string empty;
  75. std::stringstream ss;
  76. ss << empty;
  77. struct TBrush b = parse_brush<std::stringstream>(ss);
  78. REQUIRE ((b.m_Vertices.size() == 0 && b.m_Faces.size() == 0));
  79. }
  80. TEST_CASE("get entity case: recognizes brush entites", "[Parser]") {
  81. std::string sample = " type JumpPad\n\
  82. String32 target jp2\n\
  83. brush\n\
  84. vertices\n\
  85. 324.000000 36.000000 -228.000000\n\
  86. 352.000000 36.000000 -228.000000\n\
  87. 352.000000 36.000000 -380.000000\n\
  88. 324.000000 36.000000 -380.000000\n\
  89. 324.000000 32.000000 -228.000000\n\
  90. 352.000000 32.000000 -228.000000\n\
  91. 352.000000 32.000000 -380.000000\n\
  92. 324.000000 32.000000 -380.000000\n\
  93. faces\n\
  94. 0.000000 0.000000 -0.250000 -0.000000 270.000000 0 1 2 3 0x00000000 \n\
  95. 0.000000 0.000000 -0.250000 -0.000000 0.000000 6 5 4 7 0x00000000 \n\
  96. 0.000000 0.000000 -0.250000 -0.000000 0.000000 2 1 5 6 0x00000000 \n\
  97. 0.000000 0.000000 -0.250000 -0.000000 0.000000 0 3 7 4 0x00000000 \n\
  98. 0.000000 0.000000 -0.250000 -0.000000 0.000000 3 2 6 7 0x00000000 \n\
  99. 0.000000 0.000000 -0.250000 -0.000000 0.000000 1 0 4 5 0x00000000 ";
  100. std::stringstream ss;
  101. ss << sample;
  102. std::vector<std::string> ent = get_entity<std::stringstream>(ss);
  103. unsigned int count = 0;
  104. for (const std::string &x : ent) {
  105. if (x.find(KEYWORD_BRUSH) != std::string::npos) {
  106. count++;
  107. }
  108. }
  109. REQUIRE ((count == 1 && ent.size() == 19));
  110. }
  111. TEST_CASE("get entity case: omits unaffiliated brush") {
  112. std::string sample = " type WorldSpawn\n\
  113. String32 targetGameOverCamera end\n\
  114. ColourXRGB32 colorTeamA ff801313\n\
  115. ColourXRGB32 fogColor ff7f5122\n\
  116. Float fogDistanceEnd 3500.000000\n\
  117. String256 title Static Discharge\n\
  118. UInt8 playersMax 6\n\
  119. brush\n\
  120. vertices\n\
  121. -19.000000 28.000000 23.000000\n\
  122. 29.000000 28.000000 23.000000\n\
  123. 29.000000 28.000000 -25.000000\n\
  124. -19.000000 28.000000 -25.000000\n\
  125. -19.000000 -20.000000 23.000000\n\
  126. 29.000000 -20.000000 23.000000\n\
  127. 29.000000 -20.000000 -25.000000\n\
  128. -19.000000 -20.000000 -25.000000\n\
  129. faces\n\
  130. 0.000000 0.000000 1.000000 1.000000 0.000000 0 1 2 3 0xff888888 common/materials/metal/p_metal3\n\
  131. 0.000000 0.000000 1.000000 1.000000 0.000000 6 5 4 7 0xff888888 common/materials/metal/p_metal3\n\
  132. 0.000000 0.000000 1.000000 1.000000 0.000000 2 1 5 6 0xff888888 common/materials/metal/p_metal3\n\
  133. 0.000000 0.000000 1.000000 1.000000 0.000000 0 3 7 4 0xff888888 common/materials/metal/p_metal3\n\
  134. 0.000000 0.000000 1.000000 1.000000 0.000000 3 2 6 7 0xff888888 common/materials/metal/p_metal3\n\
  135. 0.000000 0.000000 1.000000 1.000000 0.000000 1 0 4 5 0xff888888 common/materials/metal/p_metal3";
  136. std::stringstream ss;
  137. ss << sample;
  138. std::vector<std::string> ent = get_entity<std::stringstream>(ss);
  139. unsigned int count = 0;
  140. for (const std::string &x : ent) {
  141. if (x.find(KEYWORD_BRUSH) != std::string::npos) {
  142. count++;
  143. }
  144. }
  145. REQUIRE ((count == 0 && ent.size() == 7));
  146. }