commit 452e64cc
Create unit tests for all public functions/methods
Also reworked the old test file into the 'integration.cpp' file.
Note that this increases build times a bit since there're both more code & more units to build.
You can pass -DBUILD_TESTING=OFF when running Cmake not not build them, which saves time.
Changed files
diff --git a/.travis.yml b/.travis.yml
index 9f7a3f4..cad7ff4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -30,5 +30,9 @@ before_script:
script:
# Run cmake, then compile and run tests with make
- cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE .. && make -j$(nproc) && make test
+after_failure:
+ # Dumps any logs so you can read the stderr
+ - if [[ -e 'Testing/Temporary/LastTest.log' ]]; then cat 'Testing/Temporary/LastTest.log'; fi
+ - if [[ -e 'CMakeFiles/CMakeError.log' ]]; then cat 'CMakeFiles/CMakeError.log'; fi
notifications:
email: false
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7657313..6353f66 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -109,17 +109,5 @@ install(FILES
include(CTest)
# BUILD_TESTING is defined (default ON) in CTest
if(BUILD_TESTING)
- set(_PROJECT_TEST_NAME "${PROJECT_NAME}_test")
- add_executable(${_PROJECT_TEST_NAME} platform_folders.cpp)
-
- target_link_libraries(${_PROJECT_TEST_NAME} PRIVATE ${PROJECT_NAME})
-
- # Since tests aren't installed, no reason to give it an INSTALL_INTERFACE
- target_include_directories(${_PROJECT_TEST_NAME} PRIVATE
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/sago>
- )
-
- # Creates the "MyTest" test that runs the test executable created above
- # This is triggered by things like "make test"
- add_test(NAME MyTest COMMAND ${_PROJECT_TEST_NAME})
+ add_subdirectory(test)
endif()
diff --git a/appveyor.yml b/appveyor.yml
index 860cb79..c4866d6 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -69,6 +69,7 @@ test_script:
# global handlers #
#---------------------------------#
-# on build failure dump cmake err log
+# on build failure dump cmake and test err log
on_failure:
- cmd: IF exist "%APPVEYOR_BUILD_FOLDER%"\build\CMakeFiles\CMakeError.log (type "%APPVEYOR_BUILD_FOLDER%"\build\CMakeFiles\CMakeError.log)
+ - cmd: IF exist "%APPVEYOR_BUILD_FOLDER%"\build\Testing\Temporary\LastTest.log (type "%APPVEYOR_BUILD_FOLDER%"\build\Testing\Temporary\LastTest.log)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..ec0083a
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,40 @@
+# Create the tester lib
+add_library(platformfolders_internal_tester
+ "tester.cpp"
+)
+
+target_compile_features(platformfolders_internal_tester
+ PUBLIC cxx_std_11
+ PRIVATE cxx_range_for
+)
+
+target_include_directories(platformfolders_internal_tester PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+)
+
+# Easily define a new test to run
+macro(_def_test _name)
+ add_executable(${_name} "${_name}.cpp")
+ target_link_libraries(${_name} PRIVATE
+ platform_folders
+ platformfolders_internal_tester
+ )
+ target_compile_features(${_name} PRIVATE
+ cxx_std_11
+ )
+ add_test(NAME "${_name}" COMMAND "${_name}")
+endmacro()
+
+_def_test("appendAdditionalConfigDirectories")
+_def_test("appendAdditionalDataDirectories")
+_def_test("getCacheDir")
+_def_test("getConfigHome")
+_def_test("getDataHome")
+_def_test("getDesktopFolder")
+_def_test("getDocumentsFolder")
+_def_test("getDownloadFolder1")
+_def_test("getMusicFolder")
+_def_test("getPicturesFolder")
+_def_test("getSaveGamesFolder1")
+_def_test("getVideoFolder")
+_def_test("integration")
diff --git a/test/appendAdditionalConfigDirectories.cpp b/test/appendAdditionalConfigDirectories.cpp
new file mode 100644
index 0000000..d1e6c15
--- /dev/null
+++ b/test/appendAdditionalConfigDirectories.cpp
@@ -0,0 +1,11 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+#include <string>
+#include <vector>
+
+int main() {
+ std::vector<std::string> extraData;
+ sago::appendAdditionalConfigDirectories(extraData);
+ run_test(extraData);
+ return 0;
+}
diff --git a/test/appendAdditionalDataDirectories.cpp b/test/appendAdditionalDataDirectories.cpp
new file mode 100644
index 0000000..ab83d22
--- /dev/null
+++ b/test/appendAdditionalDataDirectories.cpp
@@ -0,0 +1,11 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+#include <string>
+#include <vector>
+
+int main() {
+ std::vector<std::string> extraData;
+ sago::appendAdditionalDataDirectories(extraData);
+ run_test(extraData);
+ return 0;
+}
diff --git a/test/getCacheDir.cpp b/test/getCacheDir.cpp
new file mode 100644
index 0000000..c7ae8d2
--- /dev/null
+++ b/test/getCacheDir.cpp
@@ -0,0 +1,7 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getCacheDir());
+ return 0;
+}
diff --git a/test/getConfigHome.cpp b/test/getConfigHome.cpp
new file mode 100644
index 0000000..4253257
--- /dev/null
+++ b/test/getConfigHome.cpp
@@ -0,0 +1,7 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getConfigHome());
+ return 0;
+}
diff --git a/test/getDataHome.cpp b/test/getDataHome.cpp
new file mode 100644
index 0000000..e7bd564
--- /dev/null
+++ b/test/getDataHome.cpp
@@ -0,0 +1,7 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getDataHome());
+ return 0;
+}
diff --git a/test/getDesktopFolder.cpp b/test/getDesktopFolder.cpp
new file mode 100644
index 0000000..1851d67
--- /dev/null
+++ b/test/getDesktopFolder.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getDesktopFolder());
+ sago::PlatformFolders p;
+ run_test(p.getDesktopFolder());
+ return 0;
+}
diff --git a/test/getDocumentsFolder.cpp b/test/getDocumentsFolder.cpp
new file mode 100644
index 0000000..02d4de8
--- /dev/null
+++ b/test/getDocumentsFolder.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getDocumentsFolder());
+ sago::PlatformFolders p;
+ run_test(p.getDocumentsFolder());
+ return 0;
+}
diff --git a/test/getDownloadFolder1.cpp b/test/getDownloadFolder1.cpp
new file mode 100644
index 0000000..1bc9042
--- /dev/null
+++ b/test/getDownloadFolder1.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getDownloadFolder1());
+ sago::PlatformFolders p;
+ run_test(p.getDownloadFolder1());
+ return 0;
+}
diff --git a/test/getMusicFolder.cpp b/test/getMusicFolder.cpp
new file mode 100644
index 0000000..946c353
--- /dev/null
+++ b/test/getMusicFolder.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getMusicFolder());
+ sago::PlatformFolders p;
+ run_test(p.getMusicFolder());
+ return 0;
+}
diff --git a/test/getPicturesFolder.cpp b/test/getPicturesFolder.cpp
new file mode 100644
index 0000000..b1e5c09
--- /dev/null
+++ b/test/getPicturesFolder.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getPicturesFolder());
+ sago::PlatformFolders p;
+ run_test(p.getPicturesFolder());
+ return 0;
+}
diff --git a/test/getSaveGamesFolder1.cpp b/test/getSaveGamesFolder1.cpp
new file mode 100644
index 0000000..4c68a6f
--- /dev/null
+++ b/test/getSaveGamesFolder1.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getSaveGamesFolder1());
+ sago::PlatformFolders p;
+ run_test(p.getSaveGamesFolder1());
+ return 0;
+}
diff --git a/test/getVideoFolder.cpp b/test/getVideoFolder.cpp
new file mode 100644
index 0000000..ec0be60
--- /dev/null
+++ b/test/getVideoFolder.cpp
@@ -0,0 +1,9 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getVideoFolder());
+ sago::PlatformFolders p;
+ run_test(p.getVideoFolder());
+ return 0;
+}
diff --git a/test/integration.cpp b/test/integration.cpp
new file mode 100644
index 0000000..74a456a
--- /dev/null
+++ b/test/integration.cpp
@@ -0,0 +1,34 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+#include <string>
+#include <vector>
+
+// This is all tests in one
+int main() {
+ // Test plain functions
+ run_test(sago::getConfigHome());
+ run_test(sago::getDataHome());
+ run_test(sago::getCacheDir());
+ // Test non-member functions
+ run_test(sago::getDesktopFolder());
+ run_test(sago::getDocumentsFolder());
+ run_test(sago::getDownloadFolder1());
+ run_test(sago::getPicturesFolder());
+ run_test(sago::getMusicFolder());
+ run_test(sago::getVideoFolder());
+ run_test(sago::getSaveGamesFolder1());
+ // Test class methods
+ sago::PlatformFolders p;
+ run_test(p.getDocumentsFolder());
+ run_test(p.getDesktopFolder());
+ run_test(p.getPicturesFolder());
+ run_test(p.getMusicFolder());
+ run_test(p.getVideoFolder());
+ run_test(p.getDownloadFolder1());
+ run_test(p.getSaveGamesFolder1());
+ // Test vector function
+ std::vector<std::string> extraData;
+ sago::appendAdditionalDataDirectories(extraData);
+ run_test(extraData);
+ return 0;
+}
diff --git a/test/tester.cpp b/test/tester.cpp
new file mode 100644
index 0000000..b90b4a9
--- /dev/null
+++ b/test/tester.cpp
@@ -0,0 +1,40 @@
+#include "tester.hpp"
+
+#include <cstdlib>
+#include <iostream>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+// This should be passed either be char* or std::string for this to work
+static void test_internal(const std::string& data) {
+ try {
+ // Check that it actually got anything
+ if (data.empty()) {
+ throw std::logic_error("Got empty data");
+ }
+ }
+ catch (const std::exception& e) {
+ // Take any standard exception & output its message
+ std::cerr << e.what() << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+ catch (...) {
+ // If any non-std exception is thrown, also fail
+ std::cerr << "Unknown exception!" << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+}
+
+// std::string is expected input
+void run_test(const std::string& input) {
+ test_internal(input);
+}
+
+// A special overload for the two funcs that take a vector
+void run_test(const std::vector<std::string>& vec) {
+ // Check each value for validity
+ for (const std::string& elem : vec) {
+ test_internal(elem);
+ }
+}
diff --git a/test/tester.hpp b/test/tester.hpp
new file mode 100644
index 0000000..65c8e0a
--- /dev/null
+++ b/test/tester.hpp
@@ -0,0 +1,13 @@
+#ifndef SAGO_TEST_HPP
+#define SAGO_TEST_HPP
+
+#include <string>
+#include <vector>
+
+// std::string is expected input
+void run_test(const std::string&);
+
+// A special overload for the two funcs that take a vector
+void run_test(const std::vector<std::string>&);
+
+#endif