summaryrefslogtreecommitdiff
path: root/test/vyconf_tree_test.ml
blob: bf9f9827f28db1b7f0dddfc2a227d6696d85319d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 () 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 () node ["foo"] () in
    let node'' = insert () 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 () 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 () node ["foo"] () in
    assert_raises Duplicate_child (fun () -> insert () 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 () node ["foo"] () in
    let node'' = insert () 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 () node ["foo"] () in
    let node'' = delete 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 () node ["foo"; "bar"] () in
    let foo_node = insert () node ["foo"] () in
    let node'' = delete node' ["foo"; "bar"] in
    assert_equal node'' foo_node

(* Attempt to delete a node at non-existent path raises an exception *)
let test_delete_nonexistent test_ctxt =
    let node = make "root" () in
    assert_raises Nonexistent_path (fun () -> delete node ["foo"; "bar"])

(* get_child works with immediate children *)
let test_get_immediate_child test_ctxt =
    let node = make "root" () in
    let node' = insert () node ["foo"] () in
    assert_equal (name_of_node (get node' ["foo"])) "foo"

(* get_child works with multi-level paths *)
let test_get_child_multilevel test_ctxt =
    let node = make "root" () in
    let node' = insert () node ["foo"; "bar"] () in
    assert_equal (name_of_node (get node' ["foo"; "bar"])) "bar"

(* get_child raises Nonexistent_path for non-existent paths *)
let test_get_child_nonexistent test_ctxt =
    let node = make "root" () in
    assert_raises Nonexistent_path (fun () -> get node ["foo"; "bar"])

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;
        "test_delete_nonexistent" >:: test_delete_nonexistent;
        "test_get_immediate_child" >:: test_get_immediate_child;
        "test_get_child_multilevel" >:: test_get_child_multilevel;
    ]

let () =
  run_test_tt_main suite