diff options
author | deva <deva> | 2009-07-30 10:17:35 +0000 |
---|---|---|
committer | deva <deva> | 2009-07-30 10:17:35 +0000 |
commit | ed33082fd43cf88f5edb2509f4abcd847bb6841a (patch) | |
tree | 0e145008877c6ad59c8bb33288b0c02a2926cd6e /server/src/templateheaderparser.cc | |
parent | fca987525e5b6db7b4387f694122598f0a586576 (diff) |
Added unit tests for MacroHeaderParser and TemplateHeaderParser.
Diffstat (limited to 'server/src/templateheaderparser.cc')
-rw-r--r-- | server/src/templateheaderparser.cc | 106 |
1 files changed, 105 insertions, 1 deletions
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 <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#include <stdio.h> +#include <memory.h> + +static char xml[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<template name=\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</template>" +; + +static char xml_nontemplate[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<dims name=\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</dims>" +; + +static char xml_fail[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<template name\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</template>" +; + +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*/ |