diff --git a/.travis.yml b/.travis.yml index 0bb5d290..e806b468 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,19 +9,16 @@ before_install: - sudo apt-get install -qq libqt4-dev libsqlite3-dev libsqlcipher-dev script: - - mkdir tests/build - mkdir build - mkdir build_cipher - - cd tests/build - - cmake .. - - make - - ./sqlb-unittests - - cd ../../build - - cmake .. + - cd build + - cmake -DENABLE_TESTING=ON .. - make + - ctest -V - cd ../build_cipher - - cmake -Dsqlcipher=1 .. + - cmake -DENABLE_TESTING=ON -Dsqlcipher=1 .. - make + - ctest -V matrix: fast_finish: true diff --git a/BUILDING.md b/BUILDING.md index 3758193b..9a8fc564 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -172,46 +172,82 @@ qmake qmake CONFIG+=sqlcipher ## Building and running the Unit Tests -DB Browser for SQLite has unit tests in the "tests" subdirectory. +DB Browser for SQLite has unit tests in the "src/tests" subdirectory. ### Build the unit tests -Compile them like this: +The unit tests are enabled using the cmake variable `ENABLE_TESTING`; +it can be passed when running `cmake` to configure sqlitebrowser, +for example like this: -``` -$ cd tests +```bash $ mkdir build $ cd build -$ cmake .. +$ cmake -DENABLE_TESTING=ON .. $ make ``` ### Run the unit tests -Then run them like this: +Tests can be then run using `make test` or invoking `ctest` directly, +for example like this: ``` -$ ./sqlb-unittests -********* Start testing of TestTable ********* -Config: Using QTest library 4.8.6, Qt 4.8.6 -PASS : TestTable::initTestCase() -PASS : TestTable::sqlOutput() -PASS : TestTable::autoincrement() -PASS : TestTable::notnull() -PASS : TestTable::withoutRowid() -PASS : TestTable::parseSQL() -PASS : TestTable::parseSQLdefaultexpr() -PASS : TestTable::parseSQLMultiPk() -PASS : TestTable::parseSQLForeignKey() -PASS : TestTable::parseSQLSingleQuotes() -PASS : TestTable::parseSQLKeywordInIdentifier() -PASS : TestTable::parseSQLWithoutRowid() -PASS : TestTable::parseNonASCIIChars() -PASS : TestTable::createTableWithIn() -PASS : TestTable::createTableWithNotLikeConstraint() -PASS : TestTable::cleanupTestCase() -Totals: 16 passed, 0 failed, 0 skipped -********* Finished testing of TestTable ********* +$ ctest -V +UpdateCTestConfiguration from :SRCDIR/build/DartConfiguration.tcl +UpdateCTestConfiguration from :SRCDIR/build/DartConfiguration.tcl +Test project SRCDIR/build +Constructing a list of tests +Done constructing a list of tests +Checking test dependency graph... +Checking test dependency graph end +test 1 + Start 1: test-sqlobjects + +1: Test command: SRCDIR/build/src/tests/test-sqlobjects +1: Test timeout computed to be: 9.99988e+06 +1: ********* Start testing of TestTable ********* +1: Config: Using QTest library 4.8.6, Qt 4.8.6 +1: PASS : TestTable::initTestCase() +1: PASS : TestTable::sqlOutput() +1: PASS : TestTable::autoincrement() +1: PASS : TestTable::notnull() +1: PASS : TestTable::withoutRowid() +1: PASS : TestTable::foreignKeys() +1: PASS : TestTable::parseSQL() +1: PASS : TestTable::parseSQLdefaultexpr() +1: PASS : TestTable::parseSQLMultiPk() +1: PASS : TestTable::parseSQLForeignKey() +1: PASS : TestTable::parseSQLSingleQuotes() +1: PASS : TestTable::parseSQLKeywordInIdentifier() +1: PASS : TestTable::parseSQLWithoutRowid() +1: PASS : TestTable::parseNonASCIIChars() +1: PASS : TestTable::parseSQLEscapedQuotes() +1: PASS : TestTable::parseSQLForeignKeys() +1: PASS : TestTable::parseSQLCheckConstraint() +1: PASS : TestTable::createTableWithIn() +1: PASS : TestTable::createTableWithNotLikeConstraint() +1: PASS : TestTable::cleanupTestCase() +1: Totals: 20 passed, 0 failed, 0 skipped +1: ********* Finished testing of TestTable ********* +1/2 Test #1: test-sqlobjects .................. Passed 0.02 sec +test 2 + Start 2: test-import + +2: Test command: SRCDIR/build/src/tests/test-import +2: Test timeout computed to be: 9.99988e+06 +2: ********* Start testing of TestImport ********* +2: Config: Using QTest library 4.8.6, Qt 4.8.6 +2: PASS : TestImport::initTestCase() +2: PASS : TestImport::csvImport() +2: PASS : TestImport::cleanupTestCase() +2: Totals: 3 passed, 0 failed, 0 skipped +2: ********* Finished testing of TestImport ********* +2/2 Test #2: test-import ...................... Passed 0.01 sec + +100% tests passed, 0 tests failed out of 2 + +Total Test time (real) = 0.04 sec ``` Everything should PASS, with no failures, and nothing skipped. diff --git a/CMakeLists.txt b/CMakeLists.txt index f8899c18..4a1185e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ project(sqlitebrowser) cmake_minimum_required(VERSION 2.8.7) OPTION(USE_QT5 FALSE "Build with qt5") +OPTION(ENABLE_TESTING FALSE "Enable the unit tests") if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release") @@ -42,6 +43,10 @@ else() add_definitions(${QT_DEFINITIONS}) endif() +if(ENABLE_TESTING) + enable_testing() +endif() + set(SQLB_HDR src/gen_version.h src/sqlitetypes.h @@ -260,6 +265,11 @@ install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib) + +if(ENABLE_TESTING) + add_subdirectory(src/tests) +endif() + if(UNIX AND NOT APPLE) install(FILES src/icons/${PROJECT_NAME}.png DESTINATION share/icons/hicolor/256x256/apps/) diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt new file mode 100644 index 00000000..ebe2203e --- /dev/null +++ b/src/tests/CMakeLists.txt @@ -0,0 +1,89 @@ +if(USE_QT5) +else() + find_package(Qt4 COMPONENTS QtTest REQUIRED) + include("${QT_USE_FILE}") + add_definitions(${QT_DEFINITIONS}) +endif() + +include_directories("${CMAKE_CURRENT_BINARY_DIR}" "${ANTLR_DIR}" ..) + +# test-sqlobjects + +set(TESTSQLOBJECTS_SRC + ../sqlitedb.cpp + ../sqlitetablemodel.cpp + ../sqlitetypes.cpp + ../csvparser.cpp + ../grammar/Sqlite3Lexer.cpp + ../grammar/Sqlite3Parser.cpp + ../PreferencesDialog.cpp + testsqlobjects.cpp + ../FileDialog.cpp +) + +set(TESTSQLOBJECTS_HDR + ../grammar/sqlite3TokenTypes.hpp + ../grammar/Sqlite3Lexer.hpp + ../grammar/Sqlite3Parser.hpp + ../sqlitetypes.h) + +set(TESTSQLOBJECTS_FORMS + ../PreferencesDialog.ui) + +set(TESTSQLOBJECTS_MOC_HDR + ../sqlitedb.h + ../sqlitetablemodel.h + ../PreferencesDialog.h + testsqlobjects.h + ../FileDialog.h +) + +if(sqlcipher) + list(APPEND TESTSQLOBJECTS_SRC ../CipherDialog.cpp) + list(APPEND TESTSQLOBJECTS_FORMS ../CipherDialog.ui) + list(APPEND TESTSQLOBJECTS_MOC_HDR ../CipherDialog.h) +endif() + +if(USE_QT5) + QT5_WRAP_UI(TESTSQLOBJECTS_FORM_HDR ${TESTSQLOBJECTS_FORMS}) +else() + QT4_WRAP_CPP(TESTSQLOBJECTS_MOC ${TESTSQLOBJECTS_MOC_HDR}) + QT4_WRAP_UI(TESTSQLOBJECTS_FORM_HDR ${TESTSQLOBJECTS_FORMS}) +endif() + +add_executable(test-sqlobjects ${TESTSQLOBJECTS_MOC} ${TESTSQLOBJECTS_HDR} ${TESTSQLOBJECTS_SRC} ${TESTSQLOBJECTS_FORM_HDR}) + +if(USE_QT5) + qt5_use_modules(test-sqlobjects Test Widgets Gui) + set(QT_LIBRARIES "") +endif() + +add_dependencies(test-sqlobjects antlr) +target_link_libraries(test-sqlobjects antlr ${QT_LIBRARIES} ${LIBSQLITE}) +add_test(test-sqlobjects test-sqlobjects) + +# test-import + +set(TESTIMPORT_SRC + ../csvparser.cpp + TestImport.cpp +) + +set(TESTIMPORT_MOC_HDR + TestImport.h +) + +if(USE_QT5) +else() + QT4_WRAP_CPP(TESTIMPORT_MOC ${TESTIMPORT_MOC_HDR}) +endif() + +add_executable(test-import ${TESTIMPORT_MOC} ${TESTIMPORT_SRC}) + +if(USE_QT5) + qt5_use_modules(test-import Test Core) + set(QT_LIBRARIES "") +endif() + +target_link_libraries(test-import ${QT_LIBRARIES}) +add_test(test-import test-import) diff --git a/src/tests/TestImport.cpp b/src/tests/TestImport.cpp index e6f3f8f9..6e53bc19 100644 --- a/src/tests/TestImport.cpp +++ b/src/tests/TestImport.cpp @@ -1,3 +1,5 @@ +// force QtCore-only main application by QTEST_MAIN +#undef QT_GUI_LIB #include #include #include @@ -5,25 +7,17 @@ #include "csvparser.h" #include "TestImport.h" -#include "../sqlitedb.h" + +QTEST_MAIN(TestImport) Q_DECLARE_METATYPE(CSVParser::TCSVResult) TestImport::TestImport() { - // Init basic application - // The app needs to be initialized for the utf8 test - // to work - argcount = 1; - args[0] = new char[20]; - strcpy(args[0], "sqlb-unittests"); - app = new QCoreApplication(argcount, args); } TestImport::~TestImport() { - delete[] args[0]; - delete app; } void TestImport::csvImport() @@ -46,9 +40,6 @@ void TestImport::csvImport() } file.flush(); - // Call decodeCSV function - DBBrowserDB db; - CSVParser csvparser(true, separator, quote); file.seek(0); QTextStream tstream(&file); diff --git a/src/tests/TestImport.h b/src/tests/TestImport.h index e9edb3f9..18de5619 100644 --- a/src/tests/TestImport.h +++ b/src/tests/TestImport.h @@ -2,7 +2,6 @@ #define TESTIMPORT_H #include -#include class TestImport : public QObject { @@ -12,11 +11,6 @@ public: TestImport(); ~TestImport(); -private: - int argcount; - char *args[1]; // the size must match what 'argcount' is set to - QCoreApplication* app; - private slots: void csvImport(); void csvImport_data(); diff --git a/src/tests/TestMain.cpp b/src/tests/TestMain.cpp deleted file mode 100644 index f26741b6..00000000 --- a/src/tests/TestMain.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include "tests/testsqlobjects.h" -#include "tests/TestImport.h" - -int main(int argc, char** argv) -{ - int status = 0; - { - TestTable tc; - status |= QTest::qExec(&tc, argc, argv); - } - { - TestImport tc; - status |= QTest::qExec(&tc, argc, argv); - } - return status; -} diff --git a/src/tests/testsqlobjects.cpp b/src/tests/testsqlobjects.cpp index b1e83b37..63ecaba2 100644 --- a/src/tests/testsqlobjects.cpp +++ b/src/tests/testsqlobjects.cpp @@ -3,6 +3,8 @@ #include +QTEST_APPLESS_MAIN(TestTable) + using namespace sqlb; void TestTable::sqlOutput() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index ea5e52eb..00000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,84 +0,0 @@ -project(sqlb-unittests) -cmake_minimum_required(VERSION 2.8.7) - -OPTION(USE_QT5 FALSE "Build with qt5") - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE "Release") -endif() - -set(ANTLR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../libs/antlr-2.7.7") -add_subdirectory("${ANTLR_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/antlr") - -if(USE_QT5) - find_package(Qt5Widgets REQUIRED) - set(CMAKE_AUTOMOC ON) - set(CMAKE_INCLUDE_CURRENT_DIR ON) -else() - find_package(Qt4 COMPONENTS QtCore QtTest QtGui REQUIRED) - include("${QT_USE_FILE}") - add_definitions(${QT_DEFINITIONS}) -endif() - -# add extra library path for MacOS -if(APPLE) - find_library(LIBSQLITE sqlite3 HINTS /usr/local/lib /usr/local/opt/sqlite/lib) - set(ADDITIONAL_INCLUDE_PATHS /usr/local/include /usr/local/opt/sqlite/include) -else(APPLE) - find_library(LIBSQLITE sqlite3) -endif(APPLE) - -set(SQLB_SRC - ../src/sqlitedb.cpp - ../src/sqlitetablemodel.cpp - ../src/sqlitetypes.cpp - ../src/csvparser.cpp - ../src/grammar/Sqlite3Lexer.cpp - ../src/grammar/Sqlite3Parser.cpp - ../src/PreferencesDialog.cpp - ../src/tests/TestImport.cpp - ../src/tests/testsqlobjects.cpp - ../src/tests/TestMain.cpp - ../src/FileDialog.cpp -) - -set(SQLB_HDR - ../src/grammar/sqlite3TokenTypes.hpp - ../src/grammar/Sqlite3Lexer.hpp - ../src/grammar/Sqlite3Parser.hpp - ../src/csvparser.h - ../src/sqlitetypes.h) - -set(SQLB_FORMS - ../src/PreferencesDialog.ui) - -set(SQLB_MOC_HDR - ../src/sqlitedb.h - ../src/sqlitetablemodel.h - ../src/PreferencesDialog.h - ../src/tests/TestImport.h - ../src/tests/testsqlobjects.h - ../src/FileDialog.h -) - -if(USE_QT5) - QT5_WRAP_CPP(SQLB_MOC ${SQLB_MOC_HDR}) - QT5_WRAP_UI(SQLB_FORM_HDR ${SQLB_FORMS}) -else() - QT4_WRAP_CPP(SQLB_MOC ${SQLB_MOC_HDR}) - QT4_WRAP_UI(SQLB_FORM_HDR ${SQLB_FORMS}) -endif() - -include_directories("${CMAKE_CURRENT_BINARY_DIR}" "${ANTLR_DIR}" ../src) - -add_executable(${PROJECT_NAME} ${SQLB_MOC} ${SQLB_HDR} ${SQLB_SRC} ${SQLB_FORM_HDR}) - -if(USE_QT5) - qt5_use_modules(${PROJECT_NAME} Test Widgets Gui) - set(QT_LIBRARIES "") -endif() - -add_dependencies(${PROJECT_NAME} antlr) -target_link_libraries(${PROJECT_NAME} antlr ${QT_LIBRARIES} ${LIBSQLITE}) - -MESSAGE(STATUS "Build type is ${CMAKE_BUILD_TYPE}")