From 7d3acb0e3db59e8afd5335a69c249974a222aefd Mon Sep 17 00:00:00 2001 From: welcor <357770+welcor@users.noreply.github.com> Date: Sun, 19 May 2024 01:45:42 +0200 Subject: [PATCH] Testing do_remove Mocking the send_to_char function to check messages to the user. Still doesn't work for act() though. Requires libmocka-dev and libunwind-setjmp0-dev --- src/test/Makefile | 3 ++- src/test/test.act.item.c | 27 +++++++++++++++++++++- src/test/testrunner.c | 48 +++++++++++++++++++++++++++++++++++++--- src/test/testrunner.h | 10 +++++++++ 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/test/Makefile b/src/test/Makefile index bc01c7e..c3d1c72 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -8,7 +8,8 @@ ASAN:=n UBSAN:=n EXTENSION:= TEST_ENV:= -CFLAGS:=-Wall -Wno-char-subscripts -Wno-unused-but-set-variable +CFLAGS:=-Wall -Wno-char-subscripts -Wno-unused-but-set-variable +CFLAGS+=-Wl,--wrap=send_to_char,--wrap=vwrite_to_output AGGRESSIVE_WARNINGS=n LIBS:=-lcrypt diff --git a/src/test/test.act.item.c b/src/test/test.act.item.c index 92ac591..25b2f0b 100644 --- a/src/test/test.act.item.c +++ b/src/test/test.act.item.c @@ -2,7 +2,31 @@ UNIT_TEST(test_do_remove) { + + char_data *ch = create_char(); + CREATE(ch->player_specials, struct player_special_data, 1); + new_mobile_data(ch); + ch->char_specials.position = POS_STANDING; + CREATE(ch->desc, struct descriptor_data, 1); + char_to_room(ch, 0); + + do_remove(ch, "2.ring", 0, 0); + munit_assert_string_equal(get_last_messages(), "You don't seem to be using a ring.\r\n"); + + obj_data *ring1 = create_obj(); + ring1->name = "ring"; + ring1->short_description = "ring1"; + + obj_data *ring2 = create_obj(); + ring2->name = "ring"; + ring2->short_description = "ring2"; + + equip_char(ch, ring1, WEAR_FINGER_R); + equip_char(ch, ring2, WEAR_FINGER_L); + + do_remove(ch, "2.ring", 0, 0); + munit_assert_ptr_equal(ch->carrying, ring2); return MUNIT_OK; } @@ -12,4 +36,5 @@ MunitTest act_item_c_tests[] = { // end of array marker { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } -}; \ No newline at end of file +}; + diff --git a/src/test/testrunner.c b/src/test/testrunner.c index 6049cc3..be7cb92 100644 --- a/src/test/testrunner.c +++ b/src/test/testrunner.c @@ -2,6 +2,8 @@ #include "test.handler.h" #include "test.act.item.h" +void simple_world(); + static MunitSuite suites[] = { { "/handler.c", handler_c_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }, { "/act.item.c", act_item_c_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }, @@ -18,8 +20,48 @@ static const MunitSuite test_suite = { }; int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) { - /* Finally, we'll actually run our test suite! That second argument - * is the user_data parameter which will be passed either to the - * test or (if provided) the fixture setup function. */ + logfile = stderr; + simple_world(); return munit_suite_main(&test_suite, (void*) "µnit", argc, argv); } + + +/* + * test-fixtures common for many tests +*/ + +void simple_world() +{ + CREATE(world, struct room_data, 1); + top_of_world = 1; +} + +static char testbuf[MAX_OUTPUT_BUFFER]; +static int testbuf_size = 0; + +size_t __wrap_send_to_char(struct char_data *ch, const char *messg, ...) +{ + int size = testbuf_size; + va_list args; + + va_start(args, messg); + testbuf_size += vsnprintf(testbuf + size, MAX_OUTPUT_BUFFER - size, messg, args); + va_end(args); + + return testbuf_size; +} + +size_t __wrap_vwrite_to_output(struct descriptor_data *t, const char *format, va_list args) +{ + int size = testbuf_size; + testbuf_size += vsnprintf(testbuf + size, MAX_OUTPUT_BUFFER - size, format, args); + return testbuf_size; +} + +char *get_last_messages() +{ + char *stored_response = strdup(testbuf); + testbuf_size = 0; + *testbuf = '\0'; + return stored_response; +} \ No newline at end of file diff --git a/src/test/testrunner.h b/src/test/testrunner.h index 2969ad7..fd67934 100644 --- a/src/test/testrunner.h +++ b/src/test/testrunner.h @@ -18,6 +18,10 @@ #include "../quest.h" #include "../mud_event.h" #include "../munit/munit.h" +#include +#include +#include +#include /** * Utility macro for defining tests. @@ -30,4 +34,10 @@ */ #define STD_TEST(test_name, test_fun) { (char *)(test_name), (test_fun), NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +/* + * test fixtures + */ +char_data* create_test_char_data(); +char *get_last_messages(); + #endif \ No newline at end of file