-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdecoders_bs_xml_test.ml
270 lines (231 loc) · 6.56 KB
/
decoders_bs_xml_test.ml
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
open Jest
open Bs_xml
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"tag"
Decode.(
fun () ->
let xml_str = {|<root></root>|} in
let decoded = decode_string (tag "root") xml_str in
expect decoded |> toEqual (Belt.Result.Ok ())))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"empty attrs"
Decode.(
fun () ->
let xml_str = {|<root></root>|} in
let decoded = decode_string attrs xml_str in
expect decoded |> toEqual (Belt.Result.Ok [])))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"non-empty attrs"
Decode.(
fun () ->
let xml_str = {|<root id="1"></root>|} in
let decoded = decode_string attrs xml_str in
expect decoded |> toEqual (Belt.Result.Ok [ ("id", "1") ])))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"attr_opt none"
Decode.(
fun () ->
let xml_str = {|<root></root>|} in
let decoded = decode_string (attr_opt "id") xml_str in
expect decoded |> toEqual (Belt.Result.Ok None)))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"attr_opt some"
Decode.(
fun () ->
let xml_str = {|<root id="1"></root>|} in
let decoded = decode_string (attr_opt "id") xml_str in
expect decoded |> toEqual (Belt.Result.Ok (Some "1"))))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"attr fail"
Decode.(
fun () ->
let xml_str = {|<root></root>|} in
let decoded = decode_string (attr "id") xml_str in
expect (match decoded with Ok _ -> false | Error _ -> true)
|> toBe true))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"attr succeed"
Decode.(
fun () ->
let xml_str = {|<root id="1"></root>|} in
let decoded = decode_string (attr "id") xml_str in
expect decoded |> toEqual (Belt.Result.Ok "1")))
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"skip comments"
Decode.(
fun () ->
let xml_str = {|<root><!-- a comment --> Some data </root>|} in
let decoder = tag "root" >>= fun () -> children data in
let decoded = decode_string decoder xml_str in
expect decoded |> toEqual (Belt.Result.Ok [ " Some data " ])))
let xml_str =
{|
<root main_tree_to_execute="MainTree">
<BehaviorTree ID="MainTree">
<Sequence>
<Action ID="SayA" message="Hello World" env="cruel"/>
<Subtree ID="GraspObject"/>
</Sequence>
</BehaviorTree>
<BehaviorTree ID="GraspObject">
<Sequence>
<Action ID="Open"/>
<Action ID="Approach"/>
<Action ID="Close"/>
<Subtree ID="DestroyObject"/>
</Sequence>
</BehaviorTree>
<BehaviorTree ID="DestroyObject">
<Sequence>
<Action ID="Booom"/>
</Sequence>
</BehaviorTree>
</root>
|}
type root =
{ main : string
; trees : (string * node) array
}
and node =
| Action of
{ id : string
; attrs : (string * string) array
}
| Sequence of node array
| Subtree of { id : string }
let xml_tree =
{ main = "MainTree"
; trees =
[| ( "MainTree"
, Sequence
[| Action
{ id = "SayA"
; attrs = [| ("message", "Hello World"); ("env", "cruel") |]
}
; Subtree { id = "GraspObject" }
|] )
; ( "GraspObject"
, Sequence
[| Action { id = "Open"; attrs = [||] }
; Action { id = "Approach"; attrs = [||] }
; Action { id = "Close"; attrs = [||] }
; Subtree { id = "DestroyObject" }
|] )
; ("DestroyObject", Sequence [| Action { id = "Booom"; attrs = [||] } |])
|]
}
open Decode
let rec node node_ty : node decoder =
match node_ty with
| "Action" ->
attr "ID"
>>= fun id ->
attrs
>>= fun attrs ->
let attrs =
attrs |> List.filter (fun (name, _) -> name <> "ID") |> Array.of_list
in
succeed (Action { id; attrs })
| "Sequence" ->
pick_children (any_tag >>= fun node_ty -> pure (node node_ty))
>>= fun nodes -> succeed (Sequence (Array.of_list nodes))
| "Subtree" ->
attr "ID" >>= fun id -> succeed (Subtree { id })
| _ ->
fail "Unknown node type"
let tree : (string * node) decoder =
attr "ID"
>>= fun id ->
pick_children (any_tag >>= fun node_ty -> pure (node node_ty))
>>= function
| [ node ] -> succeed (id, node) | _ -> fail "Expected a single child"
let root : root decoder =
tag "root"
>>= fun () ->
attr "main_tree_to_execute"
>>= fun main ->
pick_children (tag "BehaviorTree" >>= fun () -> pure tree)
>>= fun trees -> succeed { main; trees = Array.of_list trees }
let () =
describe
"decoders-bs-xml decode"
Expect.(
fun () ->
test
"tree"
Decode.(
fun () ->
let decoded = decode_string root xml_str in
expect decoded |> toEqual (Belt.Result.Ok xml_tree)))
open Encode
let rec node = function
| Action { id; attrs } ->
tag "Action" ~attrs:(("ID", id) :: Array.to_list attrs) []
| Sequence nodes ->
tag "Sequence" (Array.to_list (Array.map node nodes))
| Subtree { id } ->
tag "Subtree" ~attrs:[ ("ID", id) ] []
let tree (name, n) = tag "BehaviorTree" ~attrs:[ ("ID", name) ] [ node n ]
let root t =
tag
"root"
~attrs:[ ("main_tree_to_execute", t.main) ]
(Array.to_list (Array.map tree t.trees))
let () =
describe
"decoders-bs-xml encode"
Expect.(
fun () ->
test "tree" (fun () ->
let encoded = encode_string root xml_tree in
let expected =
Js.String.splitByRe [%re "/\\n/"] xml_str
|> Array.to_list
|> Decoders.Util.My_list.filter_map (function
| None ->
None
| Some line ->
Some (Js.String.trim line) )
|> String.concat ""
in
expect encoded |> toEqual expected ))