fix(tests): Run unit tests on travis

This commit is contained in:
Michael Carlberg 2016-11-19 08:30:00 +01:00
parent bf23086687
commit b3df50082a
4 changed files with 41 additions and 28 deletions

View File

@ -97,9 +97,21 @@ before_script:
- cmake --version - cmake --version
- mkdir -p "${TRAVIS_BUILD_DIR}/build" - mkdir -p "${TRAVIS_BUILD_DIR}/build"
- cd "${TRAVIS_BUILD_DIR}/build" - cd "${TRAVIS_BUILD_DIR}/build"
- cmake -DCMAKE_C_COMPILER="${CC}" -DCMAKE_CXX_COMPILER="${CXX}" -DCMAKE_CXX_FLAGS="${CXXFLAGS}" -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" .. - cmake -DCMAKE_C_COMPILER="${CC}" -DCMAKE_CXX_COMPILER="${CXX}" -DCMAKE_CXX_FLAGS="${CXXFLAGS}" -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" -DBUILD_TESTS:BOOL=ON ..
script: make script:
- make
- |
for test in tests/unit_test.*; do
if [ -x $test ]; then
echo -n "${test##*/} ";
if $test; then
echo -e "\033[1;32mpassed\033[0m";
else
echo -e "\033[1;31mfailed\033[0m";
fi
fi
done
notifications: notifications:
irc: irc:

View File

@ -9,6 +9,7 @@ include_directories(${APP_INCLUDE_DIRS})
function(unit_test file) function(unit_test file)
string(REPLACE "/" "_" testname ${file}) string(REPLACE "/" "_" testname ${file})
add_executable(unit_test.${testname} ${CMAKE_CURRENT_LIST_DIR}/unit_tests/${file}.cpp) add_executable(unit_test.${testname} ${CMAKE_CURRENT_LIST_DIR}/unit_tests/${file}.cpp)
target_link_libraries(unit_test.${testname} ${APP_LIBRARIES})
add_test(unit_test.${testname} unit_test.${testname}) add_test(unit_test.${testname} unit_test.${testname})
endfunction() endfunction()

View File

@ -3,7 +3,6 @@
int main() { int main() {
using namespace polybar; using namespace polybar;
using cli_parser = command_line::parser;
// clang-format off // clang-format off
const command_line::options opts{ const command_line::options opts{
@ -13,60 +12,60 @@ int main() {
// clang-format on // clang-format on
"has_short"_test = [&opts] { "has_short"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-f", ' ')); cli.process_input(string_util::split("-f", ' '));
expect(cli.has("flag")); expect(cli.has("flag"));
expect(!cli.has("option")); expect(!cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-f -o foo", ' ')); cli.process_input(string_util::split("-f -o foo", ' '));
expect(cli.has("flag")); expect(cli.has("flag"));
expect(cli.has("option")); expect(cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-o baz", ' ')); cli.process_input(string_util::split("-o baz", ' '));
expect(!cli.has("flag")); expect(!cli.has("flag"));
expect(cli.has("option")); expect(cli.has("option"));
}; };
"has_long"_test = [&opts] { "has_long"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--flag", ' ')); cli.process_input(string_util::split("--flag", ' '));
expect(cli.has("flag")); expect(cli.has("flag"));
expect(!cli.has("option")); expect(!cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--flag --option=foo", ' ')); cli.process_input(string_util::split("--flag --option=foo", ' '));
expect(cli.has("flag")); expect(cli.has("flag"));
expect(cli.has("option")); expect(cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=foo --flag", ' ')); cli.process_input(string_util::split("--option=foo --flag", ' '));
expect(cli.has("flag")); expect(cli.has("flag"));
expect(cli.has("option")); expect(cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=baz", ' ')); cli.process_input(string_util::split("--option=baz", ' '));
expect(!cli.has("flag")); expect(!cli.has("flag"));
expect(cli.has("option")); expect(cli.has("option"));
}; };
"compare"_test = [&opts] { "compare"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("-o baz", ' ')); cli.process_input(string_util::split("-o baz", ' '));
expect(cli.compare("option", "baz")); expect(cli.compare("option", "baz"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=foo", ' ')); cli.process_input(string_util::split("--option=foo", ' '));
expect(cli.compare("option", "foo")); expect(cli.compare("option", "foo"));
}; };
"get"_test = [&opts] { "get"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=baz", ' ')); cli.process_input(string_util::split("--option=baz", ' '));
expect("baz" == cli.get("option")); expect("baz" == cli.get("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(string_util::split("--option=foo", ' ')); cli.process_input(string_util::split("--option=foo", ' '));
expect("foo" == cli.get("option")); expect("foo" == cli.get("option"));
}; };
@ -78,7 +77,7 @@ int main() {
bool exception_thrown = false; bool exception_thrown = false;
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input1); cli.process_input(input1);
} catch (const command_line::value_error&) { } catch (const command_line::value_error&) {
exception_thrown = true; exception_thrown = true;
@ -88,7 +87,7 @@ int main() {
exception_thrown = false; // reset exception_thrown = false; // reset
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input2); cli.process_input(input2);
} catch (const command_line::value_error&) { } catch (const command_line::value_error&) {
exception_thrown = true; exception_thrown = true;
@ -98,7 +97,7 @@ int main() {
exception_thrown = false; // reset exception_thrown = false; // reset
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input3); cli.process_input(input3);
} catch (const command_line::value_error&) { } catch (const command_line::value_error&) {
exception_thrown = true; exception_thrown = true;
@ -113,7 +112,7 @@ int main() {
bool exception_thrown = false; bool exception_thrown = false;
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input1); cli.process_input(input1);
} catch (const command_line::value_error&) { } catch (const command_line::value_error&) {
exception_thrown = true; exception_thrown = true;
@ -123,7 +122,7 @@ int main() {
exception_thrown = false; // reset exception_thrown = false; // reset
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input2); cli.process_input(input2);
} catch (const command_line::value_error&) { } catch (const command_line::value_error&) {
exception_thrown = true; exception_thrown = true;
@ -138,7 +137,7 @@ int main() {
bool exception_thrown = false; bool exception_thrown = false;
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input1); cli.process_input(input1);
} catch (const command_line::argument_error&) { } catch (const command_line::argument_error&) {
exception_thrown = true; exception_thrown = true;
@ -148,7 +147,7 @@ int main() {
exception_thrown = false; // reset exception_thrown = false; // reset
try { try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>(); auto cli = configure_cliparser<cliparser>("cmd", opts).create<cliparser>();
cli.process_input(input2); cli.process_input(input2);
} catch (const command_line::argument_error&) { } catch (const command_line::argument_error&) {
exception_thrown = true; exception_thrown = true;

View File

@ -3,6 +3,7 @@
#include "components/logger.hpp" #include "components/logger.hpp"
#include "utils/inotify.hpp" #include "utils/inotify.hpp"
#include "utils/string.hpp"
#define CONFIGURE_ARGS(T, V, Args) configure_##T<decltype(V)>(Args).create<decltype(V)>() #define CONFIGURE_ARGS(T, V, Args) configure_##T<decltype(V)>(Args).create<decltype(V)>()
#define CONFIGURE(T, V) configure_##T<decltype(V)>().create<decltype(V)>() #define CONFIGURE(T, V) configure_##T<decltype(V)>().create<decltype(V)>()
@ -18,9 +19,9 @@ int main() {
boost::shared_ptr<logger> instance4{CONFIGURE(logger, instance4)}; boost::shared_ptr<logger> instance4{CONFIGURE(logger, instance4)};
// clang-format on // clang-format on
string mem_addr1{string_util::from_stream(std::stringstream() << &instance1)}; string mem_addr1{string_util::from_stream(stringstream() << &instance1)};
string mem_addr2{string_util::from_stream(std::stringstream() << &instance2)}; string mem_addr2{string_util::from_stream(stringstream() << &instance2)};
string mem_addr3{string_util::from_stream(std::stringstream() << instance3.get())}; string mem_addr3{string_util::from_stream(stringstream() << instance3.get())};
expect(mem_addr1 == mem_addr2); expect(mem_addr1 == mem_addr2);
expect(mem_addr2 == mem_addr3); expect(mem_addr2 == mem_addr3);
@ -28,10 +29,10 @@ int main() {
}; };
"unique"_test = [] { "unique"_test = [] {
unique_ptr<inotify_watch> instance1{inotify_util::make_watch("A")}; inotify_util::watch_t instance1{inotify_util::make_watch("A")};
unique_ptr<inotify_watch> instance2{inotify_util::make_watch("B")}; inotify_util::watch_t instance2{inotify_util::make_watch("B")};
shared_ptr<inotify_watch> instance3{inotify_util::make_watch("B")}; inotify_util::watch_t instance3{inotify_util::make_watch("B")};
shared_ptr<inotify_watch> instance4{inotify_util::make_watch("B")}; inotify_util::watch_t instance4{inotify_util::make_watch("B")};
expect(instance1.get() != instance2.get()); expect(instance1.get() != instance2.get());
expect(instance2.get() != instance3.get()); expect(instance2.get() != instance3.get());