test_main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #define CATCH_CONFIG_MAIN
  2. #include "catch.hpp"
  3. #include "rapidjson/document.h"
  4. #include "mstch/mstch.hpp"
  5. #include "test_context.hpp"
  6. #include "test_data.hpp"
  7. #include "specs_data.hpp"
  8. #include "specs_lambdas.hpp"
  9. using namespace mstchtest;
  10. mstch::node to_value(const rapidjson::Value& val) {
  11. if (val.IsString())
  12. return std::string{val.GetString()};
  13. if (val.IsBool())
  14. return val.GetBool();
  15. if (val.IsDouble())
  16. return val.GetDouble();
  17. if (val.IsInt())
  18. return val.GetInt();
  19. return mstch::node{};
  20. }
  21. mstch::array to_array(const rapidjson::Value& val);
  22. mstch::map to_object(const rapidjson::Value& val) {
  23. mstch::map ret;
  24. for (auto i = val.MemberBegin(); i != val.MemberEnd(); ++i) {
  25. if (i->value.IsArray())
  26. ret.insert(std::make_pair(i->name.GetString(), to_array(i->value)));
  27. else if (i->value.IsObject())
  28. ret.insert(std::make_pair(i->name.GetString(), to_object(i->value)));
  29. else
  30. ret.insert(std::make_pair(i->name.GetString(), to_value(i->value)));
  31. }
  32. return ret;
  33. }
  34. mstch::array to_array(const rapidjson::Value& val) {
  35. mstch::array ret;
  36. for (auto i = val.Begin(); i != val.End(); ++i) {
  37. if (i->IsArray())
  38. ret.push_back(to_array(*i));
  39. else if (i->IsObject())
  40. ret.push_back(to_object(*i));
  41. else
  42. ret.push_back(to_value(*i));
  43. }
  44. return ret;
  45. }
  46. mstch::node parse_with_rapidjson(const std::string& str) {
  47. rapidjson::Document doc;
  48. doc.Parse(str.c_str());
  49. return to_object(doc);
  50. }
  51. #define MSTCH_PARTIAL_TEST(x) TEST_CASE(#x) { \
  52. REQUIRE(x ## _txt == mstch::render(x ## _mustache, x ## _data, {{"partial", x ## _partial}})); \
  53. }
  54. #define MSTCH_TEST(x) TEST_CASE(#x) { \
  55. REQUIRE(x ## _txt == mstch::render(x ## _mustache, x ## _data)); \
  56. }
  57. #define SPECS_TEST(x) TEST_CASE("specs_" #x) { \
  58. using boost::get; \
  59. auto data = parse_with_rapidjson(x ## _json); \
  60. for (auto& test_item: get<mstch::array>(get<mstch::map>(data)["tests"])) {\
  61. auto test = get<mstch::map>(test_item); \
  62. std::map<std::string,std::string> partials; \
  63. if (test.count("partials")) \
  64. for (auto& partial_item: get<mstch::map>(test["partials"])) \
  65. partials.insert(std::make_pair(partial_item.first, get<std::string>(partial_item.second))); \
  66. mstch::map context; \
  67. for (auto& data_item: get<mstch::map>(test["data"])) \
  68. if (data_item.first == "lambda") \
  69. context.insert(std::make_pair(data_item.first, specs_lambdas[get<std::string>(test["name"])])); \
  70. else \
  71. context.insert(data_item); \
  72. SECTION(get<std::string>(test["name"])) \
  73. REQUIRE(mstch::render( \
  74. get<std::string>(test["template"]), \
  75. context, partials) == \
  76. get<std::string>(test["expected"])); \
  77. } \
  78. }
  79. MSTCH_TEST(ampersand_escape)
  80. MSTCH_TEST(apostrophe)
  81. MSTCH_TEST(array_of_strings)
  82. MSTCH_TEST(backslashes)
  83. MSTCH_TEST(bug_11_eating_whitespace)
  84. MSTCH_TEST(bug_length_property)
  85. MSTCH_TEST(changing_delimiters)
  86. MSTCH_TEST(comments)
  87. MSTCH_TEST(complex)
  88. MSTCH_TEST(context_lookup)
  89. MSTCH_TEST(delimiters)
  90. MSTCH_TEST(disappearing_whitespace)
  91. MSTCH_TEST(dot_notation)
  92. MSTCH_TEST(double_render)
  93. MSTCH_TEST(empty_list)
  94. MSTCH_TEST(empty_sections)
  95. MSTCH_TEST(empty_string)
  96. MSTCH_TEST(empty_template)
  97. MSTCH_TEST(error_eof_in_section)
  98. MSTCH_TEST(error_eof_in_tag)
  99. MSTCH_TEST(error_not_found)
  100. MSTCH_TEST(escaped)
  101. MSTCH_TEST(falsy)
  102. MSTCH_TEST(falsy_array)
  103. MSTCH_TEST(grandparent_context)
  104. MSTCH_TEST(higher_order_sections)
  105. MSTCH_TEST(implicit_iterator)
  106. MSTCH_TEST(included_tag)
  107. MSTCH_TEST(inverted_section)
  108. MSTCH_TEST(keys_with_questionmarks)
  109. MSTCH_TEST(multiline_comment)
  110. MSTCH_TEST(nested_dot)
  111. MSTCH_TEST(nested_higher_order_sections)
  112. MSTCH_TEST(nested_iterating)
  113. MSTCH_TEST(nesting)
  114. MSTCH_TEST(nesting_same_name)
  115. MSTCH_TEST(null_lookup_array)
  116. MSTCH_TEST(null_lookup_object)
  117. MSTCH_TEST(null_string)
  118. MSTCH_TEST(null_view)
  119. MSTCH_PARTIAL_TEST(partial_array)
  120. MSTCH_PARTIAL_TEST(partial_array_of_partials)
  121. MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit)
  122. MSTCH_PARTIAL_TEST(partial_empty)
  123. MSTCH_PARTIAL_TEST(partial_template)
  124. MSTCH_PARTIAL_TEST(partial_view)
  125. MSTCH_PARTIAL_TEST(partial_whitespace)
  126. MSTCH_TEST(recursion_with_same_names)
  127. MSTCH_TEST(reuse_of_enumerables)
  128. MSTCH_TEST(section_as_context)
  129. MSTCH_PARTIAL_TEST(section_functions_in_partials)
  130. MSTCH_TEST(simple)
  131. MSTCH_TEST(string_as_context)
  132. MSTCH_TEST(two_in_a_row)
  133. MSTCH_TEST(two_sections)
  134. MSTCH_TEST(unescaped)
  135. MSTCH_TEST(whitespace)
  136. MSTCH_TEST(zero_view)
  137. SPECS_TEST(comments)
  138. SPECS_TEST(delimiters)
  139. SPECS_TEST(interpolation)
  140. SPECS_TEST(inverted)
  141. SPECS_TEST(partials)
  142. SPECS_TEST(sections)
  143. SPECS_TEST(lambdas)