smf/smfservermodule/util/qjson/tests/testparser.cpp
changeset 10 1d94eb8df9c2
parent 9 b85b0c039c14
equal deleted inserted replaced
9:b85b0c039c14 10:1d94eb8df9c2
     1 /* This file is part of QJson
       
     2  *
       
     3  * Copyright (C) 2008 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 <QtTest/QtTest>
       
    22 
       
    23 #include "parser.h"
       
    24 
       
    25 #include <QtCore/QVariant>
       
    26 
       
    27 class TestParser: public QObject
       
    28 {
       
    29   Q_OBJECT
       
    30   private slots:
       
    31     void parseNonAsciiString();
       
    32     void parseSimpleObject();
       
    33     void parseEmptyObject();
       
    34     void parseEmptyValue();
       
    35     void parseUrl();
       
    36     void parseMultipleObject();
       
    37 
       
    38     void parseSimpleArray();
       
    39     void parseInvalidObject();
       
    40     void parseMultipleArray();
       
    41 
       
    42     void testTrueFalseNullValues();
       
    43     void testEscapeChars();
       
    44     void testNumbers();
       
    45     void testNumbers_data();
       
    46 };
       
    47 
       
    48 Q_DECLARE_METATYPE(QVariant)
       
    49 Q_DECLARE_METATYPE(QVariant::Type)
       
    50 
       
    51 using namespace QJson;
       
    52 
       
    53 void TestParser::parseSimpleObject() {
       
    54   QByteArray json = "{\"foo\":\"bar\"}";
       
    55   QVariantMap map;
       
    56   map.insert (QLatin1String("foo"), QLatin1String("bar"));
       
    57   QVariant expected(map);
       
    58 
       
    59   Parser parser;
       
    60   bool ok;
       
    61   QVariant result = parser.parse (json, &ok);
       
    62   QVERIFY (ok);
       
    63   QCOMPARE(result, expected);
       
    64 }
       
    65 
       
    66 void TestParser::parseEmptyObject() {
       
    67   QByteArray json = "{}";
       
    68   QVariantMap map;
       
    69   QVariant expected (map);
       
    70 
       
    71   Parser parser;
       
    72   bool ok;
       
    73   QVariant result = parser.parse (json, &ok);
       
    74   QVERIFY (ok);
       
    75   QCOMPARE(result, expected);
       
    76 }
       
    77 
       
    78 void TestParser::parseEmptyValue() {
       
    79   QByteArray json = "{\"value\": \"\"}";
       
    80 
       
    81   QVariantMap map;
       
    82   map.insert (QLatin1String("value"), QString(QLatin1String("")));
       
    83   QVariant expected (map);
       
    84 
       
    85   Parser parser;
       
    86   bool ok;
       
    87   QVariant result = parser.parse (json, &ok);
       
    88   QVERIFY (ok);
       
    89   QCOMPARE(result, expected);
       
    90   QVERIFY (result.toMap().value(QLatin1String("value")).type() == QVariant::String);
       
    91 
       
    92   QString value = result.toMap().value(QLatin1String("value")).toString();
       
    93   QVERIFY (value.isEmpty());
       
    94 }
       
    95 
       
    96 void TestParser::parseInvalidObject() {
       
    97   QByteArray json = "{\"foo\":\"bar\"";
       
    98 
       
    99   Parser parser;
       
   100   bool ok;
       
   101   QVariant result = parser.parse (json, &ok);
       
   102   QVERIFY (!ok);
       
   103 }
       
   104 
       
   105 void TestParser::parseNonAsciiString() {
       
   106   QByteArray json = "{\"artist\":\"Queensr\\u00ffche\"}";
       
   107   QVariantMap map;
       
   108 
       
   109   QChar unicode_char (0x00ff);
       
   110   QString unicode_string;
       
   111   unicode_string.setUnicode(&unicode_char, 1);
       
   112   unicode_string = QLatin1String("Queensr") + unicode_string + QLatin1String("che");
       
   113   
       
   114   map.insert (QLatin1String("artist"), unicode_string);
       
   115   QVariant expected (map);
       
   116 
       
   117   Parser parser;
       
   118   bool ok;
       
   119   QVariant result = parser.parse (json, &ok);
       
   120   QVERIFY (ok);
       
   121   QCOMPARE(result, expected);
       
   122 }
       
   123 
       
   124 void TestParser::parseMultipleObject() {
       
   125   //put also some extra spaces inside the json string
       
   126   QByteArray json = "{ \"foo\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}";
       
   127   QVariantMap map;
       
   128   map.insert (QLatin1String("foo"), QLatin1String("bar"));
       
   129   map.insert (QLatin1String("number"), 51.3);
       
   130   QVariantList list;
       
   131   list.append (QLatin1String("item1"));
       
   132   list.append (QLatin1String("123"));
       
   133   map.insert (QLatin1String("array"), list);
       
   134   QVariant expected (map);
       
   135 
       
   136   Parser parser;
       
   137   bool ok;
       
   138   QVariant result = parser.parse (json, &ok);
       
   139   QVERIFY (ok);
       
   140   QCOMPARE(result, expected);
       
   141   QVERIFY (result.toMap().value(QLatin1String("number")).canConvert<float>());
       
   142   QVERIFY (result.toMap().value(QLatin1String("array")).canConvert<QVariantList>());
       
   143 }
       
   144 
       
   145 void TestParser::parseUrl(){
       
   146   //"http:\/\/www.last.fm\/venue\/8926427"
       
   147   QByteArray json = "[\"http:\\/\\/www.last.fm\\/venue\\/8926427\"]";
       
   148   QVariantList list;
       
   149   list.append (QVariant(QLatin1String("http://www.last.fm/venue/8926427")));
       
   150   QVariant expected (list);
       
   151 
       
   152   Parser parser;
       
   153   bool ok;
       
   154   QVariant result = parser.parse (json, &ok);
       
   155   QVERIFY (ok);
       
   156   QCOMPARE(result, expected);
       
   157 }
       
   158 
       
   159  void TestParser::parseSimpleArray() {
       
   160   QByteArray json = "[\"foo\",\"bar\"]";
       
   161   QVariantList list;
       
   162   list.append (QLatin1String("foo"));
       
   163   list.append (QLatin1String("bar"));
       
   164   QVariant expected (list);
       
   165 
       
   166   Parser parser;
       
   167   bool ok;
       
   168   QVariant result = parser.parse (json, &ok);
       
   169   QVERIFY (ok);
       
   170   QCOMPARE(result, expected);
       
   171 }
       
   172 
       
   173 void TestParser::parseMultipleArray() {
       
   174   //put also some extra spaces inside the json string
       
   175   QByteArray json = "[ {\"foo\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]";
       
   176   QVariantMap map;
       
   177   map.insert (QLatin1String("foo"), QLatin1String("bar"));
       
   178 
       
   179   QVariantList array;
       
   180   array.append (QLatin1String("item1"));
       
   181   array.append (123);
       
   182   
       
   183   QVariantList list;
       
   184   list.append (map);
       
   185   list.append (QLatin1String("number"));
       
   186   list.append (QLatin1String("51.3"));
       
   187   list.append ((QVariant) array);
       
   188 
       
   189   QVariant expected (list);
       
   190 
       
   191   Parser parser;
       
   192   bool ok;
       
   193   QVariant result = parser.parse (json, &ok);
       
   194   QVERIFY (ok);
       
   195   QCOMPARE(result, expected);
       
   196 }
       
   197 
       
   198 void TestParser::testTrueFalseNullValues() {
       
   199   QByteArray json = "[true,false, null, {\"foo\" : true}]";
       
   200   QVariantList list;
       
   201   list.append (QVariant(true));
       
   202   list.append (QVariant(false));
       
   203   list.append (QVariant());
       
   204   QVariantMap map;
       
   205   map.insert (QLatin1String("foo"), true);
       
   206   list.append (map);
       
   207   QVariant expected (list);
       
   208 
       
   209   Parser parser;
       
   210   bool ok;
       
   211   QVariant result = parser.parse (json, &ok);
       
   212   QVERIFY (ok);
       
   213   QCOMPARE(result, expected);
       
   214   QCOMPARE (result.toList().at(0).toBool(), true);
       
   215   QCOMPARE (result.toList().at(1).toBool(), false);
       
   216   QVERIFY (result.toList().at(2).isNull());
       
   217 }
       
   218 
       
   219 void TestParser::testEscapeChars() {
       
   220   QByteArray json = "[\"\\b \\f \\n \\r \\t \", \" \\\\ \\/ \\\\\", \"http:\\/\\/foo.com\"]";
       
   221 
       
   222   QVariantList list;
       
   223   list.append (QLatin1String("\b \f \n \r \t "));
       
   224   list.append (QLatin1String(" \\ / \\"));
       
   225   list.append (QLatin1String("http://foo.com"));
       
   226 
       
   227   QVariant expected (list);
       
   228 
       
   229   Parser parser;
       
   230   bool ok;
       
   231   QVariant result = parser.parse (json, &ok);
       
   232   QVERIFY (ok);
       
   233   QCOMPARE(result.toList().size(), expected.toList().size() );
       
   234   QCOMPARE(result, expected);
       
   235 }
       
   236 
       
   237 void TestParser::testNumbers() {
       
   238   QFETCH(QByteArray, input);
       
   239   QFETCH(QVariant, expected);
       
   240   QFETCH(QVariant::Type, type);
       
   241 
       
   242   Parser parser;
       
   243   bool ok;
       
   244   QVariant result = parser.parse ("[" + input +"]", &ok);
       
   245   QVERIFY (ok);
       
   246 
       
   247   QVariant value = result.toList().at(0);
       
   248   QCOMPARE(value, expected);
       
   249   QCOMPARE( value.type(), type);
       
   250 }
       
   251 
       
   252 void TestParser::testNumbers_data() {
       
   253   QTest::addColumn<QByteArray>( "input" );
       
   254   QTest::addColumn<QVariant>( "expected" );
       
   255   QTest::addColumn<QVariant::Type>( "type" );
       
   256 
       
   257   QByteArray input;
       
   258   QVariant output;
       
   259 
       
   260   // simple ulonglong
       
   261   input = QByteArray("1");
       
   262   output = QVariant(QVariant::ULongLong);
       
   263   output.setValue(1);
       
   264 
       
   265   QTest::newRow("simple ulonglong") << input << output << QVariant::ULongLong;
       
   266 
       
   267   // big number
       
   268   input = QByteArray("128708157440");
       
   269   output = QVariant(QVariant::ULongLong);
       
   270   output.setValue(128708157440ull);
       
   271 
       
   272   QTest::newRow("big number") << input << output << QVariant::ULongLong;
       
   273 
       
   274   // simple double
       
   275   input = QByteArray("2.4");
       
   276   output = QVariant(QVariant::Double);
       
   277   output.setValue(2.4);
       
   278 
       
   279   QTest::newRow("simple double") << input << output << QVariant::Double;
       
   280 
       
   281   // negative int
       
   282   input = QByteArray("-100");
       
   283   output = QVariant(QVariant::LongLong);
       
   284   output.setValue(-100);
       
   285 
       
   286   QTest::newRow("negative int") << input << output << QVariant::LongLong;
       
   287 
       
   288   // negative double
       
   289   input = QByteArray("-3.4");
       
   290   output = QVariant(QVariant::Double);
       
   291   output.setValue(-3.4);
       
   292 
       
   293   QTest::newRow("negative double") << input << output << QVariant::Double;
       
   294 
       
   295   // exp1
       
   296   input = QByteArray("-5e+");
       
   297   output = QVariant(QVariant::ByteArray);
       
   298   output.setValue(input);
       
   299 
       
   300   QTest::newRow("exp1") << input << output << QVariant::ByteArray;
       
   301 
       
   302   // exp2
       
   303   input = QByteArray("2e");
       
   304   output = QVariant(QVariant::ByteArray);
       
   305   output.setValue(input);
       
   306 
       
   307   QTest::newRow("exp2") << input << output << QVariant::ByteArray;
       
   308 
       
   309   // exp3
       
   310   input = QByteArray("3e+");
       
   311   output = QVariant(QVariant::ByteArray);
       
   312   output.setValue(input);
       
   313 
       
   314   QTest::newRow("exp3") << input << output << QVariant::ByteArray;
       
   315 
       
   316   // exp4
       
   317   input = QByteArray("4.3E");
       
   318   output = QVariant(QVariant::ByteArray);
       
   319   output.setValue(input);
       
   320 
       
   321   QTest::newRow("exp4") << input << output << QVariant::ByteArray;
       
   322 
       
   323   // exp5
       
   324   input = QByteArray("5.4E-");
       
   325   output = QVariant(QVariant::ByteArray);
       
   326   output.setValue(input);
       
   327 
       
   328   QTest::newRow("exp5") << input << output << QVariant::ByteArray;
       
   329 }
       
   330 
       
   331 QTEST_MAIN(TestParser)
       
   332 #include "moc_testparser.cxx"