1 /*
2  * Xournal++
3  *
4  * This file is part of the Xournal UnitTests
5  *
6  * @author Xournal++ Team
7  * https://github.com/xournalpp/xournalpp
8  *
9  * @license GNU GPLv2 or later
10  */
11 
12 #include <cstdlib>
13 #include <ctime>
14 
15 #include <StringUtils.h>
16 #include <config-test.h>
17 #include <cppunit/extensions/HelperMacros.h>
18 
19 using namespace std;
20 
21 class StringUtilsTest: public CppUnit::TestFixture {
22     CPPUNIT_TEST_SUITE(StringUtilsTest);
23 
24     CPPUNIT_TEST(testStartWith);
25     CPPUNIT_TEST(testSplit);
26     CPPUNIT_TEST(testSplitEmpty);
27     CPPUNIT_TEST(testSplitOne);
28     CPPUNIT_TEST(testEndsWith);
29     CPPUNIT_TEST(testCompare);
30 
31     CPPUNIT_TEST_SUITE_END();
32 
33 public:
setUp()34     void setUp() {}
35 
tearDown()36     void tearDown() {}
37 
testStartWith()38     void testStartWith() {
39         CPPUNIT_ASSERT_EQUAL(true, StringUtils::startsWith("asdfsfdafdasfda", "asdf"));
40         CPPUNIT_ASSERT_EQUAL(false, StringUtils::startsWith("111111111111111", "2222"));
41         CPPUNIT_ASSERT_EQUAL(false, StringUtils::startsWith("122221111111111", "2222"));
42         CPPUNIT_ASSERT_EQUAL(false, StringUtils::startsWith("", "asdf"));
43         CPPUNIT_ASSERT_EQUAL(true, StringUtils::startsWith("aaaaaaa", ""));
44     }
45 
testSplit()46     void testSplit() {
47         vector<string> splitted = StringUtils::split("a,,b,c,d,e,f", ',');
48 
49         CPPUNIT_ASSERT_EQUAL(7, (int)splitted.size());
50         CPPUNIT_ASSERT_EQUAL(std::string("a"), splitted[0]);
51         CPPUNIT_ASSERT_EQUAL(std::string(""), splitted[1]);
52     }
53 
testSplitEmpty()54     void testSplitEmpty() {
55         vector<string> splitted = StringUtils::split("", ',');
56 
57         CPPUNIT_ASSERT_EQUAL(0, (int)splitted.size());
58     }
59 
testSplitOne()60     void testSplitOne() {
61         vector<string> splitted = StringUtils::split("aa", ',');
62 
63         CPPUNIT_ASSERT_EQUAL(1, (int)splitted.size());
64         CPPUNIT_ASSERT_EQUAL(std::string("aa"), splitted[0]);
65     }
66 
testEndsWith()67     void testEndsWith() {
68         CPPUNIT_ASSERT_EQUAL(true, StringUtils::endsWith("asdfsfdafdasfda.xoj", ".xoj"));
69         CPPUNIT_ASSERT_EQUAL(false, StringUtils::endsWith("111111111111111", "2222"));
70         CPPUNIT_ASSERT_EQUAL(false, StringUtils::endsWith("111111111122221", "2222"));
71         CPPUNIT_ASSERT_EQUAL(false, StringUtils::endsWith("", "asdf"));
72         CPPUNIT_ASSERT_EQUAL(true, StringUtils::endsWith("aaaaaaa", ""));
73     }
74 
testCompare()75     void testCompare() {
76         CPPUNIT_ASSERT_EQUAL(true, StringUtils::iequals("", ""));
77         CPPUNIT_ASSERT_EQUAL(true, StringUtils::iequals("aaaaaaaa", "aAAAaaaa"));
78         CPPUNIT_ASSERT_EQUAL(true, StringUtils::iequals("äää", "ÄÄÄ"));
79         CPPUNIT_ASSERT_EQUAL(true, StringUtils::iequals("ööaa", "Ööaa"));
80         CPPUNIT_ASSERT_EQUAL(false, StringUtils::iequals("ööaa", "ööaaa"));
81     }
82 };
83 
84 // Registers the fixture into the 'registry'
85 CPPUNIT_TEST_SUITE_REGISTRATION(StringUtilsTest);
86