1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "googletest.h"
27 
28 #include <QString>
29 
30 #include <utf8stringvector.h>
31 
32 using namespace ::testing;
33 
TEST(Utf8,QStringConversionConstructor)34 TEST(Utf8, QStringConversionConstructor)
35 {
36     ASSERT_THAT(Utf8String(QStringLiteral("text")), Utf8StringLiteral("text"));
37 }
38 
TEST(Utf8,QByteArrayConversionFunction)39 TEST(Utf8, QByteArrayConversionFunction)
40 {
41     ASSERT_THAT(Utf8String::fromByteArray("text"), Utf8StringLiteral("text"));
42 }
43 
TEST(Utf8,QStringConversionFunction)44 TEST(Utf8, QStringConversionFunction)
45 {
46     ASSERT_THAT(Utf8String::fromString(QStringLiteral("text")), Utf8StringLiteral("text"));
47 }
48 
TEST(Utf8,Utf8ConversationFunction)49 TEST(Utf8, Utf8ConversationFunction)
50 {
51     ASSERT_THAT(Utf8String::fromUtf8("text"), Utf8StringLiteral("text"));
52 }
53 
TEST(Utf8,Mid)54 TEST(Utf8, Mid)
55 {
56     Utf8String text(Utf8StringLiteral("some text"));
57 
58     ASSERT_THAT(text.mid(5, 4), Utf8StringLiteral("text"));
59     ASSERT_THAT(text.mid(5), Utf8StringLiteral("text"));
60 }
61 
TEST(Utf8,ByteSize)62 TEST(Utf8, ByteSize)
63 {
64     ASSERT_THAT(Utf8StringLiteral("text").byteSize(), 4);
65 }
66 
TEST(Utf8,Append)67 TEST(Utf8, Append)
68 {
69     Utf8String text(Utf8StringLiteral("some "));
70     text.append(Utf8StringLiteral("text"));
71 
72     ASSERT_THAT(text, Utf8StringLiteral("some text"));
73 }
74 
TEST(Utf8,ToByteArray)75 TEST(Utf8, ToByteArray)
76 {
77     Utf8String text(Utf8StringLiteral("some text"));
78 
79     ASSERT_THAT(text.toByteArray(), QByteArrayLiteral("some text"));
80 }
81 
TEST(Utf8,ToString)82 TEST(Utf8, ToString)
83 {
84     Utf8String text(Utf8StringLiteral("some text"));
85 
86     ASSERT_THAT(text.toString(), QStringLiteral("some text"));
87 }
88 
89 
TEST(Utf8,Contains)90 TEST(Utf8, Contains)
91 {
92     Utf8String text(Utf8StringLiteral("some text"));
93 
94     ASSERT_TRUE(text.contains(Utf8StringLiteral("text")));
95     ASSERT_TRUE(text.contains("text"));
96     ASSERT_TRUE(text.contains('x'));
97 }
98 
TEST(Utf8,EqualOperator)99 TEST(Utf8, EqualOperator)
100 {
101     ASSERT_TRUE(Utf8StringLiteral("text") == Utf8StringLiteral("text"));
102     ASSERT_FALSE(Utf8StringLiteral("text") == Utf8StringLiteral("text2"));
103 }
104 
TEST(Utf8,SmallerOperator)105 TEST(Utf8, SmallerOperator)
106 {
107     ASSERT_TRUE(Utf8StringLiteral("some") < Utf8StringLiteral("text"));
108     ASSERT_TRUE(Utf8StringLiteral("text") < Utf8StringLiteral("texta"));
109     ASSERT_FALSE(Utf8StringLiteral("text") < Utf8StringLiteral("some"));
110     ASSERT_FALSE(Utf8StringLiteral("text") < Utf8StringLiteral("text"));
111 }
112 
TEST(Utf8,UnequalOperator)113 TEST(Utf8, UnequalOperator)
114 {
115     ASSERT_FALSE(Utf8StringLiteral("text") != Utf8StringLiteral("text"));
116     ASSERT_TRUE(Utf8StringLiteral("text") != Utf8StringLiteral("text2"));
117 }
118 
TEST(Utf8,Join)119 TEST(Utf8, Join)
120 {
121     Utf8StringVector vector;
122 
123     vector.append(Utf8StringLiteral("some"));
124     vector.append(Utf8StringLiteral("text"));
125 
126     ASSERT_THAT(Utf8StringLiteral("some, text"), vector.join(Utf8StringLiteral(", ")));
127 }
128 
TEST(Utf8,Split)129 TEST(Utf8, Split)
130 {
131     Utf8String test(Utf8StringLiteral("some text"));
132 
133     Utf8StringVector splittedText = test.split(' ');
134 
135     ASSERT_THAT(splittedText.at(0), Utf8StringLiteral("some"));
136     ASSERT_THAT(splittedText.at(1), Utf8StringLiteral("text"));
137 }
138 
TEST(Utf8,IsEmpty)139 TEST(Utf8, IsEmpty)
140 {
141     ASSERT_FALSE(Utf8StringLiteral("text").isEmpty());
142     ASSERT_TRUE(Utf8String().isEmpty());
143 }
144 
TEST(Utf8,HasContent)145 TEST(Utf8, HasContent)
146 {
147     ASSERT_TRUE(Utf8StringLiteral("text").hasContent());
148     ASSERT_FALSE(Utf8String().hasContent());
149 }
150 
TEST(Utf8,Replace)151 TEST(Utf8, Replace)
152 {
153     Utf8String text(Utf8StringLiteral("some text"));
154 
155     text.replace(Utf8StringLiteral("some"), Utf8StringLiteral("any"));
156 
157     ASSERT_THAT(text, Utf8StringLiteral("any text"));
158 }
159 
TEST(Utf8,ReplaceNBytesFromIndexPosition)160 TEST(Utf8, ReplaceNBytesFromIndexPosition)
161 {
162     Utf8String text(Utf8StringLiteral("min"));
163 
164     text.replace(1, 1, Utf8StringLiteral("aa"));
165 
166     ASSERT_THAT(text, Utf8StringLiteral("maan"));
167 }
168 
TEST(Utf8,StartsWith)169 TEST(Utf8, StartsWith)
170 {
171     Utf8String text(Utf8StringLiteral("$column"));
172 
173     ASSERT_TRUE(text.startsWith(Utf8StringLiteral("$col")));
174     ASSERT_FALSE(text.startsWith(Utf8StringLiteral("col")));
175     ASSERT_TRUE(text.startsWith("$col"));
176     ASSERT_FALSE(text.startsWith("col"));
177     ASSERT_TRUE(text.startsWith('$'));
178     ASSERT_FALSE(text.startsWith('@'));
179 }
180 
TEST(Utf8,EndsWith)181 TEST(Utf8, EndsWith)
182 {
183     Utf8String text(Utf8StringLiteral("/my/path"));
184 
185     ASSERT_TRUE(text.endsWith(Utf8StringLiteral("path")));
186 }
187 
TEST(Utf8,Clear)188 TEST(Utf8, Clear)
189 {
190     Utf8String text(Utf8StringLiteral("$column"));
191 
192     text.clear();
193 
194     ASSERT_TRUE(text.isEmpty());
195 }
196 
TEST(Utf8,Number)197 TEST(Utf8, Number)
198 {
199     ASSERT_THAT(Utf8String::number(20), Utf8StringLiteral("20"));
200 }
201 
TEST(Utf8,FromIntegerVector)202 TEST(Utf8, FromIntegerVector)
203 {
204     QVector<int> integers({1, 2, 3});
205 
206     ASSERT_THAT(Utf8StringVector::fromIntegerVector(integers).join(Utf8StringLiteral(", ")), Utf8StringLiteral("1, 2, 3"));
207 }
208 
TEST(Utf8,PlusOperator)209 TEST(Utf8, PlusOperator)
210 {
211     auto text = Utf8StringLiteral("foo") + Utf8StringLiteral("bar");
212 
213     ASSERT_THAT(text, Utf8StringLiteral("foobar"));
214 }
215 
TEST(Utf8,PlusAssignmentOperator)216 TEST(Utf8, PlusAssignmentOperator)
217 {
218     Utf8String text("foo", 3);
219 
220     text += Utf8StringLiteral("bar");
221 
222     ASSERT_THAT(text, Utf8StringLiteral("foobar"));
223 }
224 
TEST(Utf8,CompareCharPointer)225 TEST(Utf8, CompareCharPointer)
226 {
227     Utf8String text("foo", 3);
228 
229     ASSERT_TRUE(text == "foo");
230     ASSERT_FALSE(text == "foo2");
231 }
232 
TEST(Utf8,RemoveFastFromVectorFailed)233 TEST(Utf8, RemoveFastFromVectorFailed)
234 {
235     Utf8StringVector values({Utf8StringLiteral("a"),
236                              Utf8StringLiteral("b"),
237                              Utf8StringLiteral("c"),
238                              Utf8StringLiteral("d")});
239 
240     ASSERT_FALSE(values.removeFast(Utf8StringLiteral("e")));
241 }
242 
TEST(Utf8,RemoveFastFromVector)243 TEST(Utf8, RemoveFastFromVector)
244 {
245     Utf8StringVector values({Utf8StringLiteral("a"),
246                              Utf8StringLiteral("b"),
247                              Utf8StringLiteral("c"),
248                              Utf8StringLiteral("d")});
249 
250     bool removed = values.removeFast(Utf8StringLiteral("b"));
251 
252     ASSERT_TRUE(removed);
253     ASSERT_THAT(values, Not(Contains(Utf8StringLiteral("b"))));
254 }
255 
TEST(Utf8,ConverteAutomaticallyFromQString)256 TEST(Utf8, ConverteAutomaticallyFromQString)
257 {
258     QString text(QStringLiteral("foo"));
259 
260     Utf8String utf8Text(text);
261 
262     ASSERT_THAT(utf8Text, Utf8StringLiteral("foo"));
263 }
264 
TEST(Utf8,ConverteAutomaticallyToQString)265 TEST(Utf8, ConverteAutomaticallyToQString)
266 {
267     Utf8String utf8Text(Utf8StringLiteral("foo"));
268 
269     QString text = utf8Text;
270 
271     ASSERT_THAT(text, QStringLiteral("foo"));
272 }
273 
TEST(Utf8,ConverteAutomaticallyToQByteArray)274 TEST(Utf8, ConverteAutomaticallyToQByteArray)
275 {
276     Utf8String utf8Text(Utf8StringLiteral("foo"));
277 
278     QByteArray bytes = utf8Text;
279 
280     ASSERT_THAT(bytes, QByteArrayLiteral("foo"));
281 }
282 
283