Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2011 Collabora Ltd.
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring 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 GNU
13 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stefw@collabora.co.uk>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include "gcr/gcr.h"
25 : : #include "gcr/gcr-util.h"
26 : :
27 : : #include <errno.h>
28 : :
29 : : static void
30 : 11 : on_line_parsed_match_template (const gchar *line, gpointer user_data)
31 : : {
32 : 11 : const gchar ***matching = user_data;
33 : :
34 [ - + ]: 11 : g_assert (matching);
35 [ - + ]: 11 : g_assert (*matching);
36 : :
37 : : /* Must be another line to match */
38 [ - + ]: 11 : g_assert ((*matching)[0]);
39 : :
40 : : /* Match this line against expected, and increment to next */
41 [ - + ]: 11 : g_assert_cmpstr ((*matching)[0], ==, line);
42 : 11 : (*matching)++;
43 : 11 : }
44 : :
45 : : static void
46 : 1 : test_parse_lines (void)
47 : : {
48 : 1 : GString *string = g_string_new ("first line\nsecond line\n\nlast line");
49 : 1 : const gchar *matches[] = { "first line", "second line", "", NULL };
50 : 1 : const gchar **matching = matches;
51 : :
52 : 1 : _gcr_util_parse_lines (string, FALSE, on_line_parsed_match_template, &matching);
53 : :
54 : : /* All lines should have matched */
55 [ - + ]: 1 : g_assert (*matching == NULL);
56 : :
57 : : /* The last line should still be here */
58 [ - + ]: 1 : g_assert_cmpstr (string->str, ==, "last line");
59 : 1 : g_string_free (string, TRUE);
60 : 1 : }
61 : :
62 : : static void
63 : 1 : test_parse_lines_and_last (void)
64 : : {
65 : 1 : GString *string = g_string_new ("first line\nsecond line\n\nlast line");
66 : 1 : const gchar *matches[] = { "first line", "second line", "", "last line", NULL };
67 : 1 : const gchar **matching = matches;
68 : :
69 : 1 : _gcr_util_parse_lines (string, FALSE, on_line_parsed_match_template, &matching);
70 : 1 : _gcr_util_parse_lines (string, TRUE, on_line_parsed_match_template, &matching);
71 : :
72 : : /* All lines should have matched */
73 [ - + ]: 1 : g_assert (*matching == NULL);
74 : :
75 : : /* No more data */
76 [ - + ]: 1 : g_assert_cmpstr (string->str, ==, "");
77 [ - + ]: 1 : g_assert_cmpuint (string->len, ==, 0);
78 : 1 : g_string_free (string, TRUE);
79 : 1 : }
80 : :
81 : : static void
82 : 1 : test_parse_lines_dos (void)
83 : : {
84 : 1 : GString *string = g_string_new ("first line\r\nsecond line\r\n\r\nlast line");
85 : 1 : const gchar *matches[] = { "first line", "second line", "", "last line", NULL };
86 : 1 : const gchar **matching = matches;
87 : :
88 : 1 : _gcr_util_parse_lines (string, FALSE, on_line_parsed_match_template, &matching);
89 : 1 : _gcr_util_parse_lines (string, TRUE, on_line_parsed_match_template, &matching);
90 : :
91 : : /* All lines should have matched */
92 [ - + ]: 1 : g_assert (*matching == NULL);
93 : :
94 : : /* No more data */
95 [ - + ]: 1 : g_assert_cmpstr (string->str, ==, "");
96 [ - + ]: 1 : g_assert_cmpuint (string->len, ==, 0);
97 : 1 : g_string_free (string, TRUE);
98 : 1 : }
99 : :
100 : : int
101 : 1 : main (int argc, char **argv)
102 : : {
103 : 1 : g_test_init (&argc, &argv, NULL);
104 : :
105 : 1 : g_test_add_func ("/gcr/util/test_parse_lines", test_parse_lines);
106 : 1 : g_test_add_func ("/gcr/util/test_parse_lines_and_last", test_parse_lines_and_last);
107 : 1 : g_test_add_func ("/gcr/util/test_parse_lines_dos", test_parse_lines_dos);
108 : :
109 : 1 : return g_test_run ();
110 : : }
|