From f23b20281a5d0badd8c2d2904b44365418ad46a5 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 7 Aug 2009 13:21:27 +0000 Subject: New test program. --- server/src/macroparser.cc | 118 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 90 insertions(+), 28 deletions(-) (limited to 'server/src/macroparser.cc') diff --git a/server/src/macroparser.cc b/server/src/macroparser.cc index b91462a..0b0257e 100644 --- a/server/src/macroparser.cc +++ b/server/src/macroparser.cc @@ -325,44 +325,106 @@ Macro *MacroParser::getMacro() #ifdef TEST_MACROPARSER -void print_attributes(std::string prefix, - std::map< std::string, std::string > &att) -{ - std::map< std::string, std::string >::iterator i = att.begin(); - while(i != att.end()) { - printf("%s %s = \"%s\"\n", prefix.c_str(), (*i).first.c_str(), (*i).second.c_str()); - i++; - } -} +#define XMLFILE "/tmp/test_macroheaderparser.xml" + +#include +#include +#include +#include +#include +#include + +static char xml[] = +"\n" +"\n" +" \n" +" \n" +" \n" +" \n" +" \n" +"" +; + +static char xml_nonmacro[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +static char xml_fail[] = +"\n" +"\n" +" \n" +" \n" +"" +; int main() { - try { - MacroParser parser("../xml/macros/example.xml"); - parser.parse(); + FILE *fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml); + fclose(fp); - Macro *m = parser.getMacro(); + { + // Test parsing of correct macro xml data. + MacroParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + return 1; + } + } - printf("\t\t\t[Macro]:\n"); - print_attributes("\t\t\t\t-", m->attributes); - - std::vector< Query >::iterator qi = m->queries.begin(); - while(qi != m->queries.end()) { - printf("\t\t\t\t[Query]:\n"); - print_attributes("\t\t\t\t\t-", (*qi).attributes); - qi++; + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_nonmacro); + fclose(fp); + + // Test parsing of correct xml data, but not macro (should throw an exception). + { + MacroParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto onandon; + } + return 1; } + onandon: - std::vector< Map >::iterator mi = m->maps.begin(); - while(mi != m->maps.end()) { - printf("\t\t\t\t[Map]:\n"); - print_attributes("\t\t\t\t\t-", (*mi).attributes); - mi++; + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; } - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); + fprintf(fp, xml_fail); + fclose(fp); + + // Test parsing of invalid xml data (should throw an exception). + { + MacroParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto yetonandon; + } return 1; } + yetonandon: + + unlink(XMLFILE); return 0; } -- cgit v1.2.3