Add unit tests.
Add an optional build dependency on check and add an initial test case.
This commit is contained in:
parent
057107a233
commit
53dafd9590
@ -1,4 +1,4 @@
|
||||
SUBDIRS = src
|
||||
SUBDIRS = src . tests
|
||||
|
||||
dist_man_MANS = bip.1 bip.conf.5 bipmkpw.1
|
||||
|
||||
|
11
configure.ac
11
configure.ac
@ -69,6 +69,7 @@ AC_ARG_ENABLE([oidentd], AS_HELP_STRING([--enable-oidentd],
|
||||
[Enable oidentd support (bip overwrites ~/.oidentd.conf with this on!)]))
|
||||
AC_ARG_ENABLE([pie], AS_HELP_STRING([--disable-pie],
|
||||
[Do not build a position independent executable]))
|
||||
AC_ARG_ENABLE([tests], AS_HELP_STRING([--enable-tests], [Build tests]))
|
||||
|
||||
AM_CONDITIONAL(DEBUG, test x$enable_debug = xyes)
|
||||
AS_IF([test "x$enable_debug" = "xyes"], [
|
||||
@ -122,7 +123,12 @@ AS_IF([test "x$enable_pie" != "xno"], [
|
||||
fi
|
||||
])
|
||||
|
||||
AC_CONFIG_FILES([Makefile src/Makefile])
|
||||
AS_IF([test "x$enable_tests" != "xno"], [
|
||||
PKG_CHECK_MODULES([CHECK], [check >= 0.9.6])
|
||||
])
|
||||
|
||||
|
||||
AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile])
|
||||
AC_OUTPUT
|
||||
|
||||
AS_IF([test "x$with_openssl" != "xno"], [
|
||||
@ -131,3 +137,6 @@ echo OPENSSL: yes
|
||||
echo DEBUG: $enable_debug $backtrace
|
||||
echo OIDENTD: $enable_oidentd
|
||||
echo PIE: $enable_pie
|
||||
AS_IF([test "x$enable_tests" != "xno"], [
|
||||
echo TESTS: $enable_tests
|
||||
])
|
||||
|
@ -21,4 +21,4 @@ bipmkpw_SOURCES = bipmkpw.c md5.c util.c
|
||||
AM_YFLAGS= -d
|
||||
BUILT_SOURCES = conf.c conf.h lex.c
|
||||
|
||||
#AM_CFLAGS=-Wall -Wextra -Werror
|
||||
AM_CFLAGS=-Wall -Wextra -Werror
|
||||
|
12
src/line.c
12
src/line.c
@ -213,15 +213,3 @@ void irc_line_free(struct line *l)
|
||||
free(l->origin);
|
||||
free(l);
|
||||
}
|
||||
|
||||
void irc_line_free_args(char **elemv, int elemc)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (elemc == 0)
|
||||
return;
|
||||
for (i = 0; i < elemc; i++)
|
||||
free(elemv[i]);
|
||||
free(elemv);
|
||||
}
|
||||
|
||||
|
@ -99,7 +99,6 @@ int irc_line_count(struct line *line);
|
||||
char *irc_line_pop(struct line *l);
|
||||
int irc_line_elem_equals(struct line *line, int elem, const char *cmp);
|
||||
int irc_line_elem_case_equals(struct line *line, int elem, const char *cmp);
|
||||
void irc_line_free_args(char **elemv, int elemc);
|
||||
void irc_line_drop(struct line *line, int elem);
|
||||
|
||||
#endif
|
||||
|
5
tests/Makefile.am
Normal file
5
tests/Makefile.am
Normal file
@ -0,0 +1,5 @@
|
||||
TESTS = check_line
|
||||
check_PROGRAMS = check_line
|
||||
check_line_SOURCES = check_line.c $(top_builddir)/src/line.h
|
||||
check_line_CFLAGS = @CHECK_CFLAGS@
|
||||
check_line_LDADD = $(top_builddir)/src/libbip.a @CHECK_LIBS@
|
134
tests/check_line.c
Normal file
134
tests/check_line.c
Normal file
@ -0,0 +1,134 @@
|
||||
#include <check.h>
|
||||
#include "../src/line.h"
|
||||
|
||||
START_TEST(test_line_init)
|
||||
{
|
||||
struct line line;
|
||||
irc_line_init(&line);
|
||||
irc_line_append(&line, "line");
|
||||
char *hi_string = irc_line_to_string(&line);
|
||||
ck_assert_str_eq(hi_string, "line\r\n");
|
||||
free(hi_string);
|
||||
_irc_line_deinit(&line);
|
||||
|
||||
struct line *line2 = irc_line_new();
|
||||
irc_line_append(line2, "line");
|
||||
hi_string = irc_line_to_string(line2);
|
||||
ck_assert_str_eq(hi_string, "line\r\n");
|
||||
free(hi_string);
|
||||
irc_line_free(line2);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_line_parse)
|
||||
{
|
||||
struct line *line = irc_line_new_from_string(
|
||||
":origin COMMAND parameter1 parameter2 "
|
||||
":multi word parameter\r\n");
|
||||
ck_assert_int_eq(irc_line_count(line), 4);
|
||||
ck_assert(irc_line_includes(line, 0));
|
||||
ck_assert(irc_line_includes(line, 3));
|
||||
ck_assert(!irc_line_includes(line, 4));
|
||||
ck_assert_str_eq(line->origin, "origin");
|
||||
ck_assert_str_eq(irc_line_elem(line, 0), "COMMAND");
|
||||
ck_assert_str_eq(irc_line_elem(line, 1), "parameter1");
|
||||
ck_assert_str_eq(irc_line_elem(line, 2), "parameter2");
|
||||
ck_assert_str_eq(irc_line_elem(line, 3), "multi word parameter\r\n");
|
||||
irc_line_free(line);
|
||||
|
||||
line = irc_line_new_from_string(
|
||||
"COMMAND parameter1 parameter2 :multi word parameter\r\n");
|
||||
ck_assert_int_eq(irc_line_count(line), 4);
|
||||
ck_assert(irc_line_includes(line, 0));
|
||||
ck_assert(irc_line_includes(line, 3));
|
||||
ck_assert(!irc_line_includes(line, 4));
|
||||
ck_assert_str_eq(irc_line_elem(line, 0), "COMMAND");
|
||||
ck_assert_str_eq(irc_line_elem(line, 1), "parameter1");
|
||||
ck_assert_str_eq(irc_line_elem(line, 2), "parameter2");
|
||||
ck_assert_str_eq(irc_line_elem(line, 3), "multi word parameter\r\n");
|
||||
irc_line_free(line);
|
||||
|
||||
line = irc_line_new_from_string(":origin COMMAND\r\n");
|
||||
ck_assert_str_eq(line->origin, "origin");
|
||||
ck_assert_int_eq(irc_line_count(line), 1);
|
||||
ck_assert(irc_line_includes(line, 0));
|
||||
ck_assert(!irc_line_includes(line, 1));
|
||||
ck_assert_str_eq(irc_line_elem(line, 0), "COMMAND\r\n");
|
||||
irc_line_free(line);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_elem_equals)
|
||||
{
|
||||
struct line *line = irc_line_new_from_string(
|
||||
":origin COMMAND parameter1 parameter2 "
|
||||
":multi word parameter\r\n");
|
||||
ck_assert(irc_line_elem_equals(line, 0, "COMMAND"));
|
||||
ck_assert(irc_line_elem_case_equals(line, 0, "command"));
|
||||
ck_assert(!irc_line_elem_equals(line, 0, "notcommand"));
|
||||
ck_assert(!irc_line_elem_case_equals(line, 0, "notcommand"));
|
||||
irc_line_free(line);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_line_pop)
|
||||
{
|
||||
struct line *line = irc_line_new_from_string(
|
||||
":origin COMMAND parameter1 parameter2 "
|
||||
":multi word parameter\r\n");
|
||||
char *top = irc_line_pop(line);
|
||||
ck_assert_str_eq(top, "multi word parameter\r\n");
|
||||
free(top);
|
||||
top = irc_line_pop(line);
|
||||
ck_assert_str_eq(top, "parameter2");
|
||||
free(top);
|
||||
irc_line_free(line);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_line_drop)
|
||||
{
|
||||
struct line *line = irc_line_new_from_string(
|
||||
":origin COMMAND parameter1 parameter2 "
|
||||
":multi word parameter\r\n");
|
||||
irc_line_drop(line, 1);
|
||||
ck_assert(irc_line_elem_equals(line, 0, "COMMAND"));
|
||||
ck_assert(irc_line_elem_equals(line, 1, "parameter2"));
|
||||
irc_line_drop(line, 0);
|
||||
ck_assert(irc_line_elem_equals(line, 0, "parameter2"));
|
||||
irc_line_free(line);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
Suite *money_suite(void)
|
||||
{
|
||||
Suite *s;
|
||||
TCase *tc_core;
|
||||
|
||||
s = suite_create("bip");
|
||||
|
||||
tc_core = tcase_create("core");
|
||||
|
||||
tcase_add_test(tc_core, test_line_init);
|
||||
tcase_add_test(tc_core, test_line_parse);
|
||||
tcase_add_test(tc_core, test_elem_equals);
|
||||
tcase_add_test(tc_core, test_line_pop);
|
||||
suite_add_tcase(s, tc_core);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int number_failed;
|
||||
Suite *s;
|
||||
SRunner *sr;
|
||||
|
||||
s = money_suite();
|
||||
sr = srunner_create(s);
|
||||
|
||||
srunner_run_all(sr, CK_NORMAL);
|
||||
number_failed = srunner_ntests_failed(sr);
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
Loading…
Reference in New Issue
Block a user