summaryrefslogtreecommitdiff
path: root/test/config_tree_test.ml
blob: ad79df31377c1c7b16aa8b81570ab65108fce43f (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
open OUnit2

module VT = Vytree
module CT = Config_tree

(* Setting a value of a node that doesn't exist should create the node *)
let test_set_create_node test_ctxt =
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path (Some "baz") CT.ReplaceValue in
    let values = CT.get_values node path in
    assert_equal values ["baz"]

(* Setting a value with AddValue behaviour should append the new value *)
let test_set_add_value test_ctxt =
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path (Some "baz") CT.AddValue in
    let node = CT.set node path (Some "quux") CT.AddValue in
    let values = CT.get_values node path in
    assert_equal values ["baz"; "quux"]

(* Setting a value with ReplaceValue behaviour should replace the value *)
let test_set_replace_value test_ctxt =
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path (Some "baz") CT.ReplaceValue in
    let node = CT.set node path (Some "quux") CT.ReplaceValue in
    let values = CT.get_values node path in
    assert_equal values ["quux"]

(* Creating a node without a value should work *)
let test_create_valueless_node test_ctxt =
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path None CT.AddValue in
    assert_equal (CT.get_values node path) []

(* 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 (Some "baz") CT.AddValue in
    let node = CT.set node path (Some "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 (Some "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 (Some "baz") CT.AddValue in
    let node = CT.delete node ["foo"] None in
    assert_equal (VT.list_children node) []

(* Setting a node comment for an existent node should work *)
let test_set_comment test_ctxt =
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path None CT.AddValue in
    let node = CT.set_comment node path (Some "comment") in
    assert_equal (CT.get_comment node path) (Some "comment")

(* Creating a node without a value should default inactive and ephemeral to false *)
let test_valueless_node_inactive_ephemeral test_ctxt =
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path None CT.AddValue in
    assert_equal ((not (CT.is_inactive node path)) && (not (CT.is_ephemeral node path))) true

(* Setting a node inactive should work *)
let test_set_inactive test_ctxt = 
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path None CT.AddValue in
    let node = CT.set_inactive node path (true) in
    assert_equal (CT.is_inactive node path) true

(* Setting a node ephemeral should work *)
let test_set_ephemeral test_ctxt = 
    let path = ["foo"; "bar"] in
    let node = CT.make "root" in
    let node = CT.set node path None CT.AddValue in
    let node = CT.set_ephemeral node path (true) in
    assert_equal (CT.is_ephemeral node path) true

(*** Refactoring test setup *)
let set ?(how=CT.AddValue) path value node = CT.set node path value how

let config_tree_of_path path = CT.make "root" |> set path None

let set_in_config_tree ~how ?(path=[]) ?value =
    let node = config_tree_of_path path in
    how node path value

let toggle_in_config_tree ~how ?(path=[]) ?(value=false) =
    let node = config_tree_of_path path in
    how node path value

let load_reftree test_ctxt =
    let file_name = "interface_definition_sample.xml" in
    let r = Vytree.make Reference_tree.default_data "root" in
    Reference_tree.load_from_xml r (in_testdata_dir test_ctxt [file_name])

let foobar = ["foo"; "bar"]

(*** Rendering tests *)

(**** Standalone rendering *)
let test_render_nested_empty_with_comment test_ctxt =
    let rendered = CT.render @@
        set_in_config_tree
            ~how:CT.set_comment ~value:"comment"
            ~path:foobar
    in
    assert_equal rendered
"root {
    foo {
        /*comment*/
        bar { }
    }
}"

let test_render_ephemeral_hidden teset_ctxt =
    let rendered = CT.render @@
        toggle_in_config_tree
            ~how:CT.set_ephemeral ~value:true
            ~path:foobar
    in
    assert_equal rendered
"root {
    foo { }
}"

let test_render_ephemeral_shown teset_ctxt =
    let rendered = CT.render ~showephemeral:true @@
        toggle_in_config_tree
            ~how:CT.set_ephemeral ~value:true
            ~path:foobar
    in
    assert_equal rendered
"root {
    foo {
        #EPHEMERAL bar { }
    }
}"

(**** Reftree-based rendering *)
let test_render_rt_tag_node test_ctxt =
    let reftree = load_reftree test_ctxt in
    let path = ["system"; "login"; "user"; "full-name"] in
    let node = CT.make "root" in
    let node = CT.set node path (Some "name here") CT.AddValue in
    let rendered_curly_config = CT.render ~reftree node in
    let desired_rendered_form =
"root {
    system {
        login {
             user full-name \"name here\";
        }
    }
}"
    in
    assert_equal rendered_curly_config desired_rendered_form

let test_render_rt_unspecified_node test_ctxt =
    let reftree = load_reftree test_ctxt in
    let path = ["system"; "login"; "user"; "unspecified_node"] in
    let node = CT.make "root" in
    let node = CT.set node path (Some "name here") CT.AddValue in
    let rendered_curly_config = CT.render ~reftree node in
    let desired_rendered_form =
"root {
    system {
        login {
             user unspecified_node \"name here\";
        }
    }
}"
    in
    assert_equal rendered_curly_config desired_rendered_form

let suite =
    "VyConf config tree tests" >::: [
        "test_set_create_node" >:: test_set_create_node;
        "test_set_add_value" >:: test_set_add_value;
        "test_set_replace_value" >:: test_set_replace_value;
        "test_create_valueless_node" >:: test_create_valueless_node;
        "test_delete_just_value" >:: test_delete_just_value;
        "test_delete_last_value" >:: test_delete_last_value;
        "test_delete_subtree" >:: test_delete_subtree;
        "test_set_comment" >:: test_set_comment;
        "test_valueless_node_inactive_ephemeral" >:: test_valueless_node_inactive_ephemeral;
        "test_set_inactive" >:: test_set_inactive;
        "test_set_ephemeral" >:: test_set_ephemeral;
        "test_render_nested_empty_with_comment" >:: test_render_nested_empty_with_comment;
        "test_render_ephemeral_hidden " >:: test_render_ephemeral_hidden;
        "test_render_ephemeral_shown"  >:: test_render_ephemeral_shown;
        "test_render_rt_tag_node" >:: test_render_rt_tag_node;
        "test_render_rt_unspecified_node" >:: test_render_rt_unspecified_node
    ]

let () =
    run_test_tt_main suite