summaryrefslogtreecommitdiff
path: root/unittest/sql/my_json_writer-t.cc
blob: e05dc4836ea57eae2fb0f7f4bf82ec5cd02d1155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
   Copyright (c) 2021, MariaDB Corporation.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

#include <my_global.h>
#include <my_pthread.h>
#include <my_sys.h>
#include <stdio.h>
#include <tap.h>

/*
  Unit tests for class Json_writer. At the moment there are only tests for the
  "Fail an assertion if one attempts to produce invalid JSON" feature.
*/

struct TABLE;
struct JOIN_TAB;
class Json_writer;


/* Several fake objects */
class Opt_trace 
{
public:
  void enable_tracing_if_required() {}
  void disable_tracing_if_required() {}
  Json_writer *get_current_json() { return nullptr; }
};

class THD 
{
public:
  Opt_trace opt_trace;
};

#define JSON_WRITER_UNIT_TEST
#include "../sql/my_json_writer.h"
#include "../sql/my_json_writer.cc"

int main(int args, char **argv)
{
  plan(NO_PLAN);
  diag("Testing Json_writer checks");

  {
    Json_writer w;
    w.start_object();
    w.add_member("foo"); 
    w.end_object();
    ok(w.invalid_json, "Started a name but didn't add a value");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_ll(123678456);
    ok(w.invalid_json, "Unnamed LL value in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_ull(123);
    ok(w.invalid_json, "Unnamed ULL value in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_size(567890);
    ok(w.invalid_json, "Unnamed size value in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_double(123.567867);
    ok(w.invalid_json, "Unnamed double value in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_bool(true);
    ok(w.invalid_json, "Unnamed bool value in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_null();
    ok(w.invalid_json, "Unnamed null value in an object");
  }

  {
    Json_writer w;
    w.start_array();
    w.add_member("bebebe").add_ull(345);
    ok(w.invalid_json, "Named member in array");
  }

  {
    Json_writer w;
    w.start_object();
    w.add_str("some string");
    ok(w.invalid_json, "Unnamed string (const char*) in an object");
  }

  {
    Json_writer w;
    w.start_object();
    CHARSET_INFO cs_info{};
    String str("another string", &cs_info);
    w.add_str(str);
    ok(w.invalid_json, "Unnamed string (class String) in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.start_array();
    ok(!w.invalid_json, "Implicitly added member for the unnamed array"
                        " in an object");
  }

  {
    Json_writer w;
    w.start_object();
    w.start_object();
    ok(!w.invalid_json, "Implicitly added member for the unnamed object"
                        " in an object");
  }

  {
    Json_writer w;
    w.start_array();
    w.add_member("zzz");
    w.start_object();
    ok(w.invalid_json, "Named object in an array");
  }
  {
    Json_writer w;
    w.start_array();
    w.add_member("zzz");
    w.start_array();
    ok(w.invalid_json, "Named array in an array");
  }

  {
    Json_writer w;
    w.start_array();
    w.end_object();
    ok(w.invalid_json, "JSON object end of array");
  }

  {
    Json_writer w;
    w.start_object();
    w.end_array();
    ok(w.invalid_json, "JSON array end of object");
  }

  diag("Done");

  return exit_status();
}