1 /* 2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz> 3 * Distributed under the terms of the MIT License. 4 */ 5 #include "JsonTextWriterTest.h" 6 7 #include <AutoDeleter.h> 8 9 #include <Json.h> 10 #include <JsonTextWriter.h> 11 12 #include <cppunit/TestCaller.h> 13 #include <cppunit/TestSuite.h> 14 15 #include "JsonSamples.h" 16 17 18 #define LOOPS_TEST_OBJECT_A_FOR_PERFORMANCE 100000 19 20 21 using namespace BPrivate; 22 23 24 JsonTextWriterTest::JsonTextWriterTest() 25 { 26 } 27 28 29 JsonTextWriterTest::~JsonTextWriterTest() 30 { 31 } 32 33 34 void 35 JsonTextWriterTest::TestArrayA() 36 { 37 BMallocIO* outputData = new BMallocIO(); 38 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 39 BJsonTextWriter writer(outputData); 40 41 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteArrayStart()); 42 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("1234")); 43 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteInteger(4567)); 44 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteArrayStart()); 45 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("A")); 46 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("b")); 47 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("C\xc3\xa9zanne")); 48 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteArrayEnd()); 49 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteArrayEnd()); 50 writer.Complete(); 51 52 BString outputString((char*)outputData->Buffer(), 53 outputData->BufferLength()); 54 fprintf(stderr, "expected out >%s<\n", JSON_SAMPLE_ARRAY_A_EXPECTED_OUT); 55 fprintf(stderr, "actual out >%s< (%" B_PRIuSIZE ")\n", outputString.String(), 56 outputData->BufferLength()); 57 58 CPPUNIT_ASSERT_EQUAL(BString(JSON_SAMPLE_ARRAY_A_EXPECTED_OUT), 59 outputString); 60 } 61 62 63 void 64 JsonTextWriterTest::TestObjectA() 65 { 66 BMallocIO* outputData = new BMallocIO(); 67 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 68 BJsonTextWriter writer(outputData); 69 70 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteObjectStart()); 71 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteObjectName("weather")); 72 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("raining")); 73 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteObjectName("humidity")); 74 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("too-high")); 75 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteObjectName("daysOfWeek")); 76 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteArrayStart()); 77 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("MON")); 78 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("TUE")); 79 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("WED")); 80 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("THR")); 81 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteString("FRI")); 82 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteArrayEnd()); 83 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteObjectEnd()); 84 writer.Complete(); 85 86 BString outputString((char*)outputData->Buffer(), 87 outputData->BufferLength()); 88 fprintf(stderr, "expected out >%s<\n", JSON_SAMPLE_OBJECT_A_EXPECTED_OUT); 89 fprintf(stderr, "actual out >%s< (%" B_PRIuSIZE ")\n", outputString.String(), 90 outputData->BufferLength()); 91 92 CPPUNIT_ASSERT_EQUAL(BString(JSON_SAMPLE_OBJECT_A_EXPECTED_OUT), 93 outputString); 94 } 95 96 97 void 98 JsonTextWriterTest::TestObjectAForPerformance() 99 { 100 int i; 101 102 for (i = 0; i < LOOPS_TEST_OBJECT_A_FOR_PERFORMANCE; i++) { 103 TestObjectA(); 104 } 105 } 106 107 108 void 109 JsonTextWriterTest::TestInteger() 110 { 111 BMallocIO* outputData = new BMallocIO(); 112 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 113 BJsonTextWriter writer(outputData); 114 static const char* expectedOut = JSON_SAMPLE_NUMBER_B_EXPECTED_OUT; 115 116 CPPUNIT_ASSERT_EQUAL(B_OK, 117 writer.WriteInteger(JSON_SAMPLE_NUMBER_B_LITERAL)); 118 writer.Complete(); 119 120 BString outputString((char *)outputData->Buffer(), 121 outputData->BufferLength()); 122 fprintf(stderr, "expected out >%s<\n", expectedOut); 123 fprintf(stderr, "actual out >%s< (%" B_PRIuSIZE ")\n", outputString.String(), 124 outputData->BufferLength()); 125 126 CPPUNIT_ASSERT_EQUAL(BString(expectedOut), 127 outputString); 128 } 129 130 131 void 132 JsonTextWriterTest::TestDouble() 133 { 134 BMallocIO* outputData = new BMallocIO(); 135 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 136 BJsonTextWriter writer(outputData); 137 static const char* expectedOut = "3.142857"; 138 139 CPPUNIT_ASSERT_EQUAL(B_OK, 140 writer.WriteDouble(JSON_SAMPLE_NUMBER_A_LITERAL)); 141 writer.Complete(); 142 143 BString outputString((char *)outputData->Buffer(), 144 outputData->BufferLength()); 145 fprintf(stderr, "expected out >%s<\n", expectedOut); 146 fprintf(stderr, "actual out >%s< (%" B_PRIuSIZE ")\n", outputString.String(), 147 outputData->BufferLength()); 148 149 CPPUNIT_ASSERT_EQUAL(BString(expectedOut), 150 outputString); 151 } 152 153 154 void 155 JsonTextWriterTest::TestFalse() 156 { 157 BMallocIO* outputData = new BMallocIO(); 158 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 159 BJsonTextWriter writer(outputData); 160 static const char* expectedOut = "false"; 161 162 CPPUNIT_ASSERT_EQUAL(B_OK, writer.WriteFalse()); 163 writer.Complete(); 164 165 BString outputString((char*)outputData->Buffer(), 166 outputData->BufferLength()); 167 fprintf(stderr, "expected out >%s<\n", expectedOut); 168 fprintf(stderr, "actual out >%s< (%" B_PRIuSIZE ")\n", 169 outputString.String(), outputData->BufferLength()); 170 171 CPPUNIT_ASSERT_EQUAL(BString(expectedOut), 172 outputString); 173 } 174 175 176 /*static*/ void 177 JsonTextWriterTest::AddTests(BTestSuite& parent) 178 { 179 CppUnit::TestSuite& suite = *new CppUnit::TestSuite( 180 "JsonTextWriterTest"); 181 182 suite.addTest(new CppUnit::TestCaller<JsonTextWriterTest>( 183 "JsonTextWriterTest::TestDouble", 184 &JsonTextWriterTest::TestDouble)); 185 186 suite.addTest(new CppUnit::TestCaller<JsonTextWriterTest>( 187 "JsonTextWriterTest::TestInteger", 188 &JsonTextWriterTest::TestInteger)); 189 190 suite.addTest(new CppUnit::TestCaller<JsonTextWriterTest>( 191 "JsonTextWriterTest::TestFalse", 192 &JsonTextWriterTest::TestFalse)); 193 194 suite.addTest(new CppUnit::TestCaller<JsonTextWriterTest>( 195 "JsonTextWriterTest::TestArrayA", 196 &JsonTextWriterTest::TestArrayA)); 197 198 suite.addTest(new CppUnit::TestCaller<JsonTextWriterTest>( 199 "JsonTextWriterTest::TestObjectA", 200 &JsonTextWriterTest::TestObjectA)); 201 202 // suite.addTest(new CppUnit::TestCaller<JsonTextWriterTest>( 203 // "JsonTextWriterTest::TestObjectAForPerformance", 204 // &JsonTextWriterTest::TestObjectAForPerformance)); 205 206 parent.addTest("JsonTextWriterTest", &suite); 207 }