Commit 732ace09 authored by Jelte Jansen's avatar Jelte Jansen

more tests, data_def.cc is not covered as much as possible


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@710 e5f2f494-b856-4b98-b285-d166d9295462
parent 5b1fa8b7
......@@ -96,7 +96,9 @@ protected:
Element(int t) { type = t; }
public:
enum types { integer, real, boolean, string, list, map };
// any is a special type used in list specifications, specifying
// that the elements can be of any type
enum types { integer, real, boolean, string, list, map, any };
// base class; make dtor virtual
virtual ~Element() {};
......
......@@ -61,6 +61,7 @@ TEST(Element, from_and_to_str) {
sv.push_back("\"asdf\"");
sv.push_back("[ 1, 2, 3, 4 ]");
sv.push_back("{\"name\": \"foo\", \"value\": 47806}");
sv.push_back("[ {\"a\": 1, \"b\": \"c\"}, {\"a\": 2, \"b\": \"d\"} ]");
BOOST_FOREACH(std::string s, sv) {
// also test << operator, which uses Element::str()
......
......@@ -48,6 +48,8 @@ getType_value(const std::string& type_name) {
return Element::list;
} else if (type_name == "map") {
return Element::map;
} else if (type_name == "any") {
return Element::any;
} else {
throw DataDefinitionError(type_name + " is not a valid type name");
}
......@@ -97,7 +99,6 @@ check_config_item(const ElementPtr& spec) {
static void
check_config_item_list(const ElementPtr& spec) {
if (spec->getType() != Element::list) {
std::cout << "[XX] ERROR IN: " << spec << std::endl;
throw DataDefinitionError("config_data is not a list of elements");
}
BOOST_FOREACH(ElementPtr item, spec->listValue()) {
......@@ -210,17 +211,23 @@ check_type(ElementPtr spec, ElementPtr element)
bool
DataDefinition::validate_item(const ElementPtr spec, const ElementPtr data) {
std::cout << "Validating type of " << data << std::endl;
if (!check_type(spec, data)) {
std::cout << "type mismatch; not " << spec->get("item_type") << ": " << data << std::endl;
std::cout << spec << std::endl;
// we should do some proper error feedback here
// std::cout << "type mismatch; not " << spec->get("item_type") << ": " << data << std::endl;
// std::cout << spec << std::endl;
return false;
}
if (data->getType() == Element::list) {
ElementPtr list_spec = spec->get("list_item_spec");
BOOST_FOREACH(ElementPtr list_el, data->listValue()) {
if (!validate_spec(spec->get("list_item_spec"), list_el)) {
if (!check_type(list_spec, list_el)) {
return false;
}
if (list_spec->get("item_type")->stringValue() == "map") {
if (!validate_item(list_spec, list_el)) {
return false;
}
}
}
}
if (data->getType() == Element::map) {
......@@ -238,7 +245,6 @@ DataDefinition::validate_spec(const ElementPtr spec, const ElementPtr data) {
bool optional = spec->get("item_optional")->boolValue();
ElementPtr data_el;
std::cout << "check for item with name " << item_name << std::endl;
data_el = data->get(item_name);
if (data_el) {
if (!validate_item(spec, data_el)) {
......@@ -246,7 +252,6 @@ DataDefinition::validate_spec(const ElementPtr spec, const ElementPtr data) {
}
} else {
if (!optional) {
std::cout << "non-optional value not found" << std::endl;
return false;
}
}
......
......@@ -18,6 +18,8 @@
#include <data_def.h>
#include <fstream>
#include "data_def_unittests_config.h"
using namespace isc::data;
......@@ -41,7 +43,7 @@ data_def_error(const std::string& file,
}
}
TEST(DataDefinition, Specfiles) {
TEST(DataDefinition, ReadingSpecfiles) {
// Tests whether we can open specfiles and if we get the
// right parse errors
DataDefinition dd = DataDefinition(specfile("spec1.spec"));
......@@ -55,6 +57,16 @@ TEST(DataDefinition, Specfiles) {
"Error opening ",
specfile("doesnotexist"),
": No such file or directory");
std::ifstream file;
file.open(specfile("spec1.spec").c_str());
dd = DataDefinition(file);
EXPECT_EQ(dd.getDefinition()->get("data_specification")
->get("module_name")
->stringValue(), "Spec1");
}
TEST(DataDefinition, SpecfileItems) {
data_def_error("spec3.spec",
"item_name missing in {\"item_default\": 1, \"item_optional\": False, \"item_type\": \"integer\"}");
data_def_error("spec4.spec",
......@@ -63,8 +75,71 @@ TEST(DataDefinition, Specfiles) {
"item_optional missing in {\"item_default\": 1, \"item_name\": \"item1\", \"item_type\": \"integer\"}");
data_def_error("spec6.spec",
"item_default missing in {\"item_name\": \"item1\", \"item_optional\": False, \"item_type\": \"integer\"}");
data_def_error("spec9.spec",
"item_default not of type integer");
data_def_error("spec10.spec",
"item_default not of type real");
data_def_error("spec11.spec",
"item_default not of type boolean");
data_def_error("spec12.spec",
"item_default not of type string");
data_def_error("spec13.spec",
"item_default not of type list");
data_def_error("spec14.spec",
"item_default not of type map");
data_def_error("spec15.spec",
"badname is not a valid type name");
}
TEST(DataDefinition, SpecfileConfigData)
{
data_def_error("spec7.spec",
"module_name missing in {}");
data_def_error("spec8.spec",
"Data specification does not contain data_specification element");
data_def_error("spec16.spec",
"config_data is not a list of elements");
data_def_error("spec21.spec",
"commands is not a list of elements");
}
TEST(DataDefinition, SpecfileCommands)
{
data_def_error("spec17.spec",
"command_name missing in {\"command_args\": [ {\"item_default\": \"\", \"item_name\": \"message\", \"item_optional\": False, \"item_type\": \"string\"} ], \"command_description\": \"Print the given message to stdout\"}");
data_def_error("spec18.spec",
"command_args missing in {\"command_description\": \"Print the given message to stdout\", \"command_name\": \"print_message\"}");
data_def_error("spec19.spec",
"command_args not of type list");
data_def_error("spec20.spec",
"somethingbad is not a valid type name");
/*
data_def_error("spec22.spec",
"");
*/
}
bool
data_test(DataDefinition dd, const std::string& data_file_name)
{
std::ifstream data_file;
data_file.open(specfile(data_file_name).c_str());
ElementPtr data = Element::createFromString(data_file, data_file_name);
data_file.close();
return dd.validate(data);
}
TEST(DataDefinition, DataValidation) {
DataDefinition dd = DataDefinition(specfile("spec22.spec"));
EXPECT_TRUE(data_test(dd, "data22_1.data"));
EXPECT_FALSE(data_test(dd, "data22_2.data"));
EXPECT_FALSE(data_test(dd, "data22_3.data"));
EXPECT_FALSE(data_test(dd, "data22_4.data"));
EXPECT_FALSE(data_test(dd, "data22_5.data"));
EXPECT_TRUE(data_test(dd, "data22_6.data"));
EXPECT_TRUE(data_test(dd, "data22_7.data"));
EXPECT_FALSE(data_test(dd, "data22_8.data"));
}
{
"value1": 1,
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": True }
}
{
"value1": "asdf",
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": True }
}
{
"value1": 1,
"value2": False,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": True }
}
{
"value1": 1,
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, "a" ],
"value6": { "v61": "bar", "v62": True }
}
{
"value1": 1,
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": "Break" }
}
{
"value1": 1,
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": True },
"value7": [ 1, 2.2, "str", True ]
}
{
"value1": 1,
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": True },
"value8": [ { "a": "d" }, { "a": "e" } ]
}
{
"value1": 1,
"value2": 2.3,
"value3": True,
"value4": "foo",
"value5": [ 1, 2, 3 ],
"value6": { "v61": "bar", "v62": True },
"value8": [ { "a": "d" }, { "a": 1 } ]
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "real",
"item_optional": False,
"item_default": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "boolean",
"item_optional": False,
"item_default": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "string",
"item_optional": False,
"item_default": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "list",
"item_optional": False,
"item_default": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "map",
"item_optional": False,
"item_default": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "badname",
"item_optional": False,
"item_default": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": 1
}
}
{
"data_specification": {
"module_name": "Spec2",
"commands": [
{
"command_description": "Print the given message to stdout",
"command_args": [ {
"item_name": "message",
"item_type": "string",
"item_optional": False,
"item_default": ""
} ]
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"commands": [
{
"command_name": "print_message",
"command_description": "Print the given message to stdout"
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"commands": [
{
"command_name": "print_message",
"command_description": "Print the given message to stdout",
"command_args": 1
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"commands": [
{
"command_name": "print_message",
"command_description": "Print the given message to stdout",
"command_args": [ {
"item_name": "message",
"item_type": "somethingbad",
"item_optional": False,
"item_default": ""
} ]
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"commands": 1
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "value1",
"item_type": "integer",
"item_optional": False,
"item_default": 9
},
{ "item_name": "value2",
"item_type": "real",
"item_optional": False,
"item_default": 9.9
},
{ "item_name": "value3",
"item_type": "boolean",
"item_optional": False,
"item_default": False
},
{ "item_name": "value4",
"item_type": "string",
"item_optional": False,
"item_default": "default_string"
},
{ "item_name": "value5",
"item_type": "list",
"item_optional": False,
"item_default": [ ],
"list_item_spec": {
"item_name": "list_element",
"item_type": "integer",
"item_optional": False,
"item_default": 8
}
},
{ "item_name": "value6",
"item_type": "map",
"item_optional": False,
"item_default": {},
"map_item_spec": [
{ "item_name": "v61",
"item_type": "string",
"item_optional": False,
"item_default": "def"
},
{ "item_name": "v62",
"item_type": "boolean",
"item_optional": False,
"item_default": False
}
]
},
{ "item_name": "value7",
"item_type": "list",
"item_optional": True,
"item_default": [ ],
"list_item_spec": {
"item_name": "list_element",
"item_type": "any",
"item_optional": True
}
},
{ "item_name": "value8",
"item_type": "list",
"item_optional": True,
"item_default": [ ],
"list_item_spec": {
"item_name": "list_element",
"item_type": "map",
"item_optional": True,
"item_default": { "a": "b" },
"map_item_spec": [
{ "item_name": "a",
"item_type": "string",
"item_optional": True,
"item_default": "empty"
}
]
}
},
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"commands": [
{
"command_name": "print_message",
"command_description": "Print the given message to stdout",
"command_args": [ {
"item_name": "message",
"item_type": "string",
"item_optional": False,
"item_default": ""
} ]
}
]
}
}
{
"data_specification": {
"module_name": "Spec2",
"config_data": [
{ "item_name": "item1",
"item_type": "integer",
"item_optional": False,
"item_default": "asdf"
}
]
}
}
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