glib/tests/markup-escape-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #include <stdarg.h>
       
    22 #include <string.h>
       
    23 #include <glib.h>
       
    24 #ifdef __SYMBIAN32__
       
    25 #include "mrt2_glib2_test.h"
       
    26 #endif /*__SYMBIAN32__*/
       
    27 
       
    28 
       
    29 static void test_format (const gchar *format,
       
    30 			 const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
       
    31 
       
    32 static gboolean error = FALSE;
       
    33 
       
    34 static void
       
    35 test (const gchar *original,
       
    36       const gchar *expected)
       
    37 {
       
    38   gchar *result = g_markup_escape_text (original, -1);
       
    39 
       
    40   if (strcmp (result, expected) != 0)
       
    41     {
       
    42       g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
       
    43 		  expected, result);
       
    44       error = TRUE;
       
    45     }
       
    46 
       
    47   g_free (result);
       
    48 }
       
    49 
       
    50 static void
       
    51 test_unichar (gunichar c, 
       
    52               gboolean entity)
       
    53 {
       
    54   gint len;
       
    55   gchar outbuf[7], expected[12];
       
    56 
       
    57   len = g_unichar_to_utf8 (c, outbuf);
       
    58   outbuf[len] = 0;
       
    59 
       
    60   if (entity)
       
    61     g_snprintf (expected, 12, "&#x%x;", c);
       
    62   else
       
    63     strcpy (expected, outbuf);
       
    64 
       
    65   test (outbuf, expected);
       
    66 }
       
    67 
       
    68 static void
       
    69 test_format (const gchar *format,
       
    70 	     const gchar *expected,
       
    71 	     ...)
       
    72 {
       
    73   gchar *result;
       
    74   
       
    75   va_list args;
       
    76   
       
    77   va_start (args, expected);
       
    78   result = g_markup_vprintf_escaped (format, args);
       
    79   va_end (args);
       
    80 
       
    81   if (strcmp (result, expected) != 0)
       
    82     {
       
    83       g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
       
    84 		  expected, result);
       
    85       error = TRUE;
       
    86     }
       
    87 
       
    88   g_free (result);
       
    89 }
       
    90 
       
    91 int main (int argc, char **argv)
       
    92 {
       
    93   #ifdef __SYMBIAN32__
       
    94   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);
       
    95   g_set_print_handler(mrtPrintHandler);
       
    96   #endif /*__SYMBIAN32__*/
       
    97 	  
       
    98 
       
    99   /* Tests for g_markup_escape_text() */
       
   100   test ("&", "&amp;");
       
   101   test ("<", "&lt;");
       
   102   test (">", "&gt;");
       
   103   test ("'", "&apos;");
       
   104   test ("\"", "&quot;");
       
   105   
       
   106   test ("", "");
       
   107   test ("A", "A");
       
   108   test ("A&", "A&amp;");
       
   109   test ("&A", "&amp;A");
       
   110   test ("A&A", "A&amp;A");
       
   111   test ("&&A", "&amp;&amp;A");
       
   112   test ("A&&", "A&amp;&amp;");
       
   113   test ("A&&A", "A&amp;&amp;A");
       
   114   test ("A&A&A", "A&amp;A&amp;A");
       
   115   test ("A&#23;A", "A&amp;#23;A");
       
   116   test ("A&#xa;A", "A&amp;#xa;A");
       
   117   test_unichar (0x1, TRUE);
       
   118   test_unichar (0x8, TRUE);
       
   119   test_unichar (0x9, FALSE);
       
   120   test_unichar (0xa, FALSE);
       
   121   test_unichar (0xb, TRUE);
       
   122   test_unichar (0xc, TRUE);
       
   123   test_unichar (0xd, FALSE);
       
   124   test_unichar (0xe, TRUE);
       
   125   test_unichar (0x1f, TRUE);
       
   126   test_unichar (0x20, FALSE);
       
   127   test_unichar (0x7e, FALSE);
       
   128   test_unichar (0x7f, TRUE);
       
   129   test_unichar (0x84, TRUE);
       
   130   test_unichar (0x85, FALSE);
       
   131   test_unichar (0x86, TRUE);
       
   132   test_unichar (0x9f, TRUE);
       
   133   test_unichar (0xa0, FALSE);
       
   134   
       
   135   /* Tests for g_markup_printf_escaped() */
       
   136   test_format ("A", "A");
       
   137   test_format ("A%s", "A&amp;", "&");
       
   138   test_format ("%sA", "&amp;A", "&");
       
   139   test_format ("A%sA", "A&amp;A", "&");
       
   140   test_format ("%s%sA", "&amp;&amp;A", "&", "&");
       
   141   test_format ("A%s%s", "A&amp;&amp;", "&", "&");
       
   142   test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
       
   143   test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
       
   144   
       
   145   test_format ("%s", "&lt;B&gt;&amp;",
       
   146 	       "<B>&");
       
   147   test_format ("%c%c", "&lt;&amp;",
       
   148 	       '<', '&');
       
   149   test_format (".%c.%c.", ".&lt;.&amp;.",
       
   150 	       '<', '&');
       
   151   test_format ("%s", "",
       
   152 	       "");
       
   153   test_format ("%-5s", "A    ",
       
   154 	       "A");
       
   155   test_format ("%2$s%1$s", "B.A.",
       
   156 	       "A.", "B.");
       
   157 	       
       
   158 
       
   159   #ifdef __SYMBIAN32__
       
   160   testResultXml("markup-escape-test");
       
   161   #endif /* EMULATOR */
       
   162 
       
   163   return error ? 1 : 0;
       
   164 }