summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2017-01-12 20:23:52 +0700
committerDaniil Baturin <daniil@baturin.org>2017-01-12 20:23:52 +0700
commit26fc59887e8c596dad4e67d350b26c5e908be259 (patch)
tree83fb63fb0de514a0772a90cfbdac27c125cfd3fd /test
parentf947b3e8e7b9db2fcab250ce6d8050b650130cec (diff)
downloadvyconf-26fc59887e8c596dad4e67d350b26c5e908be259.tar.gz
vyconf-26fc59887e8c596dad4e67d350b26c5e908be259.zip
T245: improve handling of nodes with duplicate names.
Two tag nodes with the same name ("ethernet eth0 {...} ethernet eth0 {...}") is an error. Two leaf nodes with the same name, however, are not an error. Values of the next nodes are merged into the first one, while all other data (comment and inactive and ephemeral properties are inherited from the first node. This mimics the old syntax of multi nodes, so a person who uses the old syntax out of habit in a handwritten config will get the result they expect.
Diffstat (limited to 'test')
-rw-r--r--test/curly_parser_test.ml23
-rw-r--r--test/vytree_test.ml4
2 files changed, 25 insertions, 2 deletions
diff --git a/test/curly_parser_test.ml b/test/curly_parser_test.ml
index 7e53065..8e3d3fd 100644
--- a/test/curly_parser_test.ml
+++ b/test/curly_parser_test.ml
@@ -27,6 +27,10 @@ let config_with_comment = "foo { /* comment */ bar { } }"
let config_with_leaf_node_comment = "foo { /* comment */ bar baz; }"
let config_with_tag_node_comment = "foo { /* comment */ bar baz { } }"
+let config_with_duplicate_node = "foo { bar { baz {} } bar { baz {} } }"
+let config_with_duplicate_tag_node = "foo { bar baz0 { } bar baz0 { } }"
+let config_with_duplicate_leaf_node = "foo { bar baz; bar quux; }"
+
let parse s = Curly_parser.config Curly_lexer.token (Lexing.from_string s)
(* Empty config is considered valid, creates just the root node *)
@@ -99,6 +103,22 @@ let test_parse_with_tag test_ctxt =
assert_equal (CT.get_value config ["foo"; "bar"; "baz"; "quux"]) "xyzzy";
assert_equal (CT.get_value config ["foo"; "bar"; "qwerty"; "quux"]) "foobar"
+(* Normal nodes with duplicate children are detected *)
+let test_parse_node_duplicate_child test_ctxt =
+ try ignore @@ parse config_with_duplicate_node; assert_failure "Duplicated node child didn't cause errors"
+ with (Failure _) -> ()
+
+(* Tag nodes with duplicate children are detected *)
+let test_parse_tag_node_duplicate_child test_ctxt =
+ try ignore @@ parse config_with_duplicate_tag_node; assert_failure "Duplicated tag node child didn't cause errors"
+ with (Failure _) -> ()
+
+(* If there are duplicate leaf nodes, values of the next ones are merged into the first one,
+ the rest of the data is lost *)
+let test_parse_duplicate_leaf_node test_ctxt =
+ let config = parse config_with_duplicate_leaf_node in
+ assert_equal (CT.get_values config ["foo"; "bar"]) ["baz"; "quux"]
+
let suite =
"VyConf curly config parser tests" >::: [
@@ -116,6 +136,9 @@ let suite =
"test_parse_with_comment" >:: test_parse_with_comment;
"test_parse_with_leaf_node_comment" >:: test_parse_with_leaf_node_comment;
"test_parse_with_tag_node_comment" >:: test_parse_with_tag_node_comment;
+ "test_parse_node_duplicate_child" >:: test_parse_node_duplicate_child;
+ "test_parse_tag_node_duplicate_child" >:: test_parse_tag_node_duplicate_child;
+ "test_parse_duplicate_leaf_node" >:: test_parse_duplicate_leaf_node;
]
let () =
diff --git a/test/vytree_test.ml b/test/vytree_test.ml
index 3cf3ae6..4da2335 100644
--- a/test/vytree_test.ml
+++ b/test/vytree_test.ml
@@ -139,7 +139,7 @@ let test_merge_children_no_duplicates test_ctxt =
[make_full () "foo" [make () "bar"];
make () "bar";
make_full () "baz" [make () "quuz"]] in
- let node' = merge_children node in
+ let node' = merge_children (fun x y -> x) node in
assert_equal (list_children node') ["foo"; "bar"; "baz"]
@@ -151,7 +151,7 @@ let test_merge_children_has_duplicates test_ctxt =
[make_full () "foo" [make () "bar"];
make () "quux";
make_full () "foo" [make () "baz"]] in
- let node' = merge_children node in
+ let node' = merge_children (fun x y -> x) node in
assert_equal (list_children node') ["foo"; "quux"];
assert_equal (get node' ["foo"] |> list_children) ["bar"; "baz"]