Improved unit testing

FossilOrigin-Name: 786b4080abc83f076704b33ec884236a3733fef9a5a276d7df54fa35d228015b
This commit is contained in:
nekobit 2022-07-28 03:36:33 +00:00
parent eba2a360e0
commit 253f55dd36
6 changed files with 66 additions and 39 deletions

View File

@ -11,4 +11,5 @@ config.h
treebird
test/tests
scripts/*.o
templates/*.ctt
templates/*.ctt
test/unit/*.bin

View File

@ -15,9 +15,14 @@ PAGES_CMP = $(patsubst %.tmpl,%.ctmpl,$(PAGES))
PAGES_C = $(patsubst %.tmpl, %.c,$(PAGES))
PAGES_C_OBJ = $(patsubst %.c,%.o,$(PAGES_C))
TMPLS_C = $(patsubst %.tt,%.ctt,$(TMPLS))
TEST_DIR = test/unit
TESTS = $(wildcard $(TEST_DIR)/t*.c)
UNIT_TESTS = $(patsubst %.c,%.bin,$(TESTS))
DIST = dist/
PREFIX ?= /usr/local
TARGET = treebird
# For tests
OBJ_NO_MAIN = $(filter-out src/main.o,$(OBJ))
MASTODONT_URL = https://fossil.nekobit.net/mastodont-c
@ -69,8 +74,9 @@ install: $(TARGET)
install -d $(PREFIX)/share/treebird/
cp -r dist/ $(PREFIX)/share/treebird/
test:
make -C test
test: all $(UNIT_TESTS)
@echo " ... Tests ready"
@./test/test.pl
dep_build:
make -C $(MASTODONT_DIR)
@ -78,12 +84,18 @@ dep_build:
%.o: %.c %.h $(PAGES)
$(CC) $(CFLAGS) -c $< -o $@
# For tests
%.bin: %.c
@$(CC) $(CFLAGS) $< -o $@ $(OBJ_NO_MAIN) $(PAGES_C_OBJ) $(LDFLAGS)
@echo -n " $@"
clean:
rm -f $(OBJ) src/file-to-c/main.o
rm -f $(PAGES_CMP)
rm -f $(TMPLS_C)
rm -f test/unit/*.bin
rm -f filec ctemplate
rm $(TARGET)
rm $(TARGET) || true
make -C $(MASTODONT_DIR) clean
clean_deps:

39
test/test.pl Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $FAIL = "\033[91m";
my $OK = "\033[96m";
my $SUCCESS = "\033[92m";
my $ENDC = "\033[0m";
sub read_file
{
my ($filename) = @_;
open my $fh, '<', $filename or die "Error opening $filename: $!";
do { local $/; <$fh> };
}
my @files_glob = glob('./test/unit/t*.bin');
my $fail = 0;
foreach my $bin (@files_glob)
{
my $content = read_file(substr($bin, 0, -4) . ".c");
my ($name, $valgrind) = $content =~ /\/\*.*INFO name='(.*?)'.*valgrind=(.*?)\s\*\//s;
if (system(($bin)))
{
print "$FAIL -- !! -- Test '$name' failed!$ENDC\n";
$fail = 1;
last;
}
else {
print "$OK -- OK -- Test '$name' passed$ENDC\n";
}
}
print "$SUCCESS -- All Tests passed!$ENDC\n" unless $fail;

View File

@ -1,33 +0,0 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "../test.h"
// Imports
#include "mime_multipart.c"
#include "string_test.c"
int main()
{
struct test tests[] = {
{ "Mime multipart parser", mime_multipart_test },
{ "Strings", string_replace_test }
};
return iterate_tests(tests, sizeof(tests)/sizeof(tests[0]));
}

View File

@ -1,7 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "../../src/string.h"
int string_replace_test(void)
/* INFO name='Dummy test' valgrind=0 */
int main(void)
{
char* res1 = strrepl("hello world", "wo", "swi", STRREPL_ALL);
assert(strcmp(res1, "hello swirld") == 0);

View File

@ -18,6 +18,9 @@
#include <string.h>
#include <assert.h>
#include <stdlib.h>
/* INFO name='Mime checking' valgrind=0 */
void mime_boundary_check(void)
{
@ -62,7 +65,7 @@ void form_check(void)
free(multipart);
}
int mime_multipart_test(void)
int main(void)
{
mime_boundary_check();
form_check();