1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2008-2010 Kouhei Sutou <kou@clear-code.com>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif /* HAVE_CONFIG_H */
23
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <glib.h>
29 #include <glib/gstdio.h>
30
31 #include <cutter/cut-utils.h>
32 #include "gcut-assertions-helper.h"
33
34 void
gcut_assert_equal_type_helper(GType expected,GType actual,const gchar * expression_expected,const gchar * expression_actual)35 gcut_assert_equal_type_helper (GType expected,
36 GType actual,
37 const gchar *expression_expected,
38 const gchar *expression_actual)
39 {
40 if (expected == actual) {
41 cut_test_pass();
42 } else {
43 cut_set_expected(g_type_name(expected));
44 cut_set_actual(g_type_name(actual));
45 cut_test_fail(cut_take_printf("<%s == %s>",
46 expression_expected,
47 expression_actual));
48 }
49 }
50
51 void
gcut_assert_equal_value_helper(GValue * expected,GValue * actual,const gchar * expression_expected,const gchar * expression_actual)52 gcut_assert_equal_value_helper (GValue *expected,
53 GValue *actual,
54 const gchar *expression_expected,
55 const gchar *expression_actual)
56 {
57 if (gcut_value_equal(expected, actual)) {
58 cut_test_pass();
59 } else {
60 const gchar *message;
61 const gchar *inspected_expected, *inspected_actual;
62 const gchar *expected_type_name, *actual_type_name;
63
64 inspected_expected = cut_take_string(g_strdup_value_contents(expected));
65 inspected_actual = cut_take_string(g_strdup_value_contents(actual));
66 expected_type_name = g_type_name(G_VALUE_TYPE(expected));
67 actual_type_name = g_type_name(G_VALUE_TYPE(actual));
68
69 message = cut_take_printf("<%s == %s>",
70 expression_expected, expression_actual);
71 cut_set_expected(cut_take_printf("%s (%s)",
72 inspected_expected,
73 expected_type_name));
74 cut_set_actual(cut_take_printf("%s (%s)",
75 inspected_actual, actual_type_name));
76 cut_test_fail(message);
77 }
78 }
79
80 void
gcut_assert_equal_list_helper(const GList * expected,const GList * actual,GEqualFunc equal_function,GCutInspectFunction inspect_function,gpointer inspect_user_data,const gchar * expression_expected,const gchar * expression_actual,const gchar * expression_equal_function)81 gcut_assert_equal_list_helper (const GList *expected,
82 const GList *actual,
83 GEqualFunc equal_function,
84 GCutInspectFunction inspect_function,
85 gpointer inspect_user_data,
86 const gchar *expression_expected,
87 const gchar *expression_actual,
88 const gchar *expression_equal_function)
89 {
90 if (gcut_list_equal(expected, actual, equal_function)) {
91 cut_test_pass();
92 } else {
93 const gchar *inspected_expected, *inspected_actual;
94
95 inspected_expected =
96 cut_take_string(gcut_list_inspect(expected,
97 inspect_function,
98 inspect_user_data));
99 inspected_actual =
100 cut_take_string(gcut_list_inspect(actual,
101 inspect_function,
102 inspect_user_data));
103
104 cut_set_expected(inspected_expected);
105 cut_set_actual(inspected_actual);
106 cut_test_fail(cut_take_printf("<%s(%s[i], %s[i]) == TRUE>",
107 expression_equal_function,
108 expression_expected, expression_actual));
109 }
110 }
111
112 void
gcut_assert_equal_list_int_helper(const GList * expected,const GList * actual,const gchar * expression_expected,const gchar * expression_actual)113 gcut_assert_equal_list_int_helper (const GList *expected,
114 const GList *actual,
115 const gchar *expression_expected,
116 const gchar *expression_actual)
117 {
118 if (gcut_list_equal_int(expected, actual)) {
119 cut_test_pass();
120 } else {
121 const gchar *inspected_expected, *inspected_actual;
122
123 inspected_expected = cut_take_string(gcut_list_inspect_int(expected));
124 inspected_actual = cut_take_string(gcut_list_inspect_int(actual));
125
126 cut_set_expected(inspected_expected);
127 cut_set_actual(inspected_actual);
128 cut_test_fail(cut_take_printf("<%s == %s>",
129 expression_expected, expression_actual));
130 }
131 }
132
133 void
gcut_assert_equal_list_uint_helper(const GList * expected,const GList * actual,const gchar * expression_expected,const gchar * expression_actual)134 gcut_assert_equal_list_uint_helper (const GList *expected,
135 const GList *actual,
136 const gchar *expression_expected,
137 const gchar *expression_actual)
138 {
139 if (gcut_list_equal_uint(expected, actual)) {
140 cut_test_pass();
141 } else {
142 const gchar *inspected_expected, *inspected_actual;
143
144 inspected_expected = cut_take_string(gcut_list_inspect_uint(expected));
145 inspected_actual = cut_take_string(gcut_list_inspect_uint(actual));
146
147 cut_set_expected(inspected_expected);
148 cut_set_actual(inspected_actual);
149 cut_test_fail(cut_take_printf("<%s == %s>",
150 expression_expected, expression_actual));
151 }
152 }
153
154 void
gcut_assert_equal_list_string_helper(const GList * expected,const GList * actual,const gchar * expression_expected,const gchar * expression_actual)155 gcut_assert_equal_list_string_helper (const GList *expected,
156 const GList *actual,
157 const gchar *expression_expected,
158 const gchar *expression_actual)
159 {
160 if (gcut_list_equal_string(expected, actual)) {
161 cut_test_pass();
162 } else {
163 const gchar *message;
164 const gchar *inspected_expected, *inspected_actual;
165
166 inspected_expected = cut_take_string(gcut_list_inspect_string(expected));
167 inspected_actual = cut_take_string(gcut_list_inspect_string(actual));
168
169 message = cut_take_printf("<%s == %s>",
170 expression_expected, expression_actual);
171 cut_set_expected(inspected_expected);
172 cut_set_actual(inspected_actual);
173 cut_test_fail(message);
174 }
175 }
176
177 void
gcut_assert_equal_list_enum_helper(GType type,const GList * expected,const GList * actual,const gchar * expression_type,const gchar * expression_expected,const gchar * expression_actual)178 gcut_assert_equal_list_enum_helper (GType type,
179 const GList *expected,
180 const GList *actual,
181 const gchar *expression_type,
182 const gchar *expression_expected,
183 const gchar *expression_actual)
184 {
185 if (gcut_list_equal_int(expected, actual)) {
186 cut_test_pass();
187 } else {
188 const gchar *inspected_expected, *inspected_actual;
189
190 inspected_expected =
191 cut_take_string(gcut_list_inspect_enum(type, expected));
192 inspected_actual =
193 cut_take_string(gcut_list_inspect_enum(type, actual));
194
195 cut_set_expected(inspected_expected);
196 cut_set_actual(inspected_actual);
197 cut_test_fail(cut_take_printf("<%s == %s>",
198 expression_expected, expression_actual));
199 }
200 }
201
202 void
gcut_assert_equal_list_flags_helper(GType type,const GList * expected,const GList * actual,const gchar * expression_type,const gchar * expression_expected,const gchar * expression_actual)203 gcut_assert_equal_list_flags_helper (GType type,
204 const GList *expected,
205 const GList *actual,
206 const gchar *expression_type,
207 const gchar *expression_expected,
208 const gchar *expression_actual)
209 {
210 if (gcut_list_equal_uint(expected, actual)) {
211 cut_test_pass();
212 } else {
213 const gchar *inspected_expected, *inspected_actual;
214
215 inspected_expected =
216 cut_take_string(gcut_list_inspect_flags(type, expected));
217 inspected_actual =
218 cut_take_string(gcut_list_inspect_flags(type, actual));
219
220 cut_set_expected(inspected_expected);
221 cut_set_actual(inspected_actual);
222 cut_test_fail(cut_take_printf("<%s == %s>",
223 expression_expected, expression_actual));
224 }
225 }
226
227 void
gcut_assert_equal_list_object_helper(const GList * expected,const GList * actual,GEqualFunc equal_function,const gchar * expression_expected,const gchar * expression_actual)228 gcut_assert_equal_list_object_helper (const GList *expected,
229 const GList *actual,
230 GEqualFunc equal_function,
231 const gchar *expression_expected,
232 const gchar *expression_actual)
233 {
234 if (gcut_list_equal(expected, actual, equal_function)) {
235 cut_test_pass();
236 } else {
237 const gchar *inspected_expected, *inspected_actual;
238
239 inspected_expected =
240 cut_take_string(gcut_list_inspect_object(expected));
241 inspected_actual =
242 cut_take_string(gcut_list_inspect_object(actual));
243
244 cut_set_expected(inspected_expected);
245 cut_set_actual(inspected_actual);
246 cut_test_fail(cut_take_printf("<%s == %s>",
247 expression_expected, expression_actual));
248 }
249 }
250
251 void
gcut_assert_equal_hash_table_helper(GHashTable * expected,GHashTable * actual,GEqualFunc equal_function,GCutInspectFunction key_inspect_function,GCutInspectFunction value_inspect_function,gpointer inspect_user_data,const gchar * expression_expected,const gchar * expression_actual,const gchar * expression_equal_function)252 gcut_assert_equal_hash_table_helper (GHashTable *expected,
253 GHashTable *actual,
254 GEqualFunc equal_function,
255 GCutInspectFunction key_inspect_function,
256 GCutInspectFunction value_inspect_function,
257 gpointer inspect_user_data,
258 const gchar *expression_expected,
259 const gchar *expression_actual,
260 const gchar *expression_equal_function)
261 {
262 if (gcut_hash_table_equal(expected, actual, equal_function)) {
263 cut_test_pass();
264 } else {
265 gchar *inspected_expected, *inspected_actual;
266 GCompareFunc compare_function = NULL;
267
268 if (key_inspect_function == gcut_inspect_direct) {
269 compare_function = cut_utils_compare_direct;
270 } else if (key_inspect_function == gcut_inspect_string) {
271 compare_function = cut_utils_compare_string;
272 }
273 inspected_expected =
274 gcut_hash_table_inspect_sorted(expected,
275 key_inspect_function,
276 value_inspect_function,
277 compare_function,
278 inspect_user_data);
279 inspected_actual =
280 gcut_hash_table_inspect_sorted(actual,
281 key_inspect_function,
282 value_inspect_function,
283 compare_function,
284 inspect_user_data);
285
286 cut_set_expected(inspected_expected);
287 cut_set_actual(inspected_actual);
288 g_free(inspected_expected);
289 g_free(inspected_actual);
290 cut_test_fail(cut_take_printf("<%s(%s[key], %s[key]) == TRUE>",
291 expression_equal_function,
292 expression_expected, expression_actual));
293 }
294 }
295
296 void
gcut_assert_equal_hash_table_string_string_helper(GHashTable * expected,GHashTable * actual,const gchar * expression_expected,const gchar * expression_actual)297 gcut_assert_equal_hash_table_string_string_helper (GHashTable *expected,
298 GHashTable *actual,
299 const gchar *expression_expected,
300 const gchar *expression_actual)
301 {
302 if (gcut_hash_table_string_equal(expected, actual)) {
303 cut_test_pass();
304 } else {
305 gchar *inspected_expected, *inspected_actual;
306
307 inspected_expected =
308 gcut_hash_table_string_string_inspect(expected);
309 inspected_actual =
310 gcut_hash_table_string_string_inspect(actual);
311
312 cut_set_expected(inspected_expected);
313 cut_set_actual(inspected_actual);
314 g_free(inspected_expected);
315 g_free(inspected_actual);
316
317 cut_test_fail(cut_take_printf("<%s == %s>",
318 expression_expected, expression_actual));
319 }
320 }
321
322 void
gcut_assert_error_helper(GError * error,const gchar * expression_error)323 gcut_assert_error_helper (GError *error,
324 const gchar *expression_error)
325 {
326 if (error == NULL) {
327 cut_test_pass();
328 } else {
329 const gchar *inspected;
330
331 inspected = cut_take_string(gcut_error_inspect(error));
332 g_error_free(error);
333 cut_test_fail(cut_take_printf("expected: <%s> is NULL\n"
334 " actual: <%s>",
335 expression_error, inspected));
336 }
337 }
338
339 void
gcut_assert_equal_error_helper(const GError * expected,const GError * actual,const gchar * expression_expected,const gchar * expression_actual)340 gcut_assert_equal_error_helper (const GError *expected,
341 const GError *actual,
342 const gchar *expression_expected,
343 const gchar *expression_actual)
344 {
345 if (gcut_error_equal(expected, actual)) {
346 cut_test_pass();
347 } else {
348 cut_set_expected(cut_take_string(gcut_error_inspect(expected)));
349 cut_set_actual(cut_take_string(gcut_error_inspect(actual)));
350 cut_test_fail(cut_take_printf("<%s == %s>",
351 expression_expected,
352 expression_actual));
353 }
354 }
355
356 void
gcut_assert_remove_path_helper(const gchar * path,...)357 gcut_assert_remove_path_helper (const gchar *path, ...)
358 {
359 GError *remove_path_error = NULL;
360 gchar *full_path;
361 va_list args;
362
363 va_start(args, path);
364 full_path = cut_utils_build_path_va_list(path, args);
365 va_end(args);
366
367 cut_utils_remove_path_recursive(full_path, &remove_path_error);
368 gcut_assert_error_helper(remove_path_error, cut_take_string(full_path));
369 }
370
371 void
gcut_assert_equal_time_val_helper(GTimeVal * expected,GTimeVal * actual,const gchar * expression_expected,const gchar * expression_actual)372 gcut_assert_equal_time_val_helper (GTimeVal *expected,
373 GTimeVal *actual,
374 const gchar *expression_expected,
375 const gchar *expression_actual)
376 {
377 const gchar *expected_string = NULL;
378 const gchar *actual_string = NULL;
379
380 if (expected)
381 expected_string = cut_take_string(g_time_val_to_iso8601(expected));
382 if (actual)
383 actual_string = cut_take_string(g_time_val_to_iso8601(actual));
384
385 cut_assert_equal_string_helper(expected_string,
386 actual_string,
387 expression_expected,
388 expression_actual);
389 }
390
391 void
gcut_assert_equal_enum_helper(GType enum_type,gint expected,gint actual,const gchar * expression_enum_type,const gchar * expression_expected,const gchar * expression_actual)392 gcut_assert_equal_enum_helper (GType enum_type,
393 gint expected,
394 gint actual,
395 const gchar *expression_enum_type,
396 const gchar *expression_expected,
397 const gchar *expression_actual)
398 {
399 if (expected == actual) {
400 cut_test_pass();
401 } else {
402 const gchar *inspected_expected;
403 const gchar *inspected_actual;
404
405 inspected_expected =
406 cut_take_string(gcut_enum_inspect(enum_type, expected));
407 inspected_actual =
408 cut_take_string(gcut_enum_inspect(enum_type, actual));
409 cut_set_expected(inspected_expected);
410 cut_set_actual(inspected_actual);
411 cut_test_fail(cut_take_printf("<%s == %s> (%s)",
412 expression_expected,
413 expression_actual,
414 expression_enum_type));
415 }
416 }
417
418 void
gcut_assert_equal_flags_helper(GType flags_type,gint expected,gint actual,const gchar * expression_flags_type,const gchar * expression_expected,const gchar * expression_actual)419 gcut_assert_equal_flags_helper (GType flags_type,
420 gint expected,
421 gint actual,
422 const gchar *expression_flags_type,
423 const gchar *expression_expected,
424 const gchar *expression_actual)
425 {
426 if (expected == actual) {
427 cut_test_pass();
428 } else {
429 const gchar *inspected_expected;
430 const gchar *inspected_actual;
431
432 inspected_expected =
433 cut_take_string(gcut_flags_inspect(flags_type, expected));
434 inspected_actual =
435 cut_take_string(gcut_flags_inspect(flags_type, actual));
436 cut_set_expected(inspected_expected);
437 cut_set_actual(inspected_actual);
438 cut_test_fail(cut_take_printf("<%s == %s> (%s)",
439 expression_expected,
440 expression_actual,
441 expression_flags_type));
442 }
443 }
444
445 void
gcut_assert_equal_object_helper(GObject * expected,GObject * actual,GEqualFunc equal_function,const gchar * expression_expected,const gchar * expression_actual,const gchar * expression_equal_function)446 gcut_assert_equal_object_helper (GObject *expected,
447 GObject *actual,
448 GEqualFunc equal_function,
449 const gchar *expression_expected,
450 const gchar *expression_actual,
451 const gchar *expression_equal_function)
452 {
453 if (gcut_object_equal(expected, actual, equal_function)) {
454 cut_test_pass();
455 } else {
456 gchar *inspected_expected, *inspected_actual;
457 const gchar *message;
458
459 inspected_expected = gcut_object_inspect(expected);
460 inspected_actual = gcut_object_inspect(actual);
461 cut_set_expected(inspected_expected);
462 cut_set_actual(inspected_actual);
463 g_free(inspected_expected);
464 g_free(inspected_actual);
465
466 if (expression_equal_function)
467 message = cut_take_printf("<%s(%s, %s)>",
468 expression_equal_function,
469 expression_expected, expression_actual);
470 else
471 message = cut_take_printf("<%s == %s>",
472 expression_expected, expression_actual);
473 cut_test_fail(message);
474 }
475 }
476
477 void
gcut_assert_equal_int64_helper(gint64 expected,gint64 actual,const char * expression_expected,const char * expression_actual)478 gcut_assert_equal_int64_helper (gint64 expected,
479 gint64 actual,
480 const char *expression_expected,
481 const char *expression_actual)
482 {
483 if (expected == actual) {
484 cut_test_pass();
485 } else {
486 cut_set_expected(cut_take_printf("%" G_GINT64_FORMAT, expected));
487 cut_set_actual(cut_take_printf("%" G_GINT64_FORMAT, actual));
488 cut_test_fail(cut_take_printf("<%s == %s>",
489 expression_expected, expression_actual));
490 }
491 }
492
493 void
gcut_assert_not_equal_int64_helper(gint64 expected,gint64 actual,const char * expression_expected,const char * expression_actual)494 gcut_assert_not_equal_int64_helper (gint64 expected,
495 gint64 actual,
496 const char *expression_expected,
497 const char *expression_actual)
498 {
499 if (expected != actual) {
500 cut_test_pass();
501 } else {
502 cut_set_expected(cut_take_printf("%" G_GINT64_FORMAT, expected));
503 cut_set_actual(cut_take_printf("%" G_GINT64_FORMAT, actual));
504 cut_test_fail(cut_take_printf("<%s != %s>",
505 expression_expected,
506 expression_actual));
507 }
508 }
509
510 void
gcut_assert_equal_uint64_helper(guint64 expected,guint64 actual,const char * expression_expected,const char * expression_actual)511 gcut_assert_equal_uint64_helper (guint64 expected,
512 guint64 actual,
513 const char *expression_expected,
514 const char *expression_actual)
515 {
516 if (expected == actual) {
517 cut_test_pass();
518 } else {
519 cut_set_expected(cut_take_printf("%" G_GUINT64_FORMAT, expected));
520 cut_set_actual(cut_take_printf("%" G_GUINT64_FORMAT, actual));
521 cut_test_fail(cut_take_printf("<%s == %s>",
522 expression_expected,
523 expression_actual));
524 }
525 }
526
527 void
gcut_assert_not_equal_uint64_helper(guint64 expected,guint64 actual,const char * expression_expected,const char * expression_actual)528 gcut_assert_not_equal_uint64_helper (guint64 expected,
529 guint64 actual,
530 const char *expression_expected,
531 const char *expression_actual)
532 {
533 if (expected != actual) {
534 cut_test_pass();
535 } else {
536 cut_set_expected(cut_take_printf("%" G_GUINT64_FORMAT, expected));
537 cut_set_actual(cut_take_printf("%" G_GUINT64_FORMAT, actual));
538 cut_test_fail(cut_take_printf("<%s != %s>",
539 expression_expected,
540 expression_actual));
541 }
542 }
543
544 #ifdef G_OS_WIN32
545 #define GCUT_GPID_FORMAT "p"
546 #else
547 #define GCUT_GPID_FORMAT "d"
548 #endif
549 void
gcut_assert_equal_pid_helper(GPid expected,GPid actual,const char * expression_expected,const char * expression_actual)550 gcut_assert_equal_pid_helper (GPid expected,
551 GPid actual,
552 const char *expression_expected,
553 const char *expression_actual)
554 {
555 if (expected == actual) {
556 cut_test_pass();
557 } else {
558 cut_set_expected(cut_take_printf("%" GCUT_GPID_FORMAT, expected));
559 cut_set_actual(cut_take_printf("%" GCUT_GPID_FORMAT, actual));
560 cut_test_fail(cut_take_printf("<%s == %s>",
561 expression_expected,
562 expression_actual));
563 }
564 }
565
566 void
gcut_assert_not_equal_pid_helper(GPid expected,GPid actual,const char * expression_expected,const char * expression_actual)567 gcut_assert_not_equal_pid_helper (GPid expected,
568 GPid actual,
569 const char *expression_expected,
570 const char *expression_actual)
571 {
572 if (expected != actual) {
573 cut_test_pass();
574 } else {
575 cut_set_expected(cut_take_printf("%" GCUT_GPID_FORMAT, expected));
576 cut_set_actual(cut_take_printf("%" GCUT_GPID_FORMAT, actual));
577 cut_test_fail(cut_take_printf("<%s != %s>",
578 expression_expected,
579 expression_actual));
580 }
581 }
582
583 void
gcut_assert_equal_string_helper(const GString * expected,const GString * actual,const gchar * expression_expected,const gchar * expression_actual)584 gcut_assert_equal_string_helper (const GString *expected,
585 const GString *actual,
586 const gchar *expression_expected,
587 const gchar *expression_actual)
588 {
589 if (gcut_string_equal(expected, actual)) {
590 cut_test_pass();
591 } else {
592 const gchar *message;
593 const gchar *inspected_expected, *inspected_actual;
594
595 inspected_expected = cut_take_string(gcut_string_inspect(expected));
596 inspected_actual = cut_take_string(gcut_string_inspect(actual));
597
598 message = cut_take_printf("<%s == %s>",
599 expression_expected, expression_actual);
600 cut_set_expected(inspected_expected);
601 cut_set_actual(inspected_actual);
602 cut_test_fail(message);
603 }
604 }
605
606 /*
607 vi:ts=4:nowrap:ai:expandtab:sw=4
608 */
609