From aa85e29aefcfee3c3184cf61dc107e073d8e8317 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 15 Sep 2014 20:55:53 +0100 Subject: Added tests for form_remove_text_multi_value --- tests/test_form.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++- tests/test_form.h | 8 ++- tests/testsuite.c | 8 ++- 3 files changed, 171 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/test_form.c b/tests/test_form.c index 37cf2c5d..d358736d 100644 --- a/tests/test_form.c +++ b/tests/test_form.c @@ -546,7 +546,7 @@ void remove_value_does_nothing_when_doesnt_exist(void **state) form_destroy(form); } -void remove_value_does_removes_when_one(void **state) +void remove_value_removes_when_one(void **state) { form_init_module(); @@ -577,7 +577,7 @@ void remove_value_does_removes_when_one(void **state) form_destroy(form); } -void remove_value_does_removes_when_many(void **state) +void remove_value_removes_when_many(void **state) { form_init_module(); @@ -596,17 +596,174 @@ void remove_value_does_removes_when_many(void **state) gboolean res = form_remove_value(form, "tag1", "value2"); int length = -1; + int value_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, "value2") == 0) { + value_count++; + } + curr_value = g_slist_next(curr_value); + } } curr_field = g_slist_next(curr_field); } assert_true(res); assert_int_equal(length, 3); + assert_int_equal(value_count, 0); form_destroy(form); } + +void remove_text_multi_value_does_nothing_when_none(void **state) +{ + form_init_module(); + + DataForm *form = _new_form(); + g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1")); + + FormField *field1 = _new_field(); + field1->var = strdup("var1"); + field1->type_t = FIELD_LIST_MULTI; + form->fields = g_slist_append(form->fields, field1); + + gboolean res = form_remove_text_multi_value(form, "tag1", 3); + + int length = -1; + 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); + } + curr_field = g_slist_next(curr_field); + } + + assert_false(res); + assert_int_equal(length, 0); + + form_destroy(form); +} + +void remove_text_multi_value_does_nothing_when_doesnt_exist(void **state) +{ + form_init_module(); + + DataForm *form = _new_form(); + g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1")); + + FormField *field1 = _new_field(); + field1->var = strdup("var1"); + field1->type_t = FIELD_LIST_MULTI; + field1->values = g_slist_append(field1->values, strdup("value1")); + field1->values = g_slist_append(field1->values, strdup("value2")); + field1->values = g_slist_append(field1->values, strdup("value3")); + field1->values = g_slist_append(field1->values, strdup("value4")); + form->fields = g_slist_append(form->fields, field1); + + gboolean res = form_remove_text_multi_value(form, "tag1", 5); + + int length = -1; + int value_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, "value5") == 0) { + value_count++; + } + curr_value = g_slist_next(curr_value); + } + } + curr_field = g_slist_next(curr_field); + } + + assert_false(res); + assert_int_equal(length, 4); + assert_int_equal(value_count, 0); + + form_destroy(form); +} + +void remove_text_multi_value_removes_when_one(void **state) +{ + form_init_module(); + + DataForm *form = _new_form(); + g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1")); + + FormField *field1 = _new_field(); + field1->var = strdup("var1"); + field1->type_t = FIELD_LIST_MULTI; + field1->values = g_slist_append(field1->values, strdup("value4")); + form->fields = g_slist_append(form->fields, field1); + + gboolean res = form_remove_text_multi_value(form, "tag1", 1); + + int length = -1; + 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); + } + curr_field = g_slist_next(curr_field); + } + + assert_true(res); + assert_int_equal(length, 0); + + form_destroy(form); +} + +void remove_text_multi_value_removes_when_many(void **state) +{ + form_init_module(); + + DataForm *form = _new_form(); + g_hash_table_insert(form->tag_to_var, strdup("tag1"), strdup("var1")); + + FormField *field1 = _new_field(); + field1->var = strdup("var1"); + field1->type_t = FIELD_LIST_MULTI; + field1->values = g_slist_append(field1->values, strdup("value1")); + field1->values = g_slist_append(field1->values, strdup("value2")); + field1->values = g_slist_append(field1->values, strdup("value3")); + field1->values = g_slist_append(field1->values, strdup("value4")); + form->fields = g_slist_append(form->fields, field1); + + gboolean res = form_remove_text_multi_value(form, "tag1", 2); + + int length = -1; + int value_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, "value2") == 0) { + value_count++; + } + curr_value = g_slist_next(curr_value); + } + } + curr_field = g_slist_next(curr_field); + } + + assert_true(res); + assert_int_equal(length, 3); + assert_int_equal(value_count, 0); + + form_destroy(form); +} + diff --git a/tests/test_form.h b/tests/test_form.h index 74d05a0a..65911d0a 100644 --- a/tests/test_form.h +++ b/tests/test_form.h @@ -13,5 +13,9 @@ void add_value_adds_when_some(void **state); void add_value_adds_when_exists(void **state); void remove_value_does_nothing_when_none(void **state); void remove_value_does_nothing_when_doesnt_exist(void **state); -void remove_value_does_removes_when_one(void **state); -void remove_value_does_removes_when_many(void **state); +void remove_value_removes_when_one(void **state); +void remove_value_removes_when_many(void **state); +void remove_text_multi_value_does_nothing_when_none(void **state); +void remove_text_multi_value_does_nothing_when_doesnt_exist(void **state); +void remove_text_multi_value_removes_when_one(void **state); +void remove_text_multi_value_removes_when_many(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 0fe5aae3..3441ceb9 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -546,8 +546,12 @@ int main(int argc, char* argv[]) { unit_test(add_value_adds_when_exists), unit_test(remove_value_does_nothing_when_none), unit_test(remove_value_does_nothing_when_doesnt_exist), - unit_test(remove_value_does_removes_when_one), - unit_test(remove_value_does_removes_when_many), + unit_test(remove_value_removes_when_one), + unit_test(remove_value_removes_when_many), + unit_test(remove_text_multi_value_does_nothing_when_none), + unit_test(remove_text_multi_value_does_nothing_when_doesnt_exist), + unit_test(remove_text_multi_value_removes_when_one), + unit_test(remove_text_multi_value_removes_when_many), }; return run_tests(all_tests); -- cgit 1.4.1-2-gfad0