runtime-configuration_2.run-fail.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //[example_code
  7. #define BOOST_TEST_MODULE runtime_configuration2
  8. #include <boost/test/included/unit_test.hpp>
  9. using namespace boost::unit_test;
  10. /// The interface with the device driver.
  11. class DeviceInterface {
  12. public:
  13. // acquires a specific device based on its name
  14. static DeviceInterface* factory(std::string const& device_name);
  15. virtual ~DeviceInterface(){}
  16. virtual bool setup() = 0;
  17. virtual bool teardown() = 0;
  18. virtual std::string get_device_name() const = 0;
  19. };
  20. class MockDevice: public DeviceInterface {
  21. bool setup() final {
  22. return true;
  23. }
  24. bool teardown() final {
  25. return true;
  26. }
  27. std::string get_device_name() const {
  28. return "mock_device";
  29. }
  30. };
  31. DeviceInterface* DeviceInterface::factory(std::string const& device_name) {
  32. if(device_name == "mock_device") {
  33. return new MockDevice();
  34. }
  35. return nullptr;
  36. }
  37. struct CommandLineDeviceInit {
  38. CommandLineDeviceInit() {
  39. BOOST_TEST_REQUIRE( framework::master_test_suite().argc == 3 );
  40. BOOST_TEST_REQUIRE( framework::master_test_suite().argv[1] == "--device-name" );
  41. }
  42. void setup() {
  43. device = DeviceInterface::factory(framework::master_test_suite().argv[2]);
  44. BOOST_TEST_REQUIRE(
  45. device != nullptr,
  46. "Cannot create the device " << framework::master_test_suite().argv[2] );
  47. BOOST_TEST_REQUIRE(
  48. device->setup(),
  49. "Cannot initialize the device " << framework::master_test_suite().argv[2] );
  50. }
  51. void teardown() {
  52. if(device) {
  53. BOOST_TEST(
  54. device->teardown(),
  55. "Cannot tear-down the device " << framework::master_test_suite().argv[2]);
  56. }
  57. delete device;
  58. }
  59. static DeviceInterface *device;
  60. };
  61. DeviceInterface* CommandLineDeviceInit::device = nullptr;
  62. BOOST_TEST_GLOBAL_FIXTURE( CommandLineDeviceInit );
  63. BOOST_AUTO_TEST_CASE(check_device_has_meaningful_name)
  64. {
  65. BOOST_TEST(CommandLineDeviceInit::device->get_device_name() != "");
  66. }
  67. //]