Commit 1ce0a48a66fe7966177f86883fd4c53c95461cf1

Authored by Geoffrey Hunter
1 parent 9c29783b

Added virtual serial port setup to gtest global environment setup.

CHANGELOG.md 0 → 100644
  1 +# Changelog
  2 +All notable changes to this project will be documented in this file.
  3 +
  4 +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
  5 +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
  6 +
  7 +## [Unreleased]
  8 +
  9 +### Added
  10 +- CMake build support.
  11 +- Unit tests using gtest.
  12 +
  13 +### Changed
  14 +- Updated serial port to use C++14.
  15 +
  16 +## [v1.0.1] - 2014-05-21
  17 +
  18 +### Changed
  19 +- Added ability to enable/disable echo with 'SerialPort::EnableEcho()'.
  20 +
  21 +## [v1.0.0] - 2014-05-15
  22 +
  23 +### Added
  24 +- Initial commit. serial-port-cpp library has basic functions up and running.
  25 +
  26 +[Unreleased]: https://github.com/mbedded-ninja/CppLinuxSerial/compare/v1.0.1...HEAD
  27 +[1.0.1]: https://github.com/mbedded-ninja/CppLinuxSerial/compare/v1.0.1...v1.0.0
0 28 \ No newline at end of file
... ...
README.rst
... ... @@ -6,8 +6,8 @@ CppLinuxSerial
6 6 Serial port library written in C++
7 7 ----------------------------------
8 8  
9   -.. image:: https://api.travis-ci.org/gbmhunter/serial-port-cpp.png?branch=master
10   - :target: https://travis-ci.org/gbmhunter/serial-port-cpp
  9 +.. image:: https://api.travis-ci.org/gbmhunter/CppLinuxSerial.png?branch=master
  10 + :target: https://travis-ci.org/gbmhunter/CppLinuxSerial
11 11  
12 12 .. role:: bash(code)
13 13 :language: bash
... ... @@ -34,11 +34,13 @@ Dependencies
34 34  
35 35 The following table lists all of the libraries dependencies.
36 36  
37   -====================== ==================== ======================================================================
38   -Dependency Delivery Usage
39   -====================== ==================== ======================================================================
40   -<stdio.h> Standard C library snprintf()
41   -====================== ==================== ======================================================================
  37 +====================== ======================================================================
  38 +Dependency Comments
  39 +====================== ======================================================================
  40 +C++14 C++14 used for strongly typed enums, std::chrono and literals.
  41 +<stdio.h> snprintf()
  42 +stty Used in unit tests to verify the serial port is configured correctly.
  43 +====================== ======================================================================
42 44  
43 45 Issues
44 46 ======
... ... @@ -48,20 +50,7 @@ See GitHub Issues.
48 50 Usage
49 51 =====
50 52  
51   -In main.c add...
52   -
53   -::
54   -
55   -
56   -
57   -
58   - int main()
59   - {
60   -
61   -
62   - }
63   -
64   -
  53 +Nothing here yet...
65 54  
66 55 FAQ
67 56 ===
... ... @@ -72,9 +61,4 @@ FAQ
72 61 Changelog
73 62 =========
74 63  
75   -========= ========== ===================================================================================================
76   -Version Date Comment
77   -========= ========== ===================================================================================================
78   -v1.0.1.0 2014/05/21 Added ability to enable/disable echo with 'SerialPort::EnableEcho()'.
79   -v1.0.0.0 2014/05/15 Initial commit. serial-port-cpp library has basic functions up and running.
80   -========= ========== ===================================================================================================
81 64 \ No newline at end of file
  65 +See CHANGELOG.md.
82 66 \ No newline at end of file
... ...
test/unit/BasicTests.cpp
... ... @@ -23,21 +23,6 @@ namespace {
23 23 class BasicTests : public ::testing::Test {
24 24 protected:
25 25  
26   - static void SetUpTestCase() {
27   - GetTestUtil().CreateVirtualSerialPortPair();
28   - }
29   -
30   - static void TearDownTestCase() {
31   - std::cout << "Destroying virtual serial ports..." << std::endl;
32   - GetTestUtil().CloseSerialPorts();
33   - }
34   -
35   - static TestUtil& GetTestUtil() {
36   - static TestUtil testUtil;
37   - return testUtil;
38   - }
39   -
40   -
41 26 BasicTests() {
42 27 }
43 28  
... ... @@ -53,17 +38,6 @@ namespace {
53 38 EXPECT_EQ(true, true);
54 39 }
55 40  
56   - TEST_F(BasicTests, BaudRateSetCorrectly) {
57   - SerialPort serialPort0(device0_, BaudRate::B_57600);
58   - serialPort0.Open();
59   - auto retVal = TestUtil::Exec("stty -a -F " + device0_);
60   - EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud"));
61   -
62   - serialPort0.SetBaudRate(BaudRate::B_115200);
63   - retVal = TestUtil::Exec("stty -a -F " + device0_);
64   - EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud"));
65   - }
66   -
67 41 TEST_F(BasicTests, CanOpen) {
68 42 SerialPort serialPort0(device0_, BaudRate::B_57600);
69 43 serialPort0.Open();
... ...
test/unit/ConfigTests.cpp 0 → 100644
  1 +///
  2 +/// \file ConfigTests.cpp
  3 +/// \author Geoffrey Hunter <gbmhunter@gmail.com> (www.mbedded.ninja)
  4 +/// \created 2017-11-24
  5 +/// \last-modified 2017-11-24
  6 +/// \brief Configuration tests for the SerialPort class.
  7 +/// \details
  8 +/// See README.rst in repo root dir for more info.
  9 +
  10 +// System includes
  11 +#include "gtest/gtest.h"
  12 +
  13 +// 3rd party includes
  14 +#include "CppLinuxSerial/SerialPort.hpp"
  15 +
  16 +// User includes
  17 +#include "TestUtil.hpp"
  18 +
  19 +using namespace mn::CppLinuxSerial;
  20 +
  21 +namespace {
  22 +
  23 + class ConfigTests : public ::testing::Test {
  24 + protected:
  25 +
  26 + ConfigTests() {
  27 + }
  28 +
  29 + virtual ~ConfigTests() {
  30 + }
  31 +
  32 + std::string device0_ = "/dev/ttyS10";
  33 + std::string device1_ = "/dev/ttyS11";
  34 + };
  35 +
  36 + TEST_F(ConfigTests, BaudRateSetCorrectly) {
  37 + SerialPort serialPort0(device0_, BaudRate::B_57600);
  38 + serialPort0.Open();
  39 + auto retVal = TestUtil::Exec("stty -a -F " + device0_);
  40 + EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud"));
  41 +
  42 + serialPort0.SetBaudRate(BaudRate::B_115200);
  43 + retVal = TestUtil::Exec("stty -a -F " + device0_);
  44 + EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud"));
  45 + }
  46 +
  47 + TEST_F(ConfigTests, CanonicalModeOff) {
  48 + SerialPort serialPort0(device0_, BaudRate::B_57600);
  49 + serialPort0.Open();
  50 + auto retVal = TestUtil::Exec("stty -a -F " + device0_);
  51 + EXPECT_NE(std::string::npos, retVal.find("-icanon"));
  52 + }
  53 +
  54 + TEST_F(ConfigTests, EchoModeOff) {
  55 + SerialPort serialPort0(device0_, BaudRate::B_57600);
  56 + serialPort0.Open();
  57 + auto retVal = TestUtil::Exec("stty -a -F " + device0_);
  58 + EXPECT_NE(std::string::npos, retVal.find("-echo"));
  59 + }
  60 +
  61 +} // namespace
0 62 \ No newline at end of file
... ...
test/unit/TestUtil.hpp
... ... @@ -47,30 +47,20 @@ namespace mn {
47 47 return result;
48 48 }
49 49  
50   - void CreateVirtualSerialPortPair() {
  50 + static void CreateVirtualSerialPortPair() {
51 51 std::cout << "Creating virtual serial port pair..." << std::endl;
52   -// StartProcess("sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11");
53   -// std::this_thread::sleep_for(1s);
54   -// StartProcess("sudo chmod a+rw /dev/ttyS10");
55   -// StartProcess("sudo chmod a+rw /dev/ttyS11");
56   -// std::this_thread::sleep_for(1s);
57   -// std::cout << "Finished creating virtual serial port pair." << std::endl;
58   -// std::system("./run.sh");
59 52 std::system("nohup sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11 &");
60   - auto pid = std::system("echo $!");
61   - std::cout << "pid = " << pid << std::endl;
  53 +
  54 + // Hacky! Since socat is detached, we have no idea at what point it has created
  55 + // ttyS10 and ttyS11. Assume 1 second is long enough...
62 56 std::this_thread::sleep_for(1s);
63 57 std::system("sudo chmod a+rw /dev/ttyS10");
64 58 std::system("sudo chmod a+rw /dev/ttyS11");
65 59 }
66 60  
67   - void CloseSerialPorts() {
68   -// for(const auto& filePointer : processes_) {
69   -// std::cout << "Sending SIGINT..." << std::endl;
70   -// kill(filePointer.pid, SIGINT);
71   -// std::cout << "Calling pclose2()..." << std::endl;
72   -// pclose2(filePointer.fp, filePointer.pid);
73   -// }
  61 + static void CloseSerialPorts() {
  62 + // Dangerous! Kills all socat processes running
  63 + // on computer
74 64 std::system("sudo pkill socat");
75 65 }
76 66  
... ...
test/unit/main.cpp 0 → 100644
  1 +///
  2 +/// \file main.cpp
  3 +/// \author Geoffrey Hunter <gbmhunter@gmail.com> (www.mbedded.ninja)
  4 +/// \edited n/a
  5 +/// \created 2017-11-24
  6 +/// \last-modified 2017-11-24
  7 +/// \brief Contains the main entry point for the unit tests.
  8 +/// \details
  9 +/// See README.md in root dir for more info.
  10 +
  11 +// System includes
  12 +#include "gtest/gtest.h"
  13 +
  14 +// User includes
  15 +#include "TestUtil.hpp"
  16 +
  17 +using namespace mn::CppLinuxSerial;
  18 +
  19 +class Environment : public testing::Environment {
  20 +public:
  21 + virtual ~Environment() {}
  22 + // Override this to define how to set up the environment.
  23 + virtual void SetUp() {
  24 + std::cout << __PRETTY_FUNCTION__ << " called." << std::endl;
  25 + TestUtil::CreateVirtualSerialPortPair();
  26 + }
  27 + // Override this to define how to tear down the environment.
  28 + virtual void TearDown() {
  29 + TestUtil::CloseSerialPorts();
  30 + }
  31 +};
  32 +
  33 +int main(int argc, char **argv)
  34 +{
  35 + ::testing::InitGoogleTest(&argc, argv);
  36 +
  37 + // Create and register global test setup
  38 + // (gtest takes ownership of pointer, do not delete manaully!)
  39 + ::testing::AddGlobalTestEnvironment(new Environment);
  40 +
  41 + return RUN_ALL_TESTS();
  42 +}
0 43 \ No newline at end of file
... ...