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