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/templateparser.cc | 142 +++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 44 deletions(-) (limited to 'server/src/templateparser.cc') diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc index d01aa31..277a8bd 100644 --- a/server/src/templateparser.cc +++ b/server/src/templateparser.cc @@ -144,6 +144,12 @@ void TemplateParser::parseError(char *buf, size_t len, std::string error, int li if(fwrite(buf, len, 1, stderr) != len) {} fprintf(stderr, "]\n"); fflush(stderr); + + char *slineno; + if(asprintf(&slineno, " at line %d\n", lineno) != -1) { + throw Exception(error + slineno); + free(slineno); + } } Template *TemplateParser::getTemplate() @@ -153,56 +159,104 @@ Template *TemplateParser::getTemplate() #ifdef TEST_TEMPLATEPARSER -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_templateparser.xml" + +#include +#include +#include +#include +#include +#include + +static char xml[] = +"\n" +"" +; + +static char xml_nontemplate[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +static char xml_fail[] = +"\n" +"" +; int main() { - Conf::xml_basedir = "../xml/"; - - try { - TemplateParser parser("../xml/templates/example.xml"); - parser.parse(); - - Template *t = parser.getTemplate(); - - printf("\t[Template]:\n"); - print_attributes("\t\t-", t->attributes); - - printf("\t\t[Macros]:\n"); - std::vector< Macro >::iterator i = t->macros.begin(); - - while(i != t->macros.end()) { - printf("\t\t\t[Macro]:\n"); - print_attributes("\t\t\t\t-", (*i).attributes); - - std::vector< Query >::iterator qi = (*i).queries.begin(); - while(qi != (*i).queries.end()) { - printf("\t\t\t\t[Query]:\n"); - print_attributes("\t\t\t\t\t-", (*qi).attributes); - qi++; - } - - std::vector< Map >::iterator mi = (*i).maps.begin(); - while(mi != (*i).maps.end()) { - printf("\t\t\t\t[Map]:\n"); - print_attributes("\t\t\t\t\t-", (*mi).attributes); - mi++; - } - - i++; + FILE *fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml); + fclose(fp); + + { + // Test parsing of correct template xml data. + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + return 1; + } + } + + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_nontemplate); + fclose(fp); + + // Test parsing of correct xml data, but not template (should throw an exception). + { + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto onandon; + } + return 1; + } + onandon: + + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_fail); + fclose(fp); + + // Test parsing of invalid xml data (should throw an exception). + { + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto yetonandon; } - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); return 1; } + yetonandon: + + unlink(XMLFILE); + return 0; } -- cgit v1.2.3