From: Eduardo Ramos Testillano Date: Tue, 25 Jun 2013 01:16:33 +0000 (+0200) Subject: New Environment core class X-Git-Tag: REFACTORING_TESTING_LIBRARY~273 X-Git-Url: https://git.teslayout.com/public/public/public/?a=commitdiff_plain;h=294a78c6bf69289126940ac2348a89ecd536be12;p=anna.git New Environment core class --- diff --git a/include/anna/core/util/Environment.hpp b/include/anna/core/util/Environment.hpp index fa7fb09..02c6aba 100644 --- a/include/anna/core/util/Environment.hpp +++ b/include/anna/core/util/Environment.hpp @@ -74,7 +74,7 @@ public: private: - std::map a_managedVars; + std::map < std::string /* variable name */, std::string /* variable value */ > a_managedVars; Environment() { initialize();} diff --git a/pre-commit.sh b/pre-commit.sh index 8a7577b..2771db8 100755 --- a/pre-commit.sh +++ b/pre-commit.sh @@ -96,6 +96,9 @@ test_style () { rm "${newfile}" if [ $r != 0 ] ; then echo "Code style error in '$file', please fix before commiting." + echo "To autofix, create a hidden file named './.fix_style' on suite root." +pwd +echo xxxx exit 1 fi } diff --git a/source/core/util/Environment.cpp b/source/core/util/Environment.cpp index 7b51fa1..83ed2f8 100644 --- a/source/core/util/Environment.cpp +++ b/source/core/util/Environment.cpp @@ -55,27 +55,24 @@ using namespace anna; void Environment::initialize() throw() { LOGMETHOD(TraceMethod tm("Environment", "initialize", ANNA_FILE_LOCATION)); - // clear data a_managedVars.clear(); - // Register: namespace po = boost::program_options; po::options_description desc("Options"); - FILE *fp; char c_var[256]; std::string var; - /* Open the command for reading. */ fp = popen("env | cut -d'=' -f1", "r"); - if (fp == NULL) { + + if(fp == NULL) { Logger::error("Failed to get environment variables list", ANNA_FILE_LOCATION); return; } /* Read the output a line at a time - output it. */ - while (fgets(c_var, sizeof(c_var)-1, fp) != NULL) { + while(fgets(c_var, sizeof(c_var) - 1, fp) != NULL) { var = c_var; boost::trim(var); desc.add_options()(var.c_str(), var.c_str()); @@ -84,43 +81,45 @@ void Environment::initialize() throw() { /* close */ pclose(fp); - // Parsing: po::variables_map vm; - try { - po::store(po::parse_environment(desc, [](const std::string& variable) { return variable; }), vm); // can throw + try { + po::store(po::parse_environment(desc, [](const std::string & variable) { return variable; }), vm); // can throw std::map::const_iterator it; std::string var, val; - for (it = a_managedVars.begin(); it != a_managedVars.end(); it++) { + + for(it = a_managedVars.begin(); it != a_managedVars.end(); it++) { var = (*it).first; - if (vm.count(var.c_str())) { // protection + + if(vm.count(var.c_str())) { // protection val = vm[var.c_str()].as(); a_managedVars[var] = val; } } po::notify(vm); - - } catch (po::error& e) { + } catch(po::error& e) { Logger::error(e.what(), ANNA_FILE_LOCATION); } } -std::string Environment::getValue (const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) { +std::string Environment::getValue(const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) { std::string result = ""; - if (!variableName) + if(!variableName) throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION); std::map::const_iterator it = a_managedVars.find(variableName); - if (it == a_managedVars.end()) { + + if(it == a_managedVars.end()) { std::string msg = "The variable '"; msg += variableName; msg += "' is not defined in the environment."; LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION)); - if (exceptionIfMissing) - throw RuntimeException(msg, ANNA_FILE_LOCATION); + + if(exceptionIfMissing) throw RuntimeException(msg, ANNA_FILE_LOCATION); + return ""; } diff --git a/test/core/main.cpp b/test/core/main.cpp index d804ab0..09d02ce 100644 --- a/test/core/main.cpp +++ b/test/core/main.cpp @@ -55,26 +55,21 @@ using namespace anna; // http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html BOOST_AUTO_TEST_CASE(tokenizer) { - anna::Tokenizer lst; lst.apply("In three words", " "); //BOOST_REQUIRE(lst.size() == 3); BOOST_REQUIRE_EQUAL(lst.size(), 3); - anna::Tokenizer::const_iterator tok_iter(lst.begin()); BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "In"); BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "three"); BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "words"); - BOOST_REQUIRE_EQUAL(tok_iter, lst.end()); + BOOST_REQUIRE_EQUAL(tok_iter, lst.end()); BOOST_CHECK_THROW(lst.at(10), anna::RuntimeException); - lst.apply("In three words", ","); BOOST_REQUIRE_EQUAL(lst.size(), 1); - lst.apply("", ","); BOOST_REQUIRE_EQUAL(lst.size(), 0); BOOST_CHECK_THROW(lst.last(), anna::RuntimeException); - // std::string str = ""; // for (int k = 0; k < 100; k++) // str += "x "; @@ -82,44 +77,34 @@ BOOST_AUTO_TEST_CASE(tokenizer) { } BOOST_AUTO_TEST_CASE(functions_asString) { - std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8); BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs"); - char cad_aux[128]; - sprintf (cad_aux, "%d", 123); + sprintf(cad_aux, "%d", 123); BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux); - unsigned long ul = 43200111UL; - sprintf (cad_aux, "%lu", ul); + sprintf(cad_aux, "%lu", ul); BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux); - unsigned long long ull = 4321000111ULL; - sprintf (cad_aux, "%llu", ull); + sprintf(cad_aux, "%llu", ull); BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux); - Unsigned64 u64 = ull; BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux); - unsigned int ui = 1234567890U; - sprintf (cad_aux, "%u", ui); + sprintf(cad_aux, "%u", ui); BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux); - float f = 123.34; double d = 3.1415; - sprintf (cad_aux, "%4.2f", f); + sprintf(cad_aux, "%4.2f", f); BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux); - sprintf (cad_aux, "%4.2f", d); + sprintf(cad_aux, "%4.2f", d); BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux); } BOOST_AUTO_TEST_CASE(configuration) { - anna::Configuration conf; - BOOST_CHECK_THROW(conf.load("missing_file.cnf"), anna::RuntimeException); BOOST_CHECK_NO_THROW(conf.load("test/core/example.cnf")); - //[ property ] //thing = tshirt //size = 1 @@ -127,18 +112,14 @@ BOOST_AUTO_TEST_CASE(configuration) { //[ owner ] //name = edu //address = madrid - BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException); BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException); - std::string value; - BOOST_REQUIRE_EQUAL(value = conf.getValue("property", "thing"), "tshirt"); BOOST_REQUIRE_EQUAL(conf.getIntegerValue("property", "size"), 1); BOOST_REQUIRE_EQUAL(std::string(conf.getValue("property", "color")), "blue"); BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "name")), "edu"); BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "address")), "madrid"); - conf.setDefaultValue("owner", "phone", "555 55 55"); BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "phone")), "555 55 55"); BOOST_REQUIRE(!conf.getValue("owner", "phone", true) /* query value is NULL: 'true = strict' even with default value assigned */); @@ -147,9 +128,7 @@ BOOST_AUTO_TEST_CASE(configuration) { } BOOST_AUTO_TEST_CASE(environment) { - anna::Environment &env = anna::Environment::instantiate(); - BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true /* exception if missing */), anna::RuntimeException); BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException); BOOST_REQUIRE_EQUAL(env.getValue("HOME"), std::string("/home/eramos")); diff --git a/test/time/main.cpp b/test/time/main.cpp index 77d0af3..6631509 100644 --- a/test/time/main.cpp +++ b/test/time/main.cpp @@ -61,27 +61,21 @@ BOOST_AUTO_TEST_CASE(date) { std::string ntpStr = current2.asString(); current2.storeUnix(unixtime); std::string unixStr = current2.asString(); - BOOST_REQUIRE_EQUAL(ntpStr, unixStr); - anna::time::Date myBirth("CET"); // 19 December 1974, 10:00 (GMT+1 = UTC + 1 = CET) //anna::time::Date myBirth; // if Context TZ = shell TZ = "CET" myBirth.store("19741219101500"); // yyyymmddHHmmss - anna::time::Date same_moment_canary_island("GMT"); same_moment_canary_island.store(myBirth.getUnixTimestamp()); myBirth.setTzContext("GMT"); BOOST_REQUIRE_EQUAL(same_moment_canary_island.yyyymmddHHmmss(), myBirth.yyyymmddHHmmss()); - anna::time::Date birthday("EET"); birthday.store(same_moment_canary_island.getTm(), "GMT"); // TZ origin = "GMT" //BOOST_REQUIRE_EQUAL(birthday, myBirth); BOOST_REQUIRE_EQUAL(birthday.getUnixTimestamp(), myBirth.getUnixTimestamp()); - myBirth.setTzContext(anna::time::functions::getLocalTz().getValue().c_str()); birthday.setTzContext(anna::time::functions::getLocalTz().getValue().c_str()); // Go from "EET" to "CET" BOOST_REQUIRE_EQUAL(myBirth.yyyymmddHHmmss(), birthday.yyyymmddHHmmss()); - // Adding 18 years to 'myBirth': struct tm Tm = myBirth.getTm(); Tm.tm_year += 18;