Move markup escape tests to test framework

This commit is contained in:
Matthias Clasen 2010-07-26 22:12:59 -04:00
parent a804e22ed0
commit c972d4df64
4 changed files with 163 additions and 135 deletions

View File

@ -127,6 +127,10 @@ dir_LDADD = $(progs_ldadd)
TEST_PROGS += pattern
pattern_LDADD = $(progs_ldadd)
TEST_PROGS += markup-escape
markup_escape_LDADD = $(progs_ldadd)
if OS_UNIX
# some testing of gtester funcitonality

159
glib/tests/markup-escape.c Normal file
View File

@ -0,0 +1,159 @@
#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[] =
{
{ "&", "&amp;" },
{ "<", "&lt;" },
{ ">", "&gt;" },
{ "'", "&apos;" },
{ "\"", "&quot;" },
{ "", "" },
{ "A", "A" },
{ "A&", "A&amp;" },
{ "&A", "&amp;A" },
{ "A&A", "A&amp;A" },
{ "&&A", "&amp;&amp;A" },
{ "A&&", "A&amp;&amp;" },
{ "A&&A", "A&amp;&amp;A" },
{ "A&A&A", "A&amp;A&amp;A" },
{ "A&#23;A", "A&amp;#23;A" },
{ "A&#xa;A", "A&amp;#xa;A" }
};
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);
}
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&amp;", "&");
test_format ("%sA", "&amp;A", "&");
test_format ("A%sA", "A&amp;A", "&");
test_format ("%s%sA", "&amp;&amp;A", "&", "&");
test_format ("A%s%s", "A&amp;&amp;", "&", "&");
test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
test_format ("%s", "&lt;B&gt;&amp;", "<B>&");
test_format ("%c%c", "&lt;&amp;", '<', '&');
test_format (".%c.%c.", ".&lt;.&amp;.", '<', '&');
test_format ("%s", "", "");
test_format ("%-5s", "A ", "A");
test_format ("%2$s%1$s", "B.A.", "A.", "B.");
}
int main (int argc, char **argv)
{
gint 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/%d", 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/%d", 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 ();
}

View File

@ -96,7 +96,6 @@ test_programs = \
mainloop-test \
mapping-test \
markup-collect \
markup-escape-test \
module-test \
onceinit \
asyncqueue-test \
@ -145,7 +144,6 @@ list_test_LDADD = $(progs_ldadd)
mainloop_test_LDADD = $(thread_ldadd)
markup_test_LDADD = $(progs_ldadd)
mapping_test_LDADD = $(progs_ldadd)
markup_escape_test_LDADD = $(progs_ldadd)
module_test_LDADD = $(module_ldadd) $(module_test_exp)
module_test_LDFLAGS = $(G_MODULE_LDFLAGS)
onceinit_LDADD = $(thread_ldadd)
@ -167,7 +165,6 @@ type_test_LDADD = $(progs_ldadd)
unicode_encoding_LDADD = $(progs_ldadd)
unicode_caseconv_LDADD = $(progs_ldadd)
unicode_collate_LDADD = $(progs_ldadd)
markup_collect_LDADD = $(progs_ldadd)
noinst_LTLIBRARIES = libmoduletestplugin_a.la libmoduletestplugin_b.la

View File

@ -1,132 +0,0 @@
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <stdarg.h>
#include <string.h>
#include <glib.h>
static void test_format (const gchar *format,
const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
static gboolean error = FALSE;
static void
test (const gchar *original,
const gchar *expected)
{
gchar *result = g_markup_escape_text (original, -1);
if (strcmp (result, expected) != 0)
{
g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
expected, result);
error = TRUE;
}
g_free (result);
}
static void
test_unichar (gunichar c,
gboolean entity)
{
gint len;
gchar outbuf[7], expected[12];
len = g_unichar_to_utf8 (c, outbuf);
outbuf[len] = 0;
if (entity)
g_snprintf (expected, 12, "&#x%x;", c);
else
strcpy (expected, outbuf);
test (outbuf, expected);
}
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);
if (strcmp (result, expected) != 0)
{
g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
expected, result);
error = TRUE;
}
g_free (result);
}
int main (int argc, char **argv)
{
/* Tests for g_markup_escape_text() */
test ("&", "&amp;");
test ("<", "&lt;");
test (">", "&gt;");
test ("'", "&apos;");
test ("\"", "&quot;");
test ("", "");
test ("A", "A");
test ("A&", "A&amp;");
test ("&A", "&amp;A");
test ("A&A", "A&amp;A");
test ("&&A", "&amp;&amp;A");
test ("A&&", "A&amp;&amp;");
test ("A&&A", "A&amp;&amp;A");
test ("A&A&A", "A&amp;A&amp;A");
test ("A&#23;A", "A&amp;#23;A");
test ("A&#xa;A", "A&amp;#xa;A");
test_unichar (0x1, TRUE);
test_unichar (0x8, TRUE);
test_unichar (0x9, FALSE);
test_unichar (0xa, FALSE);
test_unichar (0xb, TRUE);
test_unichar (0xc, TRUE);
test_unichar (0xd, FALSE);
test_unichar (0xe, TRUE);
test_unichar (0x1f, TRUE);
test_unichar (0x20, FALSE);
test_unichar (0x7e, FALSE);
test_unichar (0x7f, TRUE);
test_unichar (0x84, TRUE);
test_unichar (0x85, FALSE);
test_unichar (0x86, TRUE);
test_unichar (0x9f, TRUE);
test_unichar (0xa0, FALSE);
/* Tests for g_markup_printf_escaped() */
test_format ("A", "A");
test_format ("A%s", "A&amp;", "&");
test_format ("%sA", "&amp;A", "&");
test_format ("A%sA", "A&amp;A", "&");
test_format ("%s%sA", "&amp;&amp;A", "&", "&");
test_format ("A%s%s", "A&amp;&amp;", "&", "&");
test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
test_format ("%s", "&lt;B&gt;&amp;",
"<B>&");
test_format ("%c%c", "&lt;&amp;",
'<', '&');
test_format (".%c.%c.", ".&lt;.&amp;.",
'<', '&');
test_format ("%s", "",
"");
test_format ("%-5s", "A ",
"A");
test_format ("%2$s%1$s", "B.A.",
"A.", "B.");
return error ? 1 : 0;
}