Branch data Line data Source code
1 : : #undef G_DISABLE_ASSERT
2 : : #undef G_LOG_DOMAIN
3 : :
4 : : #include <stdarg.h>
5 : : #include <string.h>
6 : : #include <glib.h>
7 : :
8 : : typedef struct _EscapeTest EscapeTest;
9 : :
10 : : struct _EscapeTest
11 : : {
12 : : const gchar *original;
13 : : const gchar *expected;
14 : : };
15 : :
16 : : static EscapeTest escape_tests[] =
17 : : {
18 : : { "&", "&" },
19 : : { "<", "<" },
20 : : { ">", ">" },
21 : : { "'", "'" },
22 : : { "\"", """ },
23 : : { "\"\"", """" },
24 : : { "\"അ\"", ""അ"" },
25 : : { "", "" },
26 : : { "A", "A" },
27 : : { "A&", "A&" },
28 : : { "&A", "&A" },
29 : : { "A&A", "A&A" },
30 : : { "&&A", "&&A" },
31 : : { "A&&", "A&&" },
32 : : { "A&&A", "A&&A" },
33 : : { "A&A&A", "A&A&A" },
34 : : { "AA", "A&#23;A" },
35 : : { "A
A", "A&#xa;A" },
36 : : { "N\x2N", "NN" },
37 : : { "N\xc2\x80N", "N€N" },
38 : : { "N\xc2\x79N", "N\xc2\x79N" },
39 : : { "N\xc2\x9fN", "NŸN" },
40 : :
41 : : /* As per g_markup_escape_text()'s documentation, whitespace is not escaped: */
42 : : { "\t", "\t" },
43 : : };
44 : :
45 : : static void
46 : 40 : escape_test (gconstpointer d)
47 : : {
48 : 40 : const EscapeTest *test = d;
49 : : gchar *result;
50 : :
51 : 40 : result = g_markup_escape_text (test->original, -1);
52 : :
53 : 40 : g_assert_cmpstr (result, ==, test->expected);
54 : :
55 : 40 : g_free (result);
56 : 40 : }
57 : :
58 : : typedef struct _UnicharTest UnicharTest;
59 : :
60 : : struct _UnicharTest
61 : : {
62 : : gunichar c;
63 : : gboolean entity;
64 : : };
65 : :
66 : : static UnicharTest unichar_tests[] =
67 : : {
68 : : { 0x1, TRUE },
69 : : { 0x8, TRUE },
70 : : { 0x9, FALSE },
71 : : { 0xa, FALSE },
72 : : { 0xb, TRUE },
73 : : { 0xc, TRUE },
74 : : { 0xd, FALSE },
75 : : { 0xe, TRUE },
76 : : { 0x1f, TRUE },
77 : : { 0x20, FALSE },
78 : : { 0x7e, FALSE },
79 : : { 0x7f, TRUE },
80 : : { 0x84, TRUE },
81 : : { 0x85, FALSE },
82 : : { 0x86, TRUE },
83 : : { 0x9f, TRUE },
84 : : { 0xa0, FALSE }
85 : : };
86 : :
87 : : static void
88 : 17 : unichar_test (gconstpointer d)
89 : : {
90 : 17 : const UnicharTest *test = d;
91 : : EscapeTest t;
92 : : gint len;
93 : : gchar outbuf[7], expected[12];
94 : :
95 : 17 : len = g_unichar_to_utf8 (test->c, outbuf);
96 : 17 : outbuf[len] = 0;
97 : :
98 [ + + ]: 17 : if (test->entity)
99 : 10 : g_snprintf (expected, 12, "&#x%x;", test->c);
100 : : else
101 : 7 : strcpy (expected, outbuf);
102 : :
103 : 17 : t.original = outbuf;
104 : 17 : t.expected = expected;
105 : 17 : escape_test (&t);
106 : 17 : }
107 : :
108 : : G_GNUC_PRINTF(1, 3)
109 : : static void
110 : 14 : test_format (const gchar *format,
111 : : const gchar *expected,
112 : : ...)
113 : : {
114 : : gchar *result;
115 : : va_list args;
116 : :
117 : 14 : va_start (args, expected);
118 : 14 : result = g_markup_vprintf_escaped (format, args);
119 : 14 : va_end (args);
120 : :
121 : 14 : g_assert_cmpstr (result, ==, expected);
122 : :
123 : 14 : g_free (result);
124 : 14 : }
125 : :
126 : : static void
127 : 1 : format_test (void)
128 : : {
129 : 1 : test_format ("A", "A");
130 : 1 : test_format ("A%s", "A&", "&");
131 : 1 : test_format ("%sA", "&A", "&");
132 : 1 : test_format ("A%sA", "A&A", "&");
133 : 1 : test_format ("%s%sA", "&&A", "&", "&");
134 : 1 : test_format ("A%s%s", "A&&", "&", "&");
135 : 1 : test_format ("A%s%sA", "A&&A", "&", "&");
136 : 1 : test_format ("A%sA%sA", "A&A&A", "&", "&");
137 : 1 : test_format ("%s", "<B>&", "<B>&");
138 : 1 : test_format ("%c%c", "<&", '<', '&');
139 : 1 : test_format (".%c.%c.", ".<.&.", '<', '&');
140 : 1 : test_format ("%s", "", "");
141 : 1 : test_format ("%-5s", "A ", "A");
142 : 1 : test_format ("%2$s%1$s", "B.A.", "A.", "B.");
143 : 1 : }
144 : :
145 : 1 : int main (int argc, char **argv)
146 : : {
147 : : gsize i;
148 : : gchar *path;
149 : :
150 : 1 : g_test_init (&argc, &argv, NULL);
151 : :
152 [ + + ]: 24 : for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
153 : : {
154 : 23 : path = g_strdup_printf ("/markup/escape-text/%" G_GSIZE_FORMAT, i);
155 : 23 : g_test_add_data_func (path, &escape_tests[i], escape_test);
156 : 23 : g_free (path);
157 : : }
158 : :
159 [ + + ]: 18 : for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
160 : : {
161 : 17 : path = g_strdup_printf ("/markup/escape-unichar/%" G_GSIZE_FORMAT, i);
162 : 17 : g_test_add_data_func (path, &unichar_tests[i], unichar_test);
163 : 17 : g_free (path);
164 : : }
165 : :
166 : 1 : g_test_add_func ("/markup/format", format_test);
167 : :
168 : 1 : return g_test_run ();
169 : : }
|