mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Added form tests
This commit is contained in:
parent
de0ce0b80d
commit
b934ad54d5
@ -11,7 +11,7 @@ core_sources = \
|
||||
src/xmpp/capabilities.h src/xmpp/connection.h \
|
||||
src/xmpp/roster.c src/xmpp/roster.h \
|
||||
src/xmpp/bookmark.c src/xmpp/bookmark.h \
|
||||
src/xmpp/form.c src/xmpp/form.h \
|
||||
src/xmpp/form.c src/xmpp/form.h \
|
||||
src/server_events.c src/server_events.h \
|
||||
src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \
|
||||
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
||||
@ -39,6 +39,7 @@ tests_sources = \
|
||||
src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \
|
||||
src/resource.c src/resource.h \
|
||||
src/roster_list.c src/roster_list.h \
|
||||
src/xmpp/form.c src/xmpp/form.h \
|
||||
src/xmpp/xmpp.h \
|
||||
src/ui/ui.h \
|
||||
src/command/command.h src/command/command.c src/command/history.c \
|
||||
@ -86,6 +87,7 @@ tests_sources = \
|
||||
tests/test_muc.c tests/test_muc.h \
|
||||
tests/test_cmd_roster.c tests/test_cmd_roster.h \
|
||||
tests/test_cmd_win.c tests/test_cmd_win.h \
|
||||
tests/test_form.c tests/test_form.h \
|
||||
tests/testsuite.c
|
||||
|
||||
main_source = src/main.c
|
||||
|
@ -62,10 +62,12 @@ autocomplete_new(void)
|
||||
void
|
||||
autocomplete_clear(Autocomplete ac)
|
||||
{
|
||||
g_slist_free_full(ac->items, free);
|
||||
ac->items = NULL;
|
||||
if (ac != NULL) {
|
||||
g_slist_free_full(ac->items, free);
|
||||
ac->items = NULL;
|
||||
|
||||
autocomplete_reset(ac);
|
||||
autocomplete_reset(ac);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -335,4 +337,4 @@ _search_from(Autocomplete ac, GSList *curr, gboolean quote)
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,8 @@
|
||||
#ifndef FORM_H
|
||||
#define FROM_H
|
||||
|
||||
#include "xmpp/xmpp.h"
|
||||
|
||||
DataForm* form_create(xmpp_stanza_t * const stanza);
|
||||
xmpp_stanza_t* form_create_submission(DataForm *form);
|
||||
|
||||
|
356
tests/test_form.c
Normal file
356
tests/test_form.c
Normal file
@ -0,0 +1,356 @@
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "xmpp/form.h"
|
||||
#include "xmpp/mock_xmpp.h"
|
||||
|
||||
xmpp_ctx_t* connection_get_ctx(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static DataForm*
|
||||
_new_form(void)
|
||||
{
|
||||
DataForm *form = malloc(sizeof(DataForm));
|
||||
form->type = NULL;
|
||||
form->title = NULL;
|
||||
form->instructions = NULL;
|
||||
form->fields = NULL;
|
||||
form->var_to_tag = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||||
form->tag_to_var = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||||
form->tag_ac = NULL;
|
||||
|
||||
return form;
|
||||
}
|
||||
|
||||
static FormField*
|
||||
_new_field(void)
|
||||
{
|
||||
FormField *field = malloc(sizeof(FormField));
|
||||
field->label = NULL;
|
||||
field->type = NULL;
|
||||
field->description = NULL;
|
||||
field->required = FALSE;
|
||||
field->options = NULL;
|
||||
field->var = NULL;
|
||||
field->values = NULL;
|
||||
|
||||
return field;
|
||||
|
||||
}
|
||||
|
||||
void get_form_type_field_returns_null_no_fields(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
|
||||
char *result = form_get_form_type_field(form);
|
||||
|
||||
assert_null(result);
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void get_form_type_field_returns_null_when_not_present(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
FormField *field = _new_field();
|
||||
field->var = strdup("var1");
|
||||
field->values = g_slist_append(field->values, strdup("value1"));
|
||||
form->fields = g_slist_append(form->fields, field);
|
||||
|
||||
char *result = form_get_form_type_field(form);
|
||||
|
||||
assert_null(result);
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void get_form_type_field_returns_value_when_present(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->values = g_slist_append(field1->values, strdup("value1"));
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("FORM_TYPE");
|
||||
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
FormField *field3 = _new_field();
|
||||
field3->var = strdup("var3");
|
||||
field3->values = g_slist_append(field3->values, strdup("value3"));
|
||||
form->fields = g_slist_append(form->fields, field3);
|
||||
|
||||
char *result = form_get_form_type_field(form);
|
||||
|
||||
assert_string_equal(result, "value2");
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void get_field_type_returns_unknown_when_no_fields(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
|
||||
form_field_type_t result = form_get_field_type(form, "tag");
|
||||
|
||||
assert_int_equal(result, FIELD_UNKNOWN);
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void get_field_type_returns_correct_type(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->type_t = FIELD_TEXT_SINGLE;
|
||||
field1->values = g_slist_append(field1->values, strdup("value1"));
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("var2");
|
||||
field2->type_t = FIELD_TEXT_MULTI;
|
||||
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
form_field_type_t result = form_get_field_type(form, "tag2");
|
||||
|
||||
assert_int_equal(result, FIELD_TEXT_MULTI);
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void set_value_adds_when_none(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->type_t = FIELD_TEXT_SINGLE;
|
||||
field1->values = g_slist_append(field1->values, strdup("value1"));
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("var2");
|
||||
field2->type_t = FIELD_LIST_SINGLE;
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
form_set_value(form, "tag2", "a new value");
|
||||
|
||||
int length = 0;
|
||||
char *value = NULL;
|
||||
GSList *curr_field = form->fields;
|
||||
while (curr_field != NULL) {
|
||||
FormField *field = curr_field->data;
|
||||
if (g_strcmp0(field->var, "var2") == 0) {
|
||||
length = g_slist_length(field->values);
|
||||
value = field->values->data;
|
||||
break;
|
||||
}
|
||||
curr_field = g_slist_next(curr_field);
|
||||
}
|
||||
|
||||
assert_int_equal(length, 1);
|
||||
assert_string_equal(value, "a new value");
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void set_value_updates_when_one(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->type_t = FIELD_TEXT_SINGLE;
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("var2");
|
||||
field2->type_t = FIELD_LIST_SINGLE;
|
||||
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
form_set_value(form, "tag2", "a new value");
|
||||
|
||||
int length = 0;
|
||||
char *value = NULL;
|
||||
GSList *curr_field = form->fields;
|
||||
while (curr_field != NULL) {
|
||||
FormField *field = curr_field->data;
|
||||
if (g_strcmp0(field->var, "var2") == 0) {
|
||||
length = g_slist_length(field->values);
|
||||
value = field->values->data;
|
||||
break;
|
||||
}
|
||||
curr_field = g_slist_next(curr_field);
|
||||
}
|
||||
|
||||
assert_int_equal(length, 1);
|
||||
assert_string_equal(value, "a new value");
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void add_unique_value_adds_when_none(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->type_t = FIELD_JID_MULTI;
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("var2");
|
||||
field2->type_t = FIELD_LIST_SINGLE;
|
||||
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
gboolean ret = form_add_unique_value(form, "tag1", "me@server.com");
|
||||
|
||||
int length = 0;
|
||||
char *value = NULL;
|
||||
GSList *curr_field = form->fields;
|
||||
while (curr_field != NULL) {
|
||||
FormField *field = curr_field->data;
|
||||
if (g_strcmp0(field->var, "var1") == 0) {
|
||||
length = g_slist_length(field->values);
|
||||
value = field->values->data;
|
||||
break;
|
||||
}
|
||||
curr_field = g_slist_next(curr_field);
|
||||
}
|
||||
|
||||
assert_true(ret);
|
||||
assert_int_equal(length, 1);
|
||||
assert_string_equal(value, "me@server.com");
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void add_unique_value_does_nothing_when_exists(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->type_t = FIELD_JID_MULTI;
|
||||
field1->values = g_slist_append(field1->values, strdup("me@server.com"));
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("var2");
|
||||
field2->type_t = FIELD_LIST_SINGLE;
|
||||
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
gboolean ret = form_add_unique_value(form, "tag1", "me@server.com");
|
||||
|
||||
int length = 0;
|
||||
char *value = NULL;
|
||||
GSList *curr_field = form->fields;
|
||||
while (curr_field != NULL) {
|
||||
FormField *field = curr_field->data;
|
||||
if (g_strcmp0(field->var, "var1") == 0) {
|
||||
length = g_slist_length(field->values);
|
||||
value = field->values->data;
|
||||
break;
|
||||
}
|
||||
curr_field = g_slist_next(curr_field);
|
||||
}
|
||||
|
||||
assert_false(ret);
|
||||
assert_int_equal(length, 1);
|
||||
assert_string_equal(value, "me@server.com");
|
||||
|
||||
form_destroy(form);
|
||||
}
|
||||
|
||||
void add_unique_value_adds_when_doesnt_exist(void **state)
|
||||
{
|
||||
form_init_module();
|
||||
|
||||
DataForm *form = _new_form();
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1"));
|
||||
g_hash_table_insert(form->tag_to_var, strdup("tag2"), strdup("var2"));
|
||||
|
||||
FormField *field1 = _new_field();
|
||||
field1->var = strdup("var1");
|
||||
field1->type_t = FIELD_JID_MULTI;
|
||||
field1->values = g_slist_append(field1->values, strdup("dolan@server.com"));
|
||||
field1->values = g_slist_append(field1->values, strdup("kieran@server.com"));
|
||||
field1->values = g_slist_append(field1->values, strdup("chi@server.com"));
|
||||
form->fields = g_slist_append(form->fields, field1);
|
||||
|
||||
FormField *field2 = _new_field();
|
||||
field2->var = strdup("var2");
|
||||
field2->type_t = FIELD_LIST_SINGLE;
|
||||
field2->values = g_slist_append(field2->values, strdup("value2"));
|
||||
form->fields = g_slist_append(form->fields, field2);
|
||||
|
||||
gboolean ret = form_add_unique_value(form, "tag1", "me@server.com");
|
||||
|
||||
int length = 0;
|
||||
int count = 0;
|
||||
GSList *curr_field = form->fields;
|
||||
while (curr_field != NULL) {
|
||||
FormField *field = curr_field->data;
|
||||
if (g_strcmp0(field->var, "var1") == 0) {
|
||||
length = g_slist_length(field->values);
|
||||
GSList *curr_value = field->values;
|
||||
while (curr_value != NULL) {
|
||||
if (g_strcmp0(curr_value->data, "me@server.com") == 0) {
|
||||
count++;
|
||||
}
|
||||
curr_value = g_slist_next(curr_value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
curr_field = g_slist_next(curr_field);
|
||||
}
|
||||
|
||||
assert_true(ret);
|
||||
assert_int_equal(length, 4);
|
||||
assert_int_equal(count, 1);
|
||||
|
||||
form_destroy(form);
|
||||
}
|
10
tests/test_form.h
Normal file
10
tests/test_form.h
Normal file
@ -0,0 +1,10 @@
|
||||
void get_form_type_field_returns_null_no_fields(void **state);
|
||||
void get_form_type_field_returns_null_when_not_present(void **state);
|
||||
void get_form_type_field_returns_value_when_present(void **state);
|
||||
void get_field_type_returns_unknown_when_no_fields(void **state);
|
||||
void get_field_type_returns_correct_type(void **state);
|
||||
void set_value_adds_when_none(void **state);
|
||||
void set_value_updates_when_one(void **state);
|
||||
void add_unique_value_adds_when_none(void **state);
|
||||
void add_unique_value_does_nothing_when_exists(void **state);
|
||||
void add_unique_value_adds_when_doesnt_exist(void **state);
|
@ -31,6 +31,7 @@
|
||||
#include "test_muc.h"
|
||||
#include "test_cmd_roster.h"
|
||||
#include "test_cmd_win.h"
|
||||
#include "test_form.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
const UnitTest all_tests[] = {
|
||||
@ -529,6 +530,17 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
unit_test(cmd_win_shows_message_when_win_doesnt_exist),
|
||||
unit_test(cmd_win_switches_to_given_win_when_exists),
|
||||
|
||||
unit_test(get_form_type_field_returns_null_no_fields),
|
||||
unit_test(get_form_type_field_returns_null_when_not_present),
|
||||
unit_test(get_form_type_field_returns_value_when_present),
|
||||
unit_test(get_field_type_returns_unknown_when_no_fields),
|
||||
unit_test(get_field_type_returns_correct_type),
|
||||
unit_test(set_value_adds_when_none),
|
||||
unit_test(set_value_updates_when_one),
|
||||
unit_test(add_unique_value_adds_when_none),
|
||||
unit_test(add_unique_value_does_nothing_when_exists),
|
||||
unit_test(add_unique_value_adds_when_doesnt_exist),
|
||||
};
|
||||
|
||||
return run_tests(all_tests);
|
||||
|
Loading…
Reference in New Issue
Block a user