1 | #include <QtCore/QScopedPointer> |
2 | #include <QtTest/QTest> |
3 | |
4 | #include "goo/GooString.h" |
5 | |
6 | class TestGooString : public QObject |
7 | { |
8 | Q_OBJECT |
9 | public: |
10 | explicit TestGooString(QObject *parent = nullptr) : QObject(parent) { } |
11 | private slots: |
12 | void testInsertData_data(); |
13 | void testInsertData(); |
14 | void testInsert(); |
15 | void testFormat(); |
16 | void testFromNullptr(); |
17 | }; |
18 | |
19 | void TestGooString::testInsertData_data() |
20 | { |
21 | QTest::addColumn<QByteArray>(name: "string" ); |
22 | QTest::addColumn<QByteArray>(name: "addition" ); |
23 | QTest::addColumn<int>(name: "position" ); |
24 | QTest::addColumn<QByteArray>(name: "result" ); |
25 | |
26 | QTest::newRow(dataTag: "foo" ) << QByteArray("foo" ) << QByteArray("bar" ) << 0 << QByteArray("barfoo" ); |
27 | QTest::newRow(dataTag: "<empty>" ) << QByteArray() << QByteArray("bar" ) << 0 << QByteArray("bar" ); |
28 | QTest::newRow(dataTag: "foo+bar #1" ) << QByteArray("f+bar" ) << QByteArray("oo" ) << 1 << QByteArray("foo+bar" ); |
29 | QTest::newRow(dataTag: "foo+bar #2" ) << QByteArray("fobar" ) << QByteArray("o+" ) << 2 << QByteArray("foo+bar" ); |
30 | QTest::newRow(dataTag: "foo+bar #last" ) << QByteArray("foo+r" ) << QByteArray("ba" ) << 4 << QByteArray("foo+bar" ); |
31 | QTest::newRow(dataTag: "foo+bar #end" ) << QByteArray("foo+" ) << QByteArray("bar" ) << 4 << QByteArray("foo+bar" ); |
32 | QTest::newRow(dataTag: "long #start" ) << QByteArray("very string" ) << QByteArray("long long long long long " ) << 5 << QByteArray("very long long long long long string" ); |
33 | } |
34 | |
35 | void TestGooString::testInsertData() |
36 | { |
37 | QFETCH(QByteArray, string); |
38 | QFETCH(QByteArray, addition); |
39 | QFETCH(int, position); |
40 | QFETCH(QByteArray, result); |
41 | |
42 | GooString goo(string.constData()); |
43 | QCOMPARE(goo.c_str(), string.constData()); |
44 | goo.insert(i: position, str: addition.constData()); |
45 | QCOMPARE(goo.c_str(), result.constData()); |
46 | } |
47 | |
48 | void TestGooString::testInsert() |
49 | { |
50 | { |
51 | GooString goo; |
52 | goo.insert(i: 0, str: "." ); |
53 | goo.insert(i: 0, str: "This is a very long long test string" ); |
54 | QCOMPARE(goo.c_str(), "This is a very long long test string." ); |
55 | } |
56 | { |
57 | GooString goo; |
58 | goo.insert(i: 0, str: "second-part-third-part" ); |
59 | goo.insert(i: 0, str: "first-part-" ); |
60 | QCOMPARE(goo.c_str(), "first-part-second-part-third-part" ); |
61 | } |
62 | } |
63 | |
64 | void TestGooString::testFormat() |
65 | { |
66 | { |
67 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:d},{1:x}" , 1, 0xF)); |
68 | QCOMPARE(goo->c_str(), "1,f" ); |
69 | } |
70 | { |
71 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:d},{0:x},{0:X},{0:o},{0:b},{0:w}" , 0xA)); |
72 | QCOMPARE(goo->c_str(), "10,a,A,12,1010, " ); |
73 | } |
74 | { |
75 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:d},{0:x},{0:X},{0:o},{0:b}" , -0xA)); |
76 | QCOMPARE(goo->c_str(), "-10,-a,-A,-12,-1010" ); |
77 | } |
78 | { |
79 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:c}{1:c}{2:c}{3:c}" , 'T', (char)'E', (short)'S', (int)'T')); |
80 | QCOMPARE(goo->c_str(), "TEST" ); |
81 | |
82 | const std::unique_ptr<GooString> goo2(GooString::format(fmt: "{0:s} {1:t}" , "TEST" , goo.get())); |
83 | QCOMPARE(goo2->c_str(), "TEST TEST" ); |
84 | } |
85 | { |
86 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:ud} {1:d} {2:d}" , UINT_MAX, INT_MAX, INT_MIN)); |
87 | const QByteArray expected = QStringLiteral("%1 %2 %3" ).arg(UINT_MAX).arg(INT_MAX).arg(INT_MIN).toLatin1(); |
88 | QCOMPARE(goo->c_str(), expected.constData()); |
89 | } |
90 | { |
91 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:uld} {1:ld} {2:ld}" , ULONG_MAX, LONG_MAX, LONG_MIN)); |
92 | const QByteArray expected = QStringLiteral("%1 %2 %3" ).arg(ULONG_MAX).arg(LONG_MAX).arg(LONG_MIN).toLatin1(); |
93 | QCOMPARE(goo->c_str(), expected.constData()); |
94 | } |
95 | { |
96 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:ulld} {1:lld} {2:lld}" , ULLONG_MAX, LLONG_MAX, LLONG_MIN)); |
97 | const QByteArray expected = QStringLiteral("%1 %2 %3" ).arg(ULLONG_MAX).arg(LLONG_MAX).arg(LLONG_MIN).toLatin1(); |
98 | QCOMPARE(goo->c_str(), expected.constData()); |
99 | } |
100 | { |
101 | const std::unique_ptr<GooString> gooD(GooString::format(fmt: "{0:.1f} {0:.1g} {0:.1gs} | {1:.1f} {1:.1g} {1:.1gs}" , 1., .012)); |
102 | const std::unique_ptr<GooString> gooF(GooString::format(fmt: "{0:.1f} {0:.1g} {0:.1gs} | {1:.1f} {1:.1g} {1:.1gs}" , 1.f, .012f)); |
103 | QCOMPARE(gooD->c_str(), "1.0 1 1 | 0.0 0 0.01" ); |
104 | QCOMPARE(gooF->c_str(), "1.0 1 1 | 0.0 0 0.01" ); |
105 | } |
106 | { |
107 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{0:.4f} {0:.4g} {0:.4gs}" , .012)); |
108 | QCOMPARE(goo->c_str(), "0.0120 0.012 0.012" ); |
109 | } |
110 | { |
111 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{{ SomeText {0:d} }}" , 1)); |
112 | QCOMPARE(goo->c_str(), "{ SomeText 1 }" ); |
113 | } |
114 | { |
115 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "{{{{ {{ SomeText {0:d}" , 2)); |
116 | QCOMPARE(goo->c_str(), "{{ { SomeText 2" ); |
117 | } |
118 | { |
119 | const std::unique_ptr<GooString> goo(GooString::format(fmt: "SomeText {0:d} }} }}}}" , 3)); |
120 | QCOMPARE(goo->c_str(), "SomeText 3 } }}" ); |
121 | } |
122 | } |
123 | |
124 | void TestGooString::testFromNullptr() |
125 | { |
126 | { |
127 | GooString str { static_cast<const GooString *>(nullptr) }; |
128 | QCOMPARE(str.getLength(), 0); |
129 | } |
130 | |
131 | { |
132 | GooString str; |
133 | str.Set(static_cast<const GooString *>(nullptr)); |
134 | QCOMPARE(str.getLength(), 0); |
135 | } |
136 | |
137 | { |
138 | GooString str { static_cast<const char *>(nullptr) }; |
139 | QCOMPARE(str.getLength(), 0); |
140 | } |
141 | |
142 | { |
143 | GooString str { static_cast<const char *>(nullptr), 0 }; |
144 | QCOMPARE(str.getLength(), 0); |
145 | } |
146 | |
147 | { |
148 | GooString str; |
149 | str.Set(static_cast<const char *>(nullptr)); |
150 | QCOMPARE(str.getLength(), 0); |
151 | } |
152 | |
153 | { |
154 | GooString str; |
155 | str.Set(newStr: static_cast<const char *>(nullptr), newLen: 0); |
156 | QCOMPARE(str.getLength(), 0); |
157 | } |
158 | } |
159 | |
160 | QTEST_GUILESS_MAIN(TestGooString) |
161 | #include "check_goostring.moc" |
162 | |