Permalink
Browse files

Add command line unit tests

  • Loading branch information...
feragon committed Jun 9, 2016
1 parent b5c1f2e commit 3e5f6e023444d22d778bb3e7eaf0dc55db0cd83d
View
@@ -10,6 +10,7 @@ PROJECT (LC)
option(WITH_UNITTESTS "Build unit tests" ON)
option(WITH_LCDXFDWG "Build dxf/dwg support" ON)
option(WITH_QTUI "Build Qt graphical user interface" ON)
if(WITH_LCDXFDWG)
endif(WITH_LCDXFDWG)
@@ -21,7 +22,10 @@ add_definitions(-DUSE_lcDXFDWG=1)
ADD_SUBDIRECTORY("lcviewernoqt")
ADD_SUBDIRECTORY("lcviewerqt")
ADD_SUBDIRECTORY("lcadluascript")
ADD_SUBDIRECTORY("lcUI")
if(WITH_QTUI)
add_definitions(-DWITH_QTUI=${WITH_QTUI})
ADD_SUBDIRECTORY("lcUI")
endif(WITH_QTUI)
ADD_SUBDIRECTORY("lcUILua")
ADD_SUBDIRECTORY("luacmdinterface")
View
@@ -90,5 +90,23 @@ get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTO
# message(STATUS "dir='${dir}'")
#endforeach()
set(EXTRA_LIBS
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::UiTools
${LOG4CXX_LIBRARIES}
lckernel
lcluascript
lcviewerqt
lcviewernoqt
lcdxfdwg)
#Create library for unit tests
if(WITH_UNITTESTS)
add_library(lcui SHARED ${UI_srcs} ${UI_hdrs})
target_link_libraries(lcui ${EXTRA_LIBS})
endif(WITH_UNITTESTS)
add_executable(librecad ${UI_srcs} ${UI_hdrs} ${UI_HEADERS})
target_link_libraries(librecad Qt5::Core Qt5::Gui Qt5::Widgets Qt5::UiTools ${LOG4CXX_LIBRARIES} lckernel lcluascript lcviewerqt lcviewernoqt lcdxfdwg)
target_link_libraries(librecad ${EXTRA_LIBS})
@@ -155,4 +155,9 @@ void CliCommand::onKeyPressed(QKeyEvent *event) {
ui->command->event(event);
break;
}
}
}
void CliCommand::setText(QString text) {
ui->command->setText(text);
}
@@ -26,7 +26,7 @@ class CliCommand : public QDockWidget {
bool addCommand(std::string name);
void write(QString message);
void focus();
void setText(QString text);
public slots:
void onReturnPressed();
View
@@ -28,9 +28,22 @@ code.h
testmath.h
testmatrices.h
beziertest.h
testselection.h)
testselection.h
testintersect.h)
if(WITH_QTUI)
find_package(Qt5Widgets)
find_package(Qt5Test)
set(EXTRA_LIBS ${EXTRA_LIBS} lcui Qt5::Widgets Qt5::Test)
set(hdrs
${hdrs}
ui/testcommandline.h
ui/clicommandtest.h)
include_directories("${CMAKE_SOURCE_DIR}/lcUI")
endif(WITH_QTUI)
include_directories("${CMAKE_SOURCE_DIR}/lckernel")
include_directories("${CMAKE_SOURCE_DIR}/lcviewernoqt")
add_executable(lcunittest ${src} ${hdrs} testintersect.h)
target_link_libraries(lcunittest lckernel lcviewernoqt gtest ${CMAKE_THREAD_LIBS_INIT} ${LOG4CXX_LIBRARIES})
add_executable(lcunittest ${src} ${hdrs})
target_link_libraries(lcunittest lckernel lcviewernoqt gtest ${EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT} ${LOG4CXX_LIBRARIES})
View
@@ -8,6 +8,11 @@
#include "testmatrices.h"
#include "beziertest.h"
#include "testselection.h"
#ifdef WITH_QTUI
#include "ui/testcommandline.h"
#endif
using namespace std;
using namespace lc;
using namespace entity;
@@ -0,0 +1,19 @@
#pragma once
#include <widgets/clicommand.h>
class CliCommandTest : public CliCommand {
public:
CliCommandTest() : CliCommand() {}
/* @brief testCommand
* simulate a command
* @param QString
*/
void testCommand(QString command) {
auto enterEvent = QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
setText(command);
onKeyPressed(&enterEvent);
}
};
@@ -0,0 +1,121 @@
#pragma once
#include <QApplication>
#include <QSignalSpy>
#include <gtest/gtest.h>
#include "clicommandtest.h"
static int argc = 0;
static char** argv = NULL;
Q_DECLARE_METATYPE(lc::geo::Coordinate)
TEST(CommandLine, CommandTest) {
QApplication app(argc, argv);
CliCommandTest cliTest;
QSignalSpy commandSpy(&cliTest, SIGNAL(commandEntered(QString)));
EXPECT_TRUE(commandSpy.isValid());
QString c1 = "COMMAND";
QString c2 = "INVALID";
cliTest.addCommand(c1.toStdString());
cliTest.testCommand(c1);
cliTest.testCommand(c2);
EXPECT_EQ(1, commandSpy.count());
auto commandReceived = commandSpy.takeFirst().at(0).toString();
EXPECT_EQ(c1, commandReceived);
}
TEST(CommandLine, CoordinateTest) {
QApplication app(argc, argv);
CliCommandTest cliTest;
qRegisterMetaType<lc::geo::Coordinate>();
QSignalSpy coordinateSpy(&cliTest, SIGNAL(coordinateEntered(lc::geo::Coordinate)));
EXPECT_TRUE(coordinateSpy.isValid());
lc::geo::Coordinate c1(1.1, 1.2);
QString c1_Str = "1.1,1.2";
lc::geo::Coordinate c2(1.1, 1.2, 1.3);
QString c2_Str = "1.1;1.2;1.3";
cliTest.testCommand(c1_Str);
cliTest.testCommand(c2_Str);
EXPECT_EQ(2, coordinateSpy.count());
auto c1_Received = qvariant_cast<lc::geo::Coordinate>(coordinateSpy.at(0).at(0));
auto c2_Received = qvariant_cast<lc::geo::Coordinate>(coordinateSpy.at(1).at(0));
EXPECT_FLOAT_EQ(c1.x(), c1_Received.x());
EXPECT_FLOAT_EQ(c1.y(), c1_Received.y());
EXPECT_FLOAT_EQ(c1.z(), c1_Received.z());
EXPECT_FLOAT_EQ(c2.x(), c2_Received.x());
EXPECT_FLOAT_EQ(c2.y(), c2_Received.y());
EXPECT_FLOAT_EQ(c2.z(), c2_Received.z());
}
TEST(CommandLine, RelativeCoordinateTest) {
QApplication app(argc, argv);
CliCommandTest cliTest;
qRegisterMetaType<lc::geo::Coordinate>();
QSignalSpy coordinateSpy(&cliTest, SIGNAL(relativeCoordinateEntered(lc::geo::Coordinate)));
EXPECT_TRUE(coordinateSpy.isValid());
lc::geo::Coordinate c1(1.1, 1.2);
QString c1_Str = "@1.1,1.2";
lc::geo::Coordinate c2(1.1, 1.2, 1.3);
QString c2_Str = "@1.1;1.2;1.3";
cliTest.testCommand(c1_Str);
cliTest.testCommand(c2_Str);
EXPECT_EQ(2, coordinateSpy.count());
auto c1_Received = qvariant_cast<lc::geo::Coordinate>(coordinateSpy.at(0).at(0));
auto c2_Received = qvariant_cast<lc::geo::Coordinate>(coordinateSpy.at(1).at(0));
EXPECT_FLOAT_EQ(c1.x(), c1_Received.x());
EXPECT_FLOAT_EQ(c1.y(), c1_Received.y());
EXPECT_FLOAT_EQ(c1.z(), c1_Received.z());
EXPECT_FLOAT_EQ(c2.x(), c2_Received.x());
EXPECT_FLOAT_EQ(c2.y(), c2_Received.y());
EXPECT_FLOAT_EQ(c2.z(), c2_Received.z());
}
TEST(CommandLine, NumberTest) {
QApplication app(argc, argv);
CliCommandTest cliTest;
qRegisterMetaType<lc::geo::Coordinate>();
QSignalSpy numberSpy(&cliTest, SIGNAL(numberEntered(double)));
EXPECT_TRUE(numberSpy.isValid());
double n1 = 1;
QString n1_Str = "1";
double n2 = 1.234;
QString n2_Str = "1.234";
cliTest.testCommand(n1_Str);
cliTest.testCommand(n2_Str);
EXPECT_EQ(2, numberSpy.count());
auto n1_Received = numberSpy.at(0).at(0).toDouble();
auto n2_Received = numberSpy.at(1).at(0).toDouble();
EXPECT_FLOAT_EQ(n1, n1_Received);
EXPECT_FLOAT_EQ(n2, n2_Received);
}

0 comments on commit 3e5f6e0

Please sign in to comment.