diff options
-rw-r--r-- | tests/test_form.c | 118 | ||||
-rw-r--r-- | tests/test_form.h | 3 | ||||
-rw-r--r-- | tests/testsuite.c | 3 |
3 files changed, 124 insertions, 0 deletions
diff --git a/tests/test_form.c b/tests/test_form.c index 58b454f4..e5ab89c8 100644 --- a/tests/test_form.c +++ b/tests/test_form.c @@ -354,3 +354,121 @@ void add_unique_value_adds_when_doesnt_exist(void **state) form_destroy(form); } + +void add_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")); + + FormField *field1 = _new_field(); + field1->var = strdup("var1"); + field1->type_t = FIELD_LIST_MULTI; + form->fields = g_slist_append(form->fields, field1); + + form_add_value(form, "tag1", "somevalue"); + + 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_int_equal(length, 1); + assert_string_equal(value, "somevalue"); + + form_destroy(form); +} + +void add_value_adds_when_some(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("some text")); + field1->values = g_slist_append(field1->values, strdup("some more text")); + field1->values = g_slist_append(field1->values, strdup("yet some more text")); + form->fields = g_slist_append(form->fields, field1); + + form_add_value(form, "tag1", "new value"); + + int num_values = 0; + int new_value_count = 0; + GSList *curr_field = form->fields; + while (curr_field != NULL) { + FormField *field = curr_field->data; + if (g_strcmp0(field->var, "var1") == 0) { + GSList *curr_value = field->values; + while (curr_value != NULL) { + num_values++; + if (g_strcmp0(curr_value->data, "new value") == 0) { + new_value_count++; + } + curr_value = g_slist_next(curr_value); + } + break; + } + curr_field = g_slist_next(curr_field); + } + + assert_int_equal(num_values, 4); + assert_int_equal(new_value_count, 1); + + form_destroy(form); +} + +void add_value_adds_when_exists(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("some text")); + field1->values = g_slist_append(field1->values, strdup("some more text")); + field1->values = g_slist_append(field1->values, strdup("yet some more text")); + field1->values = g_slist_append(field1->values, strdup("new value")); + form->fields = g_slist_append(form->fields, field1); + + form_add_value(form, "tag1", "new value"); + + int num_values = 0; + int new_value_count = 0; + GSList *curr_field = form->fields; + while (curr_field != NULL) { + FormField *field = curr_field->data; + if (g_strcmp0(field->var, "var1") == 0) { + GSList *curr_value = field->values; + while (curr_value != NULL) { + num_values++; + if (g_strcmp0(curr_value->data, "new value") == 0) { + new_value_count++; + } + curr_value = g_slist_next(curr_value); + } + break; + } + curr_field = g_slist_next(curr_field); + } + + assert_int_equal(num_values, 5); + assert_int_equal(new_value_count, 2); + + form_destroy(form); +} diff --git a/tests/test_form.h b/tests/test_form.h index 1daaefa7..e4f7c15c 100644 --- a/tests/test_form.h +++ b/tests/test_form.h @@ -8,3 +8,6 @@ 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); +void add_value_adds_when_none(void **state); +void add_value_adds_when_some(void **state); +void add_value_adds_when_exists(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 0219a2ca..2dc3e706 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -541,6 +541,9 @@ int main(int argc, char* argv[]) { 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), + unit_test(add_value_adds_when_none), + unit_test(add_value_adds_when_some), + unit_test(add_value_adds_when_exists), }; return run_tests(all_tests); |