Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit 5e2825a

Browse files
rogersmzachflower
authored andcommitted
Add testing infrastructure (#17)
* Adding three simple tests. * Updating Makefile to be able to compile tests * Exclude compiled tests from github. * Adding -g to gcc again so we include debug information. * Added check unit test library to Vagrant
1 parent 3c1d24a commit 5e2825a

File tree

7 files changed

+97
-11
lines changed

7 files changed

+97
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ guildmud
55
*.log
66
.vscode/settings.json
77
src/libguildmud.a
8+
tests/*.run

Makefile

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
CC = gcc
2-
C_FLAGS = -Wall -g -pedantic -Werror
2+
C_FLAGS = -Wall -pedantic -Werror -g
33
L_FLAGS = -lz -lpthread -lsqlite3
4+
C_FLAGS_TEST = -Wall -pedantic -Werror -g -Isrc/
5+
L_FLAGS_TEST = -lz -lpthread -lsqlite3 -lcheck -Lsrc/
46

57
SRC_FILES = $(wildcard src/*.c)
68
OBJ_FILES = $(filter-out src/main.o, $(SRC_FILES:.c=.o))
7-
9+
CHECK_FILES = $(wildcard tests/*.test)
10+
CHECK_FILES_EXE = $(CHECK_FILES:.test=.run)
811

912
all: src/main.o src/libguildmud.a
10-
$(CC) -o src/guildmud src/main.o src/libguildmud.a $(L_FLAGS)
13+
@$(CC) -o src/guildmud src/main.o src/libguildmud.a $(L_FLAGS)
1114

1215
src/libguildmud.a: $(OBJ_FILES)
13-
ar ru $@ $^
14-
ranlib $@
16+
@ar ru $@ $^
17+
@ranlib $@
1518

1619
.c.o: all
17-
$(CC) -c $(C_FLAGS) -o $@ $<
20+
@$(CC) -c $(C_FLAGS) -o $@ $<
1821

19-
clean:
20-
@echo Cleaning code $< ...
21-
@rm -f src/*.o src/guildmud src/libguildmud.a src/*~
2222

23-
test:
24-
@echo To be implemented shortly...
23+
test: $(CHECK_FILES_EXE)
24+
@echo Compiling tests...
25+
26+
27+
tests/%.run: tests/%.c src/libguildmud.a
28+
@$(CC) -o $@ $< src/libguildmud.a $(C_FLAGS_TEST) $(L_FLAGS_TEST)
29+
# $@ -- Uncomment if we want to run the compiled test
30+
31+
tests/%.c: tests/%.test
32+
@checkmk $< > $@
2533

2634
install:
2735
@echo To be implemented shortly...
36+
37+
.PHONY: clean
38+
clean:
39+
@echo Cleaning code ...
40+
@rm -rf src/*.o src/guildmud src/libguildmud.a src/*~ tests/*.c tests/run* tests/*.dSYM src/*.dSYM

Vagrantfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Vagrant.configure("2") do |config|
3030
config.vm.provision "shell", path: "vagrant/provisioners/home.sh"
3131
config.vm.provision "shell", path: "vagrant/provisioners/c.sh"
3232
config.vm.provision "shell", path: "vagrant/provisioners/sqlite.sh"
33+
config.vm.provision "shell", path: "vagrant/provisioners/check.sh"
3334

3435
# make /vagrant the working directory for all commands
3536
config.exec.commands '*', directory: '/vagrant'

tests/test_crypt.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <ctype.h>
5+
#include <stdio.h>
6+
#include <stdarg.h>
7+
#include <check.h>
8+
9+
#include "mud.h"
10+
11+
#suite crypt_c
12+
#tcase check_is_prefix
13+
14+
#test crypt_code_salt
15+
ck_assert_str_eq("sanem1WDqdnxo", crypt("code", "salt"));
16+
#test crypt_guildmud_cprogramming
17+
ck_assert_str_eq("cp.kzgf79whuQ", crypt("guildmud", "cprogramming"));

tests/test_strings.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <ctype.h>
5+
#include <stdio.h>
6+
#include <stdarg.h>
7+
#include <check.h>
8+
9+
#include "mud.h"
10+
11+
#suite strings_c
12+
#tcase check_is_prefix
13+
14+
#test is_prefix_A_A
15+
ck_assert_msg(TRUE == is_prefix("A", "A"), "is_prefix A, A should have returned true");
16+
#test is_prefix_A_AA
17+
ck_assert_msg(TRUE == is_prefix("A", "AA"), "is_prefix A, AA should have returned true");
18+
#test is_prefix_A_AAA
19+
ck_assert_msg(TRUE == is_prefix("A", "AAA"), "is_prefix A, AAA should have returned true");
20+
#test is_prefix_A_AAAZ
21+
ck_assert_msg(TRUE == is_prefix("A", "AAAZ"), "is_prefix A, AAAZ should have returned true");
22+
#test is_prefix_A_B_fail
23+
ck_assert_msg(TRUE != is_prefix("A", "B"), "is_prefix A, B should have returned false");

tests/test_utils.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <ctype.h>
5+
#include <stdio.h>
6+
#include <stdarg.h>
7+
#include <check.h>
8+
9+
#include "mud.h"
10+
11+
#suite utils_c
12+
#tcase check_name
13+
14+
#test check_name_valid
15+
ck_assert_msg(TRUE == check_name("abc"), "check_name abc must not fail is an alpha string >=3 and <=12");
16+
ck_assert_msg(TRUE == check_name("ALFA"), "check_name ALFA must not fail is an alpha string >=3 and <=12");
17+
ck_assert_msg(TRUE == check_name("OMEGA"), "check_name OMEGA must not fail is an alpha string >=3 and <=12");
18+
ck_assert_msg(TRUE == check_name("first"), "check_name first must not fail is an alpha string >=3 and <=12");
19+
ck_assert_msg(TRUE == check_name("second"), "check_name second must not fail is an alpha string >=3 and <=12");
20+
ck_assert_msg(TRUE == check_name("abcdefghijkl"), "check_name abcdefghijkl must not fail is an alpha string >=3 and <=12");
21+
22+
#test check_name_fail
23+
ck_assert_msg(FALSE == check_name("guild mud"), "check_name /guild mud/ must fail because has an /space/");
24+
ck_assert_msg(FALSE == check_name("ab"), "check_name /ab/ must fail because has less < 3 characters");
25+
ck_assert_msg(FALSE == check_name("abcdefghijklm"), "check_name /abcdefghijklm/ must fail because has > 13 chars");
26+
ck_assert_msg(FALSE == check_name("guild*mud"), "check_name /guild*mud/ must fail because has an /*/");
27+
ck_assert_msg(FALSE == check_name("guild1mud"), "check_name /guild1mud/ must fail because has an /1/");

vagrant/provisioners/check.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
apt-get update
4+
apt-get install check

0 commit comments

Comments
 (0)