-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Refactor a bit
1 parent
ccfcd56
commit 1d138a1
Showing
7 changed files
with
213 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef ISERIALPORT_H | ||
#define ISERIALPORT_H | ||
|
||
#include <string> | ||
#include <boost/asio.hpp> | ||
|
||
using namespace boost::asio; | ||
using std::string; | ||
|
||
class ISerialPort { | ||
public: | ||
virtual void open(const string& device) = 0; | ||
virtual void set_option(const serial_port_base::baud_rate& option) = 0; | ||
virtual void async_read_some(const boost::asio::mutable_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0; | ||
virtual void async_write(const boost::asio::const_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0; | ||
virtual ~ISerialPort() = default; | ||
}; | ||
|
||
#endif // ISERIALPORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef REALSERIALPORT_H | ||
#define REALSERIALPORT_H | ||
|
||
#include "ISerialPort.h" | ||
#include <boost/asio.hpp> | ||
|
||
class RealSerialPort : public ISerialPort { | ||
public: | ||
explicit RealSerialPort(io_service& io) : port(io) {} | ||
|
||
void open(const string& device) override { | ||
port.open(device); | ||
} | ||
|
||
void set_option(const serial_port_base::baud_rate& option) override { | ||
port.set_option(option); | ||
} | ||
|
||
void async_read_some(const boost::asio::mutable_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) override { | ||
port.async_read_some(buffer, handler); | ||
} | ||
|
||
void async_write(const boost::asio::const_buffer& buffer, std::function<void(const boost::system::error_code&, std::size_t)> handler) override { | ||
boost::asio::async_write(port, buffer, handler); | ||
} | ||
|
||
private: | ||
boost::asio::serial_port port; | ||
}; | ||
|
||
#endif // REALSERIALPORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef SERVER_H | ||
#define SERVER_H | ||
|
||
#include <boost/asio.hpp> | ||
#include <string> | ||
#include "ISerialPort.h" | ||
|
||
using namespace boost::asio; | ||
using ip::tcp; | ||
using std::string; | ||
|
||
class SerialServer { | ||
public: | ||
SerialServer(io_service& io, ISerialPort& serial, tcp::acceptor& acceptor); | ||
void run(); | ||
|
||
private: | ||
io_service& io_service_; | ||
ISerialPort& serial_; | ||
tcp::acceptor& acceptor_; | ||
tcp::socket socket_; | ||
|
||
void start_accept(); | ||
void do_read_write(); | ||
}; | ||
|
||
#endif // SERVER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,46 @@ | ||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include "server.h" // Include the server header | ||
|
||
TEST(SerialServerTest, ConnectionTest) { | ||
EXPECT_EQ(1, 1); // Dummy test for illustration | ||
using namespace boost::asio; | ||
using namespace testing; | ||
|
||
class MockSerialPort : public ISerialPort { | ||
public: | ||
MOCK_METHOD(void, open, (const std::string&), (override)); | ||
MOCK_METHOD(void, set_option, (const serial_port_base::baud_rate&), (override)); | ||
MOCK_METHOD(void, async_read_some, (const boost::asio::mutable_buffer&, std::function<void(const boost::system::error_code&, std::size_t)>), (override)); | ||
MOCK_METHOD(void, async_write, (const boost::asio::const_buffer&, std::function<void(const boost::system::error_code&, std::size_t)>), (override)); | ||
}; | ||
|
||
class SerialServerTest : public ::testing::Test { | ||
protected: | ||
io_service io; | ||
MockSerialPort serial; | ||
tcp::endpoint endpoint{tcp::v4(), 12345}; | ||
tcp::acceptor acceptor{io, endpoint}; | ||
SerialServer server{io, serial, acceptor}; | ||
|
||
void SetUp() override { | ||
// Setup expectations and actions | ||
EXPECT_CALL(serial, open("dummy_device")); | ||
EXPECT_CALL(serial, set_option(_)).Times(AtLeast(1)); // Use _ for unspecified parameters with proper scope | ||
EXPECT_CALL(serial, async_read_some(_, _)).Times(AtLeast(1)); // Using _ correctly | ||
} | ||
}; | ||
|
||
MATCHER_P(BaudRateMatcher, rate, "Matches the baud rate of a serial port setting.") { | ||
return arg.value() == rate.value(); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
TEST_F(SerialServerTest, TestBaudRateSetting) { | ||
EXPECT_CALL(serial, set_option(BaudRateMatcher(serial_port_base::baud_rate(9600)))).Times(1); | ||
server.run(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); // Initialize GoogleTest, removes all recognized flags | ||
// Now you can initialize Boost program_options if necessary | ||
return RUN_ALL_TESTS(); | ||
} | ||
|