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
This commit is contained in:
welcor 2024-05-19 01:45:42 +02:00
parent 217eac8cb3
commit 7d3acb0e3d
4 changed files with 83 additions and 5 deletions

View file

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

View file

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

View file

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

View file

@ -18,6 +18,10 @@
#include "../quest.h"
#include "../mud_event.h"
#include "../munit/munit.h"
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
/**
* 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