aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-19 19:15:37 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-19 19:15:37 +0000
commitceed973f970024dd406a68afbb0e8891a5b1b9d5 (patch)
tree50bb2971383c51a0d9921781f38a4b6899bc2e88
parenta239211d9ea815e40c6ed83fe458d7a4d88e897d (diff)
downloadpaludis-ceed973f970024dd406a68afbb0e8891a5b1b9d5.tar.gz
paludis-ceed973f970024dd406a68afbb0e8891a5b1b9d5.tar.xz
Add tests for KeyValueConfigFile. Fix off by one error with unbraced variables.
-rw-r--r--paludis/key_value_config_file.cc18
-rw-r--r--paludis/key_value_config_file_TEST.cc33
2 files changed, 41 insertions, 10 deletions
diff --git a/paludis/key_value_config_file.cc b/paludis/key_value_config_file.cc
index c2e3895..cddedb8 100644
--- a/paludis/key_value_config_file.cc
+++ b/paludis/key_value_config_file.cc
@@ -39,7 +39,7 @@ KeyValueConfigFile::replace_variables(const std::string & s) const
if ('\\' == s[p])
{
if (++p >= s.length())
- throw InternalError(__PRETTY_FUNCTION__, "todo");
+ throw InternalError(PALUDIS_HERE, "todo");
r += s[p++];
}
else if ('$' != s[p])
@@ -48,13 +48,13 @@ KeyValueConfigFile::replace_variables(const std::string & s) const
{
std::string name;
if (++p >= s.length())
- throw InternalError(__PRETTY_FUNCTION__, "todo"); /// \bug
+ throw InternalError(PALUDIS_HERE, "todo"); /// \bug
if ('{' == s[p])
{
std::string::size_type q;
if (std::string::npos == ((q = s.find("}", p))))
- throw InternalError(__PRETTY_FUNCTION__, "todo");
+ throw InternalError(PALUDIS_HERE, "todo");
name = s.substr(p + 1, q - p - 1);
p = q + 1;
@@ -68,17 +68,17 @@ KeyValueConfigFile::replace_variables(const std::string & s) const
"_0123456789", p))))
q = s.length();
- name = s.substr(p, q - p - 1);
- if (name.empty())
- throw InternalError(__PRETTY_FUNCTION__, "todo");
+ name = s.substr(p, q - p);
p = q;
}
+ if (name.empty())
+ throw InternalError(PALUDIS_HERE, "todo");
r += get(name);
}
if (p <= old_p)
- throw InternalError(__PRETTY_FUNCTION__, "Infinite loop");
+ throw InternalError(PALUDIS_HERE, "Infinite loop");
}
return r;
@@ -92,9 +92,9 @@ KeyValueConfigFile::strip_quotes(const std::string & s) const
if (std::string::npos != std::string("'\"").find(s[0]))
{
if (s.length() < 2)
- throw InternalError(__PRETTY_FUNCTION__, "todo");
+ throw InternalError(PALUDIS_HERE, "todo");
if (s[s.length() - 1] != s[0])
- throw InternalError(__PRETTY_FUNCTION__, "todo");
+ throw InternalError(PALUDIS_HERE, "todo");
return s.substr(1, s.length() - 2);
}
else
diff --git a/paludis/key_value_config_file_TEST.cc b/paludis/key_value_config_file_TEST.cc
index 4c25b3d..fa83cb3 100644
--- a/paludis/key_value_config_file_TEST.cc
+++ b/paludis/key_value_config_file_TEST.cc
@@ -1,10 +1,12 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+#include "stringify.hh"
#include "key_value_config_file.hh"
#include <test/test_framework.hh>
#include <test/test_runner.hh>
#include <vector>
#include <algorithm>
+#include <sstream>
using namespace paludis;
using namespace test;
@@ -17,8 +19,37 @@ namespace test_cases
void run()
{
- /// \todo
+ std::stringstream s;
+ s << "one=first" << std::endl;
+ s << "two = second" << std::endl;
+ s << "three" << std::endl;
+ s << "four = \"fourth\" " << std::endl;
+ KeyValueConfigFile ff(&s);
+
+ TEST_CHECK_EQUAL(ff.get("one"), "first");
+ TEST_CHECK_EQUAL(ff.get("two"), "second");
+ TEST_CHECK_EQUAL(ff.get("three"), "");
+ TEST_CHECK_EQUAL(ff.get("four"), "fourth");
+ TEST_CHECK_EQUAL(ff.get("five"), "");
}
} test_key_value_config_file;
+
+ struct KeyValueConfigFileVarsTest : TestCase
+ {
+ KeyValueConfigFileVarsTest() : TestCase("key value config file with vars") { }
+
+ void run()
+ {
+ std::stringstream s;
+ s << "x=foo" << std::endl;
+ s << "y = \"${x}\\\\${y}\\$${z}\"" << std::endl;
+ s << "z = $x$y$z" << std::endl;
+ KeyValueConfigFile ff(&s);
+
+ TEST_CHECK_EQUAL(ff.get("x"), "foo");
+ TEST_CHECK_EQUAL(ff.get("y"), "foo\\$");
+ TEST_CHECK_EQUAL(ff.get("z"), "foofoo\\$");
+ }
+ } test_key_value_config_file_vars;
}