Tizen 2.1 base
[platform/upstream/glib2.0.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 static void
100 test_format (const gchar *format,
101              const gchar *expected,
102              ...)
103 {
104   gchar *result;
105   va_list args;
106
107   va_start (args, expected);
108   result = g_markup_vprintf_escaped (format, args);
109   va_end (args);
110
111   g_assert_cmpstr (result, ==, expected);
112
113   g_free (result);
114 }
115
116 static void
117 format_test (void)
118 {
119   test_format ("A", "A");
120   test_format ("A%s", "A&amp;", "&");
121   test_format ("%sA", "&amp;A", "&");
122   test_format ("A%sA", "A&amp;A", "&");
123   test_format ("%s%sA", "&amp;&amp;A", "&", "&");
124   test_format ("A%s%s", "A&amp;&amp;", "&", "&");
125   test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
126   test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
127   test_format ("%s", "&lt;B&gt;&amp;", "<B>&");
128   test_format ("%c%c", "&lt;&amp;", '<', '&');
129   test_format (".%c.%c.", ".&lt;.&amp;.", '<', '&');
130   test_format ("%s", "", "");
131   test_format ("%-5s", "A    ", "A");
132   test_format ("%2$s%1$s", "B.A.", "A.", "B.");
133 }
134
135 int main (int argc, char **argv)
136 {
137   gint i;
138   gchar *path;
139
140   g_test_init (&argc, &argv, NULL);
141
142   for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
143     {
144       path = g_strdup_printf ("/markup/escape-text/%d", i);
145       g_test_add_data_func (path, &escape_tests[i], escape_test);
146       g_free (path);
147     }
148
149   for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
150     {
151       path = g_strdup_printf ("/markup/escape-unichar/%d", i);
152       g_test_add_data_func (path, &unichar_tests[i], unichar_test);
153       g_free (path);
154     }
155
156   g_test_add_func ("/markup/format", format_test);
157
158   return g_test_run ();
159 }