JSON_ASSERT.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "JSON_ASSERT.h"
  2. #include "../../Source/JSONDebug.h"
  3. #if defined JSON_DEBUG
  4. #ifndef JSON_STDERROR
  5. static json_string last;
  6. #ifdef JSON_LIBRARY
  7. static void callback(const json_char * p){ last = p; }
  8. #else
  9. static void callback(const json_string & p){ last = p; }
  10. #endif
  11. #endif
  12. #endif
  13. const json_string fail_consta = JSON_TEXT("fail"); //should pass the same pointer all the way through, no copies
  14. const json_string null_consta = JSON_TEXT("");
  15. #if defined JSON_DEBUG || defined JSON_SAFE
  16. json_error_callback_t origCallbacka = NULL;
  17. #endif
  18. void testJSONDebug_JSON_ASSERT::setUp(const std::string & methodName){
  19. BaseTest::setUp(methodName);
  20. #if defined JSON_DEBUG
  21. #ifndef JSON_STDERROR
  22. origCallbacka = JSONDebug::register_callback(callback); //check that the callback was called
  23. last = null_consta;
  24. #endif
  25. #endif
  26. }
  27. void testJSONDebug_JSON_ASSERT::tearDown(void){
  28. BaseTest::tearDown();
  29. #if defined JSON_DEBUG
  30. #ifndef JSON_STDERROR
  31. JSONDebug::register_callback(origCallbacka); //check that the callback was called
  32. #endif
  33. #endif
  34. }
  35. /**
  36. * Make sure asserts that pass do not call the callback or run extra code
  37. */
  38. void testJSONDebug_JSON_ASSERT::testPass(void){
  39. #if defined JSON_DEBUG
  40. #ifndef JSON_STDERROR
  41. JSON_ASSERT(1 == 1, fail_consta);
  42. assertEquals(last, null_consta); //make sure the callback was not called
  43. #endif
  44. #endif
  45. }
  46. /**
  47. * Make sure asserts that fail do call the callback and run extra code
  48. */
  49. void testJSONDebug_JSON_ASSERT::testFail(void){
  50. #if defined JSON_DEBUG
  51. #ifndef JSON_STDERROR
  52. JSON_ASSERT(1 == 0, fail_consta);
  53. assertEquals(last, fail_consta); //make sure the callback was actually called
  54. #endif
  55. #endif
  56. }