Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <iostream>
  2. #include <string>
  3. #include "CodeDweller/configuration.hpp"
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // Configuration ///////////////////////////////////////////////////////////////
  6. ////////////////////////////////////////////////////////////////////////////////
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // End of configuration ////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////////
  10. int nTotalTests = 0;
  11. int nPass = 0;
  12. int nFail = 0;
  13. bool result;
  14. #define NO_EXCEPTION_TERM(msg) \
  15. std::cout \
  16. << msg << " failed to throw exception at line " \
  17. << __LINE__ << "." << std::endl
  18. #define EXCEPTION_TERM(msg) \
  19. std::cout \
  20. << msg << " threw unexpected exception: " << e.what() << std::endl
  21. #define RETURN_FALSE(msg) \
  22. std::cout \
  23. << msg << " at line " << __LINE__ << std::endl; \
  24. return false;
  25. #define RUN_TEST(test) \
  26. std::cout << " " #test ": "; \
  27. std::cout.flush(); \
  28. result = test(); \
  29. std::cout << (result ? "ok" : "fail") << std::endl; \
  30. nTotalTests++; \
  31. if (result) nPass++; else nFail++;
  32. #define SUMMARY \
  33. std::cout \
  34. << "\nPass: " << nPass \
  35. << ", Fail: " << nFail \
  36. << ", Total: " << nTotalTests << std::endl
  37. ////////////////////////////////////////////////////////////////////////////////
  38. // Tests ///////////////////////////////////////////////////////////////////////
  39. ////////////////////////////////////////////////////////////////////////////////
  40. bool testSimilarElementName() {
  41. std::string stageContent, stage1Content, stage2Content;
  42. CodeDweller::ConfigurationElement reader("elem");
  43. reader
  44. .Element("stage", stageContent)
  45. .End("stage")
  46. .Element("stage1", stage1Content)
  47. .End("stage1")
  48. .Element("stage2", stage2Content)
  49. .End("stage2")
  50. .End("elem");
  51. std::string xml;
  52. std::string expectedStageContent = "StageContent";
  53. std::string expectedStage1Content = "Stage1Content";
  54. std::string expectedStage2Content = "Stage2Content";
  55. xml =
  56. "<elem>\n"
  57. " <stage2>" + expectedStage2Content + "</stage2>\n"
  58. " <stage>" + expectedStageContent + "</stage>\n"
  59. " <stage1>" + expectedStage1Content + "</stage1>\n"
  60. "</elem>";
  61. CodeDweller::ConfigurationData confData(xml.data(), xml.size());
  62. reader.initialize();
  63. if (0 == reader.interpret(confData)) {
  64. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  65. }
  66. if (expectedStageContent != stageContent) {
  67. RETURN_FALSE("<stage> content read failure");
  68. }
  69. if (expectedStage1Content != stage1Content) {
  70. RETURN_FALSE("<stage1> content read failure");
  71. }
  72. if (expectedStage2Content != stage2Content) {
  73. RETURN_FALSE("<stage2> content read failure");
  74. }
  75. }
  76. bool testSimilarAttributeName() {
  77. std::string nameAttr, naAttr, nameLongAttr;
  78. CodeDweller::ConfigurationElement reader("elem");
  79. reader
  80. .Element("stage")
  81. .Attribute("name", nameAttr)
  82. .Attribute("na", naAttr)
  83. .Attribute("nameLong", nameLongAttr)
  84. .End("stage")
  85. .End("elem");
  86. std::string xml;
  87. std::string expectedNameAttr = "NameValue";
  88. std::string expectedNaAttr = "NaValue";
  89. std::string expectedNameLongAttr = "NameLongValue";
  90. xml =
  91. "<elem>\n"
  92. " <stage\n"
  93. " name='" + expectedNameAttr + "'\n"
  94. " na='" + expectedNaAttr + "'\n"
  95. " nameLong='" + expectedNameLongAttr + "'/>"
  96. "</elem>";
  97. CodeDweller::ConfigurationData confData(xml.data(), xml.size());
  98. reader.initialize();
  99. if (0 == reader.interpret(confData)) {
  100. RETURN_FALSE("Error parsing XML: " + confData.Log.str());
  101. }
  102. if (expectedNameAttr != nameAttr) {
  103. RETURN_FALSE("\"name\" attribute read failure");
  104. }
  105. if (expectedNaAttr != naAttr) {
  106. RETURN_FALSE("\"na\" attribute read failure");
  107. }
  108. if (expectedNameLongAttr != nameLongAttr) {
  109. RETURN_FALSE("\"nameLong\" attribute read failure");
  110. }
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////
  113. // End of tests ////////////////////////////////////////////////////////////////
  114. ////////////////////////////////////////////////////////////////////////////////
  115. int main()
  116. {
  117. std::cout << "CodeDweller::configuration unit tests" << std::endl
  118. << std::endl;
  119. RUN_TEST(testSimilarElementName);
  120. RUN_TEST(testSimilarAttributeName);
  121. SUMMARY;
  122. return 0;
  123. }