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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
Copyright (C) 2011 Collabora Ltd.
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
see <http://www.gnu.org/licenses/>.
Author: Stef Walter <stefw@collabora.co.uk>
*/
#include "config.h"
#include "gcr/gcr.h"
#include "gcr/gcr-util.h"
#include <errno.h>
static void
on_line_parsed_match_template (const gchar *line, gpointer user_data)
{
const gchar ***matching = user_data;
g_assert (matching);
g_assert (*matching);
/* Must be another line to match */
g_assert ((*matching)[0]);
/* Match this line against expected, and increment to next */
g_assert_cmpstr ((*matching)[0], ==, line);
(*matching)++;
}
static void
test_parse_lines (void)
{
GString *string = g_string_new ("first line\nsecond line\n\nlast line");
const gchar *matches[] = { "first line", "second line", "", NULL };
const gchar **matching = matches;
_gcr_util_parse_lines (string, FALSE, on_line_parsed_match_template, &matching);
/* All lines should have matched */
g_assert (*matching == NULL);
/* The last line should still be here */
g_assert_cmpstr (string->str, ==, "last line");
g_string_free (string, TRUE);
}
static void
test_parse_lines_and_last (void)
{
GString *string = g_string_new ("first line\nsecond line\n\nlast line");
const gchar *matches[] = { "first line", "second line", "", "last line", NULL };
const gchar **matching = matches;
_gcr_util_parse_lines (string, FALSE, on_line_parsed_match_template, &matching);
_gcr_util_parse_lines (string, TRUE, on_line_parsed_match_template, &matching);
/* All lines should have matched */
g_assert (*matching == NULL);
/* No more data */
g_assert_cmpstr (string->str, ==, "");
g_assert_cmpuint (string->len, ==, 0);
g_string_free (string, TRUE);
}
static void
test_parse_lines_dos (void)
{
GString *string = g_string_new ("first line\r\nsecond line\r\n\r\nlast line");
const gchar *matches[] = { "first line", "second line", "", "last line", NULL };
const gchar **matching = matches;
_gcr_util_parse_lines (string, FALSE, on_line_parsed_match_template, &matching);
_gcr_util_parse_lines (string, TRUE, on_line_parsed_match_template, &matching);
/* All lines should have matched */
g_assert (*matching == NULL);
/* No more data */
g_assert_cmpstr (string->str, ==, "");
g_assert_cmpuint (string->len, ==, 0);
g_string_free (string, TRUE);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gcr/util/test_parse_lines", test_parse_lines);
g_test_add_func ("/gcr/util/test_parse_lines_and_last", test_parse_lines_and_last);
g_test_add_func ("/gcr/util/test_parse_lines_dos", test_parse_lines_dos);
return g_test_run ();
}
|