diff options
Diffstat (limited to 'sql')
-rw-r--r-- | sql/my_json_writer.cc | 14 | ||||
-rw-r--r-- | sql/my_json_writer.h | 22 |
2 files changed, 28 insertions, 8 deletions
diff --git a/sql/my_json_writer.cc b/sql/my_json_writer.cc index 687da202164..96bdf20ee5e 100644 --- a/sql/my_json_writer.cc +++ b/sql/my_json_writer.cc @@ -39,7 +39,7 @@ inline void Json_writer::on_start_object() #ifndef NDEBUG if(!fmt_helper.is_making_writer_calls()) { - DBUG_ASSERT(got_name == named_item_expected()); + VALIDITY_ASSERT(got_name == named_item_expected()); named_items_expectation.push_back(true); } #endif @@ -74,7 +74,7 @@ void Json_writer::start_array() #ifndef NDEBUG if(!fmt_helper.is_making_writer_calls()) { - DBUG_ASSERT(got_name == named_item_expected()); + VALIDITY_ASSERT(got_name == named_item_expected()); named_items_expectation.push_back(false); got_name= false; } @@ -98,7 +98,7 @@ void Json_writer::end_object() { #ifndef NDEBUG named_items_expectation.pop_back(); - DBUG_ASSERT(!got_name); + VALIDITY_ASSERT(!got_name); got_name= false; #endif indent_level-=INDENT_SIZE; @@ -246,8 +246,8 @@ void Json_writer::add_unquoted_str(const char* str) void Json_writer::add_unquoted_str(const char* str, size_t len) { - DBUG_ASSERT(fmt_helper.is_making_writer_calls() || - got_name == named_item_expected()); + VALIDITY_ASSERT(fmt_helper.is_making_writer_calls() || + got_name == named_item_expected()); if (on_add_str(str, len)) return; @@ -279,8 +279,8 @@ void Json_writer::add_str(const char *str) void Json_writer::add_str(const char* str, size_t num_bytes) { - DBUG_ASSERT(fmt_helper.is_making_writer_calls() || - got_name == named_item_expected()); + VALIDITY_ASSERT(fmt_helper.is_making_writer_calls() || + got_name == named_item_expected()); if (on_add_str(str, num_bytes)) return; diff --git a/sql/my_json_writer.h b/sql/my_json_writer.h index 50b277a5052..6f9ae73c5ee 100644 --- a/sql/my_json_writer.h +++ b/sql/my_json_writer.h @@ -16,7 +16,17 @@ #ifndef JSON_WRITER_INCLUDED #define JSON_WRITER_INCLUDED #include "my_base.h" + +#ifdef JSON_WRITER_UNIT_TEST +#include "sql_string.h" +#include <vector> +// Also, mock objects are defined in my_json_writer-t.cc +#define VALIDITY_ASSERT(x) if ((!x)) this->invalid_json= true; +#else #include "sql_select.h" +#define VALIDITY_ASSERT(x) DBUG_ASSERT(x) +#endif + class Opt_trace_stmt; class Opt_trace_context; class Json_writer; @@ -191,12 +201,22 @@ private: class Json_writer { #ifndef NDEBUG - + /* + In debug mode, Json_writer will fail and assertion if one attempts to + produce an invalid JSON document (e.g. JSON array having named elements). + */ std::vector<bool> named_items_expectation; bool named_item_expected() const; bool got_name; + +#ifdef JSON_WRITER_UNIT_TEST +public: + // When compiled for unit test, creating invalid JSON will set this to true + // instead of an assertion. + bool invalid_json= false; +#endif #endif public: |