/* * Boost.Reflection / basic unit test * * (C) Copyright Mariano G. Consoni and Jeremy Pack 2008 * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/ for latest version. */ #include #include #define BOOST_TEST_MAIN #define BOOST_TEST_DYN_LINK 1 #include #include class car { public: car(int year) { } int start(int speed) { return 3 + speed; } }; using namespace boost::reflections; BOOST_AUTO_TEST_CASE(argless) { reflection car_reflection; car_reflection.reflect() .constructor() .function(&car::start, "start"); // Check for argless constructor BOOST_CHECK(car_reflection.get_constructor().valid()); instance car_instance = car_reflection.get_constructor().call(4); bool start_valid = car_reflection.get_function("start").valid(); BOOST_CHECK(start_valid); // Make sure it doesn't have this undeclared method BOOST_CHECK(!car_reflection.get_function("stop").valid()); function f = car_reflection.get_function("start"); int result = f(car_instance, 86); BOOST_CHECK_EQUAL(result, 89); } class porsche : protected car { public: porsche(int year) : car(year), year_(year) { } int get_year() { return year_; } void start(float speed) { } int mileage(float day, double time) { return 3; } private: int year_; }; BOOST_AUTO_TEST_CASE(single_arg) { reflection car_reflection; car_reflection.reflect() .constructor() .function(&porsche::start, "start") .function(&porsche::mileage, "mileage") .function(&porsche::get_year, "get_year"); // Check for argless constructor BOOST_CHECK(car_reflection.get_constructor().valid()); BOOST_CHECK(!car_reflection.get_constructor().valid()); boost::reflections::instance car_instance = car_reflection.get_constructor()(1987); function f0(car_reflection.get_function("mileage")); BOOST_CHECK(f0.valid()); function f1(car_reflection.get_function("start")); BOOST_CHECK(f1.valid()); // Make sure it doesn't have this undeclared method BOOST_CHECK(!car_reflection.get_function("stop").valid()); f1(car_instance, 21.0f); function f2 = car_reflection.get_function("get_year"); BOOST_CHECK(f2.valid()); int year = f2(car_instance); BOOST_CHECK_EQUAL(year, 1987); } porsche * get_porsche(float year) { return new porsche(static_cast(year)); } BOOST_AUTO_TEST_CASE(single_arg_factory) { /* boost::reflections::reflector * car_reflector = new boost::reflections::reflector(); boost::reflections::reflection car_reflection(car_reflector); car_reflector->reflect_constructor(); car_reflector->reflect_factory(&get_porsche, "get_porsche"); car_reflector->reflect(&car::start, "start"); // Check for argless constructor BOOST_CHECK(car_reflection.has_constructor()); boost::reflections::instance car_instance = car_reflection.construct(); BOOST_CHECK(car_reflection.has_method("start")); // Make sure it doesn't have this undeclared method BOOST_CHECK(!car_reflection.has_method("stop")); car_reflector.call(car_reflection, "start");*/ }