From 1f937b007859181bb464201776c6c03cfde541d6 Mon Sep 17 00:00:00 2001 From: Andrew Christian Date: Tue, 3 Apr 2012 20:46:50 -0400 Subject: Added JsonBuffer tests Change-Id: Ic3db66cf035aec662272f01954bd0494a6719828 Reviewed-by: Alexei Rousskikh --- src/jsonbuffer_p.h | 2 +- tests/auto/auto.pro | 2 +- tests/auto/jsonbuffer/.gitignore | 1 + tests/auto/jsonbuffer/jsonbuffer.pro | 7 ++ tests/auto/jsonbuffer/tst_jsonbuffer.cpp | 121 +++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 tests/auto/jsonbuffer/.gitignore create mode 100644 tests/auto/jsonbuffer/jsonbuffer.pro create mode 100644 tests/auto/jsonbuffer/tst_jsonbuffer.cpp diff --git a/src/jsonbuffer_p.h b/src/jsonbuffer_p.h index 8069234..cae0009 100644 --- a/src/jsonbuffer_p.h +++ b/src/jsonbuffer_p.h @@ -50,7 +50,7 @@ QT_BEGIN_NAMESPACE_JSONSTREAM -class JsonBuffer : public QObject +class Q_ADDON_JSONSTREAM_EXPORT JsonBuffer : public QObject { Q_OBJECT public: diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 430c8f3..a8778ca 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS = jsonstream jsonschema +SUBDIRS = jsonstream jsonschema jsonbuffer diff --git a/tests/auto/jsonbuffer/.gitignore b/tests/auto/jsonbuffer/.gitignore new file mode 100644 index 0000000..793a6ec --- /dev/null +++ b/tests/auto/jsonbuffer/.gitignore @@ -0,0 +1 @@ +tst_jsonbuffer diff --git a/tests/auto/jsonbuffer/jsonbuffer.pro b/tests/auto/jsonbuffer/jsonbuffer.pro new file mode 100644 index 0000000..ee5118c --- /dev/null +++ b/tests/auto/jsonbuffer/jsonbuffer.pro @@ -0,0 +1,7 @@ +CONFIG += testcase +CONFIG -= app_bundle + +QT = jsonstream testlib jsonstream-private + +SOURCES = tst_jsonbuffer.cpp +TARGET = tst_jsonbuffer diff --git a/tests/auto/jsonbuffer/tst_jsonbuffer.cpp b/tests/auto/jsonbuffer/tst_jsonbuffer.cpp new file mode 100644 index 0000000..635d184 --- /dev/null +++ b/tests/auto/jsonbuffer/tst_jsonbuffer.cpp @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "private/jsonbuffer_p.h" + +QT_USE_NAMESPACE_JSONSTREAM + +class tst_JsonBuffer : public QObject +{ + Q_OBJECT + +private slots: + void utf8(); + void utf8extend(); +}; + + +const char *utf8spaces[] = { "{\"a\":123.0}{\"b\":234}", + "{\"a\": 123} \n{\"b\" :234 } ", + "{ \"a\" : 123.0000} \n { \"b\" :234 } ", + "\n {\"a\": 123} \n{\"b\" :234 } ", + "{\"a\":123.0} {\"b\":234} {\"c\":345 ", + "\xef\xbb\xbf{\"a\":123.0} {\"b\":234}" +}; + +void tst_JsonBuffer::utf8() +{ + int n = sizeof(utf8spaces) / sizeof(utf8spaces[0]); + for (int i = 0 ; i < n ; i++ ) { + JsonBuffer buf; + buf.append(utf8spaces[i], strlen(utf8spaces[i])); + + QVERIFY(buf.messageAvailable()); + QJsonObject a = buf.readMessage(); + QCOMPARE(a.value("a").toDouble(), 123.0); + + QVERIFY(buf.messageAvailable()); + QJsonObject b = buf.readMessage(); + QCOMPARE(b.value("b").toDouble(), 234.0); + + QVERIFY(!buf.messageAvailable()); + QVERIFY(buf.format() == FormatUTF8); + } +} + +struct PartialData { + const char *packet; + const char *var; + double value; +}; + +PartialData utf8packets[] = { { "{\"a\":123.0", 0, 0 }, + { " }", "a", 123.0 }, + { " \n{\"b\" :234 } ", "b", 234.0 }, + { " \n{\"c\" : 1000.0 } ", "c", 1000.0 }, + { "{\"value\": ", 0, 0 }, + { " 1.0", 0, 0 }, + { " \t\t\t}", "value", 1.0 } +}; + +void tst_JsonBuffer::utf8extend() +{ + JsonBuffer buf; + int n = sizeof(utf8packets) / sizeof(utf8packets[0]); + for (int i = 0 ; i < n ; i++ ) { + PartialData& d = utf8packets[i]; + buf.append(d.packet, strlen(d.packet)); + + QCOMPARE(buf.messageAvailable(), d.var != 0); + if (d.var) { + QJsonObject a = buf.readMessage(); + QCOMPARE(a.value(d.var).toDouble(), d.value); + } + QVERIFY(!buf.messageAvailable()); + QVERIFY(buf.format() == FormatUTF8); + } +} + +QTEST_MAIN(tst_JsonBuffer) + +#include "tst_jsonbuffer.moc" -- cgit v1.2.3