Add tests

This commit is contained in:
Alex Kotov 2021-12-04 23:42:03 +05:00
parent e7a6581dca
commit 2a44f23de2
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
13 changed files with 168 additions and 115 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@
/config/2-conditionals.mk
/polytreewm
/src/*.o
/tests/*.o
/tests/*.test

View file

@ -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

View file

@ -1,4 +1,4 @@
#include "main.h"
#include "logger.h"
#include "util.h"
#include <stdio.h>

View file

@ -2,6 +2,7 @@
#include "helpers.h"
#include "layouts.h"
#include "logger.h"
#include "main.h"
#include "settings.h"
#include "spawn.h"

93
src/logger.c Normal file
View file

@ -0,0 +1,93 @@
#include "logger.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
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);
}

17
src/logger.h Normal file
View file

@ -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

View file

@ -1,10 +1,10 @@
#include "main.h"
#include "dwm.h"
#include "logger.h"
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@ -12,16 +12,10 @@
#include <sys/wait.h>
#include <unistd.h>
#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);
}

View file

@ -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();

View file

@ -1,6 +1,6 @@
#include "util.h"
#include "main.h"
#include "logger.h"
#include <stdarg.h>
#include <stdio.h>

View file

@ -1,6 +1,6 @@
#include "xbase.h"
#include "main.h"
#include "logger.h"
#include <stdlib.h>
#include <string.h>

3
tests/geom_position.c Normal file
View file

@ -0,0 +1,3 @@
int test() {
return 0;
}

3
tests/geom_sizes.c Normal file
View file

@ -0,0 +1,3 @@
int test() {
return 0;
}

19
tests/main.c Normal file
View file

@ -0,0 +1,19 @@
#include <stdio.h>
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;
}