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
escape_test(gconstpointer d)46 escape_test (gconstpointer d)
47 {
48 const EscapeTest *test = d;
49 gchar *result;
50
51 result = g_markup_escape_text (test->original, -1);
52
53 g_assert_cmpstr (result, ==, test->expected);
54
55 g_free (result);
56 }
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
unichar_test(gconstpointer d)88 unichar_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 (test->c, outbuf);
96 outbuf[len] = 0;
97
98 if (test->entity)
99 g_snprintf (expected, 12, "&#x%x;", test->c);
100 else
101 strcpy (expected, outbuf);
102
103 t.original = outbuf;
104 t.expected = expected;
105 escape_test (&t);
106 }
107
108 G_GNUC_PRINTF(1, 3)
109 static void
test_format(const gchar * format,const gchar * expected,...)110 test_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 (result);
124 }
125
126 static void
format_test(void)127 format_test (void)
128 {
129 test_format ("A", "A");
130 test_format ("A%s", "A&", "&");
131 test_format ("%sA", "&A", "&");
132 test_format ("A%sA", "A&A", "&");
133 test_format ("%s%sA", "&&A", "&", "&");
134 test_format ("A%s%s", "A&&", "&", "&");
135 test_format ("A%s%sA", "A&&A", "&", "&");
136 test_format ("A%sA%sA", "A&A&A", "&", "&");
137 test_format ("%s", "<B>&", "<B>&");
138 test_format ("%c%c", "<&", '<', '&');
139 test_format (".%c.%c.", ".<.&.", '<', '&');
140 test_format ("%s", "", "");
141 test_format ("%-5s", "A ", "A");
142 test_format ("%2$s%1$s", "B.A.", "A.", "B.");
143 }
144
main(int argc,char ** argv)145 int main (int argc, char **argv)
146 {
147 gsize i;
148 gchar *path;
149
150 g_test_init (&argc, &argv, NULL);
151
152 for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
153 {
154 path = g_strdup_printf ("/markup/escape-text/%" G_GSIZE_FORMAT, i);
155 g_test_add_data_func (path, &escape_tests[i], escape_test);
156 g_free (path);
157 }
158
159 for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
160 {
161 path = g_strdup_printf ("/markup/escape-unichar/%" G_GSIZE_FORMAT, i);
162 g_test_add_data_func (path, &unichar_tests[i], unichar_test);
163 g_free (path);
164 }
165
166 g_test_add_func ("/markup/format", format_test);
167
168 return g_test_run ();
169 }
170