-
Notifications
You must be signed in to change notification settings - Fork 50
/
testgenerictypes.cpp
840 lines (678 loc) · 23.4 KB
/
testgenerictypes.cpp
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
This file is part of the Grantlee template system.
Copyright (c) 2010 Michael Jansen <kde@michael-jansen.biz>
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either version
2.1 of the Licence, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "metatype.h"
#include "engine.h"
#include "grantlee_paths.h"
#include "template.h"
#include "test_macros.h"
#include <QtTest/QTest>
#include <QtCore/QVariant>
#include <QtCore/QStack>
#include <QtCore/QQueue>
#include <QtCore/QVariantHash>
#include <QtCore/QMetaType>
#include <QtCore/QLinkedList>
#include <string>
#include <deque>
#include "coverageobject.h"
#ifndef GRANTLEE_NO_TR1
#ifdef Q_CC_MSVC
#include <memory>
#else
#include <tr1/memory>
#endif
Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(ThreeArray)
Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(QtUnorderedMap)
Q_DECLARE_SMART_POINTER_METATYPE(std::tr1::shared_ptr)
#endif
Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(std::deque)
class TestGenericTypes : public CoverageObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testGenericClassType();
void testSequentialContainer_Variant();
void testAssociativeContainer_Variant();
void testSequentialContainer_Type();
void testAssociativeContainer_Type();
void testSharedPointer();
void testThirdPartySharedPointer();
void testNestedContainers();
void testCustomQObjectDerived();
void testUnregistered();
void testPointerNonQObject();
}; // class TestGenericTypes
class Person
{
public:
Person() :age(0) {}
Person(std::string _name, int _age)
: name(_name), age(_age)
{
static int _uid = 0;
uid = ++_uid;
}
bool operator==(const Person &other) const
{
return uid == other.uid;
}
std::string name;
int age;
int uid;
};
int qHash( const Person &p )
{
return p.uid;
}
Q_DECLARE_METATYPE(Person)
GRANTLEE_BEGIN_LOOKUP(Person)
if ( property == QStringLiteral( "name" ) )
return QString::fromStdString( object.name );
else if ( property == QStringLiteral( "age" ) )
return object.age;
GRANTLEE_END_LOOKUP
class PersonObject : public QObject
{
Q_OBJECT
Q_PROPERTY( QString name READ name )
Q_PROPERTY( int age READ age )
public:
PersonObject(const QString &name, int age, QObject* parent = 0)
: QObject(parent), m_name(name), m_age(age)
{
}
QString name() const { return m_name; }
int age() const { return m_age; }
private:
const QString m_name;
const int m_age; // Yeah, you wish...
};
void TestGenericTypes::initTestCase()
{
// Register the handler for our custom type
Grantlee::registerMetaType<Person>();
}
void TestGenericTypes::testGenericClassType()
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "Person: \nName: {{p.name}}\nAge: {{p.age}}\nUnknown: {{p.unknown}}" ),
QStringLiteral( "template1" ) );
// Check it
QVariantHash h;
Person p( "Grant Lee", 2 );
h.insert( QStringLiteral( "p" ), QVariant::fromValue( p ) );
Grantlee::Context c( h );
QCOMPARE(
t1->render( &c ),
QStringLiteral( "Person: \nName: Grant Lee\nAge: 2\nUnknown: " ));
}
static QMap<int, Person> getPeople()
{
QMap<int, Person> people;
people.insert( 23, Person( "Claire", 23 ) );
people.insert( 32, Person( "Grant", 32 ) );
people.insert( 50, Person( "Alan", 50 ) );
return people;
}
template<typename SequentialContainer>
void insertPeopleVariants( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
const QMap<int, Person>::const_iterator end = people.constEnd();
SequentialContainer container;
for ( ; it != end; ++it )
container.push_back( QVariant::fromValue( it.value() ) );
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
template<typename AssociativeContainer>
void insertAssociatedPeopleVariants( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
const QMap<int, Person>::const_iterator end = people.constEnd();
AssociativeContainer container;
for ( ; it != end; ++it )
container.insert( QString::number( it.key() ), QVariant::fromValue( it.value() ) );
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
template<>
void insertPeopleVariants<QMap<QString, QVariant> >( Grantlee::Context &c )
{
insertAssociatedPeopleVariants<QMap<QString, QVariant> >( c );
}
template<>
void insertPeopleVariants<QHash<QString, QVariant> >( Grantlee::Context &c )
{
insertAssociatedPeopleVariants<QHash<QString, QVariant> >( c );
}
template<typename Container>
void testSequentialIteration( Grantlee::Context c )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{% for person in people %}{{ person.name }},{% endfor %}" ),
QStringLiteral( "people_template" ) );
QCOMPARE(
t1->render( &c ),
QStringLiteral( "Claire,Grant,Alan," ) );
}
}
template<typename Container>
void testSequentialIndexing( Grantlee::Context c )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{{ people.0.name }},{{ people.1.name }},{{ people.2.name }}," ),
QStringLiteral( "people_template" ) );
QCOMPARE(
t1->render( &c ),
QStringLiteral( "Claire,Grant,Alan," ) );
}
}
template<typename Container>
struct SequentialContainerTester
{
static void iteration(Grantlee::Context c)
{
testSequentialIteration<Container>(c);
}
static void indexing(Grantlee::Context c)
{
testSequentialIndexing<Container>(c);
}
};
template<typename T>
struct SequentialContainerTester<QSet<T> >
{
static void iteration(Grantlee::Context c)
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{% for person in people %}{{ person.name }},{% endfor %}" ),
QStringLiteral( "people_template" ) );
QString result = t1->render( &c );
QStringList output;
output << QString::fromLatin1("Claire,") << QString::fromLatin1("Grant,") << QString::fromLatin1("Alan,");
Q_FOREACH(const QString &s, output) {
QVERIFY( result.contains(s) );
}
QCOMPARE(result.length(), output.join(QString()).length());
}
static void indexing(Grantlee::Context)
{
}
};
template<typename T>
struct SequentialContainerTester<QLinkedList<T> >
{
static void iteration(Grantlee::Context c)
{
testSequentialIteration<QLinkedList<T> >(c);
}
static void indexing(Grantlee::Context)
{
}
};
template<typename T>
struct SequentialContainerTester<std::list<T> >
{
static void iteration(Grantlee::Context c)
{
testSequentialIteration<std::list<T> >(c);
}
static void indexing(Grantlee::Context)
{
}
};
template<typename Container>
void doTestSequentialContainer_Variant()
{
Grantlee::Context c;
insertPeopleVariants<Container>( c );
SequentialContainerTester<Container>::iteration( c );
SequentialContainerTester<Container>::indexing( c );
}
template<typename Container>
void testAssociativeValues( Grantlee::Context c, bool unordered = false )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{% for person in people.values %}({{ person.name }}:{{ person.age }}),{% endfor %}" ),
QStringLiteral( "people_template" ) );
QString result = t1->render( &c );
if ( !unordered )
QCOMPARE( result, QStringLiteral( "(Claire:23),(Grant:32),(Alan:50)," ) );
else {
QVERIFY( result.size() == 33 );
QVERIFY( result.contains( QStringLiteral( "(Claire:23)," ) ) );
QVERIFY( result.contains( QStringLiteral( "(Grant:32)," ) ) );
QVERIFY( result.contains( QStringLiteral( "(Alan:50)," ) ) );
}
}
}
template<typename Container>
void testAssociativeItems( Grantlee::Context c, bool unordered )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{% for item in people.items %}({{ item.1.name }}:{{ item.1.age }}),{% endfor %}" ),
QStringLiteral( "people_template" ) );
QString result = t1->render( &c );
if ( !unordered )
QCOMPARE( result, QStringLiteral( "(Claire:23),(Grant:32),(Alan:50)," ) );
else {
QVERIFY( result.size() == 33 );
QVERIFY( result.contains( QStringLiteral( "(Claire:23)," ) ) );
QVERIFY( result.contains( QStringLiteral( "(Grant:32)," ) ) );
QVERIFY( result.contains( QStringLiteral( "(Alan:50)," ) ) );
}
}
}
template<typename Container>
void doTestAssociativeContainer_Variant( bool unordered = false )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Context c;
insertPeopleVariants<Container>( c );
testAssociativeValues<Container>( c, unordered );
testAssociativeItems<Container>( c, unordered );
}
void TestGenericTypes::testSequentialContainer_Variant()
{
doTestSequentialContainer_Variant<QVariantList>();
doTestSequentialContainer_Variant<QVector<QVariant> >();
doTestSequentialContainer_Variant<QStack<QVariant> >();
doTestSequentialContainer_Variant<QQueue<QVariant> >();
doTestSequentialContainer_Variant<QLinkedList<QVariant> >();
}
void TestGenericTypes::testAssociativeContainer_Variant()
{
doTestAssociativeContainer_Variant<QVariantMap>();
doTestAssociativeContainer_Variant<QVariantHash>( true );
}
template<typename SequentialContainer>
void insertPeople( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
const QMap<int, Person>::const_iterator end = people.constEnd();
SequentialContainer container;
for ( ; it != end; ++it )
container.insert( container.end(), it.value() );
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
template<>
void insertPeople<QSet<Person> >( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
const QMap<int, Person>::const_iterator end = people.constEnd();
QSet<Person> container;
for ( ; it != end; ++it )
container.insert( it.value() );
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
#ifndef GRANTLEE_NO_TR1
template<>
void insertPeople<ThreeArray<Person> >( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
ThreeArray<Person> container;
for ( int i = 0; i < 3; ++i, ++it ) {
Q_ASSERT( it != people.constEnd() );
container[i] = it.value();
}
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
#endif
template<typename AssociativeContainer>
void insertAssociatedPeople( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
const QMap<int, Person>::const_iterator end = people.constEnd();
AssociativeContainer container;
for ( ; it != end; ++it )
container[QString::number( it.key() )] = it.value();
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
template<typename AssociativeContainer>
void insertAssociatedPeople_Number( Grantlee::Context &c )
{
QMap<int, Person> people = getPeople();
QMap<int, Person>::const_iterator it = people.constBegin();
const QMap<int, Person>::const_iterator end = people.constEnd();
AssociativeContainer container;
for ( ; it != end; ++it )
container[it.key()] = it.value();
c.insert( QStringLiteral( "people" ), QVariant::fromValue( container ) );
}
template<typename Container>
void doTestSequentialContainer_Type()
{
Grantlee::Context c;
insertPeople<Container>( c );
SequentialContainerTester<Container>::iteration( c );
SequentialContainerTester<Container>::indexing( c );
}
template<typename Container>
void doTestAssociativeContainer_Type( bool unordered = false )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Context c;
insertAssociatedPeople<Container>( c );
testAssociativeValues<Container>( c, unordered );
testAssociativeItems<Container>( c, unordered );
}
template<typename Container>
void doTestAssociativeContainer_Type_Number( bool unordered = false )
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Context c;
insertAssociatedPeople_Number<Container>( c );
testAssociativeValues<Container>( c, unordered );
testAssociativeItems<Container>( c, unordered );
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{{ people.23.name }}" ),
QStringLiteral( "claire_template" ) );
QString result = t1->render( &c );
QCOMPARE( result, QStringLiteral( "Claire" ) );
}
}
void TestGenericTypes::testSequentialContainer_Type()
{
doTestSequentialContainer_Type<QList<Person> >();
doTestSequentialContainer_Type<QVector<Person> >();
doTestSequentialContainer_Type<QStack<Person> >();
doTestSequentialContainer_Type<QQueue<Person> >();
doTestSequentialContainer_Type<QLinkedList<Person> >();
doTestSequentialContainer_Type<QSet<Person> >();
doTestSequentialContainer_Type<std::deque<Person> >();
doTestSequentialContainer_Type<std::vector<Person> >();
doTestSequentialContainer_Type<std::list<Person> >();
#ifndef GRANTLEE_NO_TR1
doTestSequentialContainer_Type<ThreeArray<Person> >();
#endif
}
void TestGenericTypes::testAssociativeContainer_Type()
{
doTestAssociativeContainer_Type<QMap<QString, Person> >();
doTestAssociativeContainer_Type_Number<QMap<qint16, Person> >();
doTestAssociativeContainer_Type_Number<QMap<qint32, Person> >();
doTestAssociativeContainer_Type_Number<QMap<qint64, Person> >();
doTestAssociativeContainer_Type_Number<QMap<quint16, Person> >();
doTestAssociativeContainer_Type_Number<QMap<quint32, Person> >();
doTestAssociativeContainer_Type_Number<QMap<quint64, Person> >();
doTestAssociativeContainer_Type<QHash<QString, Person> >( true );
doTestAssociativeContainer_Type_Number<QHash<qint16, Person> >( true );
doTestAssociativeContainer_Type_Number<QHash<qint32, Person> >( true );
doTestAssociativeContainer_Type_Number<QHash<qint64, Person> >( true );
doTestAssociativeContainer_Type_Number<QHash<quint16, Person> >( true );
doTestAssociativeContainer_Type_Number<QHash<quint32, Person> >( true );
doTestAssociativeContainer_Type_Number<QHash<quint64, Person> >( true );
doTestAssociativeContainer_Type<std::map<QString, Person> >();
doTestAssociativeContainer_Type_Number<std::map<qint16, Person> >();
doTestAssociativeContainer_Type_Number<std::map<qint32, Person> >();
doTestAssociativeContainer_Type_Number<std::map<qint64, Person> >();
doTestAssociativeContainer_Type_Number<std::map<quint16, Person> >();
doTestAssociativeContainer_Type_Number<std::map<quint32, Person> >();
doTestAssociativeContainer_Type_Number<std::map<quint64, Person> >();
#ifndef GRANTLEE_NO_TR1
doTestAssociativeContainer_Type<QtUnorderedMap<QString, Person> >( true );
doTestAssociativeContainer_Type_Number<QtUnorderedMap<qint16, Person> >( true );
doTestAssociativeContainer_Type_Number<QtUnorderedMap<qint32, Person> >( true );
doTestAssociativeContainer_Type_Number<QtUnorderedMap<qint64, Person> >( true );
doTestAssociativeContainer_Type_Number<QtUnorderedMap<quint16, Person> >( true );
doTestAssociativeContainer_Type_Number<QtUnorderedMap<quint32, Person> >( true );
doTestAssociativeContainer_Type_Number<QtUnorderedMap<quint64, Person> >( true );
#endif
}
void TestGenericTypes::testSharedPointer()
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{{ p.name }} {{ p.age }}" ),
QStringLiteral( "template1" ) );
// Check it
QVariantHash h;
QSharedPointer<PersonObject> p( new PersonObject( QStringLiteral( "Grant Lee" ), 2 ) );
h.insert( QStringLiteral( "p" ), QVariant::fromValue( p ) );
Grantlee::Context c( h );
QCOMPARE(
t1->render( &c ),
QStringLiteral( "Grant Lee 2" ));
}
void TestGenericTypes::testThirdPartySharedPointer()
{
#ifndef GRANTLEE_NO_TR1
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{{ p.name }} {{ p.age }}" ),
QStringLiteral( "template1" ) );
// Check it
QVariantHash h;
std::tr1::shared_ptr<PersonObject> p( new PersonObject( QStringLiteral( "Grant Lee" ), 2 ) );
h.insert( QStringLiteral( "p" ), QVariant::fromValue( p ) );
Grantlee::Context c( h );
QCOMPARE(
t1->render( &c ),
QStringLiteral( "Grant Lee 2" ));
#endif
}
typedef QList<QVector<qint16> > ListVectorInt;
typedef QMap<int, QList<QVector<qint16> > > MapListVectorInt;
typedef QStack<QMap<int, QList<QVector<qint16> > > > StackMapListVectorInt;
static QVector<qint16> getNumbers()
{
static int n = 0;
QVector<qint16> nums;
nums.push_back( ++n );
nums.push_back( ++n );
return nums;
}
static ListVectorInt getNumberLists()
{
ListVectorInt list;
for ( int i = 0; i < 2; ++i ) {
list.append( getNumbers() );
}
return list;
}
static MapListVectorInt getNumberListMap()
{
MapListVectorInt map;
for ( int i = 0; i < 2; ++i ) {
map.insert( i, getNumberLists() );
}
return map;
}
static StackMapListVectorInt getMapStack()
{
StackMapListVectorInt stack;
for ( int i = 0; i < 2; ++i ) {
stack.push( getNumberListMap() );
}
return stack;
}
void TestGenericTypes::testNestedContainers()
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
Grantlee::Context c;
c.insert( QStringLiteral( "stack" ), QVariant::fromValue( getMapStack() ) );
#if defined(Q_CC_MSVC)
// MSVC doesn't like static string concatenations like L"foo" "bar", as
// results from QStringLiteral, so use QLatinString here instead.
#define STRING_LITERAL QLatinString
#else
#define STRING_LITERAL QStringLiteral
#endif
Grantlee::Template t1 = engine.newTemplate(
STRING_LITERAL( "{% for map in stack %}"
"(M {% for key, list in map.items %}"
"({{ key }} : (L {% for vector in list %}"
"(V {% for number in vector %}"
"{{ number }},"
"{% endfor %}),"
"{% endfor %}),"
"{% endfor %}),"
"{% endfor %}" )
, QStringLiteral( "template1" ) );
#undef STRING_LITERAL
QString result = t1->render( &c );
QString expectedResult = QStringLiteral(
"(M (0 : (L (V 1,2,),(V 3,4,),),(1 : (L (V 5,6,),(V 7,8,),),),(M (0 : (L (V 9,10,),(V 11,12,),),(1 : (L (V 13,14,),(V 15,16,),),),"
);
QCOMPARE(result, expectedResult);
}
class CustomObject : public QObject
{
Q_OBJECT
public:
explicit CustomObject(QObject* parent = 0)
: QObject(parent)
{
}
};
class OtherObject : public QObject
{
Q_OBJECT
Q_PROPERTY(CustomObject* custom READ custom)
public:
explicit OtherObject(QObject* parent = 0)
: QObject(parent), m_custom(new CustomObject(this))
{
m_custom->setProperty("nestedProp", QStringLiteral("nestedValue"));
}
CustomObject* custom()
{
return m_custom;
}
private:
CustomObject *m_custom;
};
void TestGenericTypes::testCustomQObjectDerived()
{
Grantlee::Engine engine;
engine.setPluginPaths( QStringList() << QStringLiteral( GRANTLEE_PLUGIN_PATH ) );
CustomObject *customObject = new CustomObject(this);
customObject->setProperty("someProp", QStringLiteral("propValue"));
Grantlee::Context c;
c.insert( QStringLiteral( "custom" ), QVariant::fromValue( customObject ) );
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{{ custom.someProp }}"), QStringLiteral( "template1" ) );
QString result = t1->render( &c );
QString expectedResult = QStringLiteral("propValue");
QCOMPARE(result, expectedResult);
}
QObject *other = new OtherObject(this);
c.insert(QStringLiteral("other"), other);
{
Grantlee::Template t1 = engine.newTemplate(
QStringLiteral( "{{ other.custom.nestedProp }}"), QStringLiteral( "template1" ) );
QString result = t1->render( &c );
QString expectedResult = QStringLiteral("nestedValue");
QCOMPARE(result, expectedResult);
}
}
struct UnregisteredType
{
};
Q_DECLARE_METATYPE(UnregisteredType)
struct RegisteredNotListType
{
};
Q_DECLARE_METATYPE(RegisteredNotListType)
GRANTLEE_BEGIN_LOOKUP(RegisteredNotListType)
Q_UNUSED( object )
if ( property == QStringLiteral( "property" ) )
return 42;
GRANTLEE_END_LOOKUP
static QVariantList dummy( const UnregisteredType& )
{
return QVariantList() << 42;
}
QVariant dummyLookup( const QVariant &, const QString & )
{
return 42;
}
void TestGenericTypes::testUnregistered()
{
{
UnregisteredType unregType;
QVariant v = QVariant::fromValue( unregType );
QVariant result = Grantlee::MetaType::lookup( v, QStringLiteral( "property" ) );
QVERIFY( !result.isValid() );
QVERIFY( !v.canConvert<QVariantList>() );
}
Grantlee::registerMetaType<RegisteredNotListType>();
{
RegisteredNotListType nonListType;
QVariant v = QVariant::fromValue( nonListType );
QVariant result = Grantlee::MetaType::lookup( v, QStringLiteral( "property" ) );
QVERIFY( result.isValid() );
QVERIFY( !v.canConvert<QVariantList>() );
}
{
QMetaType::registerConverter<UnregisteredType, QVariantList>(&dummy);
UnregisteredType unregType;
QVariant v = QVariant::fromValue( unregType );
QVariant result = Grantlee::MetaType::lookup( v, QStringLiteral( "property" ) );
QVERIFY( !result.isValid() );
}
// Only do this in release mode?
// Grantlee::MetaType::registerLookUpOperator(0, dummyLookup);
// Grantlee::MetaType::registerToVariantListOperator(0, dummy);
}
Q_DECLARE_METATYPE( Person* )
GRANTLEE_BEGIN_LOOKUP_PTR(Person)
if ( property == QStringLiteral( "name" ) )
return QString::fromStdString( object->name );
else if ( property == QStringLiteral( "age" ) )
return object->age;
GRANTLEE_END_LOOKUP
void TestGenericTypes::testPointerNonQObject()
{
Person *p = new Person( "Adele", 21 );
QVariant v = QVariant::fromValue(p);
Grantlee::registerMetaType<Person*>();
QVariant result = Grantlee::MetaType::lookup( v, QStringLiteral( "name" ) );
QCOMPARE( result.toString(), QStringLiteral( "Adele" ) );
delete p;
}
QTEST_MAIN( TestGenericTypes )
#include "testgenerictypes.moc"