Add some more cases to the app-id unit tests
[glib.git] / glib / tests / markup-escape.c
blobb2de289c2ae84186eb6e10a6136a494c51e61047
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include <stdarg.h>
5 #include <string.h>
6 #include <glib.h>
8 typedef struct _EscapeTest EscapeTest;
10 struct _EscapeTest
12 const gchar *original;
13 const gchar *expected;
16 static EscapeTest escape_tests[] =
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" }
36 static void
37 escape_test (gconstpointer d)
39 const EscapeTest *test = d;
40 gchar *result;
42 result = g_markup_escape_text (test->original, -1);
44 g_assert_cmpstr (result, ==, test->expected);
46 g_free (result);
49 typedef struct _UnicharTest UnicharTest;
51 struct _UnicharTest
53 gunichar c;
54 gboolean entity;
57 static UnicharTest unichar_tests[] =
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 }
78 static void
79 unichar_test (gconstpointer d)
81 const UnicharTest *test = d;
82 EscapeTest t;
83 gint len;
84 gchar outbuf[7], expected[12];
86 len = g_unichar_to_utf8 (test->c, outbuf);
87 outbuf[len] = 0;
89 if (test->entity)
90 g_snprintf (expected, 12, "&#x%x;", test->c);
91 else
92 strcpy (expected, outbuf);
94 t.original = outbuf;
95 t.expected = expected;
96 escape_test (&t);
99 G_GNUC_PRINTF(1, 3)
100 static void
101 test_format (const gchar *format,
102 const gchar *expected,
103 ...)
105 gchar *result;
106 va_list args;
108 va_start (args, expected);
109 result = g_markup_vprintf_escaped (format, args);
110 va_end (args);
112 g_assert_cmpstr (result, ==, expected);
114 g_free (result);
117 static void
118 format_test (void)
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.");
136 int main (int argc, char **argv)
138 gint i;
139 gchar *path;
141 g_test_init (&argc, &argv, NULL);
143 for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
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);
150 for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
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);
157 g_test_add_func ("/markup/format", format_test);
159 return g_test_run ();