diff --git a/.gitignore b/.gitignore index e57294f..7006337 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /Makefile src/config.h src/procfetch +src/test src/Makefile ascii/Makefile debian/control diff --git a/src/Makefile.in b/src/Makefile.in index 01c4bbe..704e608 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -1,5 +1,5 @@ TARGET = procfetch -SRCS = fetch.cpp main.cpp util.cpp +SRCS = fetch.cpp main.cpp OBJS = $(SRCS:.cpp=.o) CXX = @CXX@ @@ -7,6 +7,11 @@ CXXFLAGS = -std=c++17 -Wall -Wextra --pedantic-errors @CXXFLAGS@ LIBS = @LIBS@ LDFLAGS = -pthread +TEST_TARGET = test +TEST_SRCS = test.cpp fetch.cpp +TEST_OBJS = $(TEST_SRCS:.cpp=.o) +TEST_LDFLAGS = $(LDFLAGS) -no-pie + INSTALL = /usr/bin/install -c -D FORMATTER = clang-format -i BIN_DIR = @BIN_DIR@ @@ -14,28 +19,27 @@ BIN_DIR = @BIN_DIR@ all: $(TARGET) run: all ./$(TARGET) -check: all - @if test -f not_existence; then \ - echo "Error: It is assumed that file 'not_existence' DOES NOT exist." ;\ - false ;\ - fi - ./$(TARGET) -t +build-test: $(TEST_TARGET) +check: build-test + ./$(TEST_TARGET) gcov: - gcov $(SRCS) + gcov $(TEST_TARGET) clean: - - rm -f $(TARGET) $(OBJS) *.gcov *.gcda *.gcno + - rm -f $(TARGET) $(OBJS) $(TEST_TARGET) $(TEST_OBJS) *.gcov *.gcda *.gcno install: all @echo "Installing $(BIN_DIR)/$(TARGET) ..." $(INSTALL) $(TARGET) "$(BIN_DIR)/$(TARGET)" uninstall: - rm "$(BIN_DIR)/$(TARGET)" format: - $(FORMATTER) $(SRCS) *.h + $(FORMATTER) $(SRCS) $(TEST_TARGET) *.h $(TARGET): $(OBJS) $(CXX) -o $@ $(OBJS) $(LIBS) $(LDFLAGS) +$(TEST_TARGET): $(TEST_OBJS) + $(CXX) -o $@ $(TEST_OBJS) $(LIBS) $(TEST_LDFLAGS) main.o: fetch.h color.h config.h fetch.o: fetch.h color.h -util.o: fetch.h color.h +test.o: fetch.h color.h -.PHONY: all run check gcov clean docs install uninstall dist format gif +.PHONY: all run check gcov clean docs install uninstall dist format gif build-test diff --git a/src/fetch.cpp b/src/fetch.cpp index 4923bfc..c0df49e 100644 --- a/src/fetch.cpp +++ b/src/fetch.cpp @@ -20,8 +20,7 @@ string getuser() auto *p = getpwuid(getuid()); if (p == NULL) { - throw runtime_error( - "Could not get struct passwd: "s + strerror(errno)); + throw runtime_error("Could not get struct passwd: "s + strerror(errno)); } return p->pw_name; @@ -645,13 +644,3 @@ void print(string color_name, string distro_name) return; } - -void test_getuser() -{ - expect(string(getenv("USER")), getuser(), "getuser"s); -} - -void test_fetch() -{ - test_getuser(); -} diff --git a/src/fetch.h b/src/fetch.h index ecc634c..0406353 100644 --- a/src/fetch.h +++ b/src/fetch.h @@ -67,41 +67,8 @@ void printBattery(string path); void print(string color, string distro_name); -void test_fetch(); - -// util.cpp - string getColor(string); -void test_util(); - -/** - * Tests that want and got are equal. - * Fails with the supplied failure message, file and line then halt. - * @param want - * @param got - * @param msg - * @param file - * @param line - */ -template -void expect1(const T &want, const T &got, const string &msg, const char *file, - int line) -{ - if (want == got) - return; - - if (msg.length() != 0) - cout << file << ":"s << line << ": Error: "s << msg << " want ("s - << want << "), but got ("s << got << ")"s << endl; - else - cout << file << ":"s << line << ": Error: want ("s << want - << "), but got ("s << got << ")"s << endl; - - exit(1); -} -#define expect(want, got, msg) expect1(want, got, msg, __FILE__, __LINE__) - /** * A Path represents a path. */ @@ -430,7 +397,6 @@ class Context enum class Mode { NORMAL, - EXEC_TEST, SHOW_VERSION, }; @@ -446,14 +412,11 @@ class Options Options(int argc, char *argv[]) { int opt; - while ((opt = getopt(argc, argv, "ta:d:vb")) != -1) + while ((opt = getopt(argc, argv, "a:d:vb")) != -1) { switch (opt) { - case 't': - mode = Mode::EXEC_TEST; - break; case 'a': color_name = string(optarg); break; diff --git a/src/main.cpp b/src/main.cpp index 879f11b..4e89f71 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,10 +26,6 @@ int main(int argc, char *argv[]) case Mode::NORMAL: // no-op break; - case Mode::EXEC_TEST: - test_suite(); - cout << Crayon{}.green().text("All unit tests passed."s) << endl; - return 0; case Mode::SHOW_VERSION: cout << VERSION << endl; return 0; @@ -101,9 +97,3 @@ void DisplayInfo(bool show_battery) cout << endl; } - -void test_suite() -{ - test_fetch(); - test_util(); -} diff --git a/src/util.cpp b/src/test.cpp similarity index 80% rename from src/util.cpp rename to src/test.cpp index 5d1618d..e96cd25 100644 --- a/src/util.cpp +++ b/src/test.cpp @@ -1,8 +1,60 @@ -/** +/* * @file */ +#include "color.h" #include "fetch.h" +/** + * Tests that want and got are equal. + * Fails with the supplied failure message, file and line then halt. + * @param want + * @param got + * @param msg + * @param file + * @param line + */ +template +void expect1(const T &want, const T &got, const string &msg, const char *file, + int line) +{ + if (want == got) + return; + + if (msg.length() != 0) + cout << file << ":"s << line << ": Error: "s << msg << " want ("s + << want << "), but got ("s << got << ")"s << endl; + else + cout << file << ":"s << line << ": Error: want ("s << want + << "), but got ("s << got << ")"s << endl; + + exit(1); +} +#define expect(want, got, msg) expect1(want, got, msg, __FILE__, __LINE__) + +/* + * .---------------------------------------------. + * | FETCH TESTS | + * '---------------------------------------------' + */ +static void test_getuser() +{ + expect(string(getenv("USER")), getuser(), "getuser"s); +} + +/* + * Test fetch functions + */ +static void test_fetch() +{ + test_getuser(); +} + +/* + * .---------------------------------------------. + * | UTILS TESTS | + * '---------------------------------------------' + */ + static void test_Command() { auto c = Command::exec("ls Makefile"s); @@ -176,20 +228,6 @@ static void test_Options_full() testhelper_Options("full", argc, argv, expect, 6); // remains last "arg" } -static void test_Options_test() -{ - int argc = 2; - const char *argv[] = {"procfetch", "-t", NULL}; - - Options expect; - expect.mode = Mode::EXEC_TEST; - expect.color_name = "def"s; - expect.distro_name = "def"s; - expect.show_battery = false; - - testhelper_Options("test", argc, argv, expect, 2); -} - static void test_Options_version() { int argc = 2; @@ -208,7 +246,6 @@ static void test_Options() { test_Options_default(); test_Options_full(); - test_Options_test(); test_Options_version(); } @@ -217,7 +254,7 @@ static void test_Options() * * class Path * * class Command */ -void test_util() +static void test_util() { test_Path(); test_Command(); @@ -228,3 +265,13 @@ void test_util() test_Crayon(); test_Options(); } + +int main() +{ + test_fetch(); + test_util(); + + cout << Crayon{}.green().text("All unit tests passed."s) << endl; + + return 0; +}