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

source code of gtk/subprojects/glib/glib/tests/markup-escape.c