blob: b84e5f225dd014ad8ba36ae4fe7e9fe909e979b3 (
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
|
open OUnit2
module CT = Config_tree
let config_empty = ""
let config_trivial = "foo { }"
let config_nested = "foo { bar { } baz { } }"
let config_leaf_top_level = "foo bar;"
let config_tag_top_level = "foo bar { baz quux; }"
let config_with_leaf = "foo { bar baz; }"
let config_with_leaf_url_unquoted = "foo { bar http://www2.example.org/foo; }"
let config_with_leaf_value_quoted = "foo { bar \"foo bar\"; }"
let config_with_leaf_value_single_quoted = "foo { bar \'foo bar\'; }"
let config_with_leaf_valueless = "foo { bar; }"
(* XXX: naive use of Menhir's separated_list doesn't allow [baz; xyzzy;],
perhaps we should support it too *)
let config_with_multi = "foo { bar [baz; xyzzy]; }"
let config_with_tag = "foo { bar baz { quux xyzzy; } bar qwerty { quux foobar; } }"
let config_with_tag_nonalpha = "foo { bar baz0.99 { } bar baz1-8 { } }"
let config_with_comment = "foo { /* comment */ bar { } }"
let config_with_leaf_node_comment = "foo { /* comment */ bar baz; }"
let config_with_tag_node_comment = "foo { /* comment */ bar baz { } }"
let config_with_duplicate_node = "foo { bar { baz {} } bar { baz {} } }"
let config_with_duplicate_tag_node = "foo { bar baz0 { } bar baz0 { } }"
let config_with_duplicate_leaf_node = "foo { bar baz; bar quux; }"
let parse s = Curly_parser.config Curly_lexer.token (Lexing.from_string s)
(* Empty config is considered valid, creates just the root node *)
let test_parse_empty test_ctxt =
let config = parse config_empty in
assert_equal (Vytree.list_children config) []
(* A config with just an empty node is considered valid too. Not sure if it should! *)
let test_parse_trivial test_ctxt =
let config = parse config_trivial in
assert_equal (Vytree.list_children config) ["foo"]
(* A config with nested nodes is parser correctly *)
let test_parse_nested test_ctxt =
let config = parse config_nested in
assert_equal (Vytree.get config ["foo"] |> Vytree.list_children ) ["bar"; "baz"]
(* Leaf nodes are parsed correctly *)
let test_parse_with_leaf test_ctxt =
let config = parse config_with_leaf in
assert_equal (CT.get_value config ["foo"; "bar"]) "baz"
(* Leaf nodes with non-alphanumeric characters in values are parsed correctly *)
let test_parse_with_leaf_url_unquoted test_ctxt =
let config = parse config_with_leaf_url_unquoted in
assert_equal (CT.get_value config ["foo"; "bar"]) "http://www2.example.org/foo"
(* Leaf nodes with quoted values are parsed correctly *)
let test_parse_with_leaf_value_quoted test_ctxt =
let config = parse config_with_leaf_value_quoted in
assert_equal (CT.get_value config ["foo"; "bar"]) "foo bar"
(* Leaf nodes with single quoted values are parsed correctly *)
let test_parse_with_leaf_value_single_quoted test_ctxt =
let config = parse config_with_leaf_value_single_quoted in
assert_equal (CT.get_value config ["foo"; "bar"]) "foo bar"
(* Valueless leaf nodes work *)
let test_parse_with_leaf_valueless test_ctxt =
let config = parse config_with_leaf_valueless in
assert_equal (Vytree.get config ["foo"] |> Vytree.list_children) ["bar"]
(* Top level leaf nodes are not allowed *)
let test_parse_top_level_leaf_node test_ctxt =
assert_raises Curly_parser.Error (fun () -> parse config_leaf_top_level)
(* Top level tag nodes are not allowed *)
let test_parse_top_level_tag_node test_ctxt =
assert_raises Curly_parser.Error (fun () -> parse config_tag_top_level)
(* Nodes with multiple values are handled correctly *)
let test_parse_with_multi test_ctxt =
let config = parse config_with_multi in
assert_equal (CT.get_values config ["foo"; "bar"]) ["baz"; "xyzzy"]
(* Comments should work *)
let test_parse_with_comment test_ctxt =
let config = parse config_with_comment in
assert_equal (CT.get_comment config ["foo"; "bar"]) (Some (String.trim "comment"))
(* Comments in leaf nodes should work *)
let test_parse_with_leaf_node_comment test_ctxt =
let config = parse config_with_leaf_node_comment in
assert_equal (CT.get_comment config ["foo"; "bar"]) (Some (String.trim "comment"))
(* Comments in tag nodes should work *)
let test_parse_with_tag_node_comment test_ctxt =
let config = parse config_with_tag_node_comment in
assert_equal (CT.get_comment config ["foo"; "bar"; "baz"]) (Some (String.trim "comment"))
(* Tag nodes are parsed correctly *)
let test_parse_with_tag test_ctxt =
let config = parse config_with_tag in
assert_equal (Vytree.get config ["foo"; "bar"] |> Vytree.list_children) ["baz"; "qwerty"];
assert_equal (CT.get_value config ["foo"; "bar"; "baz"; "quux"]) "xyzzy";
assert_equal (CT.get_value config ["foo"; "bar"; "qwerty"; "quux"]) "foobar"
(* Non-alphanumeric characters are allowed in tag nodes *)
let test_parse_with_tag_nonalpha test_ctxt =
let config = parse config_with_tag_nonalpha in
assert_equal (Vytree.get config ["foo"; "bar"] |> Vytree.list_children) ["baz0.99"; "baz1-8"]
(* Normal nodes with duplicate children are detected *)
let test_parse_node_duplicate_child test_ctxt =
try ignore @@ parse config_with_duplicate_node; assert_failure "Duplicated node child didn't cause errors"
with (Failure _) -> ()
(* Tag nodes with duplicate children are detected *)
let test_parse_tag_node_duplicate_child test_ctxt =
try ignore @@ parse config_with_duplicate_tag_node; assert_failure "Duplicated tag node child didn't cause errors"
with (Failure _) -> ()
(* If there are duplicate leaf nodes, values of the next ones are merged into the first one,
the rest of the data is lost *)
let test_parse_duplicate_leaf_node test_ctxt =
let config = parse config_with_duplicate_leaf_node in
assert_equal (CT.get_values config ["foo"; "bar"]) ["baz"; "quux"]
let suite =
"VyConf curly config parser tests" >::: [
"test_make_node" >:: test_parse_empty;
"test_parse_trivial" >:: test_parse_trivial;
"test_parse_nested" >:: test_parse_nested;
"test_parse_with_leaf" >:: test_parse_with_leaf;
"test_parse_with_leaf_url_unquoted" >:: test_parse_with_leaf_url_unquoted;
"test_parse_with_leaf_value_quoted" >:: test_parse_with_leaf_value_quoted;
"test_parse_with_leaf_value_single_quoted" >:: test_parse_with_leaf_value_single_quoted;
"test_parse_with_leaf_valueless" >:: test_parse_with_leaf_valueless;
"test_parse_top_level_leaf_node" >:: test_parse_top_level_leaf_node;
"test_parse_top_level_tag_node" >:: test_parse_top_level_tag_node;
"test_parse_with_multi" >:: test_parse_with_multi;
"test_parse_with_tag" >:: test_parse_with_tag;
"test_parse_with_tag_nonalpha" >:: test_parse_with_tag_nonalpha;
"test_parse_with_comment" >:: test_parse_with_comment;
"test_parse_with_leaf_node_comment" >:: test_parse_with_leaf_node_comment;
"test_parse_with_tag_node_comment" >:: test_parse_with_tag_node_comment;
"test_parse_node_duplicate_child" >:: test_parse_node_duplicate_child;
"test_parse_tag_node_duplicate_child" >:: test_parse_tag_node_duplicate_child;
"test_parse_duplicate_leaf_node" >:: test_parse_duplicate_leaf_node;
]
let () =
run_test_tt_main suite
|