Avoid potential negative array index access to cached text.
[LibreOffice.git] / tools / qa / cppunit / test_json_writer.cxx
bloba82fc769be311b172a689cbd332b87b42d45996d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
15 #include <o3tl/deleter.hxx>
16 #include <rtl/ustring.hxx>
17 #include <tools/json_writer.hxx>
19 namespace
21 class JsonWriterTest : public CppUnit::TestFixture
23 public:
24 void test1();
25 void test2();
26 void testArray();
28 CPPUNIT_TEST_SUITE(JsonWriterTest);
29 CPPUNIT_TEST(test1);
30 CPPUNIT_TEST(test2);
31 CPPUNIT_TEST(testArray);
32 CPPUNIT_TEST_SUITE_END();
35 void JsonWriterTest::test1()
37 tools::JsonWriter aJson;
40 auto testNode = aJson.startNode("node");
41 aJson.put("oustring", OUString("val1"));
42 aJson.put("charptr", "val3");
43 aJson.put("int", static_cast<sal_Int32>(12));
46 OString result(aJson.finishAndGetAsOString());
48 CPPUNIT_ASSERT_EQUAL("{ \"node\": { \"oustring\": \"val1\", "
49 "\"charptr\": \"val3\", \"int\": 12}}"_ostr,
50 result);
53 void JsonWriterTest::test2()
55 tools::JsonWriter aJson;
58 auto testNode = aJson.startNode("node");
59 aJson.put("field1", OUString("val1"));
60 aJson.put("field2", OUString("val2"));
62 auto testNode2 = aJson.startNode("node");
63 aJson.put("field3", OUString("val3"));
65 auto testNode3 = aJson.startNode("node");
66 aJson.put("field4", OUString("val4"));
67 aJson.put("field5", OUString("val5"));
72 OString result(aJson.finishAndGetAsOString());
74 CPPUNIT_ASSERT_EQUAL("{ \"node\": { \"field1\": \"val1\", \"field2\": \"val2\", "
75 "\"node\": { \"field3\": \"val3\", \"node\": { \"field4\": "
76 "\"val4\", \"field5\": \"val5\"}}}}"_ostr,
77 result);
80 void JsonWriterTest::testArray()
82 tools::JsonWriter aJson;
84 tools::ScopedJsonWriterArray aArray = aJson.startArray("items");
85 aJson.putSimpleValue("foo");
86 aJson.putSimpleValue("bar");
89 OString aResult(aJson.finishAndGetAsOString());
91 CPPUNIT_ASSERT_EQUAL("{ \"items\": [ \"foo\", \"bar\"]}"_ostr, aResult);
94 CPPUNIT_TEST_SUITE_REGISTRATION(JsonWriterTest);
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */