test.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2005 Daniel Wallin, Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef TEST_050415_HPP
  20. #define TEST_050415_HPP
  21. #include <boost/preprocessor/cat.hpp>
  22. #include <luabind/error.hpp>
  23. extern "C"
  24. {
  25. #include "lua.h"
  26. #include "lauxlib.h"
  27. #include "lualib.h"
  28. }
  29. void report_failure(char const* str, char const* file, int line);
  30. #if defined(_MSC_VER)
  31. #define COUNTER_GUARD(x)
  32. #else
  33. #define COUNTER_GUARD(type) \
  34. struct BOOST_PP_CAT(type, _counter_guard) \
  35. { \
  36. ~BOOST_PP_CAT(type, _counter_guard()) \
  37. { \
  38. TEST_CHECK(counted_type<type>::count == 0); \
  39. } \
  40. } BOOST_PP_CAT(type, _guard)
  41. #endif
  42. #define TEST_REPORT_AUX(x, line, file) \
  43. report_failure(x, line, file)
  44. #define TEST_CHECK(x) \
  45. if (!(x)) \
  46. TEST_REPORT_AUX("TEST_CHECK failed: \"" #x "\"", __FILE__, __LINE__)
  47. #define TEST_ERROR(x) \
  48. TEST_REPORT_AUX((std::string("ERROR: \"") + x + "\"").c_str(), __FILE__, __LINE__)
  49. #define TEST_NOTHROW(x) \
  50. try \
  51. { \
  52. x; \
  53. } \
  54. catch (...) \
  55. { \
  56. TEST_ERROR("Exception thrown: " #x); \
  57. }
  58. void dostring(lua_State* L, char const* str);
  59. template<class T>
  60. struct counted_type
  61. {
  62. static int count;
  63. counted_type()
  64. {
  65. ++count;
  66. }
  67. counted_type(counted_type const&)
  68. {
  69. ++count;
  70. }
  71. ~counted_type()
  72. {
  73. TEST_CHECK(--count >= 0);
  74. }
  75. };
  76. template<class T>
  77. int counted_type<T>::count = 0;
  78. #define DOSTRING_EXPECTED(state_, str, expected) \
  79. { \
  80. try \
  81. { \
  82. dostring(state_, str); \
  83. } \
  84. catch (luabind::error const& e) \
  85. { \
  86. using namespace std; \
  87. if (std::strcmp( \
  88. lua_tostring(e.state(), -1) \
  89. , (char const*)expected)) \
  90. { \
  91. TEST_ERROR(lua_tostring(e.state(), -1)); \
  92. lua_pop(L, 1); \
  93. } \
  94. } \
  95. catch (std::string const& s) \
  96. { \
  97. if (s != expected) \
  98. TEST_ERROR(s.c_str()); \
  99. } \
  100. }
  101. #define DOSTRING(state_, str) \
  102. { \
  103. try \
  104. { \
  105. dostring(state_, str); \
  106. } \
  107. catch (luabind::error const& e) \
  108. { \
  109. TEST_ERROR(lua_tostring(e.state(), -1)); \
  110. lua_pop(L, 1); \
  111. } \
  112. catch (std::string const& s) \
  113. { \
  114. TEST_ERROR(s.c_str()); \
  115. } \
  116. }
  117. #endif // TEST_050415_HPP