glib-unix: add function to ensure an fd is sealed
[platform/upstream/glib.git] / glib / tests / markup-escape.c
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   { "&", "&amp;" },
19   { "<", "&lt;" },
20   { ">", "&gt;" },
21   { "'", "&apos;" },
22   { "\"", "&quot;" },
23   { "", "" },
24   { "A", "A" },
25   { "A&", "A&amp;" },
26   { "&A", "&amp;A" },
27   { "A&A", "A&amp;A" },
28   { "&&A", "&amp;&amp;A" },
29   { "A&&", "A&amp;&amp;" },
30   { "A&&A", "A&amp;&amp;A" },
31   { "A&A&A", "A&amp;A&amp;A" },
32   { "A&#23;A", "A&amp;#23;A" },
33   { "A&#xa;A", "A&amp;#xa;A" }
34 };
35
36 static void
37 escape_test (gconstpointer d)
38 {
39   const EscapeTest *test = d;
40   gchar *result;
41
42   result = g_markup_escape_text (test->original, -1);
43
44   g_assert_cmpstr (result, ==, test->expected);
45
46   g_free (result);
47 }
48
49 typedef struct _UnicharTest UnicharTest;
50
51 struct _UnicharTest
52 {
53   gunichar c;
54   gboolean entity;
55 };
56
57 static UnicharTest unichar_tests[] =
58 {
59   { 0x1, TRUE },
60   { 0x8, TRUE },
61   { 0x9, FALSE },
62   { 0xa, FALSE },
63   { 0xb, TRUE },
64   { 0xc, TRUE },
65   { 0xd, FALSE },
66   { 0xe, TRUE },
67   { 0x1f, TRUE },
68   { 0x20, FALSE },
69   { 0x7e, FALSE },
70   { 0x7f, TRUE },
71   { 0x84, TRUE },
72   { 0x85, FALSE },
73   { 0x86, TRUE },
74   { 0x9f, TRUE },
75   { 0xa0, FALSE }
76 };
77
78 static void
79 unichar_test (gconstpointer d)
80 {
81   const UnicharTest *test = d;
82   EscapeTest t;
83   gint len;
84   gchar outbuf[7], expected[12];
85
86   len = g_unichar_to_utf8 (test->c, outbuf);
87   outbuf[len] = 0;
88
89   if (test->entity)
90     g_snprintf (expected, 12, "&#x%x;", test->c);
91   else
92     strcpy (expected, outbuf);
93
94   t.original = outbuf;
95   t.expected = expected;
96   escape_test (&t);
97 }
98
99 G_GNUC_PRINTF(1, 3)
100 static void
101 test_format (const gchar *format,
102              const gchar *expected,
103              ...)
104 {
105   gchar *result;
106   va_list args;
107
108   va_start (args, expected);
109   result = g_markup_vprintf_escaped (format, args);
110   va_end (args);
111
112   g_assert_cmpstr (result, ==, expected);
113
114   g_free (result);
115 }
116
117 static void
118 format_test (void)
119 {
120   test_format ("A", "A");
121   test_format ("A%s", "A&amp;", "&");
122   test_format ("%sA", "&amp;A", "&");
123   test_format ("A%sA", "A&amp;A", "&");
124   test_format ("%s%sA", "&amp;&amp;A", "&", "&");
125   test_format ("A%s%s", "A&amp;&amp;", "&", "&");
126   test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
127   test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
128   test_format ("%s", "&lt;B&gt;&amp;", "<B>&");
129   test_format ("%c%c", "&lt;&amp;", '<', '&');
130   test_format (".%c.%c.", ".&lt;.&amp;.", '<', '&');
131   test_format ("%s", "", "");
132   test_format ("%-5s", "A    ", "A");
133   test_format ("%2$s%1$s", "B.A.", "A.", "B.");
134 }
135
136 int main (int argc, char **argv)
137 {
138   gint i;
139   gchar *path;
140
141   g_test_init (&argc, &argv, NULL);
142
143   for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
144     {
145       path = g_strdup_printf ("/markup/escape-text/%d", i);
146       g_test_add_data_func (path, &escape_tests[i], escape_test);
147       g_free (path);
148     }
149
150   for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
151     {
152       path = g_strdup_printf ("/markup/escape-unichar/%d", i);
153       g_test_add_data_func (path, &unichar_tests[i], unichar_test);
154       g_free (path);
155     }
156
157   g_test_add_func ("/markup/format", format_test);
158
159   return g_test_run ();
160 }