Debian Codegraph @

tests/wp/state.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/test-log.h"
10
11 static void
12 test_state_basic (void)
13 {
14 g_autoptr (GError) error = NULL;
15 g_autoptr (WpState) state = wp_state_new ("basic");
16 g_assert_nonnull (state);
17
18 g_assert_cmpstr (wp_state_get_name (state), ==, "basic");
19 g_assert_true (g_str_has_suffix (wp_state_get_location (state), "basic"));
20
21 /* Save */
22 {
23 g_autoptr (WpProperties) props = wp_properties_new_empty ();
24 wp_properties_set (props, "key1", "value1");
25 wp_properties_set (props, "key2", "value2");
26 wp_properties_set (props, "key3", "value3");
27 g_assert_true (wp_state_save (state, props, &error));
28 g_assert_no_error (error);
29 }
30
31 /* Load */
32 {
33 g_autoptr (WpProperties) props = wp_state_load (state);
34 g_assert_nonnull (props);
35 g_assert_cmpstr (wp_properties_get (props, "key1"), ==, "value1");
36 g_assert_cmpstr (wp_properties_get (props, "key2"), ==, "value2");
37 g_assert_cmpstr (wp_properties_get (props, "key2"), ==, "value2");
38 g_assert_null (wp_properties_get (props, "invalid"));
39 }
40
41 /* Re-Save */
42 {
43 g_autoptr (WpProperties) props = wp_properties_new_empty ();
44 wp_properties_set (props, "new-key", "new-value");
45 g_assert_true (wp_state_save (state, props, &error));
46 g_assert_no_error (error);
47 }
48
49 /* Re-Load */
50 {
51 g_autoptr (WpProperties) props = wp_state_load (state);
52 g_assert_nonnull (props);
53 g_assert_cmpstr (wp_properties_get (props, "new-key"), ==, "new-value");
54 g_assert_null (wp_properties_get (props, "key1"));
55 g_assert_null (wp_properties_get (props, "key2"));
56 g_assert_null (wp_properties_get (props, "key3"));
57 }
58
59 wp_state_clear (state);
60
61 /* Load empty */
62 {
63 g_autoptr (WpProperties) props = wp_state_load (state);
64 g_assert_nonnull (props);
65 g_assert_null (wp_properties_get (props, "new-key"));
66 g_assert_null (wp_properties_get (props, "key1"));
67 g_assert_null (wp_properties_get (props, "key2"));
68 g_assert_null (wp_properties_get (props, "key3"));
69 }
70
71 wp_state_clear (state);
72 }
73
74 static void
75 test_state_empty (void)
76 {
77 g_autoptr (GError) error = NULL;
78 g_autoptr (WpState) state = wp_state_new ("empty");
79 g_assert_nonnull (state);
80
81 /* Save */
82 {
83 g_autoptr (WpProperties) props = wp_properties_new_empty ();
84 wp_properties_set (props, "key", "value");
85 g_assert_true (wp_state_save (state, props, &error));
86 g_assert_no_error (error);
87 }
88
89 /* Load */
90 {
91 g_autoptr (WpProperties) props = wp_state_load (state);
92 g_assert_nonnull (props);
93 g_assert_cmpstr (wp_properties_get (props, "key"), ==, "value");
94 }
95
96 /* Save empty */
97 {
98 g_autoptr (WpProperties) props = wp_properties_new_empty ();
99 g_assert_true (wp_state_save (state, props, &error));
100 g_assert_no_error (error);
101 }
102
103 /* Load empty */
104 {
105 g_autoptr (WpProperties) props = wp_state_load (state);
106 g_assert_nonnull (props);
107 g_assert_null (wp_properties_get (props, "key"));
108 }
109
110 wp_state_clear (state);
111 }
112
113 static void
114 test_state_spaces (void)
115 {
116 g_autoptr (GError) error = NULL;
117 g_autoptr (WpState) state = wp_state_new ("spaces");
118 g_assert_nonnull (state);
119
120 /* Save */
121 {
122 g_autoptr (WpProperties) props = wp_properties_new_empty ();
123 wp_properties_set (props, "key", "value with spaces");
124 g_assert_true (wp_state_save (state, props, &error));
125 g_assert_no_error (error);
126 }
127
128 /* Load */
129 {
130 g_autoptr (WpProperties) props = wp_state_load (state);
131 g_assert_nonnull (props);
132 g_assert_cmpstr (wp_properties_get (props, "key"), ==, "value with spaces");
133 }
134
135 wp_state_clear (state);
136 }
137
138 static void
139 test_state_escaped (void)
140 {
141 g_autoptr (GError) error = NULL;
142 g_autoptr (WpState) state = wp_state_new ("escaped");
143 g_assert_nonnull (state);
144
145 /* Save */
146 {
147 g_autoptr (WpProperties) props = wp_properties_new_empty ();
148 wp_properties_set (props, "[]", "v0");
149 wp_properties_set (props, "[ ]", "v1");
150 wp_properties_set (props, "[=]", "v2");
151 wp_properties_set (props, " [=]", "v3");
152 wp_properties_set (props, "[=] ", "v4");
153 wp_properties_set (props, " [=] ", "v5");
154 wp_properties_set (props, " [ =] ", "v6");
155 wp_properties_set (props, " [= ] ", "v7");
156 wp_properties_set (props, " [ = ] ", "v8");
157 wp_properties_set (props, " [", "v9");
158 wp_properties_set (props, "[ ", "v10");
159 wp_properties_set (props, " [ ", "v11");
160 wp_properties_set (props, " ]", "v12");
161 wp_properties_set (props, "] ", "v13");
162 wp_properties_set (props, " ] ", "v14");
163 wp_properties_set (props, " ", "v15");
164 wp_properties_set (props, "=", "v16");
165 wp_properties_set (props, "\\", "v17");
166 wp_properties_set (props, "\\[", "v18");
167 wp_properties_set (props, "\\a", "v19");
168 wp_properties_set (props, "\\\\", "v20");
169 wp_properties_set (props, "[][", "][]");
170 g_assert_true (wp_state_save (state, props, &error));
171 g_assert_no_error (error);
172 }
173
174 /* Load */
175 {
176 g_autoptr (WpProperties) props = wp_state_load (state);
177 g_assert_nonnull (props);
178 g_assert_cmpstr (wp_properties_get (props, "[]"), ==, "v0");
179 g_assert_cmpstr (wp_properties_get (props, "[ ]"), ==, "v1");
180 g_assert_cmpstr (wp_properties_get (props, "[=]"), ==, "v2");
181 g_assert_cmpstr (wp_properties_get (props, " [=]"), ==, "v3");
182 g_assert_cmpstr (wp_properties_get (props, "[=] "), ==, "v4");
183 g_assert_cmpstr (wp_properties_get (props, " [=] "), ==, "v5");
184 g_assert_cmpstr (wp_properties_get (props, " [ =] "), ==, "v6");
185 g_assert_cmpstr (wp_properties_get (props, " [= ] "), ==, "v7");
186 g_assert_cmpstr (wp_properties_get (props, " [ = ] "), ==, "v8");
187 g_assert_cmpstr (wp_properties_get (props, " ["), ==, "v9");
188 g_assert_cmpstr (wp_properties_get (props, "[ "), ==, "v10");
189 g_assert_cmpstr (wp_properties_get (props, " [ "), ==, "v11");
190 g_assert_cmpstr (wp_properties_get (props, " ]"), ==, "v12");
191 g_assert_cmpstr (wp_properties_get (props, "] "), ==, "v13");
192 g_assert_cmpstr (wp_properties_get (props, " ] "), ==, "v14");
193 g_assert_cmpstr (wp_properties_get (props, " "), ==, "v15");
194 g_assert_cmpstr (wp_properties_get (props, "="), ==, "v16");
195 g_assert_cmpstr (wp_properties_get (props, "\\"), ==, "v17");
196 g_assert_cmpstr (wp_properties_get (props, "\\["), ==, "v18");
197 g_assert_cmpstr (wp_properties_get (props, "\\a"), ==, "v19");
198 g_assert_cmpstr (wp_properties_get (props, "\\\\"), ==, "v20");
199 g_assert_cmpstr (wp_properties_get (props, "[]["), ==, "][]");
200 }
201
202 wp_state_clear (state);
203 }
204
205 int
206 main (int argc, char *argv[])
207 {
208 g_test_init (&argc, &argv, NULL);
209 g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
210
211 g_test_add_func ("/wp/state/basic", test_state_basic);
212 g_test_add_func ("/wp/state/empty", test_state_empty);
213 g_test_add_func ("/wp/state/spaces", test_state_spaces);
214 g_test_add_func ("/wp/state/escaped", test_state_escaped);
215
216 return g_test_run ();
217 }