sl@0: /* sl@0: * Copyright © 2007 Ryan Lortie sl@0: * Portions copyright (c) 2009 Nokia Corporation. All rights reserved. sl@0: * This program is free software: you can redistribute it and/or modify sl@0: * it under the terms of the GNU Lesser General Public License as sl@0: * published by the Free Software Foundation; either version 2 of the sl@0: * License, or (at your option) any later version. sl@0: * sl@0: * See the included COPYING file for more information. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*__SYMBIAN32__*/ sl@0: static void sl@0: start (GMarkupParseContext *context, sl@0: const char *element_name, sl@0: const char **attribute_names, sl@0: const char **attribute_values, sl@0: gpointer user_data, sl@0: GError **error) sl@0: { sl@0: GString *string = user_data; sl@0: gboolean result; sl@0: #ifndef __SYMBIAN32__ sl@0: #define collect(...) \ sl@0: g_markup_collect_attributes (element_name, attribute_names, \ sl@0: attribute_values, error, __VA_ARGS__, \ sl@0: G_MARKUP_COLLECT_INVALID) sl@0: #else sl@0: #define collect(args...) \ sl@0: g_markup_collect_attributes (element_name, attribute_names, \ sl@0: attribute_values, error, args, \ sl@0: G_MARKUP_COLLECT_INVALID) sl@0: #endif//__SYMBIAN32__ sl@0: #define BOOL G_MARKUP_COLLECT_BOOLEAN sl@0: #define OPTBOOL G_MARKUP_COLLECT_BOOLEAN | G_MARKUP_COLLECT_OPTIONAL sl@0: #define TRI G_MARKUP_COLLECT_TRISTATE sl@0: #define STR G_MARKUP_COLLECT_STRING sl@0: #define STRDUP G_MARKUP_COLLECT_STRDUP sl@0: #define OPTSTR G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL sl@0: #define OPTDUP G_MARKUP_COLLECT_STRDUP | G_MARKUP_COLLECT_OPTIONAL sl@0: #define n(x) ((x)?(x):"(null)") sl@0: sl@0: if (strcmp (element_name, "bool") == 0) sl@0: { sl@0: gboolean mb = 2, ob = 2, tri = 2; sl@0: sl@0: result = collect (BOOL, "mb", &mb, sl@0: OPTBOOL, "ob", &ob, sl@0: TRI, "tri", &tri); sl@0: sl@0: g_assert (result || sl@0: (mb == FALSE && ob == FALSE && tri != TRUE && tri != FALSE)); sl@0: sl@0: if (tri != FALSE && tri != TRUE) sl@0: tri = -1; sl@0: sl@0: g_string_append_printf (string, "", sl@0: result, mb, ob, tri); sl@0: } sl@0: sl@0: else if (strcmp (element_name, "str") == 0) sl@0: { sl@0: const char *cm, *co; sl@0: char *am, *ao; sl@0: sl@0: result = collect (STR, "cm", &cm, sl@0: STRDUP, "am", &am, sl@0: OPTDUP, "ao", &ao, sl@0: OPTSTR, "co", &co); sl@0: sl@0: g_assert (result || sl@0: (cm == NULL && am == NULL && ao == NULL && co == NULL)); sl@0: sl@0: g_string_append_printf (string, "", sl@0: result, n (cm), n (am), n (ao), n (co)); sl@0: sl@0: g_free (am); sl@0: g_free (ao); sl@0: } sl@0: } sl@0: sl@0: static GMarkupParser parser = { start }; sl@0: sl@0: struct test sl@0: { sl@0: const char *document; sl@0: const char *result; sl@0: GMarkupError error_code; sl@0: const char *error_info; sl@0: }; sl@0: sl@0: static struct test tests[] = sl@0: { sl@0: { "", "", sl@0: G_MARKUP_ERROR_PARSE, "'bool'" }, sl@0: sl@0: { "", "" }, sl@0: { "", "" }, sl@0: { "", "" }, sl@0: { "", "" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'mb'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'tri'" }, sl@0: sl@0: { "", "" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'am'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'qm'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'am'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'am'" }, sl@0: sl@0: { "", sl@0: "", sl@0: G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'a'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" }, sl@0: sl@0: { "", "", sl@0: G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" } sl@0: }; sl@0: sl@0: int sl@0: main (int argc, char **argv) sl@0: { sl@0: gboolean verbose = FALSE; sl@0: int i; sl@0: #ifdef __SYMBIAN32__ sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: if (argc > 1) sl@0: { sl@0: if (argc != 2 || strcmp (argv[1], "-v") != 0) sl@0: { sl@0: g_print ("error: call with no arguments or '-v' for verbose\n"); sl@0: return 1; sl@0: } sl@0: sl@0: verbose = TRUE; sl@0: } sl@0: sl@0: for (i = 0; i < G_N_ELEMENTS (tests); i++) sl@0: { sl@0: GMarkupParseContext *ctx; sl@0: GError *error = NULL; sl@0: GString *string; sl@0: gboolean result; sl@0: sl@0: string = g_string_new (""); sl@0: ctx = g_markup_parse_context_new (&parser, 0, string, NULL); sl@0: result = g_markup_parse_context_parse (ctx, sl@0: tests[i].document, sl@0: -1, &error); sl@0: if (result) sl@0: result = g_markup_parse_context_end_parse (ctx, &error); sl@0: sl@0: if (verbose) sl@0: g_print ("%d: %s:\n (error %d, \"%s\")\n %s\n\n", sl@0: i, tests[i].document, sl@0: error ? error->code : 0, sl@0: error ? error->message : "(no error)", sl@0: string->str); sl@0: sl@0: if (result) sl@0: { sl@0: if (error != NULL) sl@0: g_error ("parser successful but error is set: " sl@0: "%s(%d) '%s'", g_quark_to_string (error->domain), sl@0: error->code, error->message); sl@0: sl@0: if (tests[i].error_code != 0) sl@0: g_error ("parser succeeded on test %d ('%s') but " sl@0: "we expected a failure with code %d\n", i, sl@0: tests[i].document, tests[i].error_code); sl@0: } sl@0: else sl@0: { sl@0: if (error->domain != G_MARKUP_ERROR) sl@0: g_error ("error occured on test %d ('%s') but is not in " sl@0: "the GMarkupError domain, but rather '%s'", i, sl@0: tests[i].document, g_quark_to_string (error->domain)); sl@0: sl@0: if (error->code != tests[i].error_code) sl@0: g_error ("failure expected with test %d ('%s') but it " sl@0: "has error code %d (we expected code %d)", i, sl@0: tests[i].document, error->code, tests[i].error_code); sl@0: sl@0: if (strstr (error->message, tests[i].error_info) == NULL) sl@0: g_error ("failure message on test %d ('%s') fails " sl@0: "to mention '%s' in the error message", i, sl@0: tests[i].document, tests[i].error_info); sl@0: } sl@0: sl@0: if (strcmp (tests[i].result, string->str) != 0) sl@0: g_error ("result on test %d ('%s') expected to be '%s' " sl@0: "but came out as '%s'", i, tests[i].document, sl@0: tests[i].result, string->str); sl@0: sl@0: g_markup_parse_context_free (ctx); sl@0: g_string_free (string, TRUE); sl@0: g_clear_error (&error); sl@0: } sl@0: sl@0: if (verbose) sl@0: g_print ("\n*** all tests passed ***\n\n"); sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("markup-collect"); sl@0: #endif /* EMULATOR */ sl@0: return 0; sl@0: }