1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <stdarg.h>
#include <string.h>
#include <glib.h>
typedef struct _EscapeTest EscapeTest;
struct _EscapeTest
{
const gchar *original;
const gchar *expected;
};
static EscapeTest escape_tests[] =
{
{ "&", "&" },
{ "<", "<" },
{ ">", ">" },
{ "'", "'" },
{ "\"", """ },
{ "\"\"", """" },
{ "\"അ\"", ""അ"" },
{ "", "" },
{ "A", "A" },
{ "A&", "A&" },
{ "&A", "&A" },
{ "A&A", "A&A" },
{ "&&A", "&&A" },
{ "A&&", "A&&" },
{ "A&&A", "A&&A" },
{ "A&A&A", "A&A&A" },
{ "AA", "A&#23;A" },
{ "A
A", "A&#xa;A" },
{ "N\x2N", "NN" },
{ "N\xc2\x80N", "N€N" },
{ "N\xc2\x79N", "N\xc2\x79N" },
{ "N\xc2\x9fN", "NŸN" },
/* As per g_markup_escape_text()'s documentation, whitespace is not escaped: */
{ "\t", "\t" },
};
static void
escape_test (gconstpointer d)
{
const EscapeTest *test = d;
gchar *result;
result = g_markup_escape_text (test->original, -1);
g_assert_cmpstr (result, ==, test->expected);
g_free (result);
}
typedef struct _UnicharTest UnicharTest;
struct _UnicharTest
{
gunichar c;
gboolean entity;
};
static UnicharTest unichar_tests[] =
{
{ 0x1, TRUE },
{ 0x8, TRUE },
{ 0x9, FALSE },
{ 0xa, FALSE },
{ 0xb, TRUE },
{ 0xc, TRUE },
{ 0xd, FALSE },
{ 0xe, TRUE },
{ 0x1f, TRUE },
{ 0x20, FALSE },
{ 0x7e, FALSE },
{ 0x7f, TRUE },
{ 0x84, TRUE },
{ 0x85, FALSE },
{ 0x86, TRUE },
{ 0x9f, TRUE },
{ 0xa0, FALSE }
};
static void
unichar_test (gconstpointer d)
{
const UnicharTest *test = d;
EscapeTest t;
gint len;
gchar outbuf[7], expected[12];
len = g_unichar_to_utf8 (test->c, outbuf);
outbuf[len] = 0;
if (test->entity)
g_snprintf (expected, 12, "&#x%x;", test->c);
else
strcpy (expected, outbuf);
t.original = outbuf;
t.expected = expected;
escape_test (&t);
}
G_GNUC_PRINTF(1, 3)
static void
test_format (const gchar *format,
const gchar *expected,
...)
{
gchar *result;
va_list args;
va_start (args, expected);
result = g_markup_vprintf_escaped (format, args);
va_end (args);
g_assert_cmpstr (result, ==, expected);
g_free (result);
}
static void
format_test (void)
{
test_format ("A", "A");
test_format ("A%s", "A&", "&");
test_format ("%sA", "&A", "&");
test_format ("A%sA", "A&A", "&");
test_format ("%s%sA", "&&A", "&", "&");
test_format ("A%s%s", "A&&", "&", "&");
test_format ("A%s%sA", "A&&A", "&", "&");
test_format ("A%sA%sA", "A&A&A", "&", "&");
test_format ("%s", "<B>&", "<B>&");
test_format ("%c%c", "<&", '<', '&');
test_format (".%c.%c.", ".<.&.", '<', '&');
test_format ("%s", "", "");
test_format ("%-5s", "A ", "A");
test_format ("%2$s%1$s", "B.A.", "A.", "B.");
}
int main (int argc, char **argv)
{
gsize i;
gchar *path;
g_test_init (&argc, &argv, NULL);
for (i = 0; i < G_N_ELEMENTS (escape_tests); i++)
{
path = g_strdup_printf ("/markup/escape-text/%" G_GSIZE_FORMAT, i);
g_test_add_data_func (path, &escape_tests[i], escape_test);
g_free (path);
}
for (i = 0; i < G_N_ELEMENTS (unichar_tests); i++)
{
path = g_strdup_printf ("/markup/escape-unichar/%" G_GSIZE_FORMAT, i);
g_test_add_data_func (path, &unichar_tests[i], unichar_test);
g_free (path);
}
g_test_add_func ("/markup/format", format_test);
return g_test_run ();
}
|