os/ossrv/glib/tsrc/BC/tests/markup-escape-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 #include <stdio.h>
     9 
    10 #ifdef SYMBIAN
    11 #include "mrt2_glib2_test.h"
    12 #endif /*SYMBIAN*/
    13 
    14 
    15 static void test_format (const gchar *format,
    16 			 const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
    17 
    18 static gboolean error = FALSE;
    19 
    20 static void
    21 test (const gchar *original,
    22       const gchar *expected)
    23 {
    24   gchar *result = g_markup_escape_text (original, -1);
    25 
    26   if (strcmp (result, expected) != 0)
    27     {
    28       g_print ("g_markup_escape_text(): expected '%s', got '%s'\n",
    29 		  expected, result);
    30       error = TRUE;
    31       assert_failed = TRUE;
    32     }
    33 
    34   g_free (result);
    35 }
    36 
    37 static void
    38 test_format (const gchar *format,
    39 	     const gchar *expected,
    40 	     ...)
    41 {
    42   gchar *result;
    43   
    44   va_list args;
    45   
    46   va_start (args, expected);
    47   result = g_markup_vprintf_escaped (format, args);
    48   va_end (args);
    49 
    50   if (strcmp (result, expected) != 0)
    51     {
    52       g_print ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
    53 		  expected, result);
    54 	  assert_failed = TRUE;
    55       error = TRUE;
    56     }
    57 
    58   g_free (result);
    59 }
    60 
    61 int main (int argc, char **argv)
    62 {
    63   #ifdef SYMBIAN
    64   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);
    65   g_set_print_handler(mrtPrintHandler);
    66   #endif /*SYMBIAN*/
    67 	  
    68 
    69   /* Tests for g_markup_escape_text() */
    70   test ("&", "&amp;");
    71   test ("<", "&lt;");
    72   test (">", "&gt;");
    73   test ("'", "&apos;");
    74   test ("\"", "&quot;");
    75   
    76   test ("", "");
    77   test ("A", "A");
    78   test ("A&", "A&amp;");
    79   test ("&A", "&amp;A");
    80   test ("A&A", "A&amp;A");
    81   test ("&&A", "&amp;&amp;A");
    82   test ("A&&", "A&amp;&amp;");
    83   test ("A&&A", "A&amp;&amp;A");
    84   test ("A&A&A", "A&amp;A&amp;A");
    85   
    86   /* Tests for g_markup_printf_escaped() */
    87   test_format ("A", "A");
    88   test_format ("A%s", "A&amp;", "&");
    89   test_format ("%sA", "&amp;A", "&");
    90   test_format ("A%sA", "A&amp;A", "&");
    91   test_format ("%s%sA", "&amp;&amp;A", "&", "&");
    92   test_format ("A%s%s", "A&amp;&amp;", "&", "&");
    93   test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
    94   test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
    95   
    96   test_format ("%s", "&lt;B&gt;&amp;",
    97 	       "<B>&");
    98   test_format ("%c%c", "&lt;&amp;",
    99 	       '<', '&');
   100   test_format (".%c.%c.", ".&lt;.&amp;.",
   101 	       '<', '&');
   102   test_format ("%s", "",
   103 	       "");
   104   test_format ("%-5s", "A    ",
   105 	       "A");
   106   test_format ("%2$s%1$s", "B.A.",
   107 	       "A.", "B.");
   108 	       
   109 
   110   #ifdef SYMBIAN
   111   testResultXml("markup-escape-test");
   112   #endif /* EMULATOR */
   113 
   114   return error ? 1 : 0;
   115 }