summaryrefslogtreecommitdiff
path: root/tests/auto/qxmlname/tst_qxmlname.cpp
blob: afbbc92055903bcee3ac3bb747e813ea13100bc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#include <QtXmlPatterns/QXmlNamePool>
#include <QtXmlPatterns/QXmlName>

/*!
 \class tst_QXmlName
 \internal
 \since 4.4
 \brief Tests class QXmlName.

 This test is not intended for testing the engine, but the functionality specific
 to the QXmlName class.

 In other words, if you have an engine bug; don't add it here because it won't be
 tested properly. Instead add it to the test suite.

 */
class tst_QXmlName : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void defaultConstructor() const;
    void argumentConstructor() const;
    void argumentConstructor_data() const;
    void argumentConstructorDefaultArguments() const;
    void equalnessoperator() const;
    void inequalnessoperator() const;

    void isNull() const;
    void operatorEqual() const;
    void operatorEqual_data() const;
    void operatorNotEqual() const;
    void operatorNotEqual_data() const;
    void toClarkName() const;
    void toClarkName_data() const;
    void constCorrectness() const;
    void qHash() const;
    void objectSize() const;
    void withinQVariant() const;
    void typeWithinQVariant() const;
    void isNCName() const;
    void isNCName_data() const;
    void isNCNameSignature() const;
    void fromClarkName() const;
    void fromClarkName_data() const;
    void fromClarkNameSignature() const;
};

void tst_QXmlName::defaultConstructor() const
{
    /* Allocate instance in different orders. */
    {
        QXmlName name;
    }

    {
        QXmlName name1;
        QXmlName name2;
        QXmlName name3;
    }
}

Q_DECLARE_METATYPE(QXmlNamePool)
void tst_QXmlName::argumentConstructor() const
{
    QFETCH(QString, namespaceURI);
    QFETCH(QString, localName);
    QFETCH(QString, prefix);
    QFETCH(QXmlNamePool, namePool);

    const QXmlName name(namePool, localName, namespaceURI, prefix);

    QCOMPARE(name.namespaceUri(namePool), namespaceURI);
    QCOMPARE(name.localName(namePool), localName);
    QCOMPARE(name.prefix(namePool), prefix);
}

/*!
  \internal

 Below we use the same QXmlNamePool instance. This means the same name pool
 is used.
 */
void tst_QXmlName::argumentConstructor_data() const
{
    QTest::addColumn<QString>("namespaceURI");
    QTest::addColumn<QString>("localName");
    QTest::addColumn<QString>("prefix");
    QTest::addColumn<QXmlNamePool>("namePool");

    QXmlNamePool namePool;
    QTest::newRow("Basic test")
                    << QString::fromLatin1("http://example.com/Namespace1")
                    << QString::fromLatin1("localName1")
                    << QString::fromLatin1("prefix1")
                    << namePool;

    QTest::newRow("Same namespace & prefix as before, different local name.")
                    << QString::fromLatin1("http://example.com/Namespace1")
                    << QString::fromLatin1("localName2")
                    << QString::fromLatin1("prefix1")
                    << namePool;

    QTest::newRow("Same namespace & local name as before, different prefix.")
                    << QString::fromLatin1("http://example.com/Namespace1")
                    << QString::fromLatin1("localName2")
                    << QString::fromLatin1("prefix2")
                    << namePool;

    QTest::newRow("No prefix")
                    << QString::fromLatin1("http://example.com/Namespace2")
                    << QString::fromLatin1("localName3")
                    << QString()
                    << namePool;
}

/*!
 Ensure that the three last arguments have default values, and that they are null strings.
 */
void tst_QXmlName::argumentConstructorDefaultArguments() const
{
    QXmlNamePool np;
    const QXmlName n1(np, QLatin1String("localName"));
    const QXmlName n2(np, QLatin1String("localName"), QString(), QString());

    QCOMPARE(n1, n2);
    QCOMPARE(n1.toClarkName(np), QString::fromLatin1("localName"));
}

void tst_QXmlName::equalnessoperator() const
{
    const QXmlName o1;
    const QXmlName o2;
    o1 == o2;
    // TODO
}

void tst_QXmlName::inequalnessoperator() const
{
    const QXmlName o1;
    const QXmlName o2;
    o1 != o2;
    // TODO
}

void tst_QXmlName::isNull() const
{
    /* Check default value. */
    QXmlName name;
    QVERIFY(name.isNull());
}

void tst_QXmlName::operatorEqual() const
{
    QFETCH(QXmlName, op1);
    QFETCH(QXmlName, op2);
    QFETCH(bool, expected);

    QCOMPARE(op1 == op2, expected);
}

void tst_QXmlName::operatorEqual_data() const
{
    QTest::addColumn<QXmlName>("op1");
    QTest::addColumn<QXmlName>("op2");
    QTest::addColumn<bool>("expected");

    QXmlNamePool namePool;
    const QXmlName n1(namePool, QString::fromLatin1("localName1"),
                                QString::fromLatin1("http://example.com/Namespace1"),
                                QString::fromLatin1("prefix1"));

    const QXmlName n2(namePool, QString::fromLatin1("localName2"),
                                QString::fromLatin1("http://example.com/Namespace1"),
                                QString::fromLatin1("prefix1"));

    const QXmlName n3(namePool, QString::fromLatin1("localName2"),
                                QString::fromLatin1("http://example.com/Namespace1"),
                                QString::fromLatin1("prefix2"));

    const QXmlName n4(namePool, QString::fromLatin1("localName3"),
                                QString::fromLatin1("http://example.com/Namespace2"));

    const QXmlName n5(namePool, QString::fromLatin1("localName4"),
                                QString::fromLatin1("http://example.com/Namespace2"));

    const QXmlName n6(namePool, QString::fromLatin1("localName4"),
                                QString::fromLatin1("http://example.com/Namespace2"),
                                QString::fromLatin1("prefix3"));

    const QXmlName n7(namePool, QString::fromLatin1("localName2"),
                                QString::fromLatin1("http://example.com/Namespace2"),
                                QString::fromLatin1("prefix3"));

    QTest::newRow(qPrintable(n1.toClarkName(namePool)))
        << n1
        << n1
        << true;

    QTest::newRow(qPrintable(n2.toClarkName(namePool)))
        << n2
        << n2
        << true;

    QTest::newRow(qPrintable(n3.toClarkName(namePool)))
        << n3
        << n3
        << true;

    QTest::newRow(qPrintable(n4.toClarkName(namePool)))
        << n4
        << n4
        << true;

    QTest::newRow(qPrintable(n5.toClarkName(namePool)))
        << n5
        << n5
        << true;

    QTest::newRow(qPrintable(n6.toClarkName(namePool)))
        << n6
        << n6
        << true;

    QTest::newRow(qPrintable(n7.toClarkName(namePool)))
        << n7
        << n7
        << true;

    QTest::newRow("Prefix differs")
        << n2
        << n3
        << true;

    QTest::newRow("No prefix vs. prefix")
        << n5
        << n6
        << true;

    QTest::newRow("Local name differs")
        << n1
        << n2
        << false;

    QTest::newRow("Namespace differs")
        << n2
        << n7
        << false;
}

void tst_QXmlName::operatorNotEqual() const
{
    QFETCH(QXmlName, op1);
    QFETCH(QXmlName, op2);
    QFETCH(bool, expected);

    QCOMPARE(op1 != op2, !expected);
}

void tst_QXmlName::operatorNotEqual_data() const
{
    operatorEqual_data();
}

/*!
  Check that functions have the correct const qualification.
 */
void tst_QXmlName::constCorrectness() const
{
    const QXmlName name;

    /* isNull() */
    QVERIFY(name.isNull());

    /* operator==() */
    QVERIFY(name == name);

    /* operator!=() */
    QVERIFY(!(name != name));

    QXmlNamePool namePool;
    const QXmlName name2(namePool, QLatin1String("localName"), QLatin1String("http://example.com/"), QLatin1String("prefix"));

    /* namespaceUri(). */
    QCOMPARE(name2.namespaceUri(namePool), QLatin1String("http://example.com/"));

    /* localName(). */
    QCOMPARE(name2.localName(namePool), QLatin1String("localName"));

    /* prefix(). */
    QCOMPARE(name2.prefix(namePool), QLatin1String("prefix"));

    /* toClarkname(). */
    QCOMPARE(name2.toClarkName(namePool), QLatin1String("{http://example.com/}prefix:localName"));
}

void tst_QXmlName::qHash() const
{
    /* Just call it, so we know it exist and that we don't trigger undefined
     * behavior. We can't test the return value, since it's opaque. */
    QXmlName name;
    ::qHash(name);
}

void tst_QXmlName::objectSize() const
{
    QVERIFY2(sizeof(QXmlName) == sizeof(qint64), "QXmlName should have at least a d-pointer.");
}

void tst_QXmlName::toClarkName() const
{
    QFETCH(QString, produced);
    QFETCH(QString, expected);

    QCOMPARE(produced, expected);
}

void tst_QXmlName::toClarkName_data() const
{
    QTest::addColumn<QString>("produced");
    QTest::addColumn<QString>("expected");

    QXmlNamePool np;

    /* A null QXmlName. */
    {
        const QXmlName n;
        QTest::newRow("") << n.toClarkName(np)
                          << QString::fromLatin1("QXmlName(null)");
    }

    {
        const QXmlName n(np, QLatin1String("localName"));
        QTest::newRow("") << n.toClarkName(np)
                          << QString::fromLatin1("localName");
    }

    /* Local name with namespace URI, empty prefix. */
    {
        const QXmlName n(np, QLatin1String("localName"),
                             QLatin1String("http://example.com/"));
        QTest::newRow("") << n.toClarkName(np)
                          << QString::fromLatin1("{http://example.com/}localName");
    }

    /* Local name with namespace URI and prefix. */
    {
        const QXmlName n(np, QLatin1String("localName"),
                             QLatin1String("http://example.com/"),
                             QLatin1String("p"));
        QTest::newRow("") << n.toClarkName(np)
                          << QString::fromLatin1("{http://example.com/}p:localName");
    }
}

/*!
  Check that QXmlName can be used inside QVariant.
 */
void tst_QXmlName::withinQVariant() const
{
    /* The extra paranthesis silences a warning on win32-msvc2005. */
    QVariant value(qVariantFromValue(QXmlName()));
}

/*!
 Check that the user type of QXmlName holds.
 */
void tst_QXmlName::typeWithinQVariant() const
{
    const int qxmlNameType = QVariant(qVariantFromValue(QXmlName())).userType();

    const QVariant value(qVariantFromValue(QXmlName()));

    QCOMPARE(value.userType(), qxmlNameType);
}

/*!
  We don't do full testing here. Don't have the resources for it. We simply assume
  we use a code path which is fully tested elsewhere.
 */
void tst_QXmlName::isNCName() const
{
    QFETCH(QString, input);
    QFETCH(bool, expectedValidity);

    QCOMPARE(QXmlName::isNCName(input), expectedValidity);
}

void tst_QXmlName::isNCName_data() const
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<bool>("expectedValidity");

    QTest::newRow("empty string")
        << QString()
        << false;

    QTest::newRow("A number")
        << QString::fromLatin1("1")
        << false;

    QTest::newRow("Simple valid string")
        << QString::fromLatin1("abc")
        << true;

    QTest::newRow("Simple valid string")
        << QString::fromLatin1("abc.123")
        << true;
}

void tst_QXmlName::isNCNameSignature() const
{
    const QString constQString;

    /* Verify that we can take a const QString. */
    QXmlName::isNCName(constQString);

    /* Verify that we can take a temporary QString. */
    QXmlName::isNCName(QString());
}

void tst_QXmlName::fromClarkName() const
{
    QFETCH(QString,         input);
    QFETCH(QXmlName,        expected);
    QFETCH(QXmlNamePool,    namePool);

    QCOMPARE(QXmlName::fromClarkName(input, namePool), expected);
}

void tst_QXmlName::fromClarkName_data() const
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<QXmlName>("expected");
    QTest::addColumn<QXmlNamePool>("namePool");

    QXmlNamePool np;

    QTest::newRow("A null string")
        << QString()
        << QXmlName()
        << np;

    QTest::newRow("An empty string")
        << QString(QLatin1String(""))
        << QXmlName()
        << np;

    QTest::newRow("A single local name")
        << QString(QLatin1String("foo"))
        << QXmlName(np, QLatin1String("foo"))
        << np;

    QTest::newRow("Has prefix, but no namespace, that's invalid")
        << QString(QLatin1String("prefix:foo"))
        << QXmlName()
        << np;

    QTest::newRow("Namespace, local name, no prefix")
        << QString(QLatin1String("{def}abc"))
        << QXmlName(np, QLatin1String("abc"), QLatin1String("def"))
        << np;

    QTest::newRow("Namespace, local name, prefix")
        << QString(QLatin1String("{def}p:abc"))
        << QXmlName(np, QLatin1String("abc"), QLatin1String("def"), QLatin1String("p"))
        << np;

    QTest::newRow("Namespace, local name, prefix syntax error")
        << QString(QLatin1String("{def}:abc"))
        << QXmlName()
        << np;

    QTest::newRow("Namespace, local name syntax error, prefix")
        << QString(QLatin1String("{def}p:"))
        << QXmlName()
        << np;

    QTest::newRow("Only local name which is invalid")
        << QString(QLatin1String(":::"))
        << QXmlName()
        << np;

    QTest::newRow("Namespace, invalid local name")
        << QString(QLatin1String("{def}a|bc"))
        << QXmlName()
        << np;

    QTest::newRow("Namespace, local name, invalid prefix")
        << QString(QLatin1String("{def}a|b:c"))
        << QXmlName()
        << np;

    QTest::newRow("A single left curly, invalid")
        << QString(QLatin1String("{"))
        << QXmlName()
        << np;

    QTest::newRow("A single left curly, invalid")
        << QString(QLatin1String("{aaswd"))
        << QXmlName()
        << np;
}

void tst_QXmlName::fromClarkNameSignature() const
{
    /* We should take const references. */
    const QXmlNamePool np;
    const QString in;

    QXmlName::fromClarkName(in, np);
}

QTEST_MAIN(tst_QXmlName)

#include "tst_qxmlname.moc"

// vim: et:ts=4:sw=4:sts=4