JSON_ASSERT_SAFE.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "JSON_ASSERT_SAFE.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_const = JSON_TEXT("fail"); //should pass the same pointer all the way through, no copies
  14. const json_string null_const = JSON_TEXT("");
  15. #if defined JSON_DEBUG || defined JSON_SAFE
  16. json_error_callback_t origCallback = NULL;
  17. #endif
  18. void testJSONDebug_JSON_ASSERT_SAFE::setUp(const std::string & methodName){
  19. BaseTest::setUp(methodName);
  20. #if defined JSON_DEBUG
  21. #ifndef JSON_STDERROR
  22. origCallback = JSONDebug::register_callback(callback); //check that the callback was called
  23. last = null_const;
  24. #endif
  25. #endif
  26. }
  27. void testJSONDebug_JSON_ASSERT_SAFE::tearDown(void){
  28. BaseTest::tearDown();
  29. #if defined JSON_DEBUG
  30. #ifndef JSON_STDERROR
  31. JSONDebug::register_callback(origCallback); //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_SAFE::testPass(void){
  39. int i = 0;
  40. JSON_ASSERT_SAFE(1 == 1, fail_const, i = 1;);
  41. assertEquals(i, 0);
  42. #if defined JSON_DEBUG
  43. #ifndef JSON_STDERROR
  44. assertEquals(last, null_const); //make sure the callback was not called
  45. #endif
  46. #endif
  47. }
  48. /**
  49. * Make sure asserts that fail do call the callback and run extra code
  50. */
  51. void testJSONDebug_JSON_ASSERT_SAFE::testFail(void){
  52. int i = 0;
  53. JSON_ASSERT_SAFE(1 == 0, fail_const, i = 1;);
  54. #if defined(JSON_SAFE)
  55. assertEquals(i, 1); //safe caught the code
  56. #else
  57. assertEquals(i, 0); //fell through because no safety catch
  58. #endif
  59. #if defined JSON_DEBUG
  60. #ifndef JSON_STDERROR
  61. assertEquals(last, fail_const); //make sure the callback was actually called
  62. #endif
  63. #endif
  64. }