JSON_FAIL_SAFE.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "JSON_FAIL_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_constfs = JSON_TEXT("fail"); //should pass the same pointer all the way through, no copies
  14. const json_string null_constfs = JSON_TEXT("");
  15. #if defined JSON_DEBUG || defined JSON_SAFE
  16. json_error_callback_t origCallbackfs = NULL;
  17. #endif
  18. void testJSONDebug_JSON_FAIL_SAFE::setUp(const std::string & methodName){
  19. BaseTest::setUp(methodName);
  20. #if defined JSON_DEBUG
  21. #ifndef JSON_STDERROR
  22. origCallbackfs = JSONDebug::register_callback(callback); //check that the callback was called
  23. last = null_constfs;
  24. #endif
  25. #endif
  26. }
  27. void testJSONDebug_JSON_FAIL_SAFE::tearDown(void){
  28. BaseTest::tearDown();
  29. #if defined JSON_DEBUG
  30. #ifndef JSON_STDERROR
  31. JSONDebug::register_callback(origCallbackfs); //check that the callback was called
  32. #endif
  33. #endif
  34. }
  35. /**
  36. * Make sure fails do call the callback and run extra code
  37. */
  38. void testJSONDebug_JSON_FAIL_SAFE::testFail(void){
  39. int i = 0;
  40. JSON_FAIL_SAFE(fail_constfs, i = 1;);
  41. #if defined(JSON_SAFE)
  42. assertEquals(i, 1); //safe caught the code
  43. #else
  44. assertEquals(i, 0); //fell through because no safety catch
  45. #endif
  46. #if defined JSON_DEBUG
  47. #ifndef JSON_STDERROR
  48. assertEquals(last, fail_constfs); //make sure the callback was actually called
  49. #endif
  50. #endif
  51. }