diff options
author | Sergei Krivonos <sergei.krivonos@mariadb.com> | 2021-11-08 21:11:05 +0200 |
---|---|---|
committer | Sergei Krivonos <sergeikrivonos@gmail.com> | 2021-11-09 12:06:49 +0200 |
commit | e9b76b896a5cec9804e653f7df117c49284cabba (patch) | |
tree | 504c4ffc94d76ccd1fd0aacc3f4bffaa7307942a | |
parent | 5e988ff80f51e80f4d74477c4a22a065472317d4 (diff) | |
download | mariadb-git-e9b76b896a5cec9804e653f7df117c49284cabba.tar.gz |
MDEV-23766: fix by my_json_writer test
-rw-r--r-- | sql/my_json_writer.cc | 26 | ||||
-rw-r--r-- | sql/my_json_writer.h | 10 | ||||
-rw-r--r-- | unittest/sql/my_json_writer-t.cc | 4 |
3 files changed, 19 insertions, 21 deletions
diff --git a/sql/my_json_writer.cc b/sql/my_json_writer.cc index 81f7593c23f..8e35b25b822 100644 --- a/sql/my_json_writer.cc +++ b/sql/my_json_writer.cc @@ -18,7 +18,7 @@ #include "sql_string.h" #include "my_json_writer.h" -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) bool Json_writer::named_item_expected() const { return named_items_expectation.size() @@ -36,7 +36,7 @@ void Json_writer::append_indent() inline void Json_writer::on_start_object() { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) if(!fmt_helper.is_making_writer_calls()) { VALIDITY_ASSERT(got_name == named_item_expected()); @@ -58,20 +58,14 @@ void Json_writer::start_object() first_child=true; element_started= false; document_start= false; -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) got_name= false; #endif } -bool Json_writer::on_start_array() -{ - bool helped= fmt_helper.on_start_array(); - return helped; -} - void Json_writer::start_array() { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) if(!fmt_helper.is_making_writer_calls()) { VALIDITY_ASSERT(got_name == named_item_expected()); @@ -80,7 +74,7 @@ void Json_writer::start_array() } #endif - if (on_start_array()) + if (fmt_helper.on_start_array()) return; if (!element_started) @@ -96,7 +90,8 @@ void Json_writer::start_array() void Json_writer::end_object() { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) + VALIDITY_ASSERT(named_item_expected()); named_items_expectation.pop_back(); VALIDITY_ASSERT(!got_name); got_name= false; @@ -111,7 +106,8 @@ void Json_writer::end_object() void Json_writer::end_array() { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) + VALIDITY_ASSERT(!named_item_expected()); named_items_expectation.pop_back(); got_name= false; #endif @@ -142,7 +138,7 @@ Json_writer& Json_writer::add_member(const char *name, size_t len) output.append(name, len); output.append("\": ", 3); } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) if (!fmt_helper.is_making_writer_calls()) got_name= true; #endif @@ -260,7 +256,7 @@ void Json_writer::add_unquoted_str(const char* str, size_t len) inline bool Json_writer::on_add_str(const char *str, size_t num_bytes) { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) got_name= false; #endif bool helped= fmt_helper.on_add_str(str, num_bytes); diff --git a/sql/my_json_writer.h b/sql/my_json_writer.h index 6f9ae73c5ee..c8bd7f5b8fa 100644 --- a/sql/my_json_writer.h +++ b/sql/my_json_writer.h @@ -17,9 +17,12 @@ #define JSON_WRITER_INCLUDED #include "my_base.h" +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) +#include <vector> +#endif + #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 @@ -200,7 +203,7 @@ private: class Json_writer { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) /* 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). @@ -244,7 +247,6 @@ private: void add_unquoted_str(const char* val, size_t len); bool on_add_str(const char *str, size_t num_bytes); - bool on_start_array(); void on_start_object(); public: @@ -264,7 +266,7 @@ public: size_t get_truncated_bytes() { return output.get_truncated_bytes(); } Json_writer() : -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) got_name(false), #endif indent_level(0), document_start(true), element_started(false), diff --git a/unittest/sql/my_json_writer-t.cc b/unittest/sql/my_json_writer-t.cc index 46c3f4dc967..3d23c2f021e 100644 --- a/unittest/sql/my_json_writer-t.cc +++ b/unittest/sql/my_json_writer-t.cc @@ -110,7 +110,7 @@ int main(int args, char **argv) Json_writer w; w.start_array(); w.end_object(); - ok(!w.invalid_json, "BAD: not checked!"); + ok(w.invalid_json, "JSON object end of array"); } // BAD: @@ -118,7 +118,7 @@ int main(int args, char **argv) Json_writer w; w.start_object(); w.end_array(); - ok(!w.invalid_json, "BAD: not checked!"); + ok(w.invalid_json, "JSON array end of object"); } |