test_info_parser.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #include "test_utils.hpp"
  11. #include <boost/property_tree/info_parser.hpp>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // Test data
  14. const char *ok_data_1 =
  15. ";Test file for info_parser\n"
  16. "\n"
  17. "key1 data1\n"
  18. "{\n"
  19. "\tkey data\n"
  20. "}\n"
  21. "#include \"testok1_inc.info\"\n"
  22. "key2 \"data2 \" {\n"
  23. "\tkey data\n"
  24. "}\n"
  25. "#\tinclude \"testok1_inc.info\"\n"
  26. "key3 \"data\"\n"
  27. "\t \"3\" {\n"
  28. "\tkey data\n"
  29. "}\n"
  30. "\t#include \"testok1_inc.info\"\n"
  31. "\n"
  32. "\"key4\" data4\n"
  33. "{\n"
  34. "\tkey data\n"
  35. "}\n"
  36. "#include \"testok1_inc.info\"\n"
  37. "\"key.5\" \"data.5\" { \n"
  38. "\tkey data \n"
  39. "}\n"
  40. "#\tinclude \"testok1_inc.info\"\n"
  41. "\"key6\" \"data\"\n"
  42. "\t \"6\" {\n"
  43. "\tkey data\n"
  44. "}\n"
  45. "\t#include \"testok1_inc.info\"\n"
  46. " \n"
  47. "key1 data1; comment\n"
  48. "{; comment\n"
  49. "\tkey data; comment\n"
  50. "}; comment\n"
  51. "#include \"testok1_inc.info\"\n"
  52. "key2 \"data2 \" {; comment\n"
  53. "\tkey data; comment\n"
  54. "}; comment\n"
  55. "#\tinclude \"testok1_inc.info\"\n"
  56. "key3 \"data\"; comment\n"
  57. "\t \"3\" {; comment\n"
  58. "\tkey data; comment\n"
  59. "}; comment\n"
  60. "\t#include \"testok1_inc.info\"\n"
  61. "\n"
  62. "\"key4\" data4; comment\n"
  63. "{; comment\n"
  64. "\tkey data; comment\n"
  65. "}; comment\n"
  66. "#include \"testok1_inc.info\"\n"
  67. "\"key.5\" \"data.5\" {; comment\n"
  68. "\tkey data; comment\n"
  69. "}; comment\n"
  70. "#\tinclude \"testok1_inc.info\"\n"
  71. "\"key6\" \"data\"; comment\n"
  72. "\t \"6\" {; comment\n"
  73. "\tkey data; comment\n"
  74. "}; comment\n"
  75. "\t#include \"testok1_inc.info\"\n"
  76. "\\\\key\\t7 data7\\n\\\"data7\\\"\n"
  77. "{\n"
  78. "\tkey data\n"
  79. "}\n"
  80. "\"\\\\key\\t8\" \"data8\\n\\\"data8\\\"\"\n"
  81. "{\n"
  82. "\tkey data\n"
  83. "}\n"
  84. "\n";
  85. const char *ok_data_1_inc =
  86. ";Test file for info_parser\n"
  87. "\n"
  88. "inc_key inc_data ;;; comment\\";
  89. const char *ok_data_2 =
  90. "";
  91. const char *ok_data_3 =
  92. "key1 \"\"\n"
  93. "key2 \"\"\n"
  94. "key3 \"\"\n"
  95. "key4 \"\"\n";
  96. const char *ok_data_4 =
  97. "key1 data key2 data";
  98. const char *ok_data_5 =
  99. "key { key \"\" key \"\" }\n";
  100. const char *ok_data_6 =
  101. "\"key with spaces\" \"data with spaces\"\n"
  102. "\"key with spaces\" \"multiline data\"\\\n"
  103. "\"cont\"\\\n"
  104. "\"cont\"";
  105. const char *error_data_1 =
  106. ";Test file for info_parser\n"
  107. "#include \"bogus_file\"\n"; // Nonexistent include file
  108. const char *error_data_2 =
  109. ";Test file for info_parser\n"
  110. "key \"data with bad escape: \\q\"\n"; // Bad escape
  111. const char *error_data_3 =
  112. ";Test file for info_parser\n"
  113. "{\n"; // Opening brace without key
  114. const char *error_data_4 =
  115. ";Test file for info_parser\n"
  116. "}\n"; // Closing brace without opening brace
  117. const char *error_data_5 =
  118. ";Test file for info_parser\n"
  119. "key data\n"
  120. "{\n"
  121. ""; // No closing brace
  122. struct ReadFunc
  123. {
  124. template<class Ptree>
  125. void operator()(const std::string &filename, Ptree &pt) const
  126. {
  127. boost::property_tree::read_info(filename, pt);
  128. }
  129. };
  130. struct WriteFunc
  131. {
  132. template<class Ptree>
  133. void operator()(const std::string &filename, const Ptree &pt) const
  134. {
  135. boost::property_tree::write_info(filename, pt);
  136. }
  137. };
  138. template<class Ptree>
  139. void test_info_parser()
  140. {
  141. using namespace boost::property_tree;
  142. generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
  143. (
  144. ReadFunc(), WriteFunc(), ok_data_1, ok_data_1_inc,
  145. "testok1.info", "testok1_inc.info", "testok1out.info", 45, 240, 192
  146. );
  147. generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
  148. (
  149. ReadFunc(), WriteFunc(), ok_data_2, NULL,
  150. "testok2.info", NULL, "testok2out.info", 1, 0, 0
  151. );
  152. generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
  153. (
  154. ReadFunc(), WriteFunc(), ok_data_3, NULL,
  155. "testok3.info", NULL, "testok3out.info", 5, 0, 16
  156. );
  157. generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
  158. (
  159. ReadFunc(), WriteFunc(), ok_data_4, NULL,
  160. "testok4.info", NULL, "testok4out.info", 3, 8, 8
  161. );
  162. generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
  163. (
  164. ReadFunc(), WriteFunc(), ok_data_5, NULL,
  165. "testok5.info", NULL, "testok5out.info", 4, 0, 9
  166. );
  167. generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
  168. (
  169. ReadFunc(), WriteFunc(), ok_data_6, NULL,
  170. "testok6.info", NULL, "testok6out.info", 3, 38, 30
  171. );
  172. generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error>
  173. (
  174. ReadFunc(), WriteFunc(), error_data_1, NULL,
  175. "testerr1.info", NULL, "testerr1out.info", 2
  176. );
  177. generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error>
  178. (
  179. ReadFunc(), WriteFunc(), error_data_2, NULL,
  180. "testerr2.info", NULL, "testerr2out.info", 2
  181. );
  182. generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error>
  183. (
  184. ReadFunc(), WriteFunc(), error_data_3, NULL,
  185. "testerr3.info", NULL, "testerr3out.info", 2
  186. );
  187. generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error>
  188. (
  189. ReadFunc(), WriteFunc(), error_data_4, NULL,
  190. "testerr4.info", NULL, "testerr4out.info", 2
  191. );
  192. generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error>
  193. (
  194. ReadFunc(), WriteFunc(), error_data_5, NULL,
  195. "testerr5.info", NULL, "testerr5out.info", 4
  196. );
  197. // Test read with default ptree
  198. {
  199. Ptree pt, default_pt;
  200. pt.put_value(1);
  201. default_pt.put_value(2);
  202. BOOST_CHECK(pt != default_pt);
  203. read_info("nonexisting file.nonexisting file", pt, default_pt);
  204. BOOST_CHECK(pt == default_pt);
  205. }
  206. }
  207. int test_main(int argc, char *argv[])
  208. {
  209. using namespace boost::property_tree;
  210. test_info_parser<ptree>();
  211. test_info_parser<iptree>();
  212. #ifndef BOOST_NO_CWCHAR
  213. test_info_parser<wptree>();
  214. test_info_parser<wiptree>();
  215. #endif
  216. return 0;
  217. }