diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-07-25 22:44:53 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-09 19:10:19 -0300 |
commit | 35ab8b8e722b73a3a3ed9312379f6ab849252e19 (patch) | |
tree | 1411f293a32e125874b4b092bb96788b8f583d85 /tests | |
parent | e7fdca6465740132bd881ffd9d20e61be47472d0 (diff) |
Added improved functionality for the 'conversion-rule' tag.
It works for primitive, container and value types. Object types doesn't
have conversion rules because they can not have implicit conversions,
and the regular conversion is always the same (get C++ object held on
Python wrapper, and finding/creating a Python wrapper to a C++ pointer).
Unit tests were added.
Documentation was updated.
Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
Reviewed by Renato Araújo <renato.filho@openbossa.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/testconversionruletag.cpp | 177 | ||||
-rw-r--r-- | tests/testconversionruletag.h | 7 |
2 files changed, 180 insertions, 4 deletions
diff --git a/tests/testconversionruletag.cpp b/tests/testconversionruletag.cpp index 609a806bd..6228ef287 100644 --- a/tests/testconversionruletag.cpp +++ b/tests/testconversionruletag.cpp @@ -27,7 +27,7 @@ #include <QFile> #include <QTemporaryFile> -void TestConversionRuleTag::testConversionRuleTag() +void TestConversionRuleTag::testConversionRuleTagWithFile() { // temp file used later const char conversionData[] = "Hi! I'm a conversion rule."; @@ -38,7 +38,7 @@ void TestConversionRuleTag::testConversionRuleTag() const char cppCode[] = "struct A {};"; QString xmlCode = "\ - <typesystem package=\"Foo\">\ + <typesystem package='Foo'>\ <value-type name='A'>\ <conversion-rule file='"+ file.fileName() +"' />\ </value-type>\ @@ -52,6 +52,179 @@ void TestConversionRuleTag::testConversionRuleTag() QCOMPARE(typeEntry->conversionRule(), QString(conversionData)); } +void TestConversionRuleTag::testConversionRuleTagReplace() +{ + const char cppCode[] = "\ + struct A {\ + A();\ + A(const char*, int);\ + };\ + struct B {\ + A createA();\ + };\ + "; + const char* xmlCode = "\ + <typesystem package='Foo'>\ + <primitive-type name='int'/>\ + <primitive-type name='char'/>\ + <primitive-type name='A'>\ + <conversion-rule>\ + <native-to-target>\ + DoThis();\ + return ConvertFromCppToPython(%IN);\ + </native-to-target>\ + <target-to-native>\ + <add-conversion type='TargetNone' check='%IN == Target_None'>\ + DoThat();\ + DoSomething();\ + %OUT = A();\ + </add-conversion>\ + <add-conversion type='B' check='CheckIfInputObjectIsB(%IN)'>\ + %OUT = %IN.createA();\ + </add-conversion>\ + <add-conversion type='String' check='String_Check(%IN)'>\ + %OUT = new A(String_AsString(%IN), String_GetSize(%IN));\ + </add-conversion>\ + </target-to-native>\ + </conversion-rule>\ + </primitive-type>\ + <value-type name='B'/>\ + </typesystem>"; + + TestUtil t(cppCode, xmlCode); + TypeDatabase* typeDb = TypeDatabase::instance(); + PrimitiveTypeEntry* typeA = typeDb->findPrimitiveType("A"); + QVERIFY(typeA); + + CustomConversion* conversion = typeA->customConversion(); + QVERIFY(conversion); + + QCOMPARE(typeA, conversion->ownerType()); + QCOMPARE(conversion->nativeToTargetConversion().trimmed(), QString("DoThis(); return ConvertFromCppToPython(%IN);")); + + QVERIFY(conversion->replaceOriginalTargetToNativeConversions()); + QVERIFY(conversion->hasTargetToNativeConversions()); + QCOMPARE(conversion->targetToNativeConversions().size(), 3); + + CustomConversion::TargetToNativeConversion* toNative = conversion->targetToNativeConversions().at(0); + QVERIFY(toNative); + QCOMPARE(toNative->sourceTypeName(), QString("TargetNone")); + QVERIFY(toNative->isCustomType()); + QCOMPARE(toNative->sourceType(), (const TypeEntry*)0); + QCOMPARE(toNative->sourceTypeCheck(), QString("%IN == Target_None")); + QCOMPARE(toNative->conversion().trimmed(), QString("DoThat(); DoSomething(); %OUT = A();")); + + toNative = conversion->targetToNativeConversions().at(1); + QVERIFY(toNative); + QCOMPARE(toNative->sourceTypeName(), QString("B")); + QVERIFY(!toNative->isCustomType()); + TypeEntry* typeB = typeDb->findType("B"); + QVERIFY(typeB); + QCOMPARE(toNative->sourceType(), typeB); + QCOMPARE(toNative->sourceTypeCheck(), QString("CheckIfInputObjectIsB(%IN)")); + QCOMPARE(toNative->conversion().trimmed(), QString("%OUT = %IN.createA();")); + + toNative = conversion->targetToNativeConversions().at(2); + QVERIFY(toNative); + QCOMPARE(toNative->sourceTypeName(), QString("String")); + QVERIFY(toNative->isCustomType()); + QCOMPARE(toNative->sourceType(), (const TypeEntry*)0); + QCOMPARE(toNative->sourceTypeCheck(), QString("String_Check(%IN)")); + QCOMPARE(toNative->conversion().trimmed(), QString("%OUT = new A(String_AsString(%IN), String_GetSize(%IN));")); +} + +void TestConversionRuleTag::testConversionRuleTagAdd() +{ + const char cppCode[] = "\ + struct Date {\ + Date();\ + Date(int, int, int);\ + };\ + "; + const char* xmlCode = "\ + <typesystem package='Foo'>\ + <primitive-type name='int'/>\ + <value-type name='Date'>\ + <conversion-rule>\ + <target-to-native replace='no'>\ + <add-conversion type='TargetDate' check='TargetDate_Check(%IN)'>\ + if (!TargetDateTimeAPI) TargetDateTime_IMPORT;\ + %OUT = new Date(TargetDate_Day(%IN), TargetDate_Month(%IN), TargetDate_Year(%IN));\ + </add-conversion>\ + </target-to-native>\ + </conversion-rule>\ + </value-type>\ + </typesystem>"; + + TestUtil t(cppCode, xmlCode); + AbstractMetaClass* classA = t.builder()->classes().findClass("Date"); + QVERIFY(classA); + + CustomConversion* conversion = classA->typeEntry()->customConversion(); + QVERIFY(conversion); + + QCOMPARE(conversion->nativeToTargetConversion(), QString()); + + QVERIFY(!conversion->replaceOriginalTargetToNativeConversions()); + QVERIFY(conversion->hasTargetToNativeConversions()); + QCOMPARE(conversion->targetToNativeConversions().size(), 1); + + CustomConversion::TargetToNativeConversion* toNative = conversion->targetToNativeConversions().first(); + QVERIFY(toNative); + QCOMPARE(toNative->sourceTypeName(), QString("TargetDate")); + QVERIFY(toNative->isCustomType()); + QCOMPARE(toNative->sourceType(), (const TypeEntry*)0); + QCOMPARE(toNative->sourceTypeCheck(), QString("TargetDate_Check(%IN)")); + QCOMPARE(toNative->conversion().trimmed(), QString("if (!TargetDateTimeAPI) TargetDateTime_IMPORT; %OUT = new Date(TargetDate_Day(%IN), TargetDate_Month(%IN), TargetDate_Year(%IN));")); +} + +void TestConversionRuleTag::testConversionRuleTagWithInsertTemplate() +{ + const char cppCode[] = "struct A {};"; + const char* xmlCode = "\ + <typesystem package='Foo'>\ + <primitive-type name='int'/>\ + <template name='native_to_target'>\ + return ConvertFromCppToPython(%IN);\ + </template>\ + <template name='target_to_native'>\ + %OUT = %IN.createA();\ + </template>\ + <primitive-type name='A'>\ + <conversion-rule>\ + <native-to-target>\ + <insert-template name='native_to_target'/>\ + </native-to-target>\ + <target-to-native>\ + <add-conversion type='TargetType'>\ + <insert-template name='target_to_native'/>\ + </add-conversion>\ + </target-to-native>\ + </conversion-rule>\ + </primitive-type>\ + </typesystem>"; + + TestUtil t(cppCode, xmlCode); + TypeDatabase* typeDb = TypeDatabase::instance(); + PrimitiveTypeEntry* typeA = typeDb->findPrimitiveType("A"); + QVERIFY(typeA); + + CustomConversion* conversion = typeA->customConversion(); + QVERIFY(conversion); + + QCOMPARE(typeA, conversion->ownerType()); + QCOMPARE(conversion->nativeToTargetConversion().trimmed(), + QString("// TEMPLATE - native_to_target - START return ConvertFromCppToPython(%IN); // TEMPLATE - native_to_target - END")); + + QVERIFY(conversion->hasTargetToNativeConversions()); + QCOMPARE(conversion->targetToNativeConversions().size(), 1); + + CustomConversion::TargetToNativeConversion* toNative = conversion->targetToNativeConversions().first(); + QVERIFY(toNative); + QCOMPARE(toNative->conversion().trimmed(), + QString("// TEMPLATE - target_to_native - START %OUT = %IN.createA(); // TEMPLATE - target_to_native - END")); +} + QTEST_APPLESS_MAIN(TestConversionRuleTag) #include "testconversionruletag.moc" diff --git a/tests/testconversionruletag.h b/tests/testconversionruletag.h index afab68889..8ab1481b1 100644 --- a/tests/testconversionruletag.h +++ b/tests/testconversionruletag.h @@ -29,7 +29,10 @@ class TestConversionRuleTag : public QObject { Q_OBJECT private slots: - void testConversionRuleTag(); + void testConversionRuleTagWithFile(); + void testConversionRuleTagReplace(); + void testConversionRuleTagAdd(); + void testConversionRuleTagWithInsertTemplate(); }; -#endif
\ No newline at end of file +#endif |