diff options
author | Daniil Baturin <daniil@baturin.org> | 2015-09-23 01:40:18 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-09-23 01:40:18 +0600 |
commit | c007e11142c605aaa8e8f3a0ff56b0fe657f8407 (patch) | |
tree | 7e78334ab52e1f9198cd1eafaa0fbdd44f474565 | |
parent | 0ac5704dd7a374ee4736a5cecb063c2ab49a63ce (diff) | |
download | vyconf-c007e11142c605aaa8e8f3a0ff56b0fe657f8407.tar.gz vyconf-c007e11142c605aaa8e8f3a0ff56b0fe657f8407.zip |
Add delete function to Config_tree, add some tests for it.
-rw-r--r-- | src/config_tree.ml | 19 | ||||
-rw-r--r-- | src/config_tree.mli | 2 | ||||
-rw-r--r-- | test/config_tree_test.ml | 38 |
3 files changed, 54 insertions, 5 deletions
diff --git a/src/config_tree.ml b/src/config_tree.ml index b70a8bb..33af881 100644 --- a/src/config_tree.ml +++ b/src/config_tree.ml @@ -2,6 +2,7 @@ type value_behaviour = AddValue | ReplaceValue exception Duplicate_value exception Node_has_no_value +exception No_such_value type config_node_data = { values: string list; @@ -31,6 +32,11 @@ let add_value node path value = let values = values @ [value] in Vytree.update node path ({data with values=values}) +let delete_value node path value = + let data = Vytree.data_of_node @@ Vytree.get node path in + let values = Vylist.remove (fun x -> x = value) data.values in + Vytree.update node path {data with values=values} + let set_value node path value behaviour = match behaviour with | AddValue -> add_value node path value @@ -52,3 +58,16 @@ let get_value node path = match values with | [] -> raise Node_has_no_value | x :: _ -> x + +let delete node path value = + match value with + | Some v -> + (let values = get_values node path in + if Vylist.in_list values v then + (match values with + | [_] -> Vytree.delete node path + | _ -> delete_value node path v) + else raise No_such_value) + | None -> + Vytree.delete node path + diff --git a/src/config_tree.mli b/src/config_tree.mli index 7ce505c..e4c91fc 100644 --- a/src/config_tree.mli +++ b/src/config_tree.mli @@ -16,6 +16,8 @@ val make : string -> t val set : t -> string list -> string -> value_behaviour -> t +val delete : t -> string list -> string option -> t + val get_values : t -> string list -> string list val get_value : t -> string list -> string diff --git a/test/config_tree_test.ml b/test/config_tree_test.ml index 58a95ad..516960d 100644 --- a/test/config_tree_test.ml +++ b/test/config_tree_test.ml @@ -1,18 +1,46 @@ open OUnit2 module VT = Vytree -open Config_tree +module CT = Config_tree let test_set_create_node test_ctxt = - let node = make "root" in - let node = set node ["foo"; "bar"] "baz" ReplaceValue in - let data = VT.get_data node ["foo"; "bar"] in - assert_equal data.values ["baz"] + let path = ["foo"; "bar"] in + let node = CT.make "root" in + let node = CT.set node path "baz" CT.ReplaceValue in + let values = CT.get_values node path in + assert_equal values ["baz"] + +(* Deleting just one of many values should keep all other values intact *) +let test_delete_just_value test_ctxt = + let path = ["foo"; "bar"] in + let node = CT.make "root" in + let node = CT.set node path "baz" CT.AddValue in + let node = CT.set node path "quux" CT.AddValue in + let node = CT.delete node path (Some "quux") in + assert_equal (CT.get_values node path) ["baz"] + +(* Deleting the last value should delete the whole leaf *) +let test_delete_last_value test_ctxt = + let path = ["foo"; "bar"] in + let node = CT.make "root" in + let node = CT.set node path "baz" CT.AddValue in + let node = CT.delete node path (Some "baz") in + assert_equal ((not (VT.exists node path)) && (VT.exists node ["foo"])) true + +(* Deleting a non-leaf node should delete the whole subtree *) +let test_delete_subtree test_ctxt = + let path = ["foo"; "bar"] in + let node = CT.make "root" in + let node = CT.set node path "baz" CT.AddValue in + let node = CT.delete node path None in + assert_equal (VT.list_children node) [] let suite = "VyConf config tree tests" >::: [ "test_set_create_node" >:: test_set_create_node; + "test_delete_just_value" >:: test_delete_just_value; + "test_delete_last_value" >:: test_delete_last_value; ] let () = |