helper_test_suite.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <iostream>
  24. #include <stdexcept>
  25. #include <helpers/test_suite.hpp>
  26. #include <helpers/xml_helper.hpp>
  27. #include <helpers/assertions.hpp>
  28. #include <xlnt/utils/exceptions.hpp>
  29. class helper_test_suite : public test_suite
  30. {
  31. public:
  32. helper_test_suite()
  33. {
  34. register_test(test_bootstrap_asserts);
  35. register_test(test_other_asserts);
  36. register_test(test_compare);
  37. }
  38. // bootstrap asserts are used by other assert tests so they need to be tested first
  39. // without using themselves
  40. void test_bootstrap_asserts()
  41. {
  42. auto function_that_throws = []()
  43. {
  44. throw xlnt::exception("test");
  45. };
  46. auto function_that_doesnt_throw = []()
  47. {
  48. return std::string("abc");
  49. };
  50. // 1. function that throws no exception shouldn't cause exception to be thrown in xlnt_assert_throws_nothing
  51. try
  52. {
  53. xlnt_assert_throws_nothing(function_that_doesnt_throw());
  54. // good
  55. }
  56. catch (...)
  57. {
  58. throw xlnt::exception("test failed");
  59. }
  60. // 2. function that throws an exception should cause exception to be thrown in xlnt_assert_throws_nothing
  61. try
  62. {
  63. xlnt_assert_throws_nothing(function_that_throws());
  64. }
  65. catch (std::runtime_error)
  66. {
  67. // good
  68. }
  69. catch (...)
  70. {
  71. throw xlnt::exception("test failed");
  72. }
  73. // 3. function that doesn't throw an exception should cause exception to be thrown in xlnt_assert_throws
  74. try
  75. {
  76. xlnt_assert_throws(function_that_throws(), std::runtime_error);
  77. // good
  78. }
  79. catch (...)
  80. {
  81. throw xlnt::exception("test failed");
  82. }
  83. // 4. function that doesn't throw an exception should cause exception to be thrown in xlnt_assert_throws
  84. try
  85. {
  86. xlnt_assert_throws(function_that_doesnt_throw(), std::runtime_error);
  87. throw xlnt::exception("test failed");
  88. }
  89. catch (xlnt::exception)
  90. {
  91. // good
  92. }
  93. catch (...)
  94. {
  95. throw xlnt::exception("test failed");
  96. }
  97. // 5. function that doesn't throw an exception should cause exception to be thrown in xlnt_assert_throws
  98. try
  99. {
  100. xlnt_assert_throws(function_that_throws(), xlnt::exception);
  101. throw xlnt::exception("test failed");
  102. }
  103. catch (xlnt::exception)
  104. {
  105. // good
  106. }
  107. catch (...)
  108. {
  109. throw xlnt::exception("test failed");
  110. }
  111. }
  112. void test_other_asserts()
  113. {
  114. xlnt_assert_throws_nothing(xlnt_assert(true));
  115. xlnt_assert_throws(xlnt_assert(false), xlnt::exception);
  116. xlnt_assert_throws_nothing(xlnt_assert_equals(1, 1));
  117. xlnt_assert_throws(xlnt_assert_equals(1, 2), xlnt::exception);
  118. xlnt_assert_throws_nothing(xlnt_assert_differs(1, 2));
  119. xlnt_assert_throws(xlnt_assert_differs(1, 1), xlnt::exception);
  120. xlnt_assert_throws_nothing(xlnt_assert_delta(1.0, 1.05, 0.06));
  121. xlnt_assert_throws(xlnt_assert_delta(1.0, 1.05, 0.04), xlnt::exception);
  122. }
  123. void test_compare()
  124. {
  125. xlnt_assert(!xml_helper::compare_xml_exact("<a/>", "<b/>", true));
  126. xlnt_assert(!xml_helper::compare_xml_exact("<a/>", "<a b=\"4\"/>", true));
  127. xlnt_assert(!xml_helper::compare_xml_exact("<a b=\"3\"/>", "<a/>", true));
  128. xlnt_assert(!xml_helper::compare_xml_exact("<a c=\"4\"/>", "<a b=\"4\"/>", true));
  129. xlnt_assert(!xml_helper::compare_xml_exact("<a b=\"3\"/>", "<a b=\"4\"/>", true));
  130. xlnt_assert(!xml_helper::compare_xml_exact("<a>text</a>", "<a>txet</a>", true));
  131. xlnt_assert(!xml_helper::compare_xml_exact("<a>text</a>", "<a><b>txet</b></a>", true));
  132. xlnt_assert(xml_helper::compare_xml_exact("<a/>", "<a> </a>", true));
  133. xlnt_assert(xml_helper::compare_xml_exact("<a b=\"3\"/>", "<a b=\"3\"></a>", true));
  134. xlnt_assert(xml_helper::compare_xml_exact("<a>text</a>", "<a>text</a>", true));
  135. }
  136. };
  137. static helper_test_suite x;