summaryrefslogtreecommitdiff
path: root/test/vyconf_tree_test.ml
blob: cd60976ba524f20dedbc9ff0aa8f206f9d295bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
open OUnit2
open Vytree

(* Destructuting a freshly made node gives us what
   we made it from *)
let test_make_node test_ctxt =
    let node = make "root" () in
    assert_equal (name_of_node node) "root";
    assert_equal (data_of_node node) ();
    assert_equal (children_of_node node) []

(* XXX: these comparisons are awkward, but this is
        probably the only way to track the problem
        down to insert if insert breaks *)

(* Inserting at single-item path adds a child to
   children list *)
let test_insert_immediate_child test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"] () in
    assert_equal (children_of_node node')
                 [make "foo" ()]

(* Inserting one child after another adds it to the
   end of the children list *)
let test_insert_multiple_children test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"] () in
    let node'' = insert_child () node' ["bar"] () in
    assert_equal (children_of_node node'')
                 [make "foo" (); make "bar" ()]

(* Inserting a child at a two-item path creates a tree
   two levels deep *)
let test_insert_multi_level test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"; "bar"] () in
    let bar = make "bar" () in
    let foo = make_full "foo" () [bar] in
    let root = make_full "root" () [foo] in
    assert_equal root node'

(* Inserting duplicate child fails *)
let test_insert_duplicate_child test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"] () in
    assert_raises Duplicate_child (fun () -> insert_child () node' ["foo"] ())

(* list_children correctly returns a list of children names *)
let test_list_children test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"] () in
    let node'' = insert_child () node' ["bar"] () in
    assert_equal (list_children node'') ["foo"; "bar"]

(* Deleting a child, well, deletes it *)
let test_delete_immediate_child test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"] () in
    let node'' = delete_child node' ["foo"] in
    assert_equal node node''

(* Deleting a child at multi-level path works *)
let test_delete_multi_level test_ctxt =
    let node = make "root" () in
    let node' = insert_child () node ["foo"; "bar"] () in
    let foo_node = insert_child () node ["foo"] () in
    let node'' = delete_child node' ["foo"; "bar"] in
    assert_equal node'' foo_node

let suite =
    "VyConf tree tests" >::: [
        "test_make_node" >:: test_make_node;
        "test_insert_immediate_child" >:: test_insert_immediate_child;
        "test_insert_multiple_children" >:: test_insert_multiple_children;
        "test_insert_multi_level" >:: test_insert_multi_level;
        "test_insert_duplicate_child" >:: test_insert_duplicate_child;
        "test_list_children" >:: test_list_children;
        "test_delete_immediate_child" >:: test_delete_immediate_child;
        "test_delete_multi_level" >:: test_delete_multi_level
    ]

let () =
  run_test_tt_main suite