Commit 2ad09c14d0226c87f539a13cbcf0877cf4ff304c

Authored by Geoffrey Hunter
1 parent 1ce0a48a

'TestUtil' class is now a singleton.

test/unit/BasicTests.cpp
@@ -29,8 +29,9 @@ namespace { @@ -29,8 +29,9 @@ namespace {
29 virtual ~BasicTests() { 29 virtual ~BasicTests() {
30 } 30 }
31 31
32 - std::string device0_ = "/dev/ttyS10";  
33 - std::string device1_ = "/dev/ttyS11"; 32 + std::string device0Name_ = TestUtil::GetInstance().GetDevice0Name();
  33 + std::string device1Name_ = TestUtil::GetInstance().GetDevice1Name();
  34 +
34 }; 35 };
35 36
36 TEST_F(BasicTests, CanBeConstructed) { 37 TEST_F(BasicTests, CanBeConstructed) {
@@ -39,15 +40,15 @@ namespace { @@ -39,15 +40,15 @@ namespace {
39 } 40 }
40 41
41 TEST_F(BasicTests, CanOpen) { 42 TEST_F(BasicTests, CanOpen) {
42 - SerialPort serialPort0(device0_, BaudRate::B_57600); 43 + SerialPort serialPort0(device0Name_, BaudRate::B_57600);
43 serialPort0.Open(); 44 serialPort0.Open();
44 } 45 }
45 46
46 TEST_F(BasicTests, ReadWrite) { 47 TEST_F(BasicTests, ReadWrite) {
47 - SerialPort serialPort0(device0_, BaudRate::B_57600); 48 + SerialPort serialPort0(device0Name_, BaudRate::B_57600);
48 serialPort0.Open(); 49 serialPort0.Open();
49 50
50 - SerialPort serialPort1(device1_, BaudRate::B_57600); 51 + SerialPort serialPort1(device1Name_, BaudRate::B_57600);
51 serialPort1.Open(); 52 serialPort1.Open();
52 53
53 serialPort0.Write("Hello"); 54 serialPort0.Write("Hello");
@@ -60,10 +61,10 @@ namespace { @@ -60,10 +61,10 @@ namespace {
60 61
61 62
62 TEST_F(BasicTests, ReadWriteDiffBaudRates) { 63 TEST_F(BasicTests, ReadWriteDiffBaudRates) {
63 - SerialPort serialPort0(device0_, BaudRate::B_9600); 64 + SerialPort serialPort0(device0Name_, BaudRate::B_9600);
64 serialPort0.Open(); 65 serialPort0.Open();
65 66
66 - SerialPort serialPort1(device1_, BaudRate::B_57600); 67 + SerialPort serialPort1(device1Name_, BaudRate::B_57600);
67 serialPort1.Open(); 68 serialPort1.Open();
68 69
69 serialPort0.Write("Hello"); 70 serialPort0.Write("Hello");
test/unit/ConfigTests.cpp
@@ -29,32 +29,31 @@ namespace { @@ -29,32 +29,31 @@ namespace {
29 virtual ~ConfigTests() { 29 virtual ~ConfigTests() {
30 } 30 }
31 31
32 - std::string device0_ = "/dev/ttyS10";  
33 - std::string device1_ = "/dev/ttyS11"; 32 + std::string device0Name_ = TestUtil::GetInstance().GetDevice0Name();
34 }; 33 };
35 34
36 TEST_F(ConfigTests, BaudRateSetCorrectly) { 35 TEST_F(ConfigTests, BaudRateSetCorrectly) {
37 - SerialPort serialPort0(device0_, BaudRate::B_57600); 36 + SerialPort serialPort0(device0Name_, BaudRate::B_57600);
38 serialPort0.Open(); 37 serialPort0.Open();
39 - auto retVal = TestUtil::Exec("stty -a -F " + device0_); 38 + auto retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_);
40 EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud")); 39 EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud"));
41 40
42 serialPort0.SetBaudRate(BaudRate::B_115200); 41 serialPort0.SetBaudRate(BaudRate::B_115200);
43 - retVal = TestUtil::Exec("stty -a -F " + device0_); 42 + retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_);
44 EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud")); 43 EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud"));
45 } 44 }
46 45
47 TEST_F(ConfigTests, CanonicalModeOff) { 46 TEST_F(ConfigTests, CanonicalModeOff) {
48 - SerialPort serialPort0(device0_, BaudRate::B_57600); 47 + SerialPort serialPort0(device0Name_, BaudRate::B_57600);
49 serialPort0.Open(); 48 serialPort0.Open();
50 - auto retVal = TestUtil::Exec("stty -a -F " + device0_); 49 + auto retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_);
51 EXPECT_NE(std::string::npos, retVal.find("-icanon")); 50 EXPECT_NE(std::string::npos, retVal.find("-icanon"));
52 } 51 }
53 52
54 TEST_F(ConfigTests, EchoModeOff) { 53 TEST_F(ConfigTests, EchoModeOff) {
55 - SerialPort serialPort0(device0_, BaudRate::B_57600); 54 + SerialPort serialPort0(device0Name_, BaudRate::B_57600);
56 serialPort0.Open(); 55 serialPort0.Open();
57 - auto retVal = TestUtil::Exec("stty -a -F " + device0_); 56 + auto retVal = TestUtil::GetInstance().Exec("stty -a -F " + device0Name_);
58 EXPECT_NE(std::string::npos, retVal.find("-echo")); 57 EXPECT_NE(std::string::npos, retVal.find("-echo"));
59 } 58 }
60 59
test/unit/TestUtil.hpp
@@ -30,10 +30,16 @@ namespace mn { @@ -30,10 +30,16 @@ namespace mn {
30 class TestUtil { 30 class TestUtil {
31 31
32 public: 32 public:
  33 +
  34 + static TestUtil& GetInstance() {
  35 + static TestUtil testUtil;
  36 + return testUtil;
  37 + }
  38 +
33 /// \brief Executes a command on the Linux command-line. 39 /// \brief Executes a command on the Linux command-line.
34 /// \details Blocks until command is complete. 40 /// \details Blocks until command is complete.
35 /// \throws std::runtime_error is popen() fails. 41 /// \throws std::runtime_error is popen() fails.
36 - static std::string Exec(const std::string &cmd) { 42 + std::string Exec(const std::string &cmd) {
37 std::array<char, 128> buffer; 43 std::array<char, 128> buffer;
38 std::string result; 44 std::string result;
39 std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose); 45 std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
@@ -47,7 +53,7 @@ namespace mn { @@ -47,7 +53,7 @@ namespace mn {
47 return result; 53 return result;
48 } 54 }
49 55
50 - static void CreateVirtualSerialPortPair() { 56 + void CreateVirtualSerialPortPair() {
51 std::cout << "Creating virtual serial port pair..." << std::endl; 57 std::cout << "Creating virtual serial port pair..." << std::endl;
52 std::system("nohup sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11 &"); 58 std::system("nohup sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11 &");
53 59
@@ -58,12 +64,29 @@ namespace mn { @@ -58,12 +64,29 @@ namespace mn {
58 std::system("sudo chmod a+rw /dev/ttyS11"); 64 std::system("sudo chmod a+rw /dev/ttyS11");
59 } 65 }
60 66
61 - static void CloseSerialPorts() { 67 + void CloseSerialPorts() {
62 // Dangerous! Kills all socat processes running 68 // Dangerous! Kills all socat processes running
63 // on computer 69 // on computer
64 std::system("sudo pkill socat"); 70 std::system("sudo pkill socat");
65 } 71 }
66 72
  73 + std::string GetDevice0Name() {
  74 + return device0Name_;
  75 + }
  76 +
  77 + std::string GetDevice1Name() {
  78 + return device1Name_;
  79 + }
  80 +
  81 + std::string device0Name_ = "/dev/ttyS10";
  82 + std::string device1Name_ = "/dev/ttyS11";
  83 +
  84 + protected:
  85 +
  86 + TestUtil() {
  87 +
  88 + }
  89 +
67 }; 90 };
68 } // namespace CppLinuxSerial 91 } // namespace CppLinuxSerial
69 } // namespace mn 92 } // namespace mn
test/unit/main.cpp
@@ -22,11 +22,11 @@ public: @@ -22,11 +22,11 @@ public:
22 // Override this to define how to set up the environment. 22 // Override this to define how to set up the environment.
23 virtual void SetUp() { 23 virtual void SetUp() {
24 std::cout << __PRETTY_FUNCTION__ << " called." << std::endl; 24 std::cout << __PRETTY_FUNCTION__ << " called." << std::endl;
25 - TestUtil::CreateVirtualSerialPortPair(); 25 + TestUtil::GetInstance().CreateVirtualSerialPortPair();
26 } 26 }
27 // Override this to define how to tear down the environment. 27 // Override this to define how to tear down the environment.
28 virtual void TearDown() { 28 virtual void TearDown() {
29 - TestUtil::CloseSerialPorts(); 29 + TestUtil::GetInstance().CloseSerialPorts();
30 } 30 }
31 }; 31 };
32 32