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/Makefile.am | 34 ++++++++---- server/src/macroheaderparser.cc | 106 ++++++++++++++++++++++++++++++++++++- server/src/templateheaderparser.cc | 106 ++++++++++++++++++++++++++++++++++++- 3 files changed, 235 insertions(+), 11 deletions(-) diff --git a/server/src/Makefile.am b/server/src/Makefile.am index c8e1c51..2276347 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -106,6 +106,8 @@ EXTRA_DIST = \ ################ TESTFILES = \ + test_templateheaderparser \ + test_macroheaderparser \ test_templatelist \ test_saxparser \ test_transactionparser \ @@ -139,6 +141,20 @@ test: $(TESTFILES) test_clean: rm -f $(TESTFILES) $(TESTLOGS) +TEST_TEMPLATEHEADERPARSER_FILES = \ + templateheaderparser.cc \ + $(PARSERFILES) \ + $(BASICFILES) +test_templateheaderparser: $(TEST_TEMPLATEHEADERPARSER_FILES) + @../../tools/test $(TEST_TEMPLATEHEADERPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS) + +TEST_MACROHEADERPARSER_FILES = \ + macroheaderparser.cc \ + $(PARSERFILES) \ + $(BASICFILES) +test_macroheaderparser: $(TEST_MACROHEADERPARSER_FILES) + @../../tools/test $(TEST_MACROHEADERPARSER_FILES) $(BASICFLAGS) $(PARSERFLAGS) + TEST_TEMPLATELIST_FILES = \ templatelist.cc \ versionstr.cc \ @@ -148,6 +164,15 @@ TEST_TEMPLATELIST_FILES = \ test_templatelist: $(TEST_TEMPLATELIST_FILES) @../../tools/test $(TEST_TEMPLATELIST_FILES) $(PARSERFLAGS) $(BASICFLAGS) +TEST_MACROLIST_FILES = \ + macrolist.cc \ + versionstr.cc \ + macroheaderparser.cc \ + $(PARSERFILES) \ + $(BASICFILES) +test_macrolist: $(TEST_MACROLIST_FILES) + @../../tools/test $(TEST_MACROLIST_FILES) $(PARSERFLAGS) $(BASICFLAGS) + TEST_SAXPARSER_FILES = \ saxparser.cc \ $(BASICFILES) @@ -167,15 +192,6 @@ TEST_VERSIONSTR_FILES = \ test_versionstr: $(TEST_VERSIONSTR_FILES) @../../tools/test $(TEST_VERSIONSTR_FILES) $(BASICFLAGS) -TEST_MACROLIST_FILES = \ - macrolist.cc \ - versionstr.cc \ - macroheaderparser.cc \ - $(PARSERFILES) \ - $(BASICFILES) -test_macrolist: $(TEST_MACROLIST_FILES) - @../../tools/test $(TEST_MACROLIST_FILES) $(PARSERFLAGS) $(BASICFLAGS) - TEST_QUERYHANDLERPENTOMINOS_FILES = \ queryhandlerpentominos.cc \ tcpsocket.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*/ diff --git a/server/src/templateheaderparser.cc b/server/src/templateheaderparser.cc index 0b274ee..0a68a6a 100644 --- a/server/src/templateheaderparser.cc +++ b/server/src/templateheaderparser.cc @@ -135,10 +135,114 @@ void TemplateHeaderParser::parseError(char *buf, size_t len, std::string error, throw Exception(error + slineno); free(slineno); } - } Template *TemplateHeaderParser::getTemplate() { return t; } + +#ifdef TEST_TEMPLATEHEADERPARSER + +#define XMLFILE "/tmp/test_templateheaderparser.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() +{ + 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. + TemplateHeaderParser 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). + { + TemplateHeaderParser 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). + { + TemplateHeaderParser 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_TEMPLATEHEADERPARSER*/ -- cgit v1.2.3