Add test for automatic casting down in inheritance

This commit is contained in:
Jason Turner
2014-03-26 09:14:06 -06:00
parent 7fade8e841
commit 56b036052f
3 changed files with 19 additions and 2 deletions

View File

@@ -242,8 +242,6 @@ namespace chaiscript
/// chai.add(chaiscript::base_class<Base, Derived>());
/// \endcode
///
/// \todo Move share static type registration code into a mechanism that allows it to be properly
/// shared by all modules
template<typename Base, typename Derived>
Dynamic_Cast_Conversion base_class()
{

View File

@@ -35,11 +35,17 @@ class TestDerivedType : public TestBaseType
public:
virtual ~TestDerivedType() {}
virtual int func() { return 1; }
int derived_only_func() { return 19; }
private:
TestDerivedType &operator=(const TestDerivedType &);
};
boost::shared_ptr<TestBaseType> derived_type_factory()
{
return boost::shared_ptr<TestBaseType>(new TestDerivedType());
}
std::string hello_world()
{
return "Hello World";
@@ -81,6 +87,10 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
m->add(chaiscript::base_class<TestBaseType, TestDerivedType>());
m->add(chaiscript::fun(&TestDerivedType::derived_only_func), "derived_only_func");
m->add(chaiscript::fun(&derived_type_factory), "derived_type_factory");
m->add(chaiscript::fun(&TestBaseType::func), "func");
m->add(chaiscript::fun(&TestBaseType::val), "val");
m->add(chaiscript::fun(&TestBaseType::const_val), "const_val");

View File

@@ -15,3 +15,12 @@ assert_equal(15, t.const_val);
t.val = 23;
assert_equal(23, t.val)
// test_derived_factory returns a TestDerivedType contained
// in a shared_ptr<TestBaseType>. This is testing our ability
// to detect that and do the down casting for the user automatically
// at runtime
var d := derived_type_factory();
assert_equal(t.derived_only_func(), 19);
assert_equal(d.derived_only_func(), 19);