1 // Copyright (c) 2018 Raffi Enficiaud 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt or copy at 4 // http://www.boost.org/LICENSE_1_0.txt) 5 6 // See http://www.boost.org/libs/test for the library home page. 7 8 //[example_code 9 #define BOOST_TEST_MODULE runtime_configuration2 10 #include <boost/test/included/unit_test.hpp> 11 using namespace boost::unit_test; 12 13 /// The interface with the device driver. 14 class DeviceInterface { 15 public: 16 // acquires a specific device based on its name 17 static DeviceInterface* factory(std::string const& device_name); ~DeviceInterface()18 virtual ~DeviceInterface(){} 19 20 virtual bool setup() = 0; 21 virtual bool teardown() = 0; 22 virtual std::string get_device_name() const = 0; 23 }; 24 25 class MockDevice: public DeviceInterface { setup()26 bool setup() final { 27 return true; 28 } teardown()29 bool teardown() final { 30 return true; 31 } get_device_name() const32 std::string get_device_name() const { 33 return "mock_device"; 34 } 35 }; 36 factory(std::string const & device_name)37DeviceInterface* DeviceInterface::factory(std::string const& device_name) { 38 if(device_name == "mock_device") { 39 return new MockDevice(); 40 } 41 return nullptr; 42 } 43 44 struct CommandLineDeviceInit { CommandLineDeviceInitCommandLineDeviceInit45 CommandLineDeviceInit() { 46 BOOST_TEST_REQUIRE( framework::master_test_suite().argc == 3 ); 47 BOOST_TEST_REQUIRE( framework::master_test_suite().argv[1] == "--device-name" ); 48 } setupCommandLineDeviceInit49 void setup() { 50 device = DeviceInterface::factory(framework::master_test_suite().argv[2]); 51 BOOST_TEST_REQUIRE( 52 device != nullptr, 53 "Cannot create the device " << framework::master_test_suite().argv[2] ); 54 BOOST_TEST_REQUIRE( 55 device->setup(), 56 "Cannot initialize the device " << framework::master_test_suite().argv[2] ); 57 } teardownCommandLineDeviceInit58 void teardown() { 59 if(device) { 60 BOOST_TEST( 61 device->teardown(), 62 "Cannot tear-down the device " << framework::master_test_suite().argv[2]); 63 } 64 delete device; 65 } 66 static DeviceInterface *device; 67 }; 68 DeviceInterface* CommandLineDeviceInit::device = nullptr; 69 70 BOOST_TEST_GLOBAL_FIXTURE( CommandLineDeviceInit ); 71 BOOST_AUTO_TEST_CASE(check_device_has_meaningful_name)72BOOST_AUTO_TEST_CASE(check_device_has_meaningful_name) 73 { 74 BOOST_TEST(CommandLineDeviceInit::device->get_device_name() != ""); 75 } 76 //] 77