diff options
Diffstat (limited to 'server/src/configurationparser.cc')
| -rw-r--r-- | server/src/configurationparser.cc | 78 | 
1 files changed, 39 insertions, 39 deletions
diff --git a/server/src/configurationparser.cc b/server/src/configurationparser.cc index bc10e2b..0796cb9 100644 --- a/server/src/configurationparser.cc +++ b/server/src/configurationparser.cc @@ -28,7 +28,18 @@  #include "configuration.h" +const char *ConfigurationParser::ParseException::what() +  throw() +{ +  char lineno[32]; +  sprintf(lineno, "%d", l); +  _what = "Error when parsing the config file at line "; +  _what += std::string(lineno) + ": " + e; +  return _what.c_str(); +} +  ConfigurationParser::ConfigurationParser(std::string filename) +  throw(ParseException, ReadException)  {    this->filename = filename; @@ -36,17 +47,14 @@ ConfigurationParser::ConfigurationParser(std::string filename)  }  void ConfigurationParser::reload() -  throw(ConfigurationParserException) +  throw(ParseException, ReadException)  {    try {      readFile(this->filename.c_str());    } catch(libconfig::FileIOException) { -    throw ConfigurationParserException("Could not read config file: File does not exist."); +    throw ReadException();    } catch(libconfig::ParseException &e) { -    char lineno[32]; -    sprintf(lineno, "%d", e.getLine()); -    throw ConfigurationParserException(std::string("Error when parsing the config file in line ") -                                 + lineno + ": " + e.getError()); +    throw ParseException(e.getLine(), e.getError());    }    // Set internal values @@ -190,8 +198,13 @@ void ConfigurationParser::reload()  }  #ifdef TEST_CONFIGURATIONPARSER +//deps: configuration.cc +//cflags: -I.. $(CONFIG_CXXFLAGS) +//libs: $(CONFIG_LIBS) +#include <test.h>  #define CONFFILE "/tmp/configurationparser.conf" +#define NOSUCH_CONFFILE "/tmp/ladida_configurationparser.conf"  #include <sys/types.h>  #include <sys/stat.h> @@ -212,41 +225,28 @@ static char confbad[] =  "c = true;\n"  ; -int main() -{ -  FILE *fp = fopen(CONFFILE, "w"); -  if(!fp) { -    printf("Could not write to %s\n", CONFFILE); -    return 1; -  } -  fprintf(fp, conf); -  fclose(fp); -  try { -    ConfigurationParser parser(CONFFILE); -  } catch(Exception &e) { -    printf("%s\n", e.what()); -    return 1; -  } +TEST_BEGIN; -  fp = fopen(CONFFILE, "w"); -  if(!fp) { -    printf("Could not write to %s\n", CONFFILE); -    return 1; -  } -  fprintf(fp, confbad); -  fclose(fp); -  try { -    ConfigurationParser parser(CONFFILE); -  } catch(Exception &e) { -    printf("%s\n", e.what()); -    goto on; -  } -  return 1; - on: +FILE *fp = fopen(CONFFILE, "w"); +if(!fp) TEST_FATAL("Could not write to "CONFFILE"\n"); +fprintf(fp, "%s", conf); +fclose(fp); -  unlink(CONFFILE); +TEST_NOEXCEPTION(ConfigurationParser parser(CONFFILE), "Creation"); -  return 0; -} +fp = fopen(CONFFILE, "w"); +if(!fp) TEST_FATAL("Could not write to "CONFFILE"\n"); +fprintf(fp, "%s", confbad); +fclose(fp); + +TEST_EXCEPTION(ConfigurationParser parser(CONFFILE),  +               ConfigurationParser::ParseException, "Bad syntax"); + +TEST_EXCEPTION(ConfigurationParser parser(NOSUCH_CONFFILE),  +               ConfigurationParser::ReadException, "No such file"); + +unlink(CONFFILE); + +TEST_END;  #endif/*TEST_CONFIGURATIONPARSER*/  | 
