1 #include <gcutter.h>
2 #include <cutter/cut-test-data.h>
3 #include "../lib/cuttest-utils.h"
4 
5 void test_new (void);
6 void test_new_empty (void);
7 void test_new_and_destroy (void);
8 void test_set_name (void);
9 void test_to_xml (void);
10 void test_to_xml_empty (void);
11 void test_to_xml_string (void);
12 
13 static CutTestData *test_data;
14 static gboolean destroy_called;
15 static gchar *destroyed_string;
16 static GString *string;
17 
18 void
cut_setup(void)19 cut_setup (void)
20 {
21     test_data = NULL;
22     destroy_called = FALSE;
23     destroyed_string = NULL;
24     string = NULL;
25 }
26 
27 void
cut_teardown(void)28 cut_teardown (void)
29 {
30     if (test_data)
31         g_object_unref(test_data);
32 
33     if (destroyed_string)
34         g_free(destroyed_string);
35 
36     if (string)
37         g_string_free(string, TRUE);
38 }
39 
40 void
test_new(void)41 test_new (void)
42 {
43     const gchar name[] = "sample test data";
44     gchar value[] = "sample test value";
45 
46     test_data = cut_test_data_new(name, value, NULL);
47     cut_assert_equal_string(name, cut_test_data_get_name(test_data));
48     cut_assert_equal_string(value, cut_test_data_get_value(test_data));
49 }
50 
51 void
test_new_empty(void)52 test_new_empty (void)
53 {
54     test_data = cut_test_data_new_empty();
55     cut_assert_equal_string(NULL, cut_test_data_get_name(test_data));
56     cut_assert_equal_string(NULL, cut_test_data_get_value(test_data));
57 }
58 
59 static void
string_data_free(gpointer data)60 string_data_free (gpointer data)
61 {
62     destroy_called = TRUE;
63     destroyed_string = data;
64 }
65 
66 void
test_new_and_destroy(void)67 test_new_and_destroy (void)
68 {
69     const gchar name[] = "sample test data";
70     const gchar value[] = "sample test value";
71 
72     test_data = cut_test_data_new(name, g_strdup(value), string_data_free);
73     cut_assert_false(destroy_called);
74     cut_assert_equal_string(NULL, destroyed_string);
75     g_object_unref(test_data);
76     test_data = NULL;
77     cut_assert_true(destroy_called);
78     cut_assert_equal_string(value, destroyed_string);
79 }
80 
81 void
test_set_name(void)82 test_set_name (void)
83 {
84     const gchar name[] = "sample test data";
85     const gchar changed_name[] = "changed name";
86 
87     test_data = cut_test_data_new(name, NULL, NULL);
88     cut_assert_equal_string(name, cut_test_data_get_name(test_data));
89 
90     cut_test_data_set_name(test_data, changed_name);
91     cut_assert_equal_string(changed_name, cut_test_data_get_name(test_data));
92 }
93 
94 void
test_to_xml(void)95 test_to_xml (void)
96 {
97     const gchar name[] = "sample test data";
98     const gchar value[] = "sample test value";
99     const gchar expected[] =
100         "<test-data>\n"
101         "  <name>sample test data</name>\n"
102         "</test-data>\n";
103 
104     test_data = cut_test_data_new(name, g_strdup(value), g_free);
105     cut_assert_equal_string_with_free(expected, cut_test_data_to_xml(test_data));
106 }
107 
108 void
test_to_xml_empty(void)109 test_to_xml_empty (void)
110 {
111     const gchar expected[] =
112         "<test-data>\n"
113         "</test-data>\n";
114 
115     test_data = cut_test_data_new_empty();
116     cut_assert_equal_string_with_free(expected, cut_test_data_to_xml(test_data));
117 }
118 
119 void
test_to_xml_string(void)120 test_to_xml_string (void)
121 {
122     const gchar name[] = "sample test data";
123     const gchar value[] = "sample test value";
124     const gchar expected[] =
125         "before content\n"
126         "  <test-data>\n"
127         "    <name>sample test data</name>\n"
128         "  </test-data>\n";
129 
130     test_data = cut_test_data_new(name, g_strdup(value), g_free);
131     string = g_string_new("before content\n");
132     cut_test_data_to_xml_string(test_data, string, 2);
133     cut_assert_equal_string(expected, string->str);
134 }
135 
136 /*
137 vi:ts=4:nowrap:ai:expandtab:sw=4
138 */
139