tests: add tests on infolists
This commit is contained in:
parent
41d63511b4
commit
cc06b95ba7
@ -45,6 +45,10 @@ Documentation::
|
||||
|
||||
* core: split man pages weechat and weechat-headless
|
||||
|
||||
Tests::
|
||||
|
||||
* unit, scripts: add tests on infolists
|
||||
|
||||
Build::
|
||||
|
||||
* core: fix build with CMake and Ninja
|
||||
|
@ -195,6 +195,38 @@ def test_hooks():
|
||||
weechat.unhook(hook_cmplt)
|
||||
|
||||
|
||||
def infolist_cb(data, infolist_name, pointer, arguments):
|
||||
"""Infolist callback."""
|
||||
infolist = weechat.infolist_new()
|
||||
check(infolist != '')
|
||||
item = weechat.infolist_new_item(infolist)
|
||||
check(item != '')
|
||||
check(weechat.infolist_new_var_integer(item, 'integer', 123) != '')
|
||||
check(weechat.infolist_new_var_string(item, 'string', 'test string') != '')
|
||||
check(weechat.infolist_new_var_pointer(item, 'pointer', '0xabcdef') != '')
|
||||
check(weechat.infolist_new_var_time(item, 'time', 1231231230) != '')
|
||||
return infolist
|
||||
|
||||
|
||||
def test_infolist():
|
||||
"""Test infolist functions."""
|
||||
hook_infolist = weechat.hook_infolist('infolist_test_script',
|
||||
'description', '', '',
|
||||
'infolist_cb', '')
|
||||
check(weechat.infolist_get('infolist_does_not_exist', '', '') == '')
|
||||
ptr_infolist = weechat.infolist_get('infolist_test_script', '', '')
|
||||
check(ptr_infolist != '')
|
||||
check(weechat.infolist_next(ptr_infolist) == 1)
|
||||
check(weechat.infolist_integer(ptr_infolist, 'integer') == 123)
|
||||
check(weechat.infolist_string(ptr_infolist, 'string') == 'test string')
|
||||
check(weechat.infolist_pointer(ptr_infolist, 'pointer') == '0xabcdef')
|
||||
check(weechat.infolist_time(ptr_infolist, 'time') == 1231231230)
|
||||
check(weechat.infolist_fields(ptr_infolist) == 'i:integer,s:string,p:pointer,t:time')
|
||||
check(weechat.infolist_next(ptr_infolist) == 0)
|
||||
weechat.infolist_free(ptr_infolist)
|
||||
weechat.unhook(hook_infolist)
|
||||
|
||||
|
||||
def cmd_test_cb(data, buf, args):
|
||||
"""Run all the tests."""
|
||||
weechat.prnt('', '>>>')
|
||||
@ -207,6 +239,7 @@ def cmd_test_cb(data, buf, args):
|
||||
test_key()
|
||||
test_display()
|
||||
test_hooks()
|
||||
test_infolist()
|
||||
weechat.prnt('', ' > TESTS END')
|
||||
return weechat.WEECHAT_RC_OK
|
||||
|
||||
|
@ -20,14 +20,86 @@
|
||||
*/
|
||||
|
||||
#include "CppUTest/TestHarness.h"
|
||||
#include "string.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "src/core/wee-hook.h"
|
||||
#include "src/core/wee-infolist.h"
|
||||
}
|
||||
|
||||
struct t_hook *hook_test_infolist = NULL;
|
||||
|
||||
|
||||
TEST_GROUP(Infolist)
|
||||
{
|
||||
/*
|
||||
* Callback for the infolist used in tests.
|
||||
*/
|
||||
|
||||
static struct t_infolist *
|
||||
test_infolist_cb (const void *pointer, void *data,
|
||||
const char *infolist_name, void *obj_pointer,
|
||||
const char *arguments)
|
||||
{
|
||||
struct t_infolist *ptr_infolist;
|
||||
struct t_infolist_item *ptr_item;
|
||||
static const char buffer[4] = { 'a', 'b', 'c', 0 };
|
||||
|
||||
/* make C++ compiler happy */
|
||||
(void) pointer;
|
||||
(void) data;
|
||||
(void) infolist_name;
|
||||
(void) obj_pointer;
|
||||
|
||||
ptr_infolist = infolist_new (NULL);
|
||||
if (!ptr_infolist)
|
||||
return NULL;
|
||||
|
||||
ptr_item = infolist_new_item (ptr_infolist);
|
||||
if (!ptr_item)
|
||||
goto error;
|
||||
|
||||
if (!infolist_new_var_integer (ptr_item, "integer", 123456))
|
||||
goto error;
|
||||
if (!infolist_new_var_string (ptr_item, "string", "test string"))
|
||||
goto error;
|
||||
if (!infolist_new_var_pointer (ptr_item, "pointer", (void *)0x123abc))
|
||||
goto error;
|
||||
if (!infolist_new_var_buffer (ptr_item, "buffer", (void *)buffer,
|
||||
sizeof (buffer)))
|
||||
goto error;
|
||||
if (!infolist_new_var_time (ptr_item, "time", 1234567890))
|
||||
goto error;
|
||||
|
||||
if (arguments && (strcmp (arguments, "test2") == 0))
|
||||
{
|
||||
ptr_item = infolist_new_item (ptr_infolist);
|
||||
if (!ptr_item)
|
||||
goto error;
|
||||
|
||||
if (!infolist_new_var_string (ptr_item, "string2", "test2"))
|
||||
goto error;
|
||||
}
|
||||
|
||||
return ptr_infolist;
|
||||
|
||||
error:
|
||||
infolist_free (ptr_infolist);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
hook_test_infolist = hook_infolist (
|
||||
NULL, "infolist_test", "Test infolist", NULL, "test",
|
||||
&test_infolist_cb, NULL, NULL);
|
||||
}
|
||||
|
||||
void teardown()
|
||||
{
|
||||
unhook (hook_test_infolist);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
@ -43,17 +115,144 @@ TEST_GROUP(Infolist)
|
||||
|
||||
TEST(Infolist, New)
|
||||
{
|
||||
/* TODO: write tests */
|
||||
struct t_infolist *infolist;
|
||||
struct t_infolist_item *item;
|
||||
struct t_infolist_var *var_int, *var_str, *var_ptr, *var_buf, *var_time;
|
||||
const char buffer[3] = { 12, 34, 56 };
|
||||
|
||||
/* create a new infolist */
|
||||
infolist = infolist_new(NULL);
|
||||
CHECK(infolist);
|
||||
|
||||
/* check initial infolist values */
|
||||
POINTERS_EQUAL(NULL, infolist->plugin);
|
||||
POINTERS_EQUAL(NULL, infolist->items);
|
||||
POINTERS_EQUAL(NULL, infolist->last_item);
|
||||
POINTERS_EQUAL(NULL, infolist->ptr_item);
|
||||
|
||||
/* check that the infolist is the last one in list */
|
||||
POINTERS_EQUAL(last_weechat_infolist, infolist);
|
||||
|
||||
/* create a new item in infolist */
|
||||
item = infolist_new_item (infolist);
|
||||
CHECK(item);
|
||||
|
||||
/* check initial item values */
|
||||
POINTERS_EQUAL(NULL, item->vars);
|
||||
POINTERS_EQUAL(NULL, item->last_var);
|
||||
POINTERS_EQUAL(NULL, item->fields);
|
||||
POINTERS_EQUAL(NULL, item->prev_item);
|
||||
POINTERS_EQUAL(NULL, item->next_item);
|
||||
|
||||
/* check that item is in infolist */
|
||||
POINTERS_EQUAL(item, infolist->items);
|
||||
POINTERS_EQUAL(item, infolist->last_item);
|
||||
|
||||
/* add an integer variable */
|
||||
var_int = infolist_new_var_integer (item, "test_integer", 123456);
|
||||
CHECK(var_int);
|
||||
|
||||
/* check content of variable */
|
||||
STRCMP_EQUAL("test_integer", var_int->name);
|
||||
LONGS_EQUAL(INFOLIST_INTEGER, var_int->type);
|
||||
LONGS_EQUAL(123456, *((int *)var_int->value));
|
||||
LONGS_EQUAL(0, var_int->size);
|
||||
POINTERS_EQUAL(NULL, var_int->prev_var);
|
||||
POINTERS_EQUAL(NULL, var_int->next_var);
|
||||
|
||||
/* check that variable is in item */
|
||||
POINTERS_EQUAL(var_int, item->vars);
|
||||
POINTERS_EQUAL(var_int, item->last_var);
|
||||
|
||||
/* add a string variable */
|
||||
var_str = infolist_new_var_string (item, "test_string", "abc");
|
||||
CHECK(var_str);
|
||||
|
||||
/* check content of variable */
|
||||
STRCMP_EQUAL("test_string", var_str->name);
|
||||
LONGS_EQUAL(INFOLIST_STRING, var_str->type);
|
||||
STRCMP_EQUAL("abc", (const char *)var_str->value);
|
||||
LONGS_EQUAL(0, var_str->size);
|
||||
POINTERS_EQUAL(var_int, var_str->prev_var);
|
||||
POINTERS_EQUAL(NULL, var_str->next_var);
|
||||
|
||||
/* check that variable is in item */
|
||||
POINTERS_EQUAL(var_int, item->vars);
|
||||
POINTERS_EQUAL(var_str, item->last_var);
|
||||
|
||||
/* add a pointer variable */
|
||||
var_ptr = infolist_new_var_pointer (item, "test_pointer",
|
||||
(void *)0x123abc);
|
||||
CHECK(var_ptr);
|
||||
|
||||
/* check content of variable */
|
||||
STRCMP_EQUAL("test_pointer", var_ptr->name);
|
||||
LONGS_EQUAL(INFOLIST_POINTER, var_ptr->type);
|
||||
POINTERS_EQUAL(0x123abc, var_ptr->value);
|
||||
LONGS_EQUAL(0, var_ptr->size);
|
||||
POINTERS_EQUAL(var_str, var_ptr->prev_var);
|
||||
POINTERS_EQUAL(NULL, var_ptr->next_var);
|
||||
|
||||
/* check that variable is in item */
|
||||
POINTERS_EQUAL(var_int, item->vars);
|
||||
POINTERS_EQUAL(var_ptr, item->last_var);
|
||||
|
||||
/* add a buffer variable */
|
||||
var_buf = infolist_new_var_buffer (item, "test_buffer", (void *)buffer, 3);
|
||||
CHECK(var_buf);
|
||||
|
||||
/* check content of variable */
|
||||
STRCMP_EQUAL("test_buffer", var_buf->name);
|
||||
LONGS_EQUAL(INFOLIST_BUFFER, var_buf->type);
|
||||
LONGS_EQUAL(12, ((char *)var_buf->value)[0]);
|
||||
LONGS_EQUAL(34, ((char *)var_buf->value)[1]);
|
||||
LONGS_EQUAL(56, ((char *)var_buf->value)[2]);
|
||||
LONGS_EQUAL(3, var_buf->size);
|
||||
POINTERS_EQUAL(var_ptr, var_buf->prev_var);
|
||||
POINTERS_EQUAL(NULL, var_buf->next_var);
|
||||
|
||||
/* check that variable is in item */
|
||||
POINTERS_EQUAL(var_int, item->vars);
|
||||
POINTERS_EQUAL(var_buf, item->last_var);
|
||||
|
||||
/* add a buffer variable */
|
||||
var_time = infolist_new_var_time (item, "test_time", 1234567890);
|
||||
CHECK(var_time);
|
||||
|
||||
/* check content of variable */
|
||||
STRCMP_EQUAL("test_time", var_time->name);
|
||||
LONGS_EQUAL(INFOLIST_TIME, var_time->type);
|
||||
LONGS_EQUAL(1234567890, *((int *)var_time->value));
|
||||
LONGS_EQUAL(0, var_time->size);
|
||||
POINTERS_EQUAL(var_buf, var_time->prev_var);
|
||||
POINTERS_EQUAL(NULL, var_time->next_var);
|
||||
|
||||
/* check that variable is in item */
|
||||
POINTERS_EQUAL(var_int, item->vars);
|
||||
POINTERS_EQUAL(var_time, item->last_var);
|
||||
|
||||
infolist_free (infolist);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* infolist_valid
|
||||
* infolist_free
|
||||
*/
|
||||
|
||||
TEST(Infolist, Valid)
|
||||
{
|
||||
/* TODO: write tests */
|
||||
struct t_infolist *infolist;
|
||||
|
||||
LONGS_EQUAL(0, infolist_valid (NULL));
|
||||
LONGS_EQUAL(0, infolist_valid ((struct t_infolist *)0x1));
|
||||
|
||||
infolist = infolist_new (NULL);
|
||||
LONGS_EQUAL(1, infolist_valid (infolist));
|
||||
|
||||
infolist_free (infolist);
|
||||
|
||||
LONGS_EQUAL(0, infolist_valid (infolist));
|
||||
}
|
||||
|
||||
/*
|
||||
@ -63,7 +262,41 @@ TEST(Infolist, Valid)
|
||||
|
||||
TEST(Infolist, Search)
|
||||
{
|
||||
/* TODO: write tests */
|
||||
struct t_infolist *infolist;
|
||||
struct t_infolist_item *ptr_item;
|
||||
struct t_infolist_var *ptr_var;
|
||||
|
||||
infolist = hook_infolist_get (NULL, "infolist_test", NULL, "test2");
|
||||
|
||||
/* move to first item in infolist */
|
||||
ptr_item = infolist->items;
|
||||
POINTERS_EQUAL(ptr_item, infolist_next (infolist));
|
||||
|
||||
/* search the first variable */
|
||||
ptr_var = infolist_search_var (infolist, "integer");
|
||||
POINTERS_EQUAL(ptr_item->vars, ptr_var);
|
||||
|
||||
/* search the second variable */
|
||||
ptr_var = infolist_search_var (infolist, "string");
|
||||
POINTERS_EQUAL(ptr_item->vars->next_var, ptr_var);
|
||||
|
||||
/* search an unknown variable */
|
||||
ptr_var = infolist_search_var (infolist, "string2");
|
||||
POINTERS_EQUAL(NULL, ptr_var);
|
||||
|
||||
/* move to second item in infolist */
|
||||
ptr_item = ptr_item->next_item;
|
||||
POINTERS_EQUAL(ptr_item, infolist_next (infolist));
|
||||
|
||||
/* search the first variable */
|
||||
ptr_var = infolist_search_var (infolist, "string2");
|
||||
POINTERS_EQUAL(ptr_item->vars, ptr_var);
|
||||
|
||||
/* search an unknown variable */
|
||||
ptr_var = infolist_search_var (infolist, "string3");
|
||||
POINTERS_EQUAL(NULL, ptr_var);
|
||||
|
||||
infolist_free (infolist);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -75,7 +308,91 @@ TEST(Infolist, Search)
|
||||
|
||||
TEST(Infolist, Move)
|
||||
{
|
||||
/* TODO: write tests */
|
||||
struct t_infolist *infolist;
|
||||
|
||||
infolist = hook_infolist_get (NULL, "infolist_test", NULL, "test2");
|
||||
|
||||
POINTERS_EQUAL(NULL, infolist->ptr_item);
|
||||
|
||||
/* move to first item in infolist */
|
||||
POINTERS_EQUAL(infolist->items, infolist_next (infolist));
|
||||
POINTERS_EQUAL(infolist->items, infolist->ptr_item);
|
||||
|
||||
/* reset item cursor */
|
||||
infolist_reset_item_cursor (infolist);
|
||||
POINTERS_EQUAL(NULL, infolist->ptr_item);
|
||||
|
||||
infolist_next (infolist);
|
||||
|
||||
/* move to second item in infolist */
|
||||
POINTERS_EQUAL(infolist->items->next_item, infolist_next (infolist));
|
||||
POINTERS_EQUAL(infolist->items->next_item, infolist->ptr_item);
|
||||
|
||||
/* move back to first item in infolist */
|
||||
POINTERS_EQUAL(infolist->items, infolist_prev (infolist));
|
||||
POINTERS_EQUAL(infolist->items, infolist->ptr_item);
|
||||
|
||||
/* move before first item in infolist */
|
||||
POINTERS_EQUAL(NULL, infolist_prev (infolist));
|
||||
POINTERS_EQUAL(NULL, infolist->ptr_item);
|
||||
|
||||
/* move after second item in infolist */
|
||||
infolist_next (infolist);
|
||||
infolist_next (infolist);
|
||||
POINTERS_EQUAL(NULL, infolist_next (infolist));
|
||||
POINTERS_EQUAL(NULL, infolist->ptr_item);
|
||||
|
||||
infolist_free (infolist);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* infolist_get
|
||||
*/
|
||||
|
||||
TEST(Infolist, Get)
|
||||
{
|
||||
struct t_infolist *infolist;
|
||||
struct t_infolist_var *ptr_var;
|
||||
|
||||
/* get an infolist with one item */
|
||||
infolist = hook_infolist_get (NULL, "infolist_test", NULL, NULL);
|
||||
CHECK(infolist);
|
||||
|
||||
/* check that there is only one item */
|
||||
CHECK(infolist->items);
|
||||
POINTERS_EQUAL(NULL, infolist->items->next_item);
|
||||
|
||||
infolist_free (infolist);
|
||||
|
||||
/* get an infolist with two items */
|
||||
infolist = hook_infolist_get (NULL, "infolist_test", NULL, "test2");
|
||||
CHECK(infolist);
|
||||
|
||||
/* check that there are exactly two items */
|
||||
CHECK(infolist->items);
|
||||
CHECK(infolist->items->next_item);
|
||||
POINTERS_EQUAL(NULL, infolist->items->next_item->next_item);
|
||||
|
||||
/* check variables in first item */
|
||||
ptr_var = infolist->items->vars;
|
||||
STRCMP_EQUAL("integer", ptr_var->name);
|
||||
ptr_var = ptr_var->next_var;
|
||||
STRCMP_EQUAL("string", ptr_var->name);
|
||||
ptr_var = ptr_var->next_var;
|
||||
STRCMP_EQUAL("pointer", ptr_var->name);
|
||||
ptr_var = ptr_var->next_var;
|
||||
STRCMP_EQUAL("buffer", ptr_var->name);
|
||||
ptr_var = ptr_var->next_var;
|
||||
STRCMP_EQUAL("time", ptr_var->name);
|
||||
LONGS_EQUAL(NULL, ptr_var->next_var);
|
||||
|
||||
/* check variables in second item */
|
||||
ptr_var = infolist->items->next_item->vars;
|
||||
STRCMP_EQUAL("string2", ptr_var->name);
|
||||
LONGS_EQUAL(NULL, ptr_var->next_var);
|
||||
|
||||
infolist_free (infolist);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -87,20 +404,58 @@ TEST(Infolist, Move)
|
||||
* infolist_time
|
||||
*/
|
||||
|
||||
TEST(Infolist, Get)
|
||||
TEST(Infolist, GetValues)
|
||||
{
|
||||
/* TODO: write tests */
|
||||
struct t_infolist *infolist;
|
||||
void *ptr_buffer;
|
||||
int size;
|
||||
|
||||
infolist = hook_infolist_get (NULL, "infolist_test", NULL, "test2");
|
||||
CHECK(infolist);
|
||||
|
||||
infolist_next (infolist);
|
||||
|
||||
LONGS_EQUAL(123456, infolist_integer (infolist, "integer"));
|
||||
STRCMP_EQUAL("test string", infolist_string (infolist, "string"));
|
||||
LONGS_EQUAL(0x123abc, infolist_pointer (infolist, "pointer"));
|
||||
ptr_buffer = infolist_buffer (infolist, "buffer", &size);
|
||||
LONGS_EQUAL(4, size);
|
||||
LONGS_EQUAL('a', ((const char *)ptr_buffer)[0]);
|
||||
LONGS_EQUAL('b', ((const char *)ptr_buffer)[1]);
|
||||
LONGS_EQUAL('c', ((const char *)ptr_buffer)[2]);
|
||||
LONGS_EQUAL(0, ((const char *)ptr_buffer)[3]);
|
||||
LONGS_EQUAL(1234567890, infolist_time (infolist, "time"));
|
||||
|
||||
infolist_free (infolist);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* infolist_free
|
||||
* infolist_free_all_plugin
|
||||
* infolist_fields
|
||||
*/
|
||||
|
||||
TEST(Infolist, Free)
|
||||
TEST(Infolist, Fields)
|
||||
{
|
||||
/* TODO: write tests */
|
||||
struct t_infolist *infolist;
|
||||
const char *fields1 = "i:integer,s:string,p:pointer,b:buffer,t:time";
|
||||
const char *fields2 = "s:string2";
|
||||
|
||||
infolist = hook_infolist_get (NULL, "infolist_test", NULL, "test2");
|
||||
CHECK(infolist);
|
||||
|
||||
/* check fields in first item */
|
||||
infolist_next (infolist);
|
||||
LONGS_EQUAL(NULL, infolist->items->fields);
|
||||
STRCMP_EQUAL(fields1, infolist_fields (infolist));
|
||||
STRCMP_EQUAL(fields1, infolist->items->fields);
|
||||
|
||||
/* check fields in second item */
|
||||
infolist_next (infolist);
|
||||
LONGS_EQUAL(NULL, infolist->items->next_item->fields);
|
||||
STRCMP_EQUAL(fields2, infolist_fields (infolist));
|
||||
STRCMP_EQUAL(fields2, infolist->items->next_item->fields);
|
||||
|
||||
infolist_free (infolist);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user