|
1 /* This file is part of QJson |
|
2 * |
|
3 * Copyright (C) 2009 Flavio Castelli <flavio.castelli@gmail.com> |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Library General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Library General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Library General Public License |
|
16 * along with this library; see the file COPYING.LIB. If not, write to |
|
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
18 * Boston, MA 02110-1301, USA. |
|
19 */ |
|
20 |
|
21 #include <limits> |
|
22 |
|
23 #include <QtTest/QtTest> |
|
24 |
|
25 #include "parser.h" |
|
26 #include "serializer.h" |
|
27 |
|
28 #include <QtCore/QVariant> |
|
29 |
|
30 |
|
31 class TestSerializer: public QObject |
|
32 { |
|
33 Q_OBJECT |
|
34 private slots: |
|
35 void testReadWriteEmptyDocument(); |
|
36 void testReadWrite(); |
|
37 void testReadWrite_data(); |
|
38 void testValueNull(); |
|
39 void testValueString(); |
|
40 void testValueString_data(); |
|
41 void testValueInteger(); |
|
42 void testValueInteger_data(); |
|
43 void testValueDouble(); |
|
44 void testValueDouble_data(); |
|
45 void testValueBoolean(); |
|
46 void testValueBoolean_data(); |
|
47 |
|
48 private: |
|
49 void valueTest( const QVariant& value, const QString& expectedRegExp ); |
|
50 void valueTest( const QObject* object, const QString& expectedRegExp ); |
|
51 }; |
|
52 |
|
53 Q_DECLARE_METATYPE(QVariant) |
|
54 |
|
55 using namespace QJson; |
|
56 |
|
57 void TestSerializer::testReadWriteEmptyDocument() |
|
58 { |
|
59 QByteArray json = ""; |
|
60 Parser parser; |
|
61 bool ok; |
|
62 QVariant result = parser.parse( json, &ok ); |
|
63 QVERIFY(ok); |
|
64 QVERIFY( ! result.isValid() ); |
|
65 Serializer serializer; |
|
66 const QByteArray serialized = serializer.serialize( result ); |
|
67 QVERIFY( !serialized.isNull() ); |
|
68 QByteArray expected = "null"; |
|
69 QCOMPARE(expected, serialized); |
|
70 } |
|
71 |
|
72 void TestSerializer::testReadWrite() |
|
73 { |
|
74 QFETCH( QByteArray, json ); |
|
75 Parser parser; |
|
76 bool ok; |
|
77 QVariant result = parser.parse( json, &ok ); |
|
78 QVERIFY(ok); |
|
79 Serializer serializer; |
|
80 const QByteArray serialized = serializer.serialize( result ); |
|
81 QVariant writtenThenRead = parser.parse( serialized, &ok ); |
|
82 QVERIFY(ok); |
|
83 QCOMPARE( result, writtenThenRead ); |
|
84 } |
|
85 |
|
86 void TestSerializer::testReadWrite_data() |
|
87 { |
|
88 QTest::addColumn<QByteArray>( "json" ); |
|
89 |
|
90 // array tests |
|
91 QTest::newRow( "empty array" ) << QByteArray("[]"); |
|
92 QTest::newRow( "basic array" ) << QByteArray("[\"person\",\"bar\"]"); |
|
93 QTest::newRow( "single int array" ) << QByteArray("[6]"); |
|
94 QTest::newRow( "int array" ) << QByteArray("[6,5,6,7]"); |
|
95 const QByteArray json = "[1,2.4, -100, -3.4, -5e+, 2e,3e+,4.3E,5.4E-]"; |
|
96 QTest::newRow( QByteArray("array of various numbers") ) << json; |
|
97 |
|
98 // document tests |
|
99 QTest::newRow( "empty object" ) << QByteArray("{}"); |
|
100 QTest::newRow( "basic document" ) << QByteArray("{\"person\":\"bar\"}"); |
|
101 QTest::newRow( "object with ints" ) << QByteArray("{\"person\":6}"); |
|
102 const QByteArray json2 = "{ \"person\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}"; |
|
103 QTest::newRow( "complicated document" ) << json2; |
|
104 |
|
105 // more complex cases |
|
106 const QByteArray json3 = "[ {\"person\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]"; |
|
107 QTest::newRow( "complicated array" ) << json3; |
|
108 } |
|
109 |
|
110 |
|
111 void TestSerializer::valueTest( const QVariant& value, const QString& expectedRegExp ) |
|
112 { |
|
113 Serializer serializer; |
|
114 const QByteArray serialized = serializer.serialize( value ); |
|
115 const QString serializedUnicode = QString::fromUtf8( serialized ); |
|
116 QRegExp expected( expectedRegExp ); |
|
117 QVERIFY( expected.isValid() ); |
|
118 QVERIFY2( expected.exactMatch( serializedUnicode ), |
|
119 qPrintable( QString( QLatin1String( "Expected regexp \"%1\" but got \"%2\"." ) ) |
|
120 .arg( expectedRegExp ).arg( serializedUnicode ) ) ); |
|
121 } |
|
122 |
|
123 void TestSerializer::valueTest( const QObject* object, const QString& expectedRegExp ) |
|
124 { |
|
125 Serializer serializer; |
|
126 const QByteArray serialized = serializer.serialize( object ); |
|
127 const QString serializedUnicode = QString::fromUtf8( serialized ); |
|
128 QRegExp expected( expectedRegExp ); |
|
129 QVERIFY( expected.isValid() ); |
|
130 QVERIFY2( expected.exactMatch( serializedUnicode ), |
|
131 qPrintable( QString( QLatin1String( "Expected regexp \"%1\" but got \"%2\"." ) ) |
|
132 .arg( expectedRegExp ).arg( serializedUnicode ) ) ); |
|
133 } |
|
134 |
|
135 void TestSerializer::testValueNull() |
|
136 { |
|
137 valueTest( QVariant(), QLatin1String( "\\s*null\\s*" ) ); |
|
138 QVariantMap map; |
|
139 map[QLatin1String("value")] = QVariant(); |
|
140 valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:\\s*null\\s*\\}\\s*" ) ); |
|
141 } |
|
142 |
|
143 void TestSerializer::testValueString() |
|
144 { |
|
145 QFETCH( QVariant, value ); |
|
146 QFETCH( QString, expected ); |
|
147 valueTest( value, expected ); |
|
148 |
|
149 QVariantMap map; |
|
150 map[QLatin1String("value")] = value; |
|
151 valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); |
|
152 } |
|
153 |
|
154 void TestSerializer::testValueString_data() |
|
155 { |
|
156 QTest::addColumn<QVariant>( "value" ); |
|
157 QTest::addColumn<QString>( "expected" ); |
|
158 |
|
159 QTest::newRow( "null string" ) << QVariant( QString() ) << QString( QLatin1String( "\\s*\"\"\\s*" ) ); |
|
160 QTest::newRow( "empty string" ) << QVariant( QString( QLatin1String( "" ) ) ) << QString( QLatin1String( "\\s*\"\"\\s*" ) ); |
|
161 QTest::newRow( "Simple String" ) << QVariant( QString( QLatin1String( "simpleString" ) ) ) << QString( QLatin1String( "\\s*\"simpleString\"\\s*" ) ); |
|
162 QTest::newRow( "string with tab" ) << QVariant( QString( QLatin1String( "string\tstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\tstring\"\\s*" ) ); |
|
163 QTest::newRow( "string with newline" ) << QVariant( QString( QLatin1String( "string\nstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\nstring\"\\s*" ) ); |
|
164 QTest::newRow( "string with bell" ) << QVariant( QString( QLatin1String( "string\bstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\bstring\"\\s*" ) ); |
|
165 QTest::newRow( "string with return" ) << QVariant( QString( QLatin1String( "string\rstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\rstring\"\\s*" ) ); |
|
166 QTest::newRow( "string with double quote" ) << QVariant( QString( QLatin1String( "string\"string" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\\"string\"\\s*" ) ); |
|
167 QTest::newRow( "string with backslash" ) << QVariant( QString( QLatin1String( "string\\string" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\\\\\string\"\\s*" ) ); |
|
168 QString testStringWithUnicode = QString( QLatin1String( "string" ) ) + QChar( 0x2665 ) + QLatin1String( "string" ); |
|
169 QTest::newRow( "string with unicode" ) << QVariant( testStringWithUnicode ) << QLatin1String( "\\s*\"" ) + testStringWithUnicode + QLatin1String( "\"\\s*" ); |
|
170 } |
|
171 |
|
172 void TestSerializer::testValueInteger() |
|
173 { |
|
174 QFETCH( QVariant, value ); |
|
175 QFETCH( QString, expected ); |
|
176 valueTest( value, expected ); |
|
177 |
|
178 QVariantMap map; |
|
179 map[QLatin1String("value")] = value; |
|
180 valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); |
|
181 } |
|
182 |
|
183 void TestSerializer::testValueInteger_data() |
|
184 { |
|
185 QTest::addColumn<QVariant>( "value" ); |
|
186 QTest::addColumn<QString>( "expected" ); |
|
187 |
|
188 QTest::newRow( "int 0" ) << QVariant( static_cast<int>( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) ); |
|
189 QTest::newRow( "uint 0" ) << QVariant( static_cast<uint>( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) ); |
|
190 QTest::newRow( "int -1" ) << QVariant( static_cast<int>( -1 ) ) << QString( QLatin1String( "\\s*-1\\s*" ) ); |
|
191 QTest::newRow( "int 2133149800" ) << QVariant( static_cast<int>(2133149800) ) << QString( QLatin1String( "\\s*2133149800\\s*" ) ); |
|
192 QTest::newRow( "uint 4133149800" ) << QVariant( static_cast<uint>(4133149800u) ) << QString( QLatin1String( "\\s*4133149800\\s*" ) ); |
|
193 QTest::newRow( "uint64 932838457459459" ) << QVariant( Q_UINT64_C(932838457459459) ) << QString( QLatin1String( "\\s*932838457459459\\s*" ) ); |
|
194 QTest::newRow( "max unsigned long long" ) << QVariant( std::numeric_limits<unsigned long long>::max() ) << QString( QLatin1String( "\\s*%1\\s*" ) ).arg(std::numeric_limits<unsigned long long>::max()); |
|
195 } |
|
196 |
|
197 void TestSerializer::testValueDouble() |
|
198 { |
|
199 QFETCH( QVariant, value ); |
|
200 QFETCH( QString, expected ); |
|
201 valueTest( value, expected ); |
|
202 |
|
203 QVariantMap map; |
|
204 map[QLatin1String("value")] = value; |
|
205 valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); |
|
206 } |
|
207 |
|
208 void TestSerializer::testValueDouble_data() |
|
209 { |
|
210 QTest::addColumn<QVariant>( "value" ); |
|
211 QTest::addColumn<QString>( "expected" ); |
|
212 |
|
213 QTest::newRow( "double 0" ) << QVariant( 0.0 ) << QString( QLatin1String( "\\s*0.0\\s*" ) ); |
|
214 QTest::newRow( "double -1" ) << QVariant( -1.0 ) << QString( QLatin1String( "\\s*-1.0\\s*" ) ); |
|
215 QTest::newRow( "double 1.5E-20" ) << QVariant( 1.5e-20 ) << QString( QLatin1String( "\\s*1.5[Ee]-20\\s*" ) ); |
|
216 QTest::newRow( "double -1.5E-20" ) << QVariant( -1.5e-20 ) << QString( QLatin1String( "\\s*-1.5[Ee]-20\\s*" ) ); |
|
217 QTest::newRow( "double 2.0E-20" ) << QVariant( 2.0e-20 ) << QString( QLatin1String( "\\s*2(?:.0)?[Ee]-20\\s*" ) ); |
|
218 } |
|
219 |
|
220 void TestSerializer::testValueBoolean() |
|
221 { |
|
222 QFETCH( QVariant, value ); |
|
223 QFETCH( QString, expected ); |
|
224 valueTest( value, expected ); |
|
225 |
|
226 QVariantMap map; |
|
227 map[QLatin1String("value")] = value; |
|
228 valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); |
|
229 } |
|
230 |
|
231 void TestSerializer::testValueBoolean_data() |
|
232 { |
|
233 QTest::addColumn<QVariant>( "value" ); |
|
234 QTest::addColumn<QString>( "expected" ); |
|
235 |
|
236 QTest::newRow( "bool false" ) << QVariant( false ) << QString( QLatin1String( "\\s*false\\s*" ) ); |
|
237 QTest::newRow( "bool true" ) << QVariant( true ) << QString( QLatin1String( "\\s*true\\s*" ) ); |
|
238 } |
|
239 |
|
240 QTEST_MAIN(TestSerializer) |
|
241 #include "moc_testserializer.cxx" |