Simplify creation of dictionaries in tests

This commit is contained in:
Mike Bierlee 2022-09-24 19:49:38 +03:00
parent cb8e737e6c
commit 2a5ae23576

View file

@ -370,32 +370,28 @@ version (unittest) {
@("Get value in root with empty path") @("Get value in root with empty path")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ValueNode("hehehe"));
dictionary.rootNode = new ValueNode("hehehe");
assert(dictionary.get("") == "hehehe"); assert(dictionary.get("") == "hehehe");
} }
@("Get value in root with just a dot") @("Get value in root with just a dot")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary( new ValueNode("yup"));
dictionary.rootNode = new ValueNode("yup");
assert(dictionary.get(".") == "yup"); assert(dictionary.get(".") == "yup");
} }
@("Get value in root fails when root is not a value") @("Get value in root fails when root is not a value")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ArrayNode());
dictionary.rootNode = new ArrayNode();
assertThrown!ConfigReadException(dictionary.get(".")); assertThrown!ConfigReadException(dictionary.get("."));
} }
@("Get array value from root") @("Get array value from root")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ArrayNode("aap", "noot", "mies"));
dictionary.rootNode = new ArrayNode("aap", "noot", "mies");
assert(dictionary.get("[0]") == "aap"); assert(dictionary.get("[0]") == "aap");
assert(dictionary.get("[1]") == "noot"); assert(dictionary.get("[1]") == "noot");
@ -404,12 +400,12 @@ version (unittest) {
@("Get value from object at root") @("Get value from object at root")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ObjectNode([
dictionary.rootNode = new ObjectNode([ "aap": "monkey",
"aap": "monkey", "noot": "nut",
"noot": "nut", "mies": "mies" // It's a name!
"mies": "mies" // It's a name! ])
]); );
assert(dictionary.get("aap") == "monkey"); assert(dictionary.get("aap") == "monkey");
assert(dictionary.get("noot") == "nut"); assert(dictionary.get("noot") == "nut");
@ -418,94 +414,99 @@ version (unittest) {
@("Get value from object in object") @("Get value from object in object")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode([ new ObjectNode([
"server": new ObjectNode([ "server": new ObjectNode([
"port": "8080" "port": "8080"
])
]) ])
]); );
assert(dictionary.get("server.port") == "8080"); assert(dictionary.get("server.port") == "8080");
} }
@("Get value from array in object") @("Get value from array in object")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode([ new ObjectNode([
"hostname": new ArrayNode(["google.com", "dlang.org"]) "hostname": new ArrayNode(["google.com", "dlang.org"])
]); ])
);
assert(dictionary.get("hostname.[1]") == "dlang.org"); assert(dictionary.get("hostname.[1]") == "dlang.org");
} }
@("Exception is thrown when array out of bounds when fetching from root") @("Exception is thrown when array out of bounds when fetching from root")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ArrayNode(["google.com", "dlang.org"]); new ArrayNode([
"google.com", "dlang.org"
])
);
assertThrown!ConfigReadException(dictionary.get("[5]")); assertThrown!ConfigReadException(dictionary.get("[5]"));
} }
@("Exception is thrown when array out of bounds when fetching from object") @("Exception is thrown when array out of bounds when fetching from object")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode([ new ObjectNode([
"hostname": new ArrayNode(["google.com", "dlang.org"]) "hostname": new ArrayNode(["google.com", "dlang.org"])
]); ])
);
assertThrown!ConfigReadException(dictionary.get("hostname.[5]")); assertThrown!ConfigReadException(dictionary.get("hostname.[5]"));
} }
@("Exception is thrown when path does not exist") @("Exception is thrown when path does not exist")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ObjectNode(
dictionary.rootNode = new ObjectNode( [
[ "hostname": new ObjectNode(["cluster": new ValueNode("")])
"hostname": new ObjectNode(["cluster": new ValueNode("")]) ])
]); );
assertThrown!ConfigReadException(dictionary.get("hostname.cluster.spacey")); assertThrown!ConfigReadException(dictionary.get("hostname.cluster.spacey"));
} }
@("Exception is thrown when given path terminates too early") @("Exception is thrown when given path terminates too early")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ObjectNode(
dictionary.rootNode = new ObjectNode( [
[ "hostname": new ObjectNode(["cluster": new ValueNode(null)])
"hostname": new ObjectNode(["cluster": new ValueNode(null)]) ])
]); );
assertThrown!ConfigReadException(dictionary.get("hostname")); assertThrown!ConfigReadException(dictionary.get("hostname"));
} }
@("Exception is thrown when given path does not exist because config is an array") @("Exception is thrown when given path does not exist because config is an array")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ArrayNode());
dictionary.rootNode = new ArrayNode();
assertThrown!ConfigReadException(dictionary.get("hostname")); assertThrown!ConfigReadException(dictionary.get("hostname"));
} }
@("Get value from objects in array") @("Get value from objects in array")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(new ArrayNode(
dictionary.rootNode = new ArrayNode( new ObjectNode(["wrong": "yes"]),
new ObjectNode(["wrong": "yes"]), new ObjectNode(["wrong": "no"]),
new ObjectNode(["wrong": "no"]), new ObjectNode(["wrong": "very"]),
new ObjectNode(["wrong": "very"]), ));
);
assert(dictionary.get("[1].wrong") == "no"); assert(dictionary.get("[1].wrong") == "no");
} }
@("Get value from config with mixed types") @("Get value from config with mixed types")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode([ new ObjectNode([
"uno": cast(ConfigNode) new ValueNode("one"), "uno": cast(ConfigNode) new ValueNode("one"),
"dos": cast(ConfigNode) new ArrayNode(["nope", "two"]), "dos": cast(ConfigNode) new ArrayNode(["nope", "two"]),
"tres": cast(ConfigNode) new ObjectNode(["thisone": "three"]) "tres": cast(ConfigNode) new ObjectNode(["thisone": "three"])
]); ])
);
assert(dictionary.get("uno") == "one"); assert(dictionary.get("uno") == "one");
assert(dictionary.get("dos.[1]") == "two"); assert(dictionary.get("dos.[1]") == "two");
@ -514,35 +515,40 @@ version (unittest) {
@("Ignore empty segments") @("Ignore empty segments")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode( new ObjectNode(
[ [
"one": new ObjectNode(["two": new ObjectNode(["three": "four"])]) "one": new ObjectNode(["two": new ObjectNode(["three": "four"])])
]); ])
);
assert(dictionary.get(".one..two...three....") == "four"); assert(dictionary.get(".one..two...three....") == "four");
} }
@("Support conventional array indexing notation") @("Support conventional array indexing notation")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode( new ObjectNode(
[ [
"one": new ObjectNode(["two": new ArrayNode(["dino", "mino"])]) "one": new ObjectNode([
]); "two": new ArrayNode(["dino", "mino"])
])
])
);
assert(dictionary.get("one.two[1]") == "mino"); assert(dictionary.get("one.two[1]") == "mino");
} }
@("Get and convert values") @("Get and convert values")
unittest { unittest {
auto dictionary = new ConfigDictionary(); auto dictionary = new ConfigDictionary(
dictionary.rootNode = new ObjectNode([ new ObjectNode([
"uno": new ValueNode("1223"), "uno": new ValueNode("1223"),
"dos": new ValueNode("true"), "dos": new ValueNode("true"),
"tres": new ValueNode("Hi you"), "tres": new ValueNode("Hi you"),
"quatro": new ValueNode("1.3") "quatro": new ValueNode("1.3")
]); ])
);
assert(dictionary.get!int("uno") == 1223); assert(dictionary.get!int("uno") == 1223);
assert(dictionary.get!bool("dos") == true); assert(dictionary.get!bool("dos") == true);