Bump version to 6.4-15
[LibreOffice.git] / tools / qa / cppunit / test_json_writer.cxx
blob6a2cc7813574ecc19b4414c9e188306f516fdcd4
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 <cstdlib>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <test/bootstrapfixture.hxx>
16 #include <rtl/ustring.hxx>
17 #include <tools/stream.hxx>
18 #include <tools/json_writer.hxx>
20 namespace
22 class JsonWriterTest : public test::BootstrapFixture
24 public:
25 JsonWriterTest()
26 : BootstrapFixture(true, false)
30 virtual void setUp() override {}
32 void test1();
33 void test2();
35 CPPUNIT_TEST_SUITE(JsonWriterTest);
36 CPPUNIT_TEST(test1);
37 CPPUNIT_TEST(test2);
38 CPPUNIT_TEST_SUITE_END();
41 struct Free
43 void operator()(void* p) const { std::free(p); }
46 void JsonWriterTest::test1()
48 tools::JsonWriter aJson;
51 auto testNode = aJson.startNode("node");
52 aJson.put("oustring", OUString("val1"));
53 aJson.put("ostring", OString("val2"));
54 aJson.put("charptr", "val3");
55 aJson.put("int", 12);
58 std::unique_ptr<char, Free> result(aJson.extractData());
60 CPPUNIT_ASSERT_EQUAL(std::string("{ \"node\": { \"oustring\": \"val1\", \"ostring\": \"val2\", "
61 "\"charptr\": \"val3\", \"int\": 12}}"),
62 std::string(result.get()));
65 void JsonWriterTest::test2()
67 tools::JsonWriter aJson;
70 auto testNode = aJson.startNode("node");
71 aJson.put("field1", OUString("val1"));
72 aJson.put("field2", OUString("val2"));
74 auto testNode2 = aJson.startNode("node");
75 aJson.put("field3", OUString("val3"));
77 auto testNode3 = aJson.startNode("node");
78 aJson.put("field4", OUString("val4"));
79 aJson.put("field5", OUString("val5"));
84 std::unique_ptr<char, Free> result(aJson.extractData());
86 CPPUNIT_ASSERT_EQUAL(std::string("{ \"node\": { \"field1\": \"val1\", \"field2\": \"val2\", "
87 "\"node\": { \"field3\": \"val3\", \"node\": { \"field4\": "
88 "\"val4\", \"field5\": \"val5\"}}}}"),
89 std::string(result.get()));
92 CPPUNIT_TEST_SUITE_REGISTRATION(JsonWriterTest);
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */