comparison base/test/TestStringBits.h @ 1022:eecf544bed92

Unit tests for StringBits::splitQuoted
author Chris Cannam
date Mon, 01 Dec 2014 15:42:58 +0000
parents
children 64ef24ebb19c
comparison
equal deleted inserted replaced
1021:1888ca033a84 1022:eecf544bed92
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef TEST_STRINGBITS_H
16 #define TEST_STRINGBITS_H
17
18 #include "../StringBits.h"
19
20 #include <QObject>
21 #include <QStringList>
22 #include <QtTest>
23
24 #include <iostream>
25
26 using namespace std;
27
28 class TestStringBits : public QObject
29 {
30 Q_OBJECT
31
32 private:
33 void testSplitQuoted(QString in, QStringList out) {
34 // Only suitable where the output strings do not have
35 // consecutive spaces in them
36 QCOMPARE(StringBits::splitQuoted(in, ' '), out);
37 QString in2(in);
38 in2.replace(' ', ',');
39 QStringList out2;
40 foreach (QString o, out) {
41 out2 << o.replace(' ', ',');
42 }
43 QCOMPARE(StringBits::splitQuoted(in2, ','), out2);
44 }
45
46 private slots:
47 void simple() {
48 QString in = "a b c d";
49 QStringList out;
50 out << "a" << "b" << "c" << "d";
51 testSplitQuoted(in, out);
52 }
53
54 void dquoted() {
55 QString in = "a \"b c\" d";
56 QStringList out;
57 out << "a" << "b c" << "d";
58 testSplitQuoted(in, out);
59 }
60
61 void drunon() {
62 QString in = "a \"b c\"d e";
63 QStringList out;
64 out << "a" << "b cd" << "e";
65 testSplitQuoted(in, out);
66 }
67
68 void squoted() {
69 QString in = "a 'b c' d";
70 QStringList out;
71 out << "a" << "b c" << "d";
72 testSplitQuoted(in, out);
73 }
74
75 void srunon() {
76 QString in = "a 'b c'd e";
77 QStringList out;
78 out << "a" << "b cd" << "e";
79 testSplitQuoted(in, out);
80 }
81
82 void dempty() {
83 QString in = "\"\" \"\" \"\"";
84 QStringList out;
85 out << "" << "" << "";
86 testSplitQuoted(in, out);
87 }
88
89 void sempty() {
90 QString in = "'' '' ''";
91 QStringList out;
92 out << "" << "" << "";
93 testSplitQuoted(in, out);
94 }
95
96 void descaped() {
97 QString in = "a \"b c\\\" d\"";
98 QStringList out;
99 out << "a" << "b c\" d";
100 testSplitQuoted(in, out);
101 }
102
103 void sescaped() {
104 QString in = "a 'b c\\' d'";
105 QStringList out;
106 out << "a" << "b c' d";
107 testSplitQuoted(in, out);
108 }
109
110 void dnested() {
111 QString in = "a \"b c' d\"";
112 QStringList out;
113 out << "a" << "b c' d";
114 testSplitQuoted(in, out);
115 }
116
117 void snested() {
118 QString in = "a 'b c\" d'";
119 QStringList out;
120 out << "a" << "b c\" d";
121 testSplitQuoted(in, out);
122 }
123
124 void snested2() {
125 QString in = "aa 'bb cc\" dd'";
126 QStringList out;
127 out << "aa" << "bb cc\" dd";
128 testSplitQuoted(in, out);
129 }
130
131 void qquoted() {
132 QString in = "a'a 'bb' \\\"cc\" dd\\\"";
133 QStringList out;
134 out << "a'a" << "bb" << "\"cc\"" << "dd\"";
135 testSplitQuoted(in, out);
136 }
137
138 void multispace() {
139 QString in = " a'a \\' 'bb' ' \\\"cc\" ' dd\\\" '";
140 QStringList out;
141 out << "a'a" << "'" << "bb" << " \"cc\" " << "dd\"" << "'";
142 QCOMPARE(StringBits::splitQuoted(in, ' '), out);
143
144 QString in2 = ",,a'a,\\',,,,,,,,,'bb',,,,',,,,,,\\\"cc\",',dd\\\",'";
145 QStringList out2;
146 out2 << "" << "" << "a'a" << "'" << "" << "" << "" << "" << "" << ""
147 << "" << "" << "bb" << "" << "" << "" << ",,,,,,\"cc\","
148 << "dd\"" << "'";
149 QCOMPARE(StringBits::splitQuoted(in2, ','), out2);
150 }
151 };
152
153 #endif
154
155 /* r928
156 Config: Using QtTest library 5.3.2, Qt 5.3.2
157 PASS : TestStringBits::initTestCase()
158 PASS : TestStringBits::simple()
159 PASS : TestStringBits::dquoted()
160 PASS : TestStringBits::squoted()
161 PASS : TestStringBits::descaped()
162 FAIL! : TestStringBits::sescaped() Compared lists have different sizes.
163 Actual (StringBits::splitQuoted(in, ' ')) size: 3
164 Expected (out) size: 2
165 Loc: [o/../TestStringBits.h(65)]
166 PASS : TestStringBits::dnested()
167 PASS : TestStringBits::snested()
168 PASS : TestStringBits::snested2()
169 PASS : TestStringBits::qquoted()
170 FAIL! : TestStringBits::multispace() Compared lists differ at index 1.
171 Actual (StringBits::splitQuoted(in, ' ')): " "
172 Expected (out): "'"
173 Loc: [o/../TestStringBits.h(100)]
174 FAIL! : TestStringBits::qcommas() Compared lists have different sizes.
175 Actual (StringBits::splitQuoted(in, ',')) size: 4
176 Expected (out) size: 3
177 Loc: [o/../TestStringBits.h(107)]
178 PASS : TestStringBits::cleanupTestCase()
179 Totals: 10 passed, 3 failed, 0 skipped
180 */
181
182 /*curr
183 PASS : TestStringBits::initTestCase()
184 PASS : TestStringBits::simple()
185 PASS : TestStringBits::dquoted()
186 PASS : TestStringBits::squoted()
187 PASS : TestStringBits::descaped()
188 FAIL! : TestStringBits::sescaped() Compared lists have different sizes.
189 Actual (StringBits::splitQuoted(in, ' ')) size: 3
190 Expected (out) size: 2
191 Loc: [o/../TestStringBits.h(65)]
192 PASS : TestStringBits::dnested()
193 PASS : TestStringBits::snested()
194 PASS : TestStringBits::snested2()
195 PASS : TestStringBits::qquoted()
196 FAIL! : TestStringBits::multispace() Compared lists have different sizes.
197 Actual (StringBits::splitQuoted(in, ' ')) size: 5
198 Expected (out) size: 6
199 Loc: [o/../TestStringBits.h(100)]
200 PASS : TestStringBits::qcommas()
201 PASS : TestStringBits::cleanupTestCase()
202 Totals: 11 passed, 2 failed, 0 skipped
203 */