Torc  0.1
testserialisers.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QtTest/QtTest>
3 #include <QJsonDocument>
4 
5 // Torc
6 #include "torcjsonserialiser.h"
7 #include "torcxmlreader.h"
8 #include "testserialisers.h"
9 
10 void TestSerialisers::testJSONSerialiser(void)
11 {
12  QByteArray array("[\"number\",\"name\"]");
13  QJsonDocument doc = QJsonDocument::fromJson(array);
14  QVariant map = doc.toVariant();
15  TorcSerialiser* jsonserialiser = TorcSerialiser::GetSerialiser("application/json");
16  QVERIFY(jsonserialiser);
17  QByteArray data;
18  jsonserialiser->Serialise(data, map);
19  delete jsonserialiser;
20  QJsonDocument doc2 = QJsonDocument::fromJson(data);
21  QByteArray array2 = doc2.toJson(QJsonDocument::Compact).trimmed();
22 
23  QVERIFY(array.size() == array2.size());
24  QVERIFY(qstrcmp(array.constData(), array2.constData()) == 0);
25  QVERIFY(doc2 == doc);
26  QVERIFY(array2 == array);
27 }
28 
29 void TestSerialisers::doTestXMLSerialiser(QByteArray &Data)
30 {
31  TorcXMLReader reader(Data);
32  QString error;
33  QVERIFY(reader.IsValid(error));
34  QVariantMap map = reader.GetResult();
35  TorcSerialiser *xmlserialiser = TorcSerialiser::GetSerialiser("application/xml");
36  QVERIFY(xmlserialiser);
37  QByteArray array2;
38  xmlserialiser->Serialise(array2, map);
39  delete xmlserialiser;
40  QVERIFY(Data.size() == array2.size());
41 }
42 
43 void TestSerialisers::testXMLSerialiser(void)
44 {
45  QMap<QString,QByteArray> tests;
46  tests.insert("Simple1", "<list><value>name</value><value>number</value></list>");
47  tests.insert("Simple2", "<people><person><name>Jon</name><age>12</age></person></people>");
48  tests.insert("Simple3", "<list><value>1</value><value>2</value><value>3</value></list>");
49  tests.insert("Simple4", "<person>me</person><person>you</person>");
50 
51  QMap<QString,QByteArray>::iterator it = tests.begin();
52  for ( ; it != tests.end(); ++it)
53  {
54  QByteArray t = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + it.value() + "\n";
55  doTestXMLSerialiser(t);
56  }
57 }
void Serialise(QByteArray &Dest, const QVariant &Data, const QString &Type=QString())
static TorcSerialiser * GetSerialiser(const QString &MimeType)