From 2a44f23de21dac7ee06d2feb9b45ee50387c784e Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 4 Dec 2021 23:42:03 +0500 Subject: [PATCH] Add tests --- .gitignore | 2 + Makefile | 36 ++++++++++++----- src/drw.c | 2 +- src/dwm.c | 1 + src/logger.c | 93 +++++++++++++++++++++++++++++++++++++++++++ src/logger.h | 17 ++++++++ src/main.c | 93 +------------------------------------------ src/main.h | 10 ----- src/util.c | 2 +- src/xbase.c | 2 +- tests/geom_position.c | 3 ++ tests/geom_sizes.c | 3 ++ tests/main.c | 19 +++++++++ 13 files changed, 168 insertions(+), 115 deletions(-) create mode 100644 src/logger.c create mode 100644 src/logger.h create mode 100644 tests/geom_position.c create mode 100644 tests/geom_sizes.c create mode 100644 tests/main.c diff --git a/.gitignore b/.gitignore index 9a1cf70..05471f5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /config/2-conditionals.mk /polytreewm /src/*.o +/tests/*.o +/tests/*.test diff --git a/Makefile b/Makefile index d5e560b..006e01d 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ MODULES_SRC = \ src/geom.c \ src/helpers.c \ src/layouts.c \ + src/logger.c \ src/settings.c \ src/spawn.c \ src/state.c \ @@ -34,25 +35,37 @@ DWM_SRC = \ src/dwm/layouts.c \ src/dwm/xerror.c +TEST_SRC = \ + tests/geom_position.c \ + tests/geom_sizes.c + +MAIN_SRC = $(MODULES_SRC) src/main.c + MODULES_HDR = $(MODULES_SRC:.c=.h) -DWM_HDR = $(DWM_SRC:.c=.h) +DWM_HDR = $(DWM_SRC:.c=.h) +MAIN_HDR = $(MODULES_HDR) src/main.h src/config.def.h -SRC = $(MODULES_SRC) src/main.c -HDR = $(MODULES_HDR) src/main.h src/config.def.h +MODULES_OBJ = $(MODULES_SRC:.c=.o) +TEST_OBJ = $(TEST_SRC:.c=.o) tests/main.o +MAIN_OBJ = src/main.o +ALL_OBJ = $(MODULES_OBJ) $(TEST_OBJ) $(MAIN_OBJ) -OBJ = $(SRC:.c=.o) +TEST_EXE = $(TEST_SRC:.c=.test) +ALL_EXE = polytreewm $(TEST_EXE) -polytreewm: $(OBJ) - $(CC) -o $@ $(OBJ) $(LDFLAGS) +polytreewm: $(MAIN_OBJ) $(MODULES_OBJ) + $(CC) -o $@ $^ $(LDFLAGS) -%.o: %.c +%.o: %.c $(CONFIGMKS) $(HDR) $(CC) -c $< -o $@ $(CFLAGS) +%.test: %.o $(MODULES_OBJ) tests/main.o + $(CC) -o $@ $^ $(LDFLAGS) + dwm.o: $(DWM_SRC) $(DWM_HDR) -$(OBJ): $(CONFIGMKS) $(HDR) clean: - rm -f polytreewm $(OBJ) + rm -f $(ALL_OBJ) $(ALL_EXE) distclean: clean rm -f $(CONFIGMKS_TO_REMOVE) @@ -71,4 +84,7 @@ uninstall: $(DESTDIR)$(BINDIR)/polytreewm \ $(DESTDIR)$(MANDIR)/man1/polytreewm.1 -.PHONY: all clean distclean install uninstall +test: $(TEST_EXE) + @echo "$(TEST_EXE)" | awk '{ OFS="\n"; $$1=$$1 } 1' | sh + +.PHONY: all clean distclean install uninstall test diff --git a/src/drw.c b/src/drw.c index 8c84f76..f8aaa1c 100644 --- a/src/drw.c +++ b/src/drw.c @@ -1,4 +1,4 @@ -#include "main.h" +#include "logger.h" #include "util.h" #include diff --git a/src/dwm.c b/src/dwm.c index 021099c..f0bd6d7 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -2,6 +2,7 @@ #include "helpers.h" #include "layouts.h" +#include "logger.h" #include "main.h" #include "settings.h" #include "spawn.h" diff --git a/src/logger.c b/src/logger.c new file mode 100644 index 0000000..d311a2c --- /dev/null +++ b/src/logger.c @@ -0,0 +1,93 @@ +#include "logger.h" + +#include +#include +#include + +static void logger(const char *level, const char *fmt, ...); +static void logger_perror(const char *level, const char *fmt, ...); + +void logger(const char *const level, const char *const fmt, ...) +{ + fprintf(stderr, PROGRAM_NAME": %s: ", level); + + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fputc('\n', stderr); +} + +void logger_perror(const char *const level, const char *const fmt, ...) +{ + fprintf(stderr, PROGRAM_NAME": %s: ", level); + + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fprintf(stderr, ": "); + perror(NULL); +} + +void fatal(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("ERROR", fmt, ap); + va_end(ap); + + exit(EXIT_FAILURE); +} + +void fatal_perror(const char* const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger_perror("ERROR", fmt, ap); + va_end(ap); + + exit(EXIT_FAILURE); +} + +void fatal_nodie(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("ERROR", fmt, ap); + va_end(ap); +} + +void fatal_perror_nodie(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger_perror("ERROR", fmt, ap); + va_end(ap); +} + +void warning(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("WARN", fmt, ap); + va_end(ap); +} + +void warning_perror(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger_perror("WARN", fmt, ap); + va_end(ap); +} + +void info(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("INFO", fmt, ap); + va_end(ap); +} diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..bc4b3df --- /dev/null +++ b/src/logger.h @@ -0,0 +1,17 @@ +#ifndef _LOGGER_H +#define _LOGGER_H + +#define PROGRAM_TITLE "PolytreeWM" +#define PROGRAM_NAME "polytreewm" + +__attribute__((noreturn)) +void fatal(const char *fmt, ...); +__attribute__((noreturn)) +void fatal_perror(const char *fmt, ...); +void fatal_nodie(const char *fmt, ...); +void fatal_perror_nodie(const char *fmt, ...); +void warning(const char *fmt, ...); +void warning_perror(const char *fmt, ...); +void info(const char *fmt, ...); + +#endif // _LOGGER_H diff --git a/src/main.c b/src/main.c index df0be8d..0c54c1c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,10 @@ #include "main.h" #include "dwm.h" +#include "logger.h" #include #include -#include #include #include #include @@ -12,16 +12,10 @@ #include #include -#define PROGRAM_TITLE "PolytreeWM" -#define PROGRAM_NAME "polytreewm" - static char *program_exe = NULL; static void signal_callback(int signo); -static void logger(const char *level, const char *fmt, ...); -static void logger_perror(const char *level, const char *fmt, ...); - int main(int argc, char *argv[]) { if (argc == 2 && @@ -83,88 +77,3 @@ void restart() execvp(program_exe, args); fatal_perror("restart with `execvp' failed"); } - -void logger(const char *const level, const char *const fmt, ...) -{ - fprintf(stderr, PROGRAM_NAME": %s: ", level); - - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - - fputc('\n', stderr); -} - -void logger_perror(const char *const level, const char *const fmt, ...) -{ - fprintf(stderr, PROGRAM_NAME": %s: ", level); - - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - - fprintf(stderr, ": "); - perror(NULL); -} - -void fatal(const char *const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger("ERROR", fmt, ap); - va_end(ap); - - exit(EXIT_FAILURE); -} - -void fatal_perror(const char* const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger_perror("ERROR", fmt, ap); - va_end(ap); - - exit(EXIT_FAILURE); -} - -void fatal_nodie(const char *const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger("ERROR", fmt, ap); - va_end(ap); -} - -void fatal_perror_nodie(const char *const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger_perror("ERROR", fmt, ap); - va_end(ap); -} - -void warning(const char *const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger("WARN", fmt, ap); - va_end(ap); -} - -void warning_perror(const char *const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger_perror("WARN", fmt, ap); - va_end(ap); -} - -void info(const char *const fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - logger("INFO", fmt, ap); - va_end(ap); -} diff --git a/src/main.h b/src/main.h index d24651d..1927eb1 100644 --- a/src/main.h +++ b/src/main.h @@ -1,16 +1,6 @@ #ifndef _MAIN_H #define _MAIN_H -__attribute__((noreturn)) -void fatal(const char *fmt, ...); -__attribute__((noreturn)) -void fatal_perror(const char *fmt, ...); -void fatal_nodie(const char *fmt, ...); -void fatal_perror_nodie(const char *fmt, ...); -void warning(const char *fmt, ...); -void warning_perror(const char *fmt, ...); -void info(const char *fmt, ...); - __attribute__((noreturn)) void restart(); diff --git a/src/util.c b/src/util.c index b2dacb5..ab0548b 100644 --- a/src/util.c +++ b/src/util.c @@ -1,6 +1,6 @@ #include "util.h" -#include "main.h" +#include "logger.h" #include #include diff --git a/src/xbase.c b/src/xbase.c index d19af11..1f6a448 100644 --- a/src/xbase.c +++ b/src/xbase.c @@ -1,6 +1,6 @@ #include "xbase.h" -#include "main.h" +#include "logger.h" #include #include diff --git a/tests/geom_position.c b/tests/geom_position.c new file mode 100644 index 0000000..726c660 --- /dev/null +++ b/tests/geom_position.c @@ -0,0 +1,3 @@ +int test() { + return 0; +} diff --git a/tests/geom_sizes.c b/tests/geom_sizes.c new file mode 100644 index 0000000..726c660 --- /dev/null +++ b/tests/geom_sizes.c @@ -0,0 +1,3 @@ +int test() { + return 0; +} diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..fc362c5 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,19 @@ +#include + +int test(); + +void restart() {} + +int main(int argc, char **argv) +{ + const char *const test_name = argv[0]; + const int test_result = test(); + + if (test_result == 0) { + printf("[ OK ] %s\n", test_name); + } else { + printf("[FAIL] %s\n", test_name); + } + + return test_result; +}