From d1ace3282cb3177e1b37e27f122463ea5da1e9f8 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 15 Sep 2014 00:56:50 +0100 Subject: Added tests for form_add_value --- tests/test_form.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_form.h | 3 ++ tests/testsuite.c | 3 ++ 3 files changed, 124 insertions(+) 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); -- cgit 1.4.1-2-gfad0 ^
53b92561 ^


771dfab1 ^


52e7e596 ^
53b92561 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44