Debian Codegraph @

sal/qa/rtl/bootstrap/expand.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10 #include <sal/config.h>
11
12 #include <cstring>
13
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 #include <osl/file.hxx>
18 #include <rtl/bootstrap.hxx>
19 #include <rtl/ustrbuf.hxx>
20 #include <rtl/ustring.hxx>
21
22 namespace {
23
24 class Test: public CppUnit::TestFixture {
25 public:
26 virtual void setUp() override;
27
28 private:
29 CPPUNIT_TEST_SUITE(Test);
30 CPPUNIT_TEST(testDollar);
31 CPPUNIT_TEST(testIndirectDollar);
32 CPPUNIT_TEST(testThreeSegments);
33 CPPUNIT_TEST_SUITE_END();
34
35 void testDollar();
36
37 void testIndirectDollar();
38
39 void testThreeSegments();
40 };
41
42 void Test::setUp() {
43 rtl::Bootstrap::set(u"TEST"_ustr, u"<expanded TEST>"_ustr);
44 rtl::Bootstrap::set(u"WITH_DOLLAR"_ustr, u"foo\\$TEST"_ustr);
45 rtl::Bootstrap::set(u"INDIRECT"_ustr, u"$WITH_DOLLAR"_ustr);
46 }
47
48 void Test::testDollar() {
49 OUString s(u"$WITH_DOLLAR"_ustr);
50 rtl::Bootstrap::expandMacros(s);
51 CPPUNIT_ASSERT_EQUAL(u"foo$TEST"_ustr, s);
52 }
53
54 void Test::testIndirectDollar() {
55 OUString s(u"$INDIRECT"_ustr);
56 rtl::Bootstrap::expandMacros(s);
57 CPPUNIT_ASSERT_EQUAL(u"foo$TEST"_ustr, s);
58 }
59
60 void Test::testThreeSegments() {
61 OUString url;
62 CPPUNIT_ASSERT(rtl::Bootstrap::get(u"UserInstallation"_ustr, url));
63 url += "/testThreeSegments.ini";
64 {
65 osl::File f(url);
66 CPPUNIT_ASSERT_EQUAL(
67 osl::FileBase::E_None, f.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create));
68 char const data[] = "[section1]\nkey=value1\n[section2]\nkey=value2\n";
69 sal_uInt64 length = std::strlen(data);
70 sal_uInt64 written = 0;
71 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, f.write(data, length, written));
72 CPPUNIT_ASSERT_EQUAL(length, written);
73 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, f.close());
74 }
75 OUString esc;
76 {
77 OUStringBuffer buf;
78 for (sal_Int32 i = 0; i != url.getLength(); ++i) {
79 auto const c = url[i];
80 switch (c) {
81 case ':':
82 case '\\':
83 case '{':
84 case '}':
85 buf.append('\\');
86 }
87 buf.append(c);
88 }
89 esc = buf.makeStringAndClear();
90 }
91 {
92 OUString s("${" + esc + ":section1:key}");
93 rtl::Bootstrap::expandMacros(s);
94 CPPUNIT_ASSERT_EQUAL(u"value1"_ustr, s);
95 }
96 {
97 OUString s("${" + esc + ":section2:key}");
98 rtl::Bootstrap::expandMacros(s);
99 CPPUNIT_ASSERT_EQUAL(u"value2"_ustr, s);
100 }
101 }
102
103 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
104
105 }
106
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */