From ed33082fd43cf88f5edb2509f4abcd847bb6841a Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 30 Jul 2009 10:17:35 +0000 Subject: Added unit tests for MacroHeaderParser and TemplateHeaderParser. --- server/src/macroheaderparser.cc | 106 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) (limited to 'server/src/macroheaderparser.cc') diff --git a/server/src/macroheaderparser.cc b/server/src/macroheaderparser.cc index def80c3..6193e94 100644 --- a/server/src/macroheaderparser.cc +++ b/server/src/macroheaderparser.cc @@ -135,10 +135,114 @@ void MacroHeaderParser::parseError(char *buf, size_t len, std::string error, int throw Exception(error + slineno); free(slineno); } - } Macro *MacroHeaderParser::getMacro() { return m; } + +#ifdef TEST_MACROHEADERPARSER + +#define XMLFILE "/tmp/test_macroheaderparser.xml" + +#include +#include +#include +#include +#include +#include + +static char xml[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +static char xml_nonmacro[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +static char xml_fail[] = +"\n" +"\n" +" \n" +" \n" +"" +; + +int main() +{ + 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 macro xml data. + MacroHeaderParser 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_nonmacro); + fclose(fp); + + // Test parsing of correct xml data, but not macro (should throw an exception). + { + MacroHeaderParser 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). + { + MacroHeaderParser 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; +} + +#endif/*TEST_MACROHEADERPARSER*/ -- cgit v1.2.3