1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Updating tests while on the mac

This commit is contained in:
William Woodall 2012-01-23 14:28:14 -06:00
parent da2307c8a6
commit 0ea153a9b4
4 changed files with 72 additions and 33 deletions

View File

@ -24,7 +24,7 @@ int run(int argc, char **argv)
int count = 0; int count = 0;
while (count >= 0) { while (count >= 0) {
int bytes_wrote = serial.write("Testing."); size_t bytes_wrote = serial.write("Testing.");
std::string result = serial.read(8); std::string result = serial.read(8);
std::cout << ">" << count << ">" << bytes_wrote << ">"; std::cout << ">" << count << ">" << bytes_wrote << ">";
std::cout << result.length() << "<" << result << std::endl; std::cout << result.length() << "<" << result << std::endl;

View File

@ -42,4 +42,10 @@ rosbuild_add_executable(serial_listener_example
examples/serial_listener_example.cc) examples/serial_listener_example.cc)
target_link_libraries(serial_listener_example ${PROJECT_NAME}) target_link_libraries(serial_listener_example ${PROJECT_NAME})
# Create unit tests
rosbuild_add_gtest(serial_tests tests/serial_tests.cc)
target_link_libraries(serial_tests ${PROJECT_NAME})
rosbuild_add_gtest(serial_listener_tests tests/serial_listener_tests.cc)
target_link_libraries(serial_listener_tests ${PROJECT_NAME})
endmacro(build_serial) endmacro(build_serial)

View File

@ -17,7 +17,7 @@
*/ */
#define SERIAL_PORT_NAME "/dev/tty.usbserial" #define SERIAL_PORT_NAME "/dev/tty.usbserial-A900cfJA"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -39,11 +39,19 @@ void default_handler(std::string line) {
namespace { namespace {
void my_sleep(long milliseconds) {
boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
}
class SerialListenerTests : public ::testing::Test { class SerialListenerTests : public ::testing::Test {
protected: protected:
virtual void SetUp() { virtual void SetUp() {
port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); port1 = new Serial(SERIAL_PORT_NAME, 115200, 250);
// Need to wait a bit for the Arduino to come up
my_sleep(1000);
listener.setDefaultHandler(default_handler); listener.setDefaultHandler(default_handler);
listener.startListening((*port1)); listener.startListening((*port1));
} }
@ -59,10 +67,6 @@ protected:
}; };
void my_sleep(long milliseconds) {
boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
}
TEST_F(SerialListenerTests, handlesPartialMessage) { TEST_F(SerialListenerTests, handlesPartialMessage) {
global_count = 0; global_count = 0;
std::string input_str = "?$1E\r$1E=Robo"; std::string input_str = "?$1E\r$1E=Robo";

View File

@ -1,35 +1,64 @@
#include <string> /* To run these tests you need to change the define below to the serial port
#include <iostream> * with a loop back device attached.
*
* Alternatively you could use an Arduino:
void setup()
{
Serial.begin(115200);
}
void loop()
{
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
}
*/
#define SERIAL_PORT_NAME "/dev/tty.usbserial"
#include "gtest/gtest.h"
#include <boost/bind.hpp>
// OMG this is so nasty...
#define private public
#define protected public
#include "serial/serial.h" #include "serial/serial.h"
using namespace serial;
using std::string; namespace {
using std::cout;
using std::endl; class SerialTests : public ::testing::Test {
using serial::Serial; protected:
using serial::SerialExecption; virtual void SetUp() {
port1 = new Serial(SERIAL_PORT_NAME, 115200, 250);
}
virtual void TearDown() {
port1->close();
delete port1;
}
Serial * port1;
};
// TEST_F(SerialTests, throwsOnInvalidPort) {
//
// }
} // namespace
int main(int argc, char **argv) { int main(int argc, char **argv) {
try { try {
Serial s("/dev/tty.usbserial-A900adHq", 115200, 100); ::testing::InitGoogleTest(&argc, argv);
s.flush(); return RUN_ALL_TESTS();
long long count = 0; } catch (std::exception &e) {
while (1) { std::cerr << "Unhandled Exception: " << e.what() << std::endl;
// size_t available = s.available();
// cout << "avialable: " << available << endl;
string line = s.readline();
if (line.empty())
cout << "Nothing\n";
cout << count << ": " << line << line.length() << endl;
count++;
}
}
catch (SerialExecption e)
{
cout << "Caught SerialException: " << e.what() << endl;
}
catch (...)
{
cout << "Caught an error." << endl;
} }
return 1;
} }