~ubuntu-branches/ubuntu/vivid/kdesdk/vivid

« back to all changes in this revision

Viewing changes to okteta/kasten/controllers/test/scriptclassestest.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-06 11:49:54 UTC
  • mfrom: (0.4.21)
  • Revision ID: package-import@ubuntu.com-20120606114954-rdls73fzlpzxglbx
Tags: 4:4.8.80-0ubuntu1
* New uptream beta release
* Update dont_export_private_classes.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This file is part of the Okteta Kasten module, made within the KDE community.
 
3
 *
 
4
 *    Copyright 2012  Alex Richardson <alex.richardson@gmx.de>
 
5
 *
 
6
 *    This library is free software; you can redistribute it and/or
 
7
 *    modify it under the terms of the GNU Lesser General Public
 
8
 *    License as published by the Free Software Foundation; either
 
9
 *    version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 *    This library is distributed in the hope that it will be useful,
 
12
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 *    Lesser General Public License for more details.
 
15
 *
 
16
 *    You should have received a copy of the GNU Lesser General Public
 
17
 *    License along with this library; if not, write to the Free Software
 
18
 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include <QtTest>
 
22
#include <qtest_kde.h>
 
23
 
 
24
#include <QObject>
 
25
#include <QScriptValueIterator>
 
26
#include <QScriptEngine>
 
27
 
 
28
#include "view/structures/datatypes/primitive/primitivetemplateinfo.h"
 
29
#include "view/structures/datatypes/primitive/primitivedatainformation.h"
 
30
#include "view/structures/datatypes/primitive/bitfield/signedbitfielddatainformation.h"
 
31
#include "view/structures/datatypes/primitive/bitfield/unsignedbitfielddatainformation.h"
 
32
#include "view/structures/datatypes/primitive/bitfield/boolbitfielddatainformation.h"
 
33
#include "view/structures/datatypes/primitive/enumdatainformation.h"
 
34
#include "view/structures/datatypes/primitive/flagdatainformation.h"
 
35
#include "view/structures/datatypes/primitive/bitfield/boolbitfielddatainformation.h"
 
36
#include "view/structures/datatypes/primitive/bitfield/unsignedbitfielddatainformation.h"
 
37
#include "view/structures/datatypes/primitive/bitfield/signedbitfielddatainformation.h"
 
38
#include "view/structures/datatypes/primitivefactory.h"
 
39
#include "view/structures/datatypes/topleveldatainformation.h"
 
40
#include "view/structures/datatypes/strings/stringdatainformation.h"
 
41
#include "view/structures/datatypes/array/arraydatainformation.h"
 
42
#include "view/structures/datatypes/structuredatainformation.h"
 
43
#include "view/structures/datatypes/uniondatainformation.h"
 
44
#include "view/structures/script/scripthandler.h"
 
45
#include "view/structures/script/scriptengineinitializer.h"
 
46
 
 
47
class ScriptClassesTest : public QObject
 
48
{
 
49
Q_OBJECT
 
50
 
 
51
    typedef QPair<QString, QScriptValue::PropertyFlags> PropertyPair;
 
52
 
 
53
private:
 
54
    static void checkProperties(const QVector<PropertyPair>& expected, DataInformation* data,
 
55
            const char* tag);
 
56
    static PropertyPair pair(const char* name,
 
57
            QScriptValue::PropertyFlags flags = QScriptValue::Undeletable | QScriptValue::ReadOnly)
 
58
    {
 
59
        return PropertyPair(QLatin1String(name), flags);
 
60
    }
 
61
private Q_SLOTS:
 
62
    void initTestCase();
 
63
    //check that all properties are available in the iterator
 
64
    void checkIterators();
 
65
    void cleanupTestCase();
 
66
 
 
67
private:
 
68
    QVector<PropertyPair> commonProperties;
 
69
    QVector<PropertyPair> primitiveProperties;
 
70
    QVector<TopLevelDataInformation*> primitives;
 
71
 
 
72
    QVector<PropertyPair> enumProperties;
 
73
    EnumDataInformation* enumData;
 
74
    QScopedPointer<TopLevelDataInformation> enumDataTop;
 
75
    FlagDataInformation* flagData;
 
76
    QScopedPointer<TopLevelDataInformation> flagDataTop;
 
77
 
 
78
    QVector<PropertyPair> bitfieldProperties;
 
79
    SignedBitfieldDataInformation* signedBitfield;
 
80
    QScopedPointer<TopLevelDataInformation> signedBitfieldTop;
 
81
    UnsignedBitfieldDataInformation* unsignedBitfield;
 
82
    QScopedPointer<TopLevelDataInformation> unsignedBitfieldTop;
 
83
    BoolBitfieldDataInformation* boolBitfield;
 
84
    QScopedPointer<TopLevelDataInformation> boolBitfieldTop;
 
85
 
 
86
    QVector<PropertyPair> structUnionProperties; //without children
 
87
    StructureDataInformation* structData;
 
88
    QScopedPointer<TopLevelDataInformation> structDataTop;
 
89
    UnionDataInformation* unionData;
 
90
    QScopedPointer<TopLevelDataInformation> unionDataTop;
 
91
 
 
92
    QVector<PropertyPair> arrayProperties; //without children
 
93
    ArrayDataInformation* arrayData;
 
94
    QScopedPointer<TopLevelDataInformation> arrayDataTop;
 
95
 
 
96
    QVector<PropertyPair> stringProperties; //without children
 
97
    StringDataInformation* stringData;
 
98
    QScopedPointer<TopLevelDataInformation> stringDataTop;
 
99
 
 
100
};
 
101
 
 
102
void ScriptClassesTest::initTestCase()
 
103
{
 
104
    //TODO allow changing name -> no longer read only?
 
105
    commonProperties << pair("name") << pair("wasAbleToRead") << pair("parent")
 
106
            << pair("valid", QScriptValue::Undeletable)
 
107
            << pair("validationError", QScriptValue::Undeletable)
 
108
            << pair("byteOrder", QScriptValue::Undeletable);
 
109
 
 
110
    //TODO add these to common
 
111
    //commonProperties << pair("updateFunc") << pair("validationFunc");
 
112
    //commonProperties << pair("type");
 
113
 
 
114
    primitiveProperties << commonProperties << pair("value") << pair("char") << pair("int")
 
115
            << pair("int8") << pair("int16") << pair("int32") << pair("int64") << pair("uint")
 
116
            << pair("uint8") << pair("uint16") << pair("uint32") << pair("uint64") << pair("bool")
 
117
            << pair("float") << pair("double") << pair("int64high32") << pair("int64low32")
 
118
            << pair("uint64high32") << pair("uint64low32") << pair("type");
 
119
    qSort(primitiveProperties);
 
120
    for (int i = Type_START; i < Type_Bitfield; ++i)
 
121
    {
 
122
        PrimitiveDataInformation* prim = PrimitiveFactory::newInstance(
 
123
                QLatin1String("prim"), static_cast<PrimitiveDataTypeEnum>(i));
 
124
        prim->setValue(10);
 
125
        primitives.append(
 
126
                new TopLevelDataInformation(prim, 0, ScriptEngineInitializer::newEngine()));
 
127
    }
 
128
 
 
129
    enumProperties << primitiveProperties << pair("enumValues", QScriptValue::Undeletable);
 
130
    //TODO valueString property (i.e. the current value as enumerator name)
 
131
    qSort(enumProperties);
 
132
 
 
133
    QMap<AllPrimitiveTypes, QString> enumValues;
 
134
    enumValues.insert(1, QLatin1String("one"));
 
135
    enumValues.insert(2, QLatin1String("tow"));
 
136
    enumValues.insert(4, QLatin1String("four"));
 
137
    EnumDefinition::Ptr enumDef(new EnumDefinition(enumValues,
 
138
            QLatin1String("theEnum"), Type_Int32));
 
139
    enumData = new EnumDataInformation(QLatin1String("enumData"),
 
140
            PrimitiveFactory::newInstance(QLatin1String("dummy"), Type_Int32), enumDef);
 
141
    enumDataTop.reset(
 
142
            new TopLevelDataInformation(enumData, 0, ScriptEngineInitializer::newEngine()));
 
143
    flagData = new FlagDataInformation(QLatin1String("flagData"),
 
144
            PrimitiveFactory::newInstance(QLatin1String("dummy"), Type_Int32), enumDef);
 
145
    flagDataTop.reset(
 
146
            new TopLevelDataInformation(flagData, 0, ScriptEngineInitializer::newEngine()));
 
147
 
 
148
    bitfieldProperties << primitiveProperties << pair("width", QScriptValue::Undeletable);
 
149
    qSort(bitfieldProperties);
 
150
    unsignedBitfield = new UnsignedBitfieldDataInformation(QLatin1String("unsignedBit"), 42);
 
151
    unsignedBitfieldTop.reset(
 
152
            new TopLevelDataInformation(unsignedBitfield, 0, ScriptEngineInitializer::newEngine()));
 
153
    signedBitfield = new SignedBitfieldDataInformation(QLatin1String("signedBit"), 42);
 
154
    signedBitfieldTop.reset(
 
155
            new TopLevelDataInformation(signedBitfield, 0, ScriptEngineInitializer::newEngine()));
 
156
    boolBitfield = new BoolBitfieldDataInformation(QLatin1String("boolBit"), 42);
 
157
    boolBitfieldTop.reset(
 
158
            new TopLevelDataInformation(boolBitfield, 0, ScriptEngineInitializer::newEngine()));
 
159
 
 
160
    stringProperties << commonProperties << pair("terminatedBy", QScriptValue::Undeletable)
 
161
            << pair("byteCount") << pair("maxCharCount", QScriptValue::Undeletable)
 
162
            << pair("charCount") << pair("encoding", QScriptValue::Undeletable)
 
163
            << pair("maxByteCount", QScriptValue::Undeletable);
 
164
    qSort(stringProperties);
 
165
    stringData = new StringDataInformation(QLatin1String("string"), StringDataInformation::Latin1);
 
166
    stringDataTop.reset(
 
167
            new TopLevelDataInformation(stringData, 0, ScriptEngineInitializer::newEngine()));
 
168
 
 
169
    arrayProperties << commonProperties << pair("length", QScriptValue::Undeletable)
 
170
            << pair("childType", QScriptValue::Undeletable);
 
171
    qSort(arrayProperties);
 
172
    arrayData = new ArrayDataInformation(QLatin1String("array"), 20,
 
173
            PrimitiveFactory::newInstance(QLatin1String("inner"), Type_Int32));
 
174
    arrayDataTop.reset(
 
175
            new TopLevelDataInformation(arrayData, 0, ScriptEngineInitializer::newEngine()));
 
176
 
 
177
    structUnionProperties << commonProperties << pair("childCount");
 
178
    //property children is only writable -> it is not in the iterator
 
179
    structData = new StructureDataInformation(QLatin1String("struct"));
 
180
    structDataTop.reset(
 
181
            new TopLevelDataInformation(structData, 0, ScriptEngineInitializer::newEngine()));
 
182
    unionData = new UnionDataInformation(QLatin1String("union"));
 
183
    unionDataTop.reset(
 
184
            new TopLevelDataInformation(unionData, 0, ScriptEngineInitializer::newEngine()));
 
185
    qSort(structUnionProperties);
 
186
 
 
187
}
 
188
 
 
189
void ScriptClassesTest::checkProperties(const QVector<PropertyPair>& expected,
 
190
        DataInformation* data, const char* tag)
 
191
{
 
192
    QScriptValue value = data->toScriptValue(data->topLevelDataInformation()->scriptEngine(),
 
193
            data->topLevelDataInformation()->scriptHandler()->handlerInfo());
 
194
 
 
195
    QScriptValueIterator it(value);
 
196
    QList<PropertyPair> foundProperties;
 
197
    while (it.hasNext())
 
198
    {
 
199
        it.next();
 
200
        foundProperties.append(qMakePair(it.name(), it.flags()));
 
201
    }
 
202
    qSort(foundProperties);
 
203
    if (foundProperties.size() != expected.size())
 
204
        qWarning() << tag << ": size differs: expected"
 
205
                << expected.size() << ", got" << foundProperties.size();
 
206
    QCOMPARE(foundProperties.size(), expected.size());
 
207
    for (int i = 0; i < foundProperties.size(); ++i)
 
208
    {
 
209
        if (foundProperties.at(i) != expected.at(i))
 
210
            qWarning() << tag << ":" << foundProperties.at(i) << "!=" << expected.at(i);
 
211
        QCOMPARE(foundProperties.at(i).first, expected.at(i).first);
 
212
        QCOMPARE(foundProperties.at(i).second, expected.at(i).second);
 
213
    }
 
214
}
 
215
 
 
216
void ScriptClassesTest::checkIterators()
 
217
{
 
218
    for (int i = 0; i < primitives.size(); ++i)
 
219
    {
 
220
        TopLevelDataInformation* top = primitives.at(i);
 
221
        checkProperties(primitiveProperties, top->actualDataInformation(), "primitives");
 
222
    }
 
223
    checkProperties(enumProperties, enumData, "enum");
 
224
    checkProperties(enumProperties, flagData, "flag");
 
225
 
 
226
    checkProperties(bitfieldProperties, boolBitfield, "bool bitfield");
 
227
    checkProperties(bitfieldProperties, signedBitfield, "signed bitfield");
 
228
    checkProperties(bitfieldProperties, unsignedBitfield, "unsignedBitfield");
 
229
 
 
230
    checkProperties(stringProperties, stringData, "string");
 
231
 
 
232
    checkProperties(arrayProperties, arrayData, "array");
 
233
 
 
234
    checkProperties(structUnionProperties, structData, "struct");
 
235
    checkProperties(structUnionProperties, unionData, "union");
 
236
}
 
237
 
 
238
void ScriptClassesTest::cleanupTestCase()
 
239
{
 
240
    qDeleteAll(primitives);
 
241
}
 
242
 
 
243
QTEST_MAIN(ScriptClassesTest)
 
244
 
 
245
#include "scriptclassestest.moc"