Commit e9b76b89 authored by Sergei Krivonos's avatar Sergei Krivonos Committed by Sergei Krivonos

MDEV-23766: fix by my_json_writer test

parent 5e988ff8
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "sql_string.h" #include "sql_string.h"
#include "my_json_writer.h" #include "my_json_writer.h"
#ifndef NDEBUG #if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
bool Json_writer::named_item_expected() const bool Json_writer::named_item_expected() const
{ {
return named_items_expectation.size() return named_items_expectation.size()
...@@ -36,7 +36,7 @@ void Json_writer::append_indent() ...@@ -36,7 +36,7 @@ void Json_writer::append_indent()
inline void Json_writer::on_start_object() inline void Json_writer::on_start_object()
{ {
#ifndef NDEBUG #if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
if(!fmt_helper.is_making_writer_calls()) if(!fmt_helper.is_making_writer_calls())
{ {
VALIDITY_ASSERT(got_name == named_item_expected()); VALIDITY_ASSERT(got_name == named_item_expected());
...@@ -58,20 +58,14 @@ void Json_writer::start_object() ...@@ -58,20 +58,14 @@ void Json_writer::start_object()
first_child=true; first_child=true;
element_started= false; element_started= false;
document_start= false; document_start= false;
#ifndef NDEBUG #if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
got_name= false; got_name= false;
#endif #endif
} }
bool Json_writer::on_start_array()
{
bool helped= fmt_helper.on_start_array();
return helped;
}
void Json_writer::start_array() void Json_writer::start_array()
{ {
#ifndef NDEBUG #if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
if(!fmt_helper.is_making_writer_calls()) if(!fmt_helper.is_making_writer_calls())
{ {
VALIDITY_ASSERT(got_name == named_item_expected()); VALIDITY_ASSERT(got_name == named_item_expected());
...@@ -80,7 +74,7 @@ void Json_writer::start_array() ...@@ -80,7 +74,7 @@ void Json_writer::start_array()
} }
#endif #endif
if (on_start_array()) if (fmt_helper.on_start_array())
return; return;
if (!element_started) if (!element_started)
...@@ -96,7 +90,8 @@ void Json_writer::start_array() ...@@ -96,7 +90,8 @@ void Json_writer::start_array()
void Json_writer::end_object() 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(); named_items_expectation.pop_back();
VALIDITY_ASSERT(!got_name); VALIDITY_ASSERT(!got_name);
got_name= false; got_name= false;
...@@ -111,7 +106,8 @@ void Json_writer::end_object() ...@@ -111,7 +106,8 @@ void Json_writer::end_object()
void Json_writer::end_array() 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(); named_items_expectation.pop_back();
got_name= false; got_name= false;
#endif #endif
...@@ -142,7 +138,7 @@ Json_writer& Json_writer::add_member(const char *name, size_t len) ...@@ -142,7 +138,7 @@ Json_writer& Json_writer::add_member(const char *name, size_t len)
output.append(name, len); output.append(name, len);
output.append("\": ", 3); output.append("\": ", 3);
} }
#ifndef NDEBUG #if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
if (!fmt_helper.is_making_writer_calls()) if (!fmt_helper.is_making_writer_calls())
got_name= true; got_name= true;
#endif #endif
...@@ -260,7 +256,7 @@ void Json_writer::add_unquoted_str(const char* str, size_t len) ...@@ -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) 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; got_name= false;
#endif #endif
bool helped= fmt_helper.on_add_str(str, num_bytes); bool helped= fmt_helper.on_add_str(str, num_bytes);
......
...@@ -17,9 +17,12 @@ ...@@ -17,9 +17,12 @@
#define JSON_WRITER_INCLUDED #define JSON_WRITER_INCLUDED
#include "my_base.h" #include "my_base.h"
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
#include <vector>
#endif
#ifdef JSON_WRITER_UNIT_TEST #ifdef JSON_WRITER_UNIT_TEST
#include "sql_string.h" #include "sql_string.h"
#include <vector>
// Also, mock objects are defined in my_json_writer-t.cc // Also, mock objects are defined in my_json_writer-t.cc
#define VALIDITY_ASSERT(x) if ((!x)) this->invalid_json= true; #define VALIDITY_ASSERT(x) if ((!x)) this->invalid_json= true;
#else #else
...@@ -200,7 +203,7 @@ class String_with_limit ...@@ -200,7 +203,7 @@ class String_with_limit
class Json_writer 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 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). produce an invalid JSON document (e.g. JSON array having named elements).
...@@ -244,7 +247,6 @@ class Json_writer ...@@ -244,7 +247,6 @@ class Json_writer
void add_unquoted_str(const char* val, size_t len); void add_unquoted_str(const char* val, size_t len);
bool on_add_str(const char *str, size_t num_bytes); bool on_add_str(const char *str, size_t num_bytes);
bool on_start_array();
void on_start_object(); void on_start_object();
public: public:
...@@ -264,7 +266,7 @@ class Json_writer ...@@ -264,7 +266,7 @@ class Json_writer
size_t get_truncated_bytes() { return output.get_truncated_bytes(); } size_t get_truncated_bytes() { return output.get_truncated_bytes(); }
Json_writer() : Json_writer() :
#ifndef NDEBUG #if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
got_name(false), got_name(false),
#endif #endif
indent_level(0), document_start(true), element_started(false), indent_level(0), document_start(true), element_started(false),
......
...@@ -110,7 +110,7 @@ int main(int args, char **argv) ...@@ -110,7 +110,7 @@ int main(int args, char **argv)
Json_writer w; Json_writer w;
w.start_array(); w.start_array();
w.end_object(); w.end_object();
ok(!w.invalid_json, "BAD: not checked!"); ok(w.invalid_json, "JSON object end of array");
} }
// BAD: // BAD:
...@@ -118,7 +118,7 @@ int main(int args, char **argv) ...@@ -118,7 +118,7 @@ int main(int args, char **argv)
Json_writer w; Json_writer w;
w.start_object(); w.start_object();
w.end_array(); w.end_array();
ok(!w.invalid_json, "BAD: not checked!"); ok(w.invalid_json, "JSON array end of object");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment