diff options
| author | deva <deva> | 2009-08-07 13:21:27 +0000 | 
|---|---|---|
| committer | deva <deva> | 2009-08-07 13:21:27 +0000 | 
| commit | f23b20281a5d0badd8c2d2904b44365418ad46a5 (patch) | |
| tree | a7ff5e9961126e7893d888c5022977c028cf64cc /server/src/macroparser.cc | |
| parent | 1e1217e49bcac9228e37c7b6868d63da900ab07f (diff) | |
New test program.
Diffstat (limited to 'server/src/macroparser.cc')
| -rw-r--r-- | server/src/macroparser.cc | 118 | 
1 files changed, 90 insertions, 28 deletions
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 <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" +"<macro name=\"testmacro\" version=\"1.0\">\n" +" <resume/>\n" +" <queries/>\n" +" <maps/>\n" +" <scripts/>\n" +" <widgets/>\n" +"</macro>" +; + +static char xml_nonmacro[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<dims name=\"testmacro\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</dims>" +; + +static char xml_fail[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<macro name\"testmacro\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</macro>" +;  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;  }  | 
