reader_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SuperTux
  2. // Copyright (C) 2015 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <gtest/gtest.h>
  17. #include "util/reader_document.hpp"
  18. #include "util/reader_mapping.hpp"
  19. TEST(ReaderTest, get)
  20. {
  21. std::istringstream in(
  22. "(supertux-test\n"
  23. " (mybool #t)\r"
  24. " (myint 123456789)\r\n"
  25. " (myfloat 1.125)\n\r"
  26. " (mystring \"Hello World\")\n"
  27. " (mystringtrans (_ \"Hello World\"))\n"
  28. " (myboolarray #t #f #t #f)\n"
  29. " (myintarray 5 4 3 2 1 0)\n"
  30. " (myfloatarray 6.5 5.25 4.125 3.0625 2.0 1.0 0.5 0.25 0.125)\n"
  31. " (mystringarray \"One\" \"Two\" \"Three\")\n"
  32. " (mymapping (a 1) (b 2))\n"
  33. " (mycustom \"1234\")\n"
  34. ")\n");
  35. auto doc = ReaderDocument::from_stream(in);
  36. auto root = doc.get_root();
  37. ASSERT_EQ("supertux-test", root.get_name());
  38. auto mapping = root.get_mapping();
  39. {
  40. bool mybool;
  41. mapping.get("mybool", mybool);
  42. ASSERT_EQ(true, mybool);
  43. }
  44. {
  45. int myint;
  46. mapping.get("myint", myint);
  47. ASSERT_EQ(123456789, myint);
  48. }
  49. {
  50. float myfloat;
  51. mapping.get("myfloat", myfloat);
  52. ASSERT_EQ(1.125, myfloat);
  53. }
  54. {
  55. std::string mystring;
  56. mapping.get("mystring", mystring);
  57. ASSERT_EQ("Hello World", mystring);
  58. }
  59. {
  60. std::string mystringtrans;
  61. mapping.get("mystringtrans", mystringtrans);
  62. ASSERT_EQ("Hello World", mystringtrans);
  63. }
  64. {
  65. std::vector<bool> expected{ true, false, true, false };
  66. std::vector<bool> result;
  67. mapping.get("myboolarray", result);
  68. ASSERT_EQ(expected, result);
  69. }
  70. {
  71. std::vector<int> expected{ 5, 4, 3, 2, 1, 0 };
  72. std::vector<int> result;
  73. mapping.get("myintarray", result);
  74. ASSERT_EQ(expected, result);
  75. }
  76. {
  77. std::vector<float> expected({6.5f, 5.25f, 4.125f, 3.0625f, 2.0f, 1.0f, 0.5f, 0.25f, 0.125f});
  78. std::vector<float> result;
  79. mapping.get("myfloatarray", result);
  80. ASSERT_EQ(expected, result);
  81. }
  82. {
  83. std::vector<std::string> expected{"One", "Two", "Three"};
  84. std::vector<std::string> result;
  85. mapping.get("mystringarray", result);
  86. ASSERT_EQ(expected, result);
  87. }
  88. {
  89. boost::optional<ReaderMapping> child_mapping;
  90. mapping.get("mymapping", child_mapping);
  91. int a;
  92. child_mapping->get("a", a);
  93. ASSERT_EQ(1, a);
  94. int b;
  95. child_mapping->get("b", b);
  96. ASSERT_EQ(2, b);
  97. }
  98. {
  99. auto from_string = [](const std::string& text){ return std::stoi(text); };
  100. int value = 0;
  101. mapping.get_custom("mycustom", value, from_string);
  102. ASSERT_EQ(1234, value);
  103. int value2 = 0;
  104. mapping.get_custom("does-not-exist", value2, from_string);
  105. ASSERT_EQ(0, value2);
  106. int value3 = 0;
  107. mapping.get_custom("does-not-exist", value3, from_string, 4321);
  108. ASSERT_EQ(4321, value3);
  109. }
  110. {
  111. bool mybool;
  112. int myint;
  113. float myfloat;
  114. ASSERT_THROW({mapping.get("mybool", myfloat);}, std::runtime_error);
  115. ASSERT_THROW({mapping.get("myint", mybool);}, std::runtime_error);
  116. ASSERT_THROW({mapping.get("myfloat", myint);}, std::runtime_error);
  117. ASSERT_THROW({mapping.get("mymapping", myint);}, std::runtime_error);
  118. }
  119. }
  120. TEST(ReaderTest, syntax_error)
  121. {
  122. std::istringstream in(
  123. "(supertux-test\n"
  124. " (mybool #t err)\r"
  125. " (myint 123456789 err)\r\n"
  126. " (myfloat 1.125 err)\n\r"
  127. " (mystring \"Hello World\" err)\n"
  128. " (mystringtrans (_ \"Hello World\" err))\n"
  129. " (mymapping err (a 1) (b 2))\n"
  130. ")\n");
  131. auto doc = ReaderDocument::from_stream(in);
  132. auto root = doc.get_root();
  133. ASSERT_EQ("supertux-test", root.get_name());
  134. auto mapping = root.get_mapping();
  135. bool mybool;
  136. int myint;
  137. float myfloat;
  138. boost::optional<ReaderMapping> mymapping;
  139. ASSERT_THROW({mapping.get("mybool", mybool);}, std::runtime_error);
  140. ASSERT_THROW({mapping.get("myint", myint);}, std::runtime_error);
  141. ASSERT_THROW({mapping.get("myfloat", myfloat);}, std::runtime_error);
  142. mapping.get("mymapping", mymapping);
  143. ASSERT_THROW({mymapping->get("a", myint);}, std::runtime_error);
  144. ASSERT_THROW({mymapping->get("b", myint);}, std::runtime_error);
  145. }
  146. /* EOF */