2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
3 * Distributed under the terms of the MIT License.
5 #include "JsonEndToEndTest.h"
7 #include <AutoDeleter.h>
10 #include <JsonTextWriter.h>
12 #include <cppunit/TestCaller.h>
13 #include <cppunit/TestSuite.h>
15 #include "JsonSamples.h"
18 using namespace BPrivate
;
21 JsonEndToEndTest::JsonEndToEndTest()
26 JsonEndToEndTest::~JsonEndToEndTest()
32 JsonEndToEndTest::TestParseAndWrite(const char* input
, const char* expectedOutput
)
34 BDataIO
* inputData
= new BMemoryIO(input
, strlen(input
));
35 ObjectDeleter
<BDataIO
> inputDataDeleter(inputData
);
36 BMallocIO
* outputData
= new BMallocIO();
37 ObjectDeleter
<BMallocIO
> outputDataDeleter(outputData
);
38 BPrivate::BJsonTextWriter
* listener
39 = new BJsonTextWriter(outputData
);
40 ObjectDeleter
<BPrivate::BJsonTextWriter
> listenerDeleter(listener
);
42 // ----------------------
43 BPrivate::BJson::Parse(inputData
, listener
);
44 // ----------------------
46 CPPUNIT_ASSERT_EQUAL(B_OK
, listener
->ErrorStatus());
47 fprintf(stderr
, "in >%s<\n", input
);
48 fprintf(stderr
, "expected out >%s<\n", expectedOutput
);
49 fprintf(stderr
, "actual out >%s<\n", (char*)outputData
->Buffer());
50 CPPUNIT_ASSERT_MESSAGE("expected did no equal actual output",
51 0 == strncmp(expectedOutput
, (char*)outputData
->Buffer(),
52 strlen(expectedOutput
)));
57 JsonEndToEndTest::TestNullA()
59 TestParseAndWrite(JSON_SAMPLE_NULL_A_IN
,
60 JSON_SAMPLE_NULL_A_EXPECTED_OUT
);
65 JsonEndToEndTest::TestTrueA()
67 TestParseAndWrite(JSON_SAMPLE_TRUE_A_IN
,
68 JSON_SAMPLE_TRUE_A_EXPECTED_OUT
);
73 JsonEndToEndTest::TestFalseA()
75 TestParseAndWrite(JSON_SAMPLE_FALSE_A_IN
,
76 JSON_SAMPLE_FALSE_A_EXPECTED_OUT
);
81 JsonEndToEndTest::TestNumberA()
83 TestParseAndWrite(JSON_SAMPLE_NUMBER_A_IN
,
84 JSON_SAMPLE_NUMBER_A_EXPECTED_OUT
);
89 JsonEndToEndTest::TestStringA()
91 TestParseAndWrite(JSON_SAMPLE_STRING_A_IN
,
92 JSON_SAMPLE_STRING_A_EXPECTED_OUT
);
97 JsonEndToEndTest::TestStringB()
99 TestParseAndWrite(JSON_SAMPLE_STRING_B_IN
,
100 JSON_SAMPLE_STRING_B_EXPECTED_OUT
);
104 /* In this test, there are some UTF-8 characters. */
107 JsonEndToEndTest::TestStringA2()
109 TestParseAndWrite(JSON_SAMPLE_STRING_A2_IN
,
110 JSON_SAMPLE_STRING_A_EXPECTED_OUT
);
115 JsonEndToEndTest::TestArrayA()
117 TestParseAndWrite(JSON_SAMPLE_ARRAY_A_IN
, JSON_SAMPLE_ARRAY_A_EXPECTED_OUT
);
122 JsonEndToEndTest::TestArrayB()
124 TestParseAndWrite(JSON_SAMPLE_ARRAY_B_IN
, JSON_SAMPLE_ARRAY_B_EXPECTED_OUT
);
129 JsonEndToEndTest::TestObjectA()
131 TestParseAndWrite(JSON_SAMPLE_OBJECT_A_IN
,
132 JSON_SAMPLE_OBJECT_A_EXPECTED_OUT
);
136 /*! This method will test an element being unterminated; such an object that
137 is missing the terminating "}" symbol or a string that has no closing
138 quote. This is tested here because the writer
142 JsonEndToEndTest::TestUnterminated(const char *input
)
144 BDataIO
* inputData
= new BMemoryIO(input
, strlen(input
));
145 ObjectDeleter
<BDataIO
> inputDataDeleter(inputData
);
146 BMallocIO
* outputData
= new BMallocIO();
147 ObjectDeleter
<BMallocIO
> outputDataDeleter(outputData
);
148 BPrivate::BJsonTextWriter
* listener
149 = new BJsonTextWriter(outputData
);
150 ObjectDeleter
<BPrivate::BJsonTextWriter
> listenerDeleter(listener
);
152 // ----------------------
153 BPrivate::BJson::Parse(inputData
, listener
);
154 // ----------------------
156 CPPUNIT_ASSERT_EQUAL(B_BAD_DATA
, listener
->ErrorStatus());
161 JsonEndToEndTest::TestStringUnterminated()
163 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_STRING
);
167 JsonEndToEndTest::TestArrayUnterminated()
169 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_ARRAY
);
173 JsonEndToEndTest::TestObjectUnterminated()
175 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_OBJECT
);
180 JsonEndToEndTest::AddTests(BTestSuite
& parent
)
182 CppUnit::TestSuite
& suite
= *new CppUnit::TestSuite("JsonEndToEndTest");
184 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
185 "JsonEndToEndTest::TestNullA", &JsonEndToEndTest::TestNullA
));
186 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
187 "JsonEndToEndTest::TestTrueA", &JsonEndToEndTest::TestTrueA
));
188 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
189 "JsonEndToEndTest::TestFalseA", &JsonEndToEndTest::TestFalseA
));
190 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
191 "JsonEndToEndTest::TestNumberA", &JsonEndToEndTest::TestNumberA
));
192 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
193 "JsonEndToEndTest::TestStringA", &JsonEndToEndTest::TestStringA
));
194 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
195 "JsonEndToEndTest::TestStringA2", &JsonEndToEndTest::TestStringA2
));
196 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
197 "JsonEndToEndTest::TestStringB", &JsonEndToEndTest::TestStringB
));
198 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
199 "JsonEndToEndTest::TestArrayA", &JsonEndToEndTest::TestArrayA
));
200 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
201 "JsonEndToEndTest::TestArrayB", &JsonEndToEndTest::TestArrayB
));
202 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
203 "JsonEndToEndTest::TestObjectA", &JsonEndToEndTest::TestObjectA
));
204 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
205 "JsonEndToEndTest::TestStringUnterminated",
206 &JsonEndToEndTest::TestStringUnterminated
));
207 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
208 "JsonEndToEndTest::TestArrayUnterminated",
209 &JsonEndToEndTest::TestArrayUnterminated
));
210 suite
.addTest(new CppUnit::TestCaller
<JsonEndToEndTest
>(
211 "JsonEndToEndTest::TestObjectUnterminated",
212 &JsonEndToEndTest::TestObjectUnterminated
));
214 parent
.addTest("JsonEndToEndTest", &suite
);