summaryrefslogtreecommitdiffstats
blob: 161b4f1b574bc4dea8e0db25c0d585855df6fe42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include "Swiften/Base/String.h"

using namespace Swift;

class StringTest : public CppUnit::TestFixture
{
		CPPUNIT_TEST_SUITE(StringTest);
		CPPUNIT_TEST(testGetUnicodeCodePoints);
		CPPUNIT_TEST(testGetSplittedAtFirst);
		CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtEnd);
		CPPUNIT_TEST(testGetSplittedAtFirst_NoSuchCharacter);
		CPPUNIT_TEST(testRemoveAll);
		CPPUNIT_TEST(testRemoveAll_LastChar);
		CPPUNIT_TEST(testRemoveAll_ConsecutiveChars);
		CPPUNIT_TEST(testReplaceAll);
		CPPUNIT_TEST(testReplaceAll_LastChar);
		CPPUNIT_TEST(testReplaceAll_ConsecutiveChars);
		CPPUNIT_TEST(testReplaceAll_MatchingReplace);
		CPPUNIT_TEST(testGetLowerCase);
		CPPUNIT_TEST(testSplit);
 		CPPUNIT_TEST(testContains);
		CPPUNIT_TEST(testContainsFalse);
		CPPUNIT_TEST(testContainsExact);
		CPPUNIT_TEST(testEndsWith);
		CPPUNIT_TEST(testEndsWith_DoesNotEndWith);
		CPPUNIT_TEST_SUITE_END();

	public:
		void testGetUnicodeCodePoints() {
			String testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d");
			std::vector<unsigned int> points = testling.getUnicodeCodePoints();

			CPPUNIT_ASSERT_EQUAL(0x24U, points[0]);
			CPPUNIT_ASSERT_EQUAL(0xA2U, points[1]);
			CPPUNIT_ASSERT_EQUAL(0x20ACU, points[2]);
			CPPUNIT_ASSERT_EQUAL(0x10ABCDU, points[3]);
		}

		void testGetSplittedAtFirst() {
			String testling("ab@cd@ef");

			std::pair<String,String> result = testling.getSplittedAtFirst('@');
			CPPUNIT_ASSERT_EQUAL(String("ab"), result.first);
			CPPUNIT_ASSERT_EQUAL(String("cd@ef"), result.second);
		}

		void testGetSplittedAtFirst_CharacterAtEnd() {
			String testling("ab@");

			std::pair<String,String> result = testling.getSplittedAtFirst('@');
			CPPUNIT_ASSERT_EQUAL(String("ab"), result.first);
			CPPUNIT_ASSERT(result.second.isEmpty());
		}

		void testGetSplittedAtFirst_NoSuchCharacter() {
			String testling("ab");

			std::pair<String,String> result = testling.getSplittedAtFirst('@');
			CPPUNIT_ASSERT_EQUAL(String("ab"), result.first);
			CPPUNIT_ASSERT(result.second.isEmpty());
		}

		void testRemoveAll() {
			String testling("ab c de");

			testling.removeAll(' ');
			
			CPPUNIT_ASSERT_EQUAL(String("abcde"), testling);
		}

		void testRemoveAll_LastChar() {
			String testling("abcde ");

			testling.removeAll(' ');
			
			CPPUNIT_ASSERT_EQUAL(String("abcde"), testling);
		}

		void testRemoveAll_ConsecutiveChars() {
			String testling("ab  cde");

			testling.removeAll(' ');
			
			CPPUNIT_ASSERT_EQUAL(String("abcde"), testling);
		}

		void testReplaceAll() {
			String testling("abcbd");

			testling.replaceAll('b', "xyz");
			
			CPPUNIT_ASSERT_EQUAL(String("axyzcxyzd"), testling);
		}

		void testReplaceAll_LastChar() {
			String testling("abc");

			testling.replaceAll('c', "xyz");
			
			CPPUNIT_ASSERT_EQUAL(String("abxyz"), testling);
		}

		void testReplaceAll_ConsecutiveChars() {
			String testling("abbc");

			testling.replaceAll('b',"xyz");
			
			CPPUNIT_ASSERT_EQUAL(String("axyzxyzc"), testling);
		}

		void testReplaceAll_MatchingReplace() {
			String testling("abc");

			testling.replaceAll('b',"bbb");
			
			CPPUNIT_ASSERT_EQUAL(String("abbbc"), testling);
		}

		void testGetLowerCase() {
			String testling("aBcD e");

			CPPUNIT_ASSERT_EQUAL(String("abcd e"), testling.getLowerCase());
		}

		void testSplit() {
			std::vector<String> result = String("abc def ghi").split(' ');

			CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size()));
			CPPUNIT_ASSERT_EQUAL(String("abc"), result[0]);
			CPPUNIT_ASSERT_EQUAL(String("def"), result[1]);
			CPPUNIT_ASSERT_EQUAL(String("ghi"), result[2]);
		}

		void testContains() {
			CPPUNIT_ASSERT(String("abcde").contains(String("bcd")));
		}

		void testContainsFalse() {
			CPPUNIT_ASSERT(!String("abcde").contains(String("abcdef")));
		}

		void testContainsExact() {
			CPPUNIT_ASSERT(String("abcde").contains(String("abcde")));
		}

		void testEndsWith() {
			CPPUNIT_ASSERT(String("abcdef").endsWith("cdef"));
		}

		void testEndsWith_DoesNotEndWith() {
			CPPUNIT_ASSERT(!String("abcdef").endsWith("ddef"));
		}

};

CPPUNIT_TEST_SUITE_REGISTRATION(StringTest);