tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / svl / qa / unit / test_lngmisc.cxx
blob5af4d3b6ac84cc6ff89d2d1e50df8c030402819e
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 <o3tl/cppunittraitshelper.hxx>
13 #include <sal/types.h>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 #include <cppunit/plugin/TestPlugIn.h>
19 #include <svl/lngmisc.hxx>
21 #include <rtl/ustrbuf.hxx>
23 namespace
25 class LngMiscTest : public CppUnit::TestFixture
27 private:
28 void testRemoveHyphens();
29 void testRemoveControlChars();
30 void testReplaceControlChars();
31 void testGetThesaurusReplaceText();
33 CPPUNIT_TEST_SUITE(LngMiscTest);
35 CPPUNIT_TEST(testRemoveHyphens);
36 CPPUNIT_TEST(testRemoveControlChars);
37 CPPUNIT_TEST(testReplaceControlChars);
38 CPPUNIT_TEST(testGetThesaurusReplaceText);
40 CPPUNIT_TEST_SUITE_END();
43 void LngMiscTest::testRemoveHyphens()
45 OUString str1(u""_ustr);
46 OUString str2(u"a-b--c---"_ustr);
48 OUString str3 = OUStringChar(SVT_SOFT_HYPHEN) + OUStringChar(SVT_HARD_HYPHEN)
49 + OUStringChar(SVT_HARD_HYPHEN);
51 OUString str4(u"asdf"_ustr);
53 bool bModified = linguistic::RemoveHyphens(str1);
54 CPPUNIT_ASSERT(!bModified);
55 CPPUNIT_ASSERT(str1.isEmpty());
57 // Note that '-' isn't a hyphen to RemoveHyphens.
58 bModified = linguistic::RemoveHyphens(str2);
59 CPPUNIT_ASSERT(!bModified);
60 CPPUNIT_ASSERT_EQUAL(u"a-b--c---"_ustr, str2);
62 bModified = linguistic::RemoveHyphens(str3);
63 CPPUNIT_ASSERT(bModified);
64 CPPUNIT_ASSERT(str3.isEmpty());
66 bModified = linguistic::RemoveHyphens(str4);
67 CPPUNIT_ASSERT(!bModified);
68 CPPUNIT_ASSERT_EQUAL(u"asdf"_ustr, str4);
71 void LngMiscTest::testRemoveControlChars()
73 OUString str1(u""_ustr);
74 OUString str2(u"asdf"_ustr);
75 OUString str3(u"asdf\nasdf"_ustr);
77 OUStringBuffer str4Buf(33);
78 str4Buf.setLength(33);
79 for (int i = 0; i < 33; i++)
80 str4Buf[i] = static_cast<sal_Unicode>(i);
81 // TODO: is this a bug? shouldn't RemoveControlChars remove this?
82 // str4Buf[33] = static_cast<sal_Unicode>(0x7F);
83 OUString str4(str4Buf.makeStringAndClear());
85 bool bModified = linguistic::RemoveControlChars(str1);
86 CPPUNIT_ASSERT(!bModified);
87 CPPUNIT_ASSERT(str1.isEmpty());
89 bModified = linguistic::RemoveControlChars(str2);
90 CPPUNIT_ASSERT(!bModified);
91 CPPUNIT_ASSERT_EQUAL(u"asdf"_ustr, str2);
93 bModified = linguistic::RemoveControlChars(str3);
94 CPPUNIT_ASSERT(bModified);
95 CPPUNIT_ASSERT_EQUAL(u"asdfasdf"_ustr, str3);
97 bModified = linguistic::RemoveControlChars(str4);
98 CPPUNIT_ASSERT(bModified);
99 CPPUNIT_ASSERT_EQUAL(u" "_ustr, str4);
102 void LngMiscTest::testReplaceControlChars()
104 OUString str1(u""_ustr);
105 OUString str2(u"asdf"_ustr);
106 OUString str3(u"asdf\nasdf"_ustr);
108 OUStringBuffer str4Buf(33);
109 str4Buf.setLength(33);
110 for (int i = 0; i < 33; i++)
111 str4Buf[i] = static_cast<sal_Unicode>(i);
112 // TODO: is this a bug? shouldn't RemoveControlChars remove this?
113 // str4Buf[33] = static_cast<sal_Unicode>(0x7F);
114 OUString str4(str4Buf.makeStringAndClear());
116 bool bModified = linguistic::ReplaceControlChars(str1);
117 CPPUNIT_ASSERT(!bModified);
118 CPPUNIT_ASSERT(str1.isEmpty());
120 bModified = linguistic::ReplaceControlChars(str2);
121 CPPUNIT_ASSERT(!bModified);
122 CPPUNIT_ASSERT_EQUAL(u"asdf"_ustr, str2);
124 bModified = linguistic::ReplaceControlChars(str3);
125 CPPUNIT_ASSERT(bModified);
126 CPPUNIT_ASSERT_EQUAL(u"asdf asdf"_ustr, str3);
128 bModified = linguistic::ReplaceControlChars(str4);
129 CPPUNIT_ASSERT(bModified);
130 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(32), str4.getLength());
131 for (int i = 0; i < 32; i++)
132 CPPUNIT_ASSERT_EQUAL(u' ', str4[i]);
135 void LngMiscTest::testGetThesaurusReplaceText()
137 static constexpr OUString str2(u"asdf"_ustr);
139 OUString r = linguistic::GetThesaurusReplaceText(u""_ustr);
140 CPPUNIT_ASSERT(r.isEmpty());
142 r = linguistic::GetThesaurusReplaceText(str2);
143 CPPUNIT_ASSERT_EQUAL(str2, r);
145 r = linguistic::GetThesaurusReplaceText(u"asdf (abc)"_ustr);
146 CPPUNIT_ASSERT_EQUAL(str2, r);
148 r = linguistic::GetThesaurusReplaceText(u"asdf*"_ustr);
149 CPPUNIT_ASSERT_EQUAL(str2, r);
151 r = linguistic::GetThesaurusReplaceText(u"asdf * "_ustr);
152 CPPUNIT_ASSERT_EQUAL(str2, r);
154 r = linguistic::GetThesaurusReplaceText(u"asdf (abc) *"_ustr);
155 CPPUNIT_ASSERT_EQUAL(str2, r);
157 r = linguistic::GetThesaurusReplaceText(u"asdf asdf * (abc)"_ustr);
158 CPPUNIT_ASSERT_EQUAL(u"asdf asdf"_ustr, r);
160 r = linguistic::GetThesaurusReplaceText(u" * (abc) asdf *"_ustr);
161 CPPUNIT_ASSERT(r.isEmpty());
164 CPPUNIT_TEST_SUITE_REGISTRATION(LngMiscTest);
166 CPPUNIT_PLUGIN_IMPLEMENT();
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */