1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

MDEV-23766: fix by my_json_writer test

This commit is contained in:
Sergei Krivonos
2021-11-08 21:11:05 +02:00
committed by Sergei Krivonos
parent 5e988ff80f
commit e9b76b896a
3 changed files with 19 additions and 21 deletions

View File

@ -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()
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()
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()
} }
#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()
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()
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)
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)
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);

View File

@ -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 @@ private:
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 @@ private:
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 @@ public:
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),

View File

@ -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)
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");
} }