reader_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. " (mymapping (a 1) (b 2))\n"
  29. ")\n");
  30. auto doc = ReaderDocument::parse(in);
  31. auto root = doc.get_root();
  32. ASSERT_EQ("supertux-test", root.get_name());
  33. auto mapping = root.get_mapping();
  34. {
  35. bool mybool;
  36. mapping.get("mybool", mybool);
  37. ASSERT_EQ(true, mybool);
  38. }
  39. {
  40. int myint;
  41. mapping.get("myint", myint);
  42. ASSERT_EQ(123456789, myint);
  43. }
  44. {
  45. float myfloat;
  46. mapping.get("myfloat", myfloat);
  47. ASSERT_EQ(1.125, myfloat);
  48. }
  49. {
  50. std::string mystring;
  51. mapping.get("mystring", mystring);
  52. ASSERT_EQ("Hello World", mystring);
  53. }
  54. {
  55. std::string mystringtrans;
  56. mapping.get("mystringtrans", mystringtrans);
  57. ASSERT_EQ("Hello World", mystringtrans);
  58. }
  59. {
  60. ReaderMapping child_mapping;
  61. mapping.get("mymapping", child_mapping);
  62. int a;
  63. child_mapping.get("a", a);
  64. ASSERT_EQ(1, a);
  65. int b;
  66. child_mapping.get("b", b);
  67. ASSERT_EQ(2, b);
  68. }
  69. {
  70. bool mybool;
  71. int myint;
  72. float myfloat;
  73. ASSERT_THROW({mapping.get("mybool", myfloat);}, std::runtime_error);
  74. ASSERT_THROW({mapping.get("myint", mybool);}, std::runtime_error);
  75. ASSERT_THROW({mapping.get("myfloat", myint);}, std::runtime_error);
  76. ASSERT_THROW({mapping.get("mymapping", myint);}, std::runtime_error);
  77. }
  78. }
  79. TEST(ReaderTest, syntax_error)
  80. {
  81. std::istringstream in(
  82. "(supertux-test\n"
  83. " (mybool #t err)\r"
  84. " (myint 123456789 err)\r\n"
  85. " (myfloat 1.125 err)\n\r"
  86. " (mystring \"Hello World\" err)\n"
  87. " (mystringtrans (_ \"Hello World\" err))\n"
  88. " (mymapping err (a 1) (b 2))\n"
  89. ")\n");
  90. auto doc = ReaderDocument::parse(in);
  91. auto root = doc.get_root();
  92. ASSERT_EQ("supertux-test", root.get_name());
  93. auto mapping = root.get_mapping();
  94. bool mybool;
  95. int myint;
  96. float myfloat;
  97. ReaderMapping mymapping;
  98. ASSERT_THROW({mapping.get("mybool", mybool);}, std::runtime_error);
  99. ASSERT_THROW({mapping.get("myint", myint);}, std::runtime_error);
  100. ASSERT_THROW({mapping.get("myfloat", myfloat);}, std::runtime_error);
  101. mapping.get("mymapping", mymapping);
  102. ASSERT_THROW({mymapping.get("a", myint);}, std::runtime_error);
  103. ASSERT_THROW({mymapping.get("b", myint);}, std::runtime_error);
  104. }
  105. /* EOF */