Commit 33fdc92e authored by Jelte Jansen's avatar Jelte Jansen
Browse files

100% coverage on config_data.cc :)


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@925 e5f2f494-b856-4b98-b285-d166d9295462
parent 96cfe830
......@@ -34,8 +34,7 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
//std::cout << "in: " << std::endl << spec << std::endl;
ElementPtr spec_part = spec;
if (identifier == "") {
//std::cout << "[XX] empty id" << std::endl;
return ElementPtr();
dns_throw(DataNotFoundError, "Empty identifier");
}
std::string id = identifier;
size_t sep = id.find('/');
......@@ -55,27 +54,8 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
if (!found) {
dns_throw(DataNotFoundError, identifier);
}
} else if (spec_part->getType() == Element::map) {
if (spec_part->contains("map_item_spec")) {
bool found = false;
BOOST_FOREACH(ElementPtr list_el, spec_part->get("map_item_spec")->listValue()) {
if (list_el->getType() == Element::map &&
list_el->contains("item_name") &&
list_el->get("item_name")->stringValue() == part) {
spec_part = list_el;
found = true;
}
}
if (!found) {
dns_throw(DataNotFoundError, identifier);
}
}
}
if (sep < id.size()) {
id = id.substr(sep + 1);
} else {
id = "";
}
id = id.substr(sep + 1);
sep = id.find("/");
}
if (id != "" && id != "/") {
......@@ -106,6 +86,8 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
if (!found) {
dns_throw(DataNotFoundError, identifier);
}
} else {
dns_throw(DataNotFoundError, identifier);
}
}
}
......@@ -135,6 +117,8 @@ spec_name_list(ElementPtr result, ElementPtr spec_part, std::string prefix, bool
}
}
}
} else if (spec_part->getType() == Element::map && spec_part->contains("map_item_spec")) {
spec_name_list(result, spec_part->get("map_item_spec"), prefix, recurse);
}
}
......@@ -158,7 +142,7 @@ ConfigData::getValue(bool& is_default, const std::string& identifier)
is_default = true;
} else {
is_default = false;
return ElementPtr();
value = ElementPtr();
}
}
return value;
......
......@@ -28,7 +28,7 @@ using namespace isc::config;
ConfigData
setupSpec2()
{
ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec2.spec");
ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec22.spec");
return ConfigData(spec2);
}
......@@ -38,36 +38,42 @@ TEST(ConfigData, Creation) {
}
TEST(ConfigData, getValue) {
ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec2.spec");
ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec22.spec");
ConfigData cd = ConfigData(spec2);
//std::cout << "[XX] SPEC2: " << cd.getModuleSpec().getFullSpec() << std::endl;
bool is_default;
//ElementPtr value = cd.getValue(is_default, "item1");
EXPECT_EQ(1, cd.getValue("item1")->intValue());
EXPECT_EQ(1, cd.getValue(is_default, "item1")->intValue());
EXPECT_EQ(9, cd.getValue("value1")->intValue());
EXPECT_EQ(9, cd.getValue(is_default, "value1")->intValue());
EXPECT_TRUE(is_default);
EXPECT_EQ(1.1, cd.getValue("item2")->doubleValue());
EXPECT_EQ(1.1, cd.getValue(is_default, "item2")->doubleValue());
EXPECT_EQ(9.9, cd.getValue("value2")->doubleValue());
EXPECT_EQ(9.9, cd.getValue(is_default, "value2")->doubleValue());
EXPECT_TRUE(is_default);
EXPECT_TRUE(cd.getValue("item3")->boolValue());
EXPECT_TRUE(cd.getValue(is_default, "item3")->boolValue());
EXPECT_FALSE(cd.getValue("value3")->boolValue());
EXPECT_FALSE(cd.getValue(is_default, "value3")->boolValue());
EXPECT_TRUE(is_default);
EXPECT_EQ("test", cd.getValue("item4")->stringValue());
EXPECT_EQ("test", cd.getValue(is_default, "item4")->stringValue());
EXPECT_EQ("default_string", cd.getValue("value4")->stringValue());
EXPECT_EQ("default_string", cd.getValue(is_default, "value4")->stringValue());
EXPECT_TRUE(is_default);
EXPECT_EQ("a", cd.getValue("item5")->get(0)->stringValue());
EXPECT_EQ("a", cd.getValue(is_default, "item5")->get(0)->stringValue());
EXPECT_EQ("a", cd.getValue("value5")->get(0)->stringValue());
EXPECT_EQ("a", cd.getValue(is_default, "value5")->get(0)->stringValue());
EXPECT_TRUE(is_default);
EXPECT_EQ("b", cd.getValue("item5")->get(1)->stringValue());
EXPECT_EQ("b", cd.getValue(is_default, "item5")->get(1)->stringValue());
EXPECT_EQ("b", cd.getValue("value5")->get(1)->stringValue());
EXPECT_EQ("b", cd.getValue(is_default, "value5")->get(1)->stringValue());
EXPECT_EQ("b", cd.getValue(is_default, "value5/")->get(1)->stringValue());
EXPECT_TRUE(is_default);
EXPECT_EQ("{}", cd.getValue("item6")->str());
EXPECT_EQ("{}", cd.getValue(is_default, "item6")->str());
EXPECT_EQ("{}", cd.getValue("value6")->str());
EXPECT_EQ("{}", cd.getValue(is_default, "value6")->str());
EXPECT_EQ("{}", cd.getValue(is_default, "value6/")->str());
EXPECT_TRUE(is_default);
EXPECT_EQ("[ ]", cd.getValue("value8")->str());
EXPECT_THROW(cd.getValue("")->str(), DataNotFoundError);
EXPECT_THROW(cd.getValue("/")->str(), DataNotFoundError);
EXPECT_THROW(cd.getValue("no_such_item")->str(), DataNotFoundError);
EXPECT_THROW(cd.getValue("item6/no_such_item")->str(), DataNotFoundError);
EXPECT_THROW(cd.getValue("value6/a")->str(), DataNotFoundError);
EXPECT_THROW(cd.getValue("value6/no_such_item")->str(), DataNotFoundError);
EXPECT_THROW(cd.getValue("value8/a")->str(), DataNotFoundError);
}
TEST(ConfigData, setLocalConfig) {
......@@ -111,6 +117,7 @@ TEST(ConfigData, getItemList) {
EXPECT_EQ("[ \"item1\", \"item2\", \"item3\", \"item4\", \"item5/\", \"item6/\" ]", cd.getItemList()->str());
EXPECT_EQ("[ \"item1\", \"item2\", \"item3\", \"item4\", \"item5/\", \"item6/value1\", \"item6/value2\" ]", cd.getItemList("", true)->str());
EXPECT_EQ("[ \"item6/value1\", \"item6/value2\" ]", cd.getItemList("item6")->str());
}
TEST(ConfigData, getFullConfig) {
......
......@@ -195,7 +195,7 @@ class MultiConfigData:
def set_specification(self, spec):
"""Add or update a ModuleSpec"""
if type(spec) != isc.config.ModuleSpec:
raise Exception("not a datadef")
raise Exception("not a datadef: " + str(type(spec)))
self._specifications[spec.get_module_name()] = spec
def get_module_spec(self, module):
......
......@@ -29,6 +29,7 @@ class TestConfigData(unittest.TestCase):
else:
self.data_path = "../../../testdata"
spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
print("SPEC: " + str(type(spec)))
self.cd = ConfigData(spec)
#def test_module_spec_from_file(self):
......
......@@ -50,7 +50,9 @@ def module_spec_from_file(spec_file, check = True):
if 'module_spec' not in module_spec:
raise ModuleSpecError("Data definition has no module_spec element")
return ModuleSpec(module_spec['module_spec'], check)
result = ModuleSpec(module_spec['module_spec'], check)
print("RETURNING: " + str(type(result)))
return result
class ModuleSpec:
def __init__(self, module_spec, check = True):
......
......@@ -25,7 +25,7 @@
{ "item_name": "value5",
"item_type": "list",
"item_optional": False,
"item_default": [ ],
"item_default": [ "a", "b" ],
"list_item_spec": {
"item_name": "list_element",
"item_type": "integer",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment