diff options
-rw-r--r-- | src/vytree.ml | 11 | ||||
-rw-r--r-- | src/vytree.mli | 2 | ||||
-rw-r--r-- | test/vyconf_tree_test.ml | 8 |
3 files changed, 21 insertions, 0 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index 10ccf0e..3177e24 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -92,3 +92,14 @@ let rec get node path = | [] -> raise Empty_path | [name] -> find_or_fail node name | name :: names -> get (find_or_fail node name) names + +let get_existent_path node path = + let rec aux node path acc = + match path with + | [] -> acc + | name :: names -> + let child = find node name in + match child with + | None -> acc + | Some c -> aux c names (name :: acc) + in List.rev (aux node path []) diff --git a/src/vytree.mli b/src/vytree.mli index 8224fdd..2ce7a36 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -25,3 +25,5 @@ val update : 'a t -> string list -> 'a -> 'a t val list_children : 'a t -> string list val get : 'a t -> string list -> 'a t + +val get_existent_path : 'a t -> string list -> string list diff --git a/test/vyconf_tree_test.ml b/test/vyconf_tree_test.ml index edafb78..2aa100e 100644 --- a/test/vyconf_tree_test.ml +++ b/test/vyconf_tree_test.ml @@ -99,6 +99,13 @@ let test_update test_ctxt = let node = insert node ["foo"] 1 in assert_equal (data_of_node (get (update node ["foo"] 9) ["foo"])) 9 +(* get_existent_path works *) +let test_get_existent_path test_ctxt = + let node = make () "root" in + let node = insert node ["foo"] () in + let node = insert node ["foo"; "bar"] () in + assert_equal (get_existent_path node ["foo"; "bar"; "baz"]) ["foo"; "bar"] + let suite = "VyConf tree tests" >::: [ "test_make_node" >:: test_make_node; @@ -114,6 +121,7 @@ let suite = "test_get_child_multilevel" >:: test_get_child_multilevel; "test_get_child_nonexistent" >:: test_get_child_nonexistent; "test_update" >:: test_update; + "test_get_existent_path" >:: test_get_existent_path; ] let () = |