diff --git a/test/unit/BasicTests.cpp b/test/unit/BasicTests.cpp index 26cf15e..4d5bcad 100644 --- a/test/unit/BasicTests.cpp +++ b/test/unit/BasicTests.cpp @@ -29,8 +29,9 @@ namespace { virtual ~BasicTests() { } - std::string device0_ = "/dev/ttyS10"; - std::string device1_ = "/dev/ttyS11"; + std::string device0Name_ = TestUtil::GetInstance().GetDevice0Name(); + std::string device1Name_ = TestUtil::GetInstance().GetDevice1Name(); + }; TEST_F(BasicTests, CanBeConstructed) { @@ -39,15 +40,15 @@ namespace { } TEST_F(BasicTests, CanOpen) { - SerialPort serialPort0(device0_, BaudRate::B_57600); + SerialPort serialPort0(device0Name_, BaudRate::B_57600); serialPort0.Open(); } TEST_F(BasicTests, ReadWrite) { - SerialPort serialPort0(device0_, BaudRate::B_57600); + SerialPort serialPort0(device0Name_, BaudRate::B_57600); serialPort0.Open(); - SerialPort serialPort1(device1_, BaudRate::B_57600); + SerialPort serialPort1(device1Name_, BaudRate::B_57600); serialPort1.Open(); serialPort0.Write("Hello"); @@ -60,10 +61,10 @@ namespace { TEST_F(BasicTests, ReadWriteDiffBaudRates) { - SerialPort serialPort0(device0_, BaudRate::B_9600); + SerialPort serialPort0(device0Name_, BaudRate::B_9600); serialPort0.Open(); - SerialPort serialPort1(device1_, BaudRate::B_57600); + SerialPort serialPort1(device1Name_, BaudRate::B_57600); serialPort1.Open(); serialPort0.Write("Hello"); diff --git a/test/unit/ConfigTests.cpp b/test/unit/ConfigTests.cpp index b73ef96..6101329 100644 --- a/test/unit/ConfigTests.cpp +++ b/test/unit/ConfigTests.cpp @@ -29,32 +29,31 @@ namespace { virtual ~ConfigTests() { } - std::string device0_ = "/dev/ttyS10"; - std::string device1_ = "/dev/ttyS11"; + std::string device0Name_ = TestUtil::GetInstance().GetDevice0Name(); }; TEST_F(ConfigTests, BaudRateSetCorrectly) { - SerialPort serialPort0(device0_, BaudRate::B_57600); + SerialPort serialPort0(device0Name_, BaudRate::B_57600); serialPort0.Open(); - auto retVal = TestUtil::Exec("stty -a -F " + device0_); + auto retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_); EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud")); serialPort0.SetBaudRate(BaudRate::B_115200); - retVal = TestUtil::Exec("stty -a -F " + device0_); + retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_); EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud")); } TEST_F(ConfigTests, CanonicalModeOff) { - SerialPort serialPort0(device0_, BaudRate::B_57600); + SerialPort serialPort0(device0Name_, BaudRate::B_57600); serialPort0.Open(); - auto retVal = TestUtil::Exec("stty -a -F " + device0_); + auto retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_); EXPECT_NE(std::string::npos, retVal.find("-icanon")); } TEST_F(ConfigTests, EchoModeOff) { - SerialPort serialPort0(device0_, BaudRate::B_57600); + SerialPort serialPort0(device0Name_, BaudRate::B_57600); serialPort0.Open(); - auto retVal = TestUtil::Exec("stty -a -F " + device0_); + auto retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_); EXPECT_NE(std::string::npos, retVal.find("-echo")); } diff --git a/test/unit/TestUtil.hpp b/test/unit/TestUtil.hpp index dd42570..1cf1a34 100644 --- a/test/unit/TestUtil.hpp +++ b/test/unit/TestUtil.hpp @@ -30,10 +30,16 @@ namespace mn { class TestUtil { public: + + static TestUtil& GetInstance() { + static TestUtil testUtil; + return testUtil; + } + /// \brief Executes a command on the Linux command-line. /// \details Blocks until command is complete. /// \throws std::runtime_error is popen() fails. - static std::string Exec(const std::string &cmd) { + std::string Exec(const std::string &cmd) { std::array buffer; std::string result; std::shared_ptr pipe(popen(cmd.c_str(), "r"), pclose); @@ -47,7 +53,7 @@ namespace mn { return result; } - static void CreateVirtualSerialPortPair() { + void CreateVirtualSerialPortPair() { std::cout << "Creating virtual serial port pair..." << std::endl; std::system("nohup sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11 &"); @@ -58,12 +64,29 @@ namespace mn { std::system("sudo chmod a+rw /dev/ttyS11"); } - static void CloseSerialPorts() { + void CloseSerialPorts() { // Dangerous! Kills all socat processes running // on computer std::system("sudo pkill socat"); } + std::string GetDevice0Name() { + return device0Name_; + } + + std::string GetDevice1Name() { + return device1Name_; + } + + std::string device0Name_ = "/dev/ttyS10"; + std::string device1Name_ = "/dev/ttyS11"; + + protected: + + TestUtil() { + + } + }; } // namespace CppLinuxSerial } // namespace mn diff --git a/test/unit/main.cpp b/test/unit/main.cpp index 7fa883a..10d32db 100644 --- a/test/unit/main.cpp +++ b/test/unit/main.cpp @@ -22,11 +22,11 @@ public: // Override this to define how to set up the environment. virtual void SetUp() { std::cout << __PRETTY_FUNCTION__ << " called." << std::endl; - TestUtil::CreateVirtualSerialPortPair(); + TestUtil::GetInstance().CreateVirtualSerialPortPair(); } // Override this to define how to tear down the environment. virtual void TearDown() { - TestUtil::CloseSerialPorts(); + TestUtil::GetInstance().CloseSerialPorts(); } };