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 | escape_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 | |
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 | 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 (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 | |
108 | G_GNUC_PRINTF(1, 3) |
109 | static void |
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 (mem: result); |
124 | } |
125 | |
126 | static void |
127 | format_test (void) |
128 | { |
129 | test_format (format: "A" , expected: "A" ); |
130 | test_format (format: "A%s" , expected: "A&" , "&" ); |
131 | test_format (format: "%sA" , expected: "&A" , "&" ); |
132 | test_format (format: "A%sA" , expected: "A&A" , "&" ); |
133 | test_format (format: "%s%sA" , expected: "&&A" , "&" , "&" ); |
134 | test_format (format: "A%s%s" , expected: "A&&" , "&" , "&" ); |
135 | test_format (format: "A%s%sA" , expected: "A&&A" , "&" , "&" ); |
136 | test_format (format: "A%sA%sA" , expected: "A&A&A" , "&" , "&" ); |
137 | test_format (format: "%s" , expected: "<B>&" , "<B>&" ); |
138 | test_format (format: "%c%c" , expected: "<&" , '<', '&'); |
139 | test_format (format: ".%c.%c." , expected: ".<.&." , '<', '&'); |
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 | |
145 | int 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 | |