glib/tests/markup-escape-test.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
       
     2 #undef G_DISABLE_ASSERT
       
     3 #undef G_LOG_DOMAIN
       
     4 
       
     5 #include <stdarg.h>
       
     6 #include <string.h>
       
     7 #include <glib.h>
       
     8 #ifdef __SYMBIAN32__
       
     9 #include "mrt2_glib2_test.h"
       
    10 #endif /*__SYMBIAN32__*/
       
    11 
       
    12 
       
    13 static void test_format (const gchar *format,
       
    14 			 const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
       
    15 
       
    16 static gboolean error = FALSE;
       
    17 
       
    18 static void
       
    19 test (const gchar *original,
       
    20       const gchar *expected)
       
    21 {
       
    22   gchar *result = g_markup_escape_text (original, -1);
       
    23 
       
    24   if (strcmp (result, expected) != 0)
       
    25     {
       
    26       g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
       
    27 		  expected, result);
       
    28       error = TRUE;
       
    29     }
       
    30 
       
    31   g_free (result);
       
    32 }
       
    33 
       
    34 static void
       
    35 test_unichar (gunichar c, 
       
    36               gboolean entity)
       
    37 {
       
    38   gint len;
       
    39   gchar outbuf[7], expected[12];
       
    40 
       
    41   len = g_unichar_to_utf8 (c, outbuf);
       
    42   outbuf[len] = 0;
       
    43 
       
    44   if (entity)
       
    45     g_snprintf (expected, 12, "&#x%x;", c);
       
    46   else
       
    47     strcpy (expected, outbuf);
       
    48 
       
    49   test (outbuf, expected);
       
    50 }
       
    51 
       
    52 static void
       
    53 test_format (const gchar *format,
       
    54 	     const gchar *expected,
       
    55 	     ...)
       
    56 {
       
    57   gchar *result;
       
    58   
       
    59   va_list args;
       
    60   
       
    61   va_start (args, expected);
       
    62   result = g_markup_vprintf_escaped (format, args);
       
    63   va_end (args);
       
    64 
       
    65   if (strcmp (result, expected) != 0)
       
    66     {
       
    67       g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
       
    68 		  expected, result);
       
    69       error = TRUE;
       
    70     }
       
    71 
       
    72   g_free (result);
       
    73 }
       
    74 
       
    75 int main (int argc, char **argv)
       
    76 {
       
    77   #ifdef __SYMBIAN32__
       
    78   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
    79   g_set_print_handler(mrtPrintHandler);
       
    80   #endif /*__SYMBIAN32__*/
       
    81 	  
       
    82 
       
    83   /* Tests for g_markup_escape_text() */
       
    84   test ("&", "&amp;");
       
    85   test ("<", "&lt;");
       
    86   test (">", "&gt;");
       
    87   test ("'", "&apos;");
       
    88   test ("\"", "&quot;");
       
    89   
       
    90   test ("", "");
       
    91   test ("A", "A");
       
    92   test ("A&", "A&amp;");
       
    93   test ("&A", "&amp;A");
       
    94   test ("A&A", "A&amp;A");
       
    95   test ("&&A", "&amp;&amp;A");
       
    96   test ("A&&", "A&amp;&amp;");
       
    97   test ("A&&A", "A&amp;&amp;A");
       
    98   test ("A&A&A", "A&amp;A&amp;A");
       
    99   test ("A&#23;A", "A&amp;#23;A");
       
   100   test ("A&#xa;A", "A&amp;#xa;A");
       
   101   test_unichar (0x1, TRUE);
       
   102   test_unichar (0x8, TRUE);
       
   103   test_unichar (0x9, FALSE);
       
   104   test_unichar (0xa, FALSE);
       
   105   test_unichar (0xb, TRUE);
       
   106   test_unichar (0xc, TRUE);
       
   107   test_unichar (0xd, FALSE);
       
   108   test_unichar (0xe, TRUE);
       
   109   test_unichar (0x1f, TRUE);
       
   110   test_unichar (0x20, FALSE);
       
   111   test_unichar (0x7e, FALSE);
       
   112   test_unichar (0x7f, TRUE);
       
   113   test_unichar (0x84, TRUE);
       
   114   test_unichar (0x85, FALSE);
       
   115   test_unichar (0x86, TRUE);
       
   116   test_unichar (0x9f, TRUE);
       
   117   test_unichar (0xa0, FALSE);
       
   118   
       
   119   /* Tests for g_markup_printf_escaped() */
       
   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   
       
   129   test_format ("%s", "&lt;B&gt;&amp;",
       
   130 	       "<B>&");
       
   131   test_format ("%c%c", "&lt;&amp;",
       
   132 	       '<', '&');
       
   133   test_format (".%c.%c.", ".&lt;.&amp;.",
       
   134 	       '<', '&');
       
   135   test_format ("%s", "",
       
   136 	       "");
       
   137   test_format ("%-5s", "A    ",
       
   138 	       "A");
       
   139   test_format ("%2$s%1$s", "B.A.",
       
   140 	       "A.", "B.");
       
   141 	       
       
   142 
       
   143   #ifdef __SYMBIAN32__
       
   144   testResultXml("markup-escape-test");
       
   145   #endif /* EMULATOR */
       
   146 
       
   147   return error ? 1 : 0;
       
   148 }