52
52
QCOMPARE(typeEntry->conversionRule(), QString(conversionData));
55
void TestConversionRuleTag::testConversionRuleTagReplace()
57
const char cppCode[] = "\
66
const char* xmlCode = "\
67
<typesystem package='Foo'>\
68
<primitive-type name='int'/>\
69
<primitive-type name='char'/>\
70
<primitive-type name='A'>\
74
return ConvertFromCppToPython(%IN);\
77
<add-conversion type='TargetNone' check='%IN == Target_None'>\
82
<add-conversion type='B' check='CheckIfInputObjectIsB(%IN)'>\
83
%OUT = %IN.createA();\
85
<add-conversion type='String' check='String_Check(%IN)'>\
86
%OUT = new A(String_AsString(%IN), String_GetSize(%IN));\
91
<value-type name='B'/>\
94
TestUtil t(cppCode, xmlCode);
95
TypeDatabase* typeDb = TypeDatabase::instance();
96
PrimitiveTypeEntry* typeA = typeDb->findPrimitiveType("A");
99
CustomConversion* conversion = typeA->customConversion();
102
QCOMPARE(typeA, conversion->ownerType());
103
QCOMPARE(conversion->nativeToTargetConversion().trimmed(), QString("DoThis(); return ConvertFromCppToPython(%IN);"));
105
QVERIFY(conversion->replaceOriginalTargetToNativeConversions());
106
QVERIFY(conversion->hasTargetToNativeConversions());
107
QCOMPARE(conversion->targetToNativeConversions().size(), 3);
109
CustomConversion::TargetToNativeConversion* toNative = conversion->targetToNativeConversions().at(0);
111
QCOMPARE(toNative->sourceTypeName(), QString("TargetNone"));
112
QVERIFY(toNative->isCustomType());
113
QCOMPARE(toNative->sourceType(), (const TypeEntry*)0);
114
QCOMPARE(toNative->sourceTypeCheck(), QString("%IN == Target_None"));
115
QCOMPARE(toNative->conversion().trimmed(), QString("DoThat(); DoSomething(); %OUT = A();"));
117
toNative = conversion->targetToNativeConversions().at(1);
119
QCOMPARE(toNative->sourceTypeName(), QString("B"));
120
QVERIFY(!toNative->isCustomType());
121
TypeEntry* typeB = typeDb->findType("B");
123
QCOMPARE(toNative->sourceType(), typeB);
124
QCOMPARE(toNative->sourceTypeCheck(), QString("CheckIfInputObjectIsB(%IN)"));
125
QCOMPARE(toNative->conversion().trimmed(), QString("%OUT = %IN.createA();"));
127
toNative = conversion->targetToNativeConversions().at(2);
129
QCOMPARE(toNative->sourceTypeName(), QString("String"));
130
QVERIFY(toNative->isCustomType());
131
QCOMPARE(toNative->sourceType(), (const TypeEntry*)0);
132
QCOMPARE(toNative->sourceTypeCheck(), QString("String_Check(%IN)"));
133
QCOMPARE(toNative->conversion().trimmed(), QString("%OUT = new A(String_AsString(%IN), String_GetSize(%IN));"));
136
void TestConversionRuleTag::testConversionRuleTagAdd()
138
const char cppCode[] = "\
141
Date(int, int, int);\
144
const char* xmlCode = "\
145
<typesystem package='Foo'>\
146
<primitive-type name='int'/>\
147
<value-type name='Date'>\
149
<target-to-native replace='no'>\
150
<add-conversion type='TargetDate' check='TargetDate_Check(%IN)'>\
151
if (!TargetDateTimeAPI) TargetDateTime_IMPORT;\
152
%OUT = new Date(TargetDate_Day(%IN), TargetDate_Month(%IN), TargetDate_Year(%IN));\
159
TestUtil t(cppCode, xmlCode);
160
AbstractMetaClass* classA = t.builder()->classes().findClass("Date");
163
CustomConversion* conversion = classA->typeEntry()->customConversion();
166
QCOMPARE(conversion->nativeToTargetConversion(), QString());
168
QVERIFY(!conversion->replaceOriginalTargetToNativeConversions());
169
QVERIFY(conversion->hasTargetToNativeConversions());
170
QCOMPARE(conversion->targetToNativeConversions().size(), 1);
172
CustomConversion::TargetToNativeConversion* toNative = conversion->targetToNativeConversions().first();
174
QCOMPARE(toNative->sourceTypeName(), QString("TargetDate"));
175
QVERIFY(toNative->isCustomType());
176
QCOMPARE(toNative->sourceType(), (const TypeEntry*)0);
177
QCOMPARE(toNative->sourceTypeCheck(), QString("TargetDate_Check(%IN)"));
178
QCOMPARE(toNative->conversion().trimmed(), QString("if (!TargetDateTimeAPI) TargetDateTime_IMPORT; %OUT = new Date(TargetDate_Day(%IN), TargetDate_Month(%IN), TargetDate_Year(%IN));"));
181
void TestConversionRuleTag::testConversionRuleTagWithInsertTemplate()
183
const char cppCode[] = "struct A {};";
184
const char* xmlCode = "\
185
<typesystem package='Foo'>\
186
<primitive-type name='int'/>\
187
<template name='native_to_target'>\
188
return ConvertFromCppToPython(%IN);\
190
<template name='target_to_native'>\
191
%OUT = %IN.createA();\
193
<primitive-type name='A'>\
196
<insert-template name='native_to_target'/>\
199
<add-conversion type='TargetType'>\
200
<insert-template name='target_to_native'/>\
207
TestUtil t(cppCode, xmlCode);
208
TypeDatabase* typeDb = TypeDatabase::instance();
209
PrimitiveTypeEntry* typeA = typeDb->findPrimitiveType("A");
212
CustomConversion* conversion = typeA->customConversion();
215
QCOMPARE(typeA, conversion->ownerType());
216
QCOMPARE(conversion->nativeToTargetConversion().trimmed(),
217
QString("// TEMPLATE - native_to_target - START return ConvertFromCppToPython(%IN); // TEMPLATE - native_to_target - END"));
219
QVERIFY(conversion->hasTargetToNativeConversions());
220
QCOMPARE(conversion->targetToNativeConversions().size(), 1);
222
CustomConversion::TargetToNativeConversion* toNative = conversion->targetToNativeConversions().first();
224
QCOMPARE(toNative->conversion().trimmed(),
225
QString("// TEMPLATE - target_to_native - START %OUT = %IN.createA(); // TEMPLATE - target_to_native - END"));
55
228
QTEST_APPLESS_MAIN(TestConversionRuleTag)
57
230
#include "testconversionruletag.moc"