New Environment core class
[anna.git] / test / core / main.cpp
index 9208755..d804ab0 100644 (file)
@@ -44,6 +44,8 @@
 #include <limits.h>
 
 #include <anna/core/util/Tokenizer.hpp>
+#include <anna/core/util/Configuration.hpp>
+#include <anna/core/util/Environment.hpp>
 #include <anna/core/functions.hpp>
 
 using namespace std;
@@ -111,3 +113,46 @@ BOOST_AUTO_TEST_CASE(functions_asString) {
   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
+  //color = blue
+  //[ 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 */);
+  BOOST_REQUIRE(!conf.exists("owner", "phone"));
+  BOOST_REQUIRE(conf.exists("owner", "name"));
+}
+
+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"));
+  BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string(""));
+}
+