void ConstructorTestSuite::testClassRef() { Class test = Class::lookup("ConstructorTest::Test"); Class test2 = Class::lookup("ConstructorTest::Test2"); auto it = test.constructors().begin(); Constructor c1 = *it++; Constructor c2 = *it++; Constructor c3 = *it++; TS_ASSERT_EQUALS(c1.getClass(), test); TS_ASSERT_EQUALS(c2.getClass(), test); TS_ASSERT_EQUALS(c3.getClass(), test); TS_ASSERT_EQUALS(c1.getClass(), c2.getClass()); TS_ASSERT_DIFFERS(c1.getClass(), test2); }
void ConstructorTestSuite::testConstructorHash() { using namespace std; unordered_map<Constructor, int> cmap; Class test = Class::lookup("ConstructorTest::Test"); auto it = test.constructors().begin(); Constructor c1 = *it++; Constructor c2 = *it++; Constructor c3 = *it++; cmap[c1] = 1; cmap[c2] = 2; cmap[c3] = 3; TS_ASSERT_EQUALS(cmap[c1], 1); TS_ASSERT_EQUALS(cmap[c2], 2); TS_ASSERT_EQUALS(cmap[c3], 3); }
void ConstructorTestSuite::testParametersByConstReference() { Class test = Class::lookup("ConstructorTest::TestConsParamPassing3"); Attribute attr = test.attributes().front(); Constructor cons = test.constructors().front(); CopyCount::resetAll(); CopyCount c(77); TestConsParamPassing3 inst(c); TS_ASSERT_EQUALS(inst.id, 77); TS_ASSERT_EQUALS(c.id(), 77); int directCallCopies = CopyCount::numberOfCopies(); int directCallMoves = CopyCount::numberOfMoves(); TS_ASSERT_EQUALS(directCallCopies, 0); TS_ASSERT_EQUALS(directCallMoves, 0); CopyCount::resetAll(); VariantValue vinst = cons.call(c); VariantValue v = attr.get(vinst); TS_ASSERT(v.isValid()); TS_ASSERT_EQUALS(v.value<int>(), 77); TS_ASSERT_EQUALS(c.id(), 77); int reflectionCopies = CopyCount::numberOfCopies(); int reflectionMoves = CopyCount::numberOfMoves(); TS_ASSERT_EQUALS(reflectionCopies, 0); TS_ASSERT_EQUALS(reflectionMoves, 0); TS_ASSERT(!c.hasBeenMoved()); }
void ClassTestSuite::testClass() { //Class test = ClassOf<Test1>(); Class test = Class::lookup("ClassTest::Test1"); TS_ASSERT_EQUALS(test.simpleName(), "Test1"); #ifndef NO_RTTI TS_ASSERT(test.describes<ClassTest::Test1>()) Class test2 = Class::lookup(typeid(ClassTest::Test1)); TS_ASSERT_EQUALS(test2.simpleName(), "Test1"); TS_ASSERT_EQUALS(test, test2); #endif Class::ClassList superClasses = test.superclasses(); TS_ASSERT_EQUALS(superClasses.size(), 2); Class base1; Class base2; TS_ASSERT_THROWS(base1.simpleName(), std::runtime_error); // uninitialized handle for(const Class& c: superClasses) { if (c.fullyQualifiedName() == "ClassTest::TestBase1") { base1 = c; } else if (c.fullyQualifiedName() == "ClassTest::TestBase2") { base2 = c; } } TS_ASSERT_EQUALS(base1.fullyQualifiedName(), "ClassTest::TestBase1"); TS_ASSERT_EQUALS(base1.simpleName(), "TestBase1"); TS_ASSERT_EQUALS(base2.fullyQualifiedName(), "ClassTest::TestBase2"); TS_ASSERT_EQUALS(base2.simpleName(), "TestBase2"); TS_ASSERT(inheritanceRelation(test, base1)); TS_ASSERT(inherits(test, base1)); TS_ASSERT(inheritedBy(base1, test)); TS_ASSERT(inheritanceRelation(test, base2)); TS_ASSERT(inherits(test, base2)); TS_ASSERT(inheritedBy(base2, test)); TS_ASSERT(!inheritanceRelation(base1, base2)); Class::ConstructorList base1Constructors = base1.constructors(); TS_ASSERT_EQUALS(base1.constructors().size(), 1); Constructor base1Constructor = base1Constructors.front(); TS_ASSERT_EQUALS(base1Constructor.numberOfArguments(), 0); TS_ASSERT_THROWS(base1Constructor.call(), std::runtime_error); // abstract class Class::ConstructorList base2Constructors = base2.constructors(); TS_ASSERT_EQUALS(base2.constructors().size(), 1); Constructor base2Constructor = base2Constructors.front(); TS_ASSERT_EQUALS(base2Constructor.numberOfArguments(), 0); VariantValue b2Inst = base2Constructor.call(); TS_ASSERT(b2Inst.isValid()); TS_ASSERT_EQUALS(base2.attributes().size(), 0); Class::MethodList base2Methods = base2.methods(); TS_ASSERT_EQUALS(base2Methods.size(), 1); Method base2Method1 = base2Methods.front(); TS_ASSERT_EQUALS(base2Method1.name(), "base2Method1"); TS_ASSERT(base2Method1.isConst()); TS_ASSERT(!base2Method1.isVolatile()); TS_ASSERT(!base2Method1.isStatic()); TS_ASSERT_EQUALS(base2Method1.numberOfArguments(), 0); TS_ASSERT_EQUALS(base2Method1.call(b2Inst).value<int>(), 6); Class::ConstructorList testConstructors = test.constructors(); Class::AttributeList attributes = test.attributes(); TS_ASSERT_EQUALS(attributes.size(), 2); Attribute attr = attributes.front(); WITH_RTTI(TS_ASSERT(attr.type() == typeid(int))); TS_ASSERT_EQUALS(testConstructors.size(), 3); Constructor defaultConstr; Constructor intConstr; TS_ASSERT_THROWS(defaultConstr.argumentSpellings(), std::runtime_error); for (const Constructor& c: testConstructors) { if (c.numberOfArguments() == 0) { defaultConstr = c; } else if (c.numberOfArguments() == 1 && c.argumentSpellings()[0] == "int"){ intConstr = c; } } TS_ASSERT_EQUALS(defaultConstr.numberOfArguments(), 0); TS_ASSERT_EQUALS(intConstr.numberOfArguments(), 1); VariantValue testInst1 = defaultConstr.call(); TS_ASSERT(testInst1.isA<Test1>()); #ifndef NO_RTTI Class test3 = Class::lookup(testInst1.typeId()); TS_ASSERT_EQUALS(test3.simpleName(), "Test1"); TS_ASSERT_EQUALS(test, test3); #endif TS_ASSERT_EQUALS(attr.get(testInst1).value<const int&>(), 3); VariantValue testInst2 = intConstr.call(77); TS_ASSERT(testInst2.isA<Test1>()); TS_ASSERT_EQUALS(attr.get(testInst2).value<const int&>(), 77); Class::MethodList methods = test.methods(); TS_ASSERT_EQUALS(methods.size(), 9); Method base1Method1; Method method1; Method method2; Method staticMethod; for (const Method& m: methods) { if (m.name() == "base1Method1") { base1Method1 = m; } else if (m.name() == "base2Method1") { base2Method1 = m; } else if (m.name() == "method1") { method1 = m; } else if (m.name() == "method2") { method2 = m; } else if (m.name() == "staticMethod") { staticMethod = m; } } TS_ASSERT_EQUALS(base1Method1.name(), "base1Method1"); TS_ASSERT(!base1Method1.isConst()); TS_ASSERT(!base1Method1.isStatic()); TS_ASSERT(!base1Method1.isVolatile()); WITH_RTTI(TS_ASSERT(base1Method1.returnType() == typeid(int))); TS_ASSERT_EQUALS(base1Method1.numberOfArguments(), 0); TS_ASSERT_EQUALS(base1Method1.call(testInst2).value<int>(), 5); TS_ASSERT_EQUALS(base2Method1.name(), "base2Method1"); TS_ASSERT(base2Method1.isConst()); TS_ASSERT(!base2Method1.isStatic()); TS_ASSERT(!base2Method1.isVolatile()); WITH_RTTI(TS_ASSERT(base2Method1.returnType() == typeid(int))); TS_ASSERT_EQUALS(base2Method1.numberOfArguments(), 0); TS_ASSERT_EQUALS(base2Method1.call(testInst2).value<int>(), 6); TS_ASSERT_EQUALS(method2.name(), "method2"); TS_ASSERT(!method2.isConst()); TS_ASSERT(!method2.isStatic()); TS_ASSERT(!method2.isVolatile()); WITH_RTTI(TS_ASSERT(method2.returnType() == typeid(double))); TS_ASSERT_EQUALS(method2.numberOfArguments(), 1); WITH_RTTI(TS_ASSERT(*method2.argumentTypes()[0] == typeid(double))); TS_ASSERT_EQUALS(method2.call(testInst2, 2).value<double>(), 6.28); TS_ASSERT_EQUALS(staticMethod.name(), "staticMethod"); TS_ASSERT(!staticMethod.isConst()); TS_ASSERT(staticMethod.isStatic()); TS_ASSERT(!staticMethod.isVolatile()); WITH_RTTI(TS_ASSERT(staticMethod.returnType() == typeid(double))); TS_ASSERT_EQUALS(staticMethod.numberOfArguments(), 0); TS_ASSERT_EQUALS(staticMethod.call().value<double>(), 3.14); ClassTest::Test1 t(666); VariantValue inst3; inst3.construct<ClassTest::TestBase1&>(t); TS_ASSERT_THROWS_ANYTHING(method1.call(inst3).value<string>()); VariantValue inst4 = test.castUp(inst3, base1); TS_ASSERT(inst4.isValid()); bool success = false; ClassTest::Test1& derivedRef = inst4.convertTo<ClassTest::Test1&>(&success); TS_ASSERT(success); TS_ASSERT_EQUALS(derivedRef.attribute1, 666); TS_ASSERT_EQUALS(method1.call(inst4).value<string>(), "this is a test"); Class test_3 = Class::lookup("ClassTest::Test3"); ClassTest::Test3 t2(666); TS_ASSERT_EQUALS(t2.attribute1, 333); TS_ASSERT_EQUALS(t2.attribute3, 666); VariantValue inst5; inst5.construct<ClassTest::TestBase1&>(t2); VariantValue inst6 = test_3.castUp(inst5, base1); TS_ASSERT(inst6.isValid()); success = false; ClassTest::Test3& derivedRef2 = inst6.convertTo<ClassTest::Test3&>(&success); TS_ASSERT(success); TS_ASSERT_EQUALS(derivedRef2.attribute1, 333); TS_ASSERT_EQUALS(derivedRef2.attribute3, 666); t2.attribute1 = 787; t2.attribute3 = 13; TS_ASSERT_EQUALS(derivedRef2.attribute1, 787); TS_ASSERT_EQUALS(derivedRef2.attribute3, 13); ClassTest::Test1 t3(666); VariantValue inst7; inst7.construct<ClassTest::TestBase1&>(t3); VariantValue inst8 = test_3.castUp(inst7, base1); TS_ASSERT(!inst8.isValid()); Class test_2 = Class::lookup("ClassTest::Test2"); VariantValue inst9 = test_2.castUp(inst7, base1); TS_ASSERT(!inst9.isValid()); }
void ClassTestSuite::testClass() { //Class test = ClassOf<Test1>(); Class test = Class::lookup("ClassTest::Test1"); TS_ASSERT_EQUALS(test.simpleName(), "Test1"); Class::ClassList superClasses = test.superclasses(); TS_ASSERT_EQUALS(superClasses.size(), 2); Class base1; Class base2; TS_ASSERT_THROWS(base1.simpleName(), std::runtime_error); // uninitialized handle for(const Class& c: superClasses) { if (c.fullyQualifiedName() == "ClassTest::TestBase1") { base1 = c; } else if (c.fullyQualifiedName() == "ClassTest::TestBase2") { base2 = c; } } TS_ASSERT_EQUALS(base1.fullyQualifiedName(), "ClassTest::TestBase1"); TS_ASSERT_EQUALS(base1.simpleName(), "TestBase1"); TS_ASSERT_EQUALS(base2.fullyQualifiedName(), "ClassTest::TestBase2"); TS_ASSERT_EQUALS(base2.simpleName(), "TestBase2"); Class::ConstructorList base1Constructors = base1.constructors(); TS_ASSERT_EQUALS(base1.constructors().size(), 1); Constructor base1Constructor = base1Constructors.front(); TS_ASSERT_EQUALS(base1Constructor.numberOfArguments(), 0); TS_ASSERT_THROWS(base1Constructor.call(), std::runtime_error); // abstract class Class::ConstructorList base2Constructors = base2.constructors(); TS_ASSERT_EQUALS(base2.constructors().size(), 1); Constructor base2Constructor = base2Constructors.front(); TS_ASSERT_EQUALS(base2Constructor.numberOfArguments(), 0); VariantValue b2Inst = base2Constructor.call(); TS_ASSERT(b2Inst.isValid()); TS_ASSERT_EQUALS(base2.attributes().size(), 0); Class::MethodList base2Methods = base2.methods(); TS_ASSERT_EQUALS(base2Methods.size(), 1); Method base2Method1 = base2Methods.front(); TS_ASSERT_EQUALS(base2Method1.name(), "base2Method1"); TS_ASSERT(base2Method1.isConst()); TS_ASSERT(!base2Method1.isVolatile()); TS_ASSERT(!base2Method1.isStatic()); TS_ASSERT_EQUALS(base2Method1.numberOfArguments(), 0); TS_ASSERT_EQUALS(base2Method1.call(b2Inst).value<int>(), 6); Class::ConstructorList testConstructors = test.constructors(); Class::AttributeList attributes = test.attributes(); TS_ASSERT_EQUALS(attributes.size(), 2); Attribute attr = attributes.front(); WITH_RTTI(TS_ASSERT(attr.type() == typeid(int))); TS_ASSERT_EQUALS(testConstructors.size(), 3); Constructor defaultConstr; Constructor intConstr; TS_ASSERT_THROWS(defaultConstr.argumentSpellings(), std::runtime_error); for (const Constructor& c: testConstructors) { if (c.numberOfArguments() == 0) { defaultConstr = c; } else if (c.numberOfArguments() == 1 && c.argumentSpellings()[0] == "int") { intConstr = c; } } TS_ASSERT_EQUALS(defaultConstr.numberOfArguments(), 0); TS_ASSERT_EQUALS(intConstr.numberOfArguments(), 1); VariantValue testInst1 = defaultConstr.call(); TS_ASSERT(testInst1.isA<Test1>()); TS_ASSERT_EQUALS(attr.get(testInst1).value<int>(), 3); VariantValue testInst2 = intConstr.call(77); TS_ASSERT(testInst2.isA<Test1>()); TS_ASSERT_EQUALS(attr.get(testInst2).value<int>(), 77); Class::MethodList methods = test.methods(); TS_ASSERT_EQUALS(methods.size(), 7); Method base1Method1; Method method1; Method method2; Method staticMethod; for (const Method& m: methods) { if (m.name() == "base1Method1") { base1Method1 = m; } else if (m.name() == "base2Method1") { base2Method1 = m; } else if (m.name() == "method1") { method1 = m; } else if (m.name() == "method2") { method2 = m; } else if (m.name() == "staticMethod") { staticMethod = m; } } TS_ASSERT_EQUALS(base1Method1.name(), "base1Method1"); TS_ASSERT(!base1Method1.isConst()); TS_ASSERT(!base1Method1.isStatic()); TS_ASSERT(!base1Method1.isVolatile()); WITH_RTTI(TS_ASSERT(base1Method1.returnType() == typeid(int))); TS_ASSERT_EQUALS(base1Method1.numberOfArguments(), 0); TS_ASSERT_EQUALS(base1Method1.call(testInst2).value<int>(), 5); TS_ASSERT_EQUALS(base2Method1.name(), "base2Method1"); TS_ASSERT(base2Method1.isConst()); TS_ASSERT(!base2Method1.isStatic()); TS_ASSERT(!base2Method1.isVolatile()); WITH_RTTI(TS_ASSERT(base2Method1.returnType() == typeid(int))); TS_ASSERT_EQUALS(base2Method1.numberOfArguments(), 0); TS_ASSERT_EQUALS(base2Method1.call(testInst2).value<int>(), 6); TS_ASSERT_EQUALS(method2.name(), "method2"); TS_ASSERT(!method2.isConst()); TS_ASSERT(!method2.isStatic()); TS_ASSERT(!method2.isVolatile()); WITH_RTTI(TS_ASSERT(method2.returnType() == typeid(double))); TS_ASSERT_EQUALS(method2.numberOfArguments(), 1); WITH_RTTI(TS_ASSERT(*method2.argumentTypes()[0] == typeid(double))); TS_ASSERT_EQUALS(method2.call(testInst2, 2).value<double>(), 6.28); TS_ASSERT_EQUALS(staticMethod.name(), "staticMethod"); TS_ASSERT(!staticMethod.isConst()); TS_ASSERT(staticMethod.isStatic()); TS_ASSERT(!staticMethod.isVolatile()); WITH_RTTI(TS_ASSERT(staticMethod.returnType() == typeid(double))); TS_ASSERT_EQUALS(staticMethod.numberOfArguments(), 0); TS_ASSERT_EQUALS(staticMethod.call().value<double>(), 3.14); }