···11-open Bytesrw
22-33-let () =
44- let codec1 =
55- Jsont.Object.map ~kind:"Test" (fun arr -> arr)
66- |> Jsont.Object.mem "values" (Jsont.array Jsont.string) ~enc:(fun arr ->
77- arr)
88- |> Jsont.Object.finish
99- in
1010-1111- let yaml1 = "values: [a, b, c]" in
1212-1313- Printf.printf "Test 1: Non-optional array:\n";
1414- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml1) with
1515- | Ok arr -> Printf.printf "Result: [%d items]\n" (Array.length arr)
1616- | Error e -> Printf.printf "Error: %s\n" e);
1717-1818- let codec2 =
1919- Jsont.Object.map ~kind:"Test" (fun arr -> arr)
2020- |> Jsont.Object.mem "values"
2121- (Jsont.option (Jsont.array Jsont.string))
2222- ~enc:(fun arr -> arr)
2323- |> Jsont.Object.finish
2424- in
2525-2626- Printf.printf "\nTest 2: Jsont.option (Jsont.array):\n";
2727- match Yamlt.decode codec2 (Bytes.Reader.of_string yaml1) with
2828- | Ok arr -> (
2929- match arr with
3030- | None -> Printf.printf "Result: None\n"
3131- | Some a -> Printf.printf "Result: Some([%d items])\n" (Array.length a))
3232- | Error e -> Printf.printf "Error: %s\n" e
-334
tests/bin/test_arrays.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test array codec functionality with Yamlt *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-let show_result_both label json_result yaml_result =
2424- Printf.printf "JSON: ";
2525- show_result label json_result;
2626- Printf.printf "YAML: ";
2727- show_result label yaml_result
2828-2929-(* Test: Simple int array *)
3030-let test_int_array file =
3131- let module M = struct
3232- type numbers = { values : int array }
3333-3434- let numbers_codec =
3535- Jsont.Object.map ~kind:"Numbers" (fun values -> { values })
3636- |> Jsont.Object.mem "values" (Jsont.array Jsont.int) ~enc:(fun n ->
3737- n.values)
3838- |> Jsont.Object.finish
3939-4040- let show n =
4141- Printf.sprintf "[%s]"
4242- (String.concat "; " (Array.to_list (Array.map string_of_int n.values)))
4343- end in
4444- let yaml = read_file file in
4545- let json = read_file (file ^ ".json") in
4646- let json_result = Jsont_bytesrw.decode_string M.numbers_codec json in
4747- let yaml_result =
4848- Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml)
4949- in
5050-5151- show_result_both "int_array"
5252- (Result.map M.show json_result)
5353- (Result.map M.show yaml_result)
5454-5555-(* Test: String array *)
5656-let test_string_array file =
5757- let module M = struct
5858- type tags = { items : string array }
5959-6060- let tags_codec =
6161- Jsont.Object.map ~kind:"Tags" (fun items -> { items })
6262- |> Jsont.Object.mem "items" (Jsont.array Jsont.string) ~enc:(fun t ->
6363- t.items)
6464- |> Jsont.Object.finish
6565-6666- let show t =
6767- Printf.sprintf "[%s]"
6868- (String.concat "; "
6969- (Array.to_list (Array.map (Printf.sprintf "%S") t.items)))
7070- end in
7171- let yaml = read_file file in
7272- let json = read_file (file ^ ".json") in
7373- let json_result = Jsont_bytesrw.decode_string M.tags_codec json in
7474- let yaml_result = Yamlt.decode M.tags_codec (Bytes.Reader.of_string yaml) in
7575-7676- show_result_both "string_array"
7777- (Result.map M.show json_result)
7878- (Result.map M.show yaml_result)
7979-8080-(* Test: Float/number array *)
8181-let test_float_array file =
8282- let module M = struct
8383- type measurements = { values : float array }
8484-8585- let measurements_codec =
8686- Jsont.Object.map ~kind:"Measurements" (fun values -> { values })
8787- |> Jsont.Object.mem "values" (Jsont.array Jsont.number) ~enc:(fun m ->
8888- m.values)
8989- |> Jsont.Object.finish
9090-9191- let show m =
9292- Printf.sprintf "[%s]"
9393- (String.concat "; "
9494- (Array.to_list (Array.map (Printf.sprintf "%.2f") m.values)))
9595- end in
9696- let yaml = read_file file in
9797- let json = read_file (file ^ ".json") in
9898- let json_result = Jsont_bytesrw.decode_string M.measurements_codec json in
9999- let yaml_result =
100100- Yamlt.decode M.measurements_codec (Bytes.Reader.of_string yaml)
101101- in
102102-103103- show_result_both "float_array"
104104- (Result.map M.show json_result)
105105- (Result.map M.show yaml_result)
106106-107107-(* Test: Empty array *)
108108-let test_empty_array file =
109109- let module M = struct
110110- type empty = { items : int array }
111111-112112- let empty_codec =
113113- Jsont.Object.map ~kind:"Empty" (fun items -> { items })
114114- |> Jsont.Object.mem "items" (Jsont.array Jsont.int) ~enc:(fun e ->
115115- e.items)
116116- |> Jsont.Object.finish
117117-118118- let show e = Printf.sprintf "length=%d" (Stdlib.Array.length e.items)
119119- end in
120120- let yaml = read_file file in
121121- let json = read_file (file ^ ".json") in
122122- let json_result = Jsont_bytesrw.decode_string M.empty_codec json in
123123- let yaml_result = Yamlt.decode M.empty_codec (Bytes.Reader.of_string yaml) in
124124-125125- show_result_both "empty_array"
126126- (Result.map M.show json_result)
127127- (Result.map M.show yaml_result)
128128-129129-(* Test: Array of objects *)
130130-let test_object_array file =
131131- let module M = struct
132132- type person = { name : string; age : int }
133133- type people = { persons : person array }
134134-135135- let person_codec =
136136- Jsont.Object.map ~kind:"Person" (fun name age -> { name; age })
137137- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
138138- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
139139- |> Jsont.Object.finish
140140-141141- let people_codec =
142142- Jsont.Object.map ~kind:"People" (fun persons -> { persons })
143143- |> Jsont.Object.mem "persons" (Jsont.array person_codec) ~enc:(fun p ->
144144- p.persons)
145145- |> Jsont.Object.finish
146146-147147- let show_person p = Printf.sprintf "{%s,%d}" p.name p.age
148148-149149- let show ps =
150150- Printf.sprintf "[%s]"
151151- (String.concat "; " (Array.to_list (Array.map show_person ps.persons)))
152152- end in
153153- let yaml = read_file file in
154154- let json = read_file (file ^ ".json") in
155155- let json_result = Jsont_bytesrw.decode_string M.people_codec json in
156156- let yaml_result = Yamlt.decode M.people_codec (Bytes.Reader.of_string yaml) in
157157-158158- show_result_both "object_array"
159159- (Result.map M.show json_result)
160160- (Result.map M.show yaml_result)
161161-162162-(* Test: Nested arrays *)
163163-let test_nested_arrays file =
164164- let module M = struct
165165- type matrix = { data : int array array }
166166-167167- let matrix_codec =
168168- Jsont.Object.map ~kind:"Matrix" (fun data -> { data })
169169- |> Jsont.Object.mem "data"
170170- (Jsont.array (Jsont.array Jsont.int))
171171- ~enc:(fun m -> m.data)
172172- |> Jsont.Object.finish
173173-174174- let show_row row =
175175- Printf.sprintf "[%s]"
176176- (String.concat "; " (Array.to_list (Array.map string_of_int row)))
177177-178178- let show m =
179179- Printf.sprintf "[%s]"
180180- (String.concat "; " (Array.to_list (Array.map show_row m.data)))
181181- end in
182182- let yaml = read_file file in
183183- let json = read_file (file ^ ".json") in
184184- let json_result = Jsont_bytesrw.decode_string M.matrix_codec json in
185185- let yaml_result = Yamlt.decode M.matrix_codec (Bytes.Reader.of_string yaml) in
186186-187187- show_result_both "nested_arrays"
188188- (Result.map M.show json_result)
189189- (Result.map M.show yaml_result)
190190-191191-(* Test: Mixed types in array (should fail with homogeneous codec) *)
192192-let test_type_mismatch file =
193193- let module M = struct
194194- type numbers = { values : int array }
195195-196196- let numbers_codec =
197197- Jsont.Object.map ~kind:"Numbers" (fun values -> { values })
198198- |> Jsont.Object.mem "values" (Jsont.array Jsont.int) ~enc:(fun n ->
199199- n.values)
200200- |> Jsont.Object.finish
201201- end in
202202- let yaml = read_file file in
203203- let result = Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml) in
204204- match result with
205205- | Ok _ -> Printf.printf "Unexpected success\n"
206206- | Error e -> Printf.printf "Expected error: %s\n" e
207207-208208-(* Test: Bool array *)
209209-let test_bool_array file =
210210- let module M = struct
211211- type flags = { values : bool array }
212212-213213- let flags_codec =
214214- Jsont.Object.map ~kind:"Flags" (fun values -> { values })
215215- |> Jsont.Object.mem "values" (Jsont.array Jsont.bool) ~enc:(fun f ->
216216- f.values)
217217- |> Jsont.Object.finish
218218-219219- let show f =
220220- Printf.sprintf "[%s]"
221221- (String.concat "; " (Array.to_list (Array.map string_of_bool f.values)))
222222- end in
223223- let yaml = read_file file in
224224- let json = read_file (file ^ ".json") in
225225- let json_result = Jsont_bytesrw.decode_string M.flags_codec json in
226226- let yaml_result = Yamlt.decode M.flags_codec (Bytes.Reader.of_string yaml) in
227227-228228- show_result_both "bool_array"
229229- (Result.map M.show json_result)
230230- (Result.map M.show yaml_result)
231231-232232-(* Test: Array with nulls *)
233233-let test_nullable_array file =
234234- let module M = struct
235235- type nullable = { values : string option array }
236236-237237- let nullable_codec =
238238- Jsont.Object.map ~kind:"Nullable" (fun values -> { values })
239239- |> Jsont.Object.mem "values"
240240- (Jsont.array (Jsont.some Jsont.string))
241241- ~enc:(fun n -> n.values)
242242- |> Jsont.Object.finish
243243-244244- let show_opt = function None -> "null" | Some s -> Printf.sprintf "%S" s
245245-246246- let show n =
247247- Printf.sprintf "[%s]"
248248- (String.concat "; " (Array.to_list (Array.map show_opt n.values)))
249249- end in
250250- let yaml = read_file file in
251251- let json = read_file (file ^ ".json") in
252252- let json_result = Jsont_bytesrw.decode_string M.nullable_codec json in
253253- let yaml_result =
254254- Yamlt.decode M.nullable_codec (Bytes.Reader.of_string yaml)
255255- in
256256-257257- show_result_both "nullable_array"
258258- (Result.map M.show json_result)
259259- (Result.map M.show yaml_result)
260260-261261-(* Test: Encoding arrays to different formats *)
262262-let test_encode_arrays () =
263263- let module M = struct
264264- type data = { numbers : int array; strings : string array }
265265-266266- let data_codec =
267267- Jsont.Object.map ~kind:"Data" (fun numbers strings ->
268268- { numbers; strings })
269269- |> Jsont.Object.mem "numbers" (Jsont.array Jsont.int) ~enc:(fun d ->
270270- d.numbers)
271271- |> Jsont.Object.mem "strings" (Jsont.array Jsont.string) ~enc:(fun d ->
272272- d.strings)
273273- |> Jsont.Object.finish
274274- end in
275275- let data =
276276- { M.numbers = [| 1; 2; 3; 4; 5 |]; strings = [| "hello"; "world" |] }
277277- in
278278-279279- (* Encode to JSON *)
280280- (match Jsont_bytesrw.encode_string M.data_codec data with
281281- | Ok s -> Printf.printf "JSON: %s\n" (String.trim s)
282282- | Error e -> Printf.printf "JSON ERROR: %s\n" e);
283283-284284- (* Encode to YAML Block *)
285285- (let b = Buffer.create 256 in
286286- let writer = Bytes.Writer.of_buffer b in
287287- match
288288- Yamlt.encode ~format:Yamlt.Block M.data_codec data ~eod:true writer
289289- with
290290- | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b)
291291- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
292292-293293- (* Encode to YAML Flow *)
294294- let b = Buffer.create 256 in
295295- let writer = Bytes.Writer.of_buffer b in
296296- match Yamlt.encode ~format:Yamlt.Flow M.data_codec data ~eod:true writer with
297297- | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b)
298298- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e
299299-300300-let () =
301301- let usage = "Usage: test_arrays <command> [args...]" in
302302-303303- if Array.length Sys.argv < 2 then begin
304304- prerr_endline usage;
305305- exit 1
306306- end;
307307-308308- match Sys.argv.(1) with
309309- | "int" when Array.length Sys.argv = 3 -> test_int_array Sys.argv.(2)
310310- | "string" when Array.length Sys.argv = 3 -> test_string_array Sys.argv.(2)
311311- | "float" when Array.length Sys.argv = 3 -> test_float_array Sys.argv.(2)
312312- | "empty" when Array.length Sys.argv = 3 -> test_empty_array Sys.argv.(2)
313313- | "objects" when Array.length Sys.argv = 3 -> test_object_array Sys.argv.(2)
314314- | "nested" when Array.length Sys.argv = 3 -> test_nested_arrays Sys.argv.(2)
315315- | "type-mismatch" when Array.length Sys.argv = 3 ->
316316- test_type_mismatch Sys.argv.(2)
317317- | "bool" when Array.length Sys.argv = 3 -> test_bool_array Sys.argv.(2)
318318- | "nullable" when Array.length Sys.argv = 3 ->
319319- test_nullable_array Sys.argv.(2)
320320- | "encode" when Array.length Sys.argv = 2 -> test_encode_arrays ()
321321- | _ ->
322322- prerr_endline usage;
323323- prerr_endline "Commands:";
324324- prerr_endline " int <file> - Test int array";
325325- prerr_endline " string <file> - Test string array";
326326- prerr_endline " float <file> - Test float array";
327327- prerr_endline " empty <file> - Test empty array";
328328- prerr_endline " objects <file> - Test array of objects";
329329- prerr_endline " nested <file> - Test nested arrays";
330330- prerr_endline " type-mismatch <file> - Test type mismatch error";
331331- prerr_endline " bool <file> - Test bool array";
332332- prerr_endline " nullable <file> - Test array with nulls";
333333- prerr_endline " encode - Test encoding arrays";
334334- exit 1
-208
tests/bin/test_complex.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test complex nested types with Yamlt *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-let show_result_both label json_result yaml_result =
2424- Printf.printf "JSON: ";
2525- show_result label json_result;
2626- Printf.printf "YAML: ";
2727- show_result label yaml_result
2828-2929-(* Test: Deeply nested objects *)
3030-let test_deep_nesting file =
3131- let module M = struct
3232- type level3 = { value : int }
3333- type level2 = { data : level3 }
3434- type level1 = { nested : level2 }
3535- type root = { top : level1 }
3636-3737- let level3_codec =
3838- Jsont.Object.map ~kind:"Level3" (fun value -> { value })
3939- |> Jsont.Object.mem "value" Jsont.int ~enc:(fun l -> l.value)
4040- |> Jsont.Object.finish
4141-4242- let level2_codec =
4343- Jsont.Object.map ~kind:"Level2" (fun data -> { data })
4444- |> Jsont.Object.mem "data" level3_codec ~enc:(fun l -> l.data)
4545- |> Jsont.Object.finish
4646-4747- let level1_codec =
4848- Jsont.Object.map ~kind:"Level1" (fun nested -> { nested })
4949- |> Jsont.Object.mem "nested" level2_codec ~enc:(fun l -> l.nested)
5050- |> Jsont.Object.finish
5151-5252- let root_codec =
5353- Jsont.Object.map ~kind:"Root" (fun top -> { top })
5454- |> Jsont.Object.mem "top" level1_codec ~enc:(fun r -> r.top)
5555- |> Jsont.Object.finish
5656-5757- let show r = Printf.sprintf "depth=4, value=%d" r.top.nested.data.value
5858- end in
5959- let yaml = read_file file in
6060- let json = read_file (file ^ ".json") in
6161- let json_result = Jsont_bytesrw.decode_string M.root_codec json in
6262- let yaml_result = Yamlt.decode M.root_codec (Bytes.Reader.of_string yaml) in
6363-6464- show_result_both "deep_nesting"
6565- (Result.map M.show json_result)
6666- (Result.map M.show yaml_result)
6767-6868-(* Test: Array of objects with nested arrays *)
6969-let test_mixed_structure file =
7070- let module M = struct
7171- type item = { id : int; tags : string array }
7272- type collection = { name : string; items : item array }
7373-7474- let item_codec =
7575- Jsont.Object.map ~kind:"Item" (fun id tags -> { id; tags })
7676- |> Jsont.Object.mem "id" Jsont.int ~enc:(fun i -> i.id)
7777- |> Jsont.Object.mem "tags" (Jsont.array Jsont.string) ~enc:(fun i ->
7878- i.tags)
7979- |> Jsont.Object.finish
8080-8181- let collection_codec =
8282- Jsont.Object.map ~kind:"Collection" (fun name items -> { name; items })
8383- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun c -> c.name)
8484- |> Jsont.Object.mem "items" (Jsont.array item_codec) ~enc:(fun c ->
8585- c.items)
8686- |> Jsont.Object.finish
8787-8888- let show c =
8989- let total_tags =
9090- Stdlib.Array.fold_left
9191- (fun acc item -> acc + Stdlib.Array.length item.tags)
9292- 0 c.items
9393- in
9494- Printf.sprintf "name=%S, items=%d, total_tags=%d" c.name
9595- (Stdlib.Array.length c.items)
9696- total_tags
9797- end in
9898- let yaml = read_file file in
9999- let json = read_file (file ^ ".json") in
100100- let json_result = Jsont_bytesrw.decode_string M.collection_codec json in
101101- let yaml_result =
102102- Yamlt.decode M.collection_codec (Bytes.Reader.of_string yaml)
103103- in
104104-105105- show_result_both "mixed_structure"
106106- (Result.map M.show json_result)
107107- (Result.map M.show yaml_result)
108108-109109-(* Test: Complex optional and nullable combinations *)
110110-let test_complex_optional file =
111111- let module M = struct
112112- type config = {
113113- host : string;
114114- port : int option;
115115- ssl : bool option;
116116- cert_path : string option;
117117- fallback_hosts : string array option;
118118- }
119119-120120- let config_codec =
121121- Jsont.Object.map ~kind:"Config"
122122- (fun host port ssl cert_path fallback_hosts ->
123123- { host; port; ssl; cert_path; fallback_hosts })
124124- |> Jsont.Object.mem "host" Jsont.string ~enc:(fun c -> c.host)
125125- |> Jsont.Object.opt_mem "port" Jsont.int ~enc:(fun c -> c.port)
126126- |> Jsont.Object.opt_mem "ssl" Jsont.bool ~enc:(fun c -> c.ssl)
127127- |> Jsont.Object.opt_mem "cert_path" Jsont.string ~enc:(fun c ->
128128- c.cert_path)
129129- |> Jsont.Object.opt_mem "fallback_hosts" (Jsont.array Jsont.string)
130130- ~enc:(fun c -> c.fallback_hosts)
131131- |> Jsont.Object.finish
132132-133133- let show c =
134134- let port_str =
135135- match c.port with None -> "None" | Some p -> string_of_int p
136136- in
137137- let ssl_str =
138138- match c.ssl with None -> "None" | Some b -> string_of_bool b
139139- in
140140- let fallbacks =
141141- match c.fallback_hosts with
142142- | None -> 0
143143- | Some arr -> Stdlib.Array.length arr
144144- in
145145- Printf.sprintf "host=%S, port=%s, ssl=%s, fallbacks=%d" c.host port_str
146146- ssl_str fallbacks
147147- end in
148148- let yaml = read_file file in
149149- let json = read_file (file ^ ".json") in
150150- let json_result = Jsont_bytesrw.decode_string M.config_codec json in
151151- let yaml_result = Yamlt.decode M.config_codec (Bytes.Reader.of_string yaml) in
152152-153153- show_result_both "complex_optional"
154154- (Result.map M.show json_result)
155155- (Result.map M.show yaml_result)
156156-157157-(* Test: Heterogeneous data via any type *)
158158-let test_heterogeneous file =
159159- let module M = struct
160160- type data = { mixed : Jsont.json array }
161161-162162- let data_codec =
163163- Jsont.Object.map ~kind:"Data" (fun mixed -> { mixed })
164164- |> Jsont.Object.mem "mixed"
165165- (Jsont.array (Jsont.any ()))
166166- ~enc:(fun d -> d.mixed)
167167- |> Jsont.Object.finish
168168-169169- let show d = Printf.sprintf "items=%d" (Stdlib.Array.length d.mixed)
170170- end in
171171- let yaml = read_file file in
172172- let json = read_file (file ^ ".json") in
173173- let json_result = Jsont_bytesrw.decode_string M.data_codec json in
174174- let yaml_result = Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml) in
175175-176176- show_result_both "heterogeneous"
177177- (Result.map M.show json_result)
178178- (Result.map M.show yaml_result)
179179-180180-let () =
181181- let usage = "Usage: test_complex <command> [args...]" in
182182-183183- if Stdlib.Array.length Sys.argv < 2 then begin
184184- prerr_endline usage;
185185- exit 1
186186- end;
187187-188188- match Sys.argv.(1) with
189189- | "deep-nesting" when Stdlib.Array.length Sys.argv = 3 ->
190190- test_deep_nesting Sys.argv.(2)
191191- | "mixed-structure" when Stdlib.Array.length Sys.argv = 3 ->
192192- test_mixed_structure Sys.argv.(2)
193193- | "complex-optional" when Stdlib.Array.length Sys.argv = 3 ->
194194- test_complex_optional Sys.argv.(2)
195195- | "heterogeneous" when Stdlib.Array.length Sys.argv = 3 ->
196196- test_heterogeneous Sys.argv.(2)
197197- | _ ->
198198- prerr_endline usage;
199199- prerr_endline "Commands:";
200200- prerr_endline " deep-nesting <file> - Test deeply nested objects";
201201- prerr_endline
202202- " mixed-structure <file> - Test arrays of objects with nested arrays";
203203- prerr_endline
204204- " complex-optional <file> - Test complex optional/nullable \
205205- combinations";
206206- prerr_endline
207207- " heterogeneous <file> - Test heterogeneous data via any type";
208208- exit 1
-103
tests/bin/test_comprehensive.ml
···11-open Bytesrw
22-33-let () =
44- (* Test 1: Null handling with option types *)
55- Printf.printf "=== NULL HANDLING ===\n";
66- let opt_codec =
77- Jsont.Object.map ~kind:"Test" (fun v -> v)
88- |> Jsont.Object.mem "value" (Jsont.option Jsont.string) ~enc:(fun v -> v)
99- |> Jsont.Object.finish
1010- in
1111-1212- (match Yamlt.decode opt_codec (Bytes.Reader.of_string "value: null") with
1313- | Ok None -> Printf.printf "✓ Plain 'null' with option codec: None\n"
1414- | _ -> Printf.printf "✗ FAIL\n");
1515-1616- (match Yamlt.decode opt_codec (Bytes.Reader.of_string "value: hello") with
1717- | Ok (Some "hello") ->
1818- Printf.printf "✓ Plain 'hello' with option codec: Some(hello)\n"
1919- | _ -> Printf.printf "✗ FAIL\n");
2020-2121- let string_codec =
2222- Jsont.Object.map ~kind:"Test" (fun v -> v)
2323- |> Jsont.Object.mem "value" Jsont.string ~enc:(fun v -> v)
2424- |> Jsont.Object.finish
2525- in
2626-2727- (match Yamlt.decode string_codec (Bytes.Reader.of_string "value: null") with
2828- | Error _ ->
2929- Printf.printf "✓ Plain 'null' with string codec: ERROR (expected)\n"
3030- | _ -> Printf.printf "✗ FAIL\n");
3131-3232- (match Yamlt.decode string_codec (Bytes.Reader.of_string "value: \"\"") with
3333- | Ok "" -> Printf.printf "✓ Quoted empty string: \"\"\n"
3434- | _ -> Printf.printf "✗ FAIL\n");
3535-3636- (match
3737- Yamlt.decode string_codec (Bytes.Reader.of_string "value: \"null\"")
3838- with
3939- | Ok "null" -> Printf.printf "✓ Quoted 'null': \"null\"\n"
4040- | _ -> Printf.printf "✗ FAIL\n");
4141-4242- (* Test 2: Number formats *)
4343- Printf.printf "\n=== NUMBER FORMATS ===\n";
4444- let num_codec =
4545- Jsont.Object.map ~kind:"Test" (fun v -> v)
4646- |> Jsont.Object.mem "value" Jsont.number ~enc:(fun v -> v)
4747- |> Jsont.Object.finish
4848- in
4949-5050- (match Yamlt.decode num_codec (Bytes.Reader.of_string "value: 0xFF") with
5151- | Ok 255. -> Printf.printf "✓ Hex 0xFF: 255\n"
5252- | _ -> Printf.printf "✗ FAIL\n");
5353-5454- (match Yamlt.decode num_codec (Bytes.Reader.of_string "value: 0o77") with
5555- | Ok 63. -> Printf.printf "✓ Octal 0o77: 63\n"
5656- | _ -> Printf.printf "✗ FAIL\n");
5757-5858- (match Yamlt.decode num_codec (Bytes.Reader.of_string "value: 0b1010") with
5959- | Ok 10. -> Printf.printf "✓ Binary 0b1010: 10\n"
6060- | _ -> Printf.printf "✗ FAIL\n");
6161-6262- (* Test 3: Optional arrays *)
6363- Printf.printf "\n=== OPTIONAL ARRAYS ===\n";
6464- let opt_array_codec =
6565- Jsont.Object.map ~kind:"Test" (fun v -> v)
6666- |> Jsont.Object.opt_mem "values" (Jsont.array Jsont.string) ~enc:(fun v ->
6767- v)
6868- |> Jsont.Object.finish
6969- in
7070-7171- (match
7272- Yamlt.decode opt_array_codec (Bytes.Reader.of_string "values: [a, b, c]")
7373- with
7474- | Ok (Some arr) when Array.length arr = 3 ->
7575- Printf.printf "✓ Optional array [a, b, c]: Some([3 items])\n"
7676- | _ -> Printf.printf "✗ FAIL\n");
7777-7878- (match Yamlt.decode opt_array_codec (Bytes.Reader.of_string "{}") with
7979- | Ok None -> Printf.printf "✓ Missing optional array: None\n"
8080- | _ -> Printf.printf "✗ FAIL\n");
8181-8282- (* Test 4: Flow encoding *)
8383- Printf.printf "\n=== FLOW ENCODING ===\n";
8484- let encode_codec =
8585- Jsont.Object.map ~kind:"Test" (fun name values -> (name, values))
8686- |> Jsont.Object.mem "name" Jsont.string ~enc:fst
8787- |> Jsont.Object.mem "values" (Jsont.array Jsont.number) ~enc:snd
8888- |> Jsont.Object.finish
8989- in
9090-9191- let b = Buffer.create 256 in
9292- let writer = Bytes.Writer.of_buffer b in
9393- match
9494- Yamlt.encode ~format:Flow encode_codec
9595- ("test", [| 1.; 2.; 3. |])
9696- ~eod:true writer
9797- with
9898- | Ok ()
9999- when String.equal (Buffer.contents b)
100100- "{name: test, values: [1.0, 2.0, 3.0]}\n" ->
101101- Printf.printf "✓ Flow encoding with comma separator\n"
102102- | Ok () -> Printf.printf "✗ FAIL: %S\n" (Buffer.contents b)
103103- | Error e -> Printf.printf "✗ ERROR: %s\n" e
-216
tests/bin/test_edge.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test edge cases with Yamlt *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-let show_result_both label json_result yaml_result =
2424- Printf.printf "JSON: ";
2525- show_result label json_result;
2626- Printf.printf "YAML: ";
2727- show_result label yaml_result
2828-2929-(* Test: Very large numbers *)
3030-let test_large_numbers file =
3131- let module M = struct
3232- type numbers = {
3333- large_int : float;
3434- large_float : float;
3535- small_float : float;
3636- }
3737-3838- let numbers_codec =
3939- Jsont.Object.map ~kind:"Numbers" (fun large_int large_float small_float ->
4040- { large_int; large_float; small_float })
4141- |> Jsont.Object.mem "large_int" Jsont.number ~enc:(fun n -> n.large_int)
4242- |> Jsont.Object.mem "large_float" Jsont.number ~enc:(fun n ->
4343- n.large_float)
4444- |> Jsont.Object.mem "small_float" Jsont.number ~enc:(fun n ->
4545- n.small_float)
4646- |> Jsont.Object.finish
4747-4848- let show n =
4949- Printf.sprintf "large_int=%.0f, large_float=%e, small_float=%e"
5050- n.large_int n.large_float n.small_float
5151- end in
5252- let yaml = read_file file in
5353- let json = read_file (file ^ ".json") in
5454- let json_result = Jsont_bytesrw.decode_string M.numbers_codec json in
5555- let yaml_result =
5656- Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml)
5757- in
5858-5959- show_result_both "large_numbers"
6060- (Result.map M.show json_result)
6161- (Result.map M.show yaml_result)
6262-6363-(* Test: Special characters in strings *)
6464-let test_special_chars file =
6565- let module M = struct
6666- type text = { content : string }
6767-6868- let text_codec =
6969- Jsont.Object.map ~kind:"Text" (fun content -> { content })
7070- |> Jsont.Object.mem "content" Jsont.string ~enc:(fun t -> t.content)
7171- |> Jsont.Object.finish
7272-7373- let show t =
7474- Printf.sprintf "length=%d, contains_newline=%b, contains_tab=%b"
7575- (String.length t.content)
7676- (String.contains t.content '\n')
7777- (String.contains t.content '\t')
7878- end in
7979- let yaml = read_file file in
8080- let json = read_file (file ^ ".json") in
8181- let json_result = Jsont_bytesrw.decode_string M.text_codec json in
8282- let yaml_result = Yamlt.decode M.text_codec (Bytes.Reader.of_string yaml) in
8383-8484- show_result_both "special_chars"
8585- (Result.map M.show json_result)
8686- (Result.map M.show yaml_result)
8787-8888-(* Test: Unicode strings *)
8989-let test_unicode file =
9090- let module M = struct
9191- type text = { emoji : string; chinese : string; rtl : string }
9292-9393- let text_codec =
9494- Jsont.Object.map ~kind:"Text" (fun emoji chinese rtl ->
9595- { emoji; chinese; rtl })
9696- |> Jsont.Object.mem "emoji" Jsont.string ~enc:(fun t -> t.emoji)
9797- |> Jsont.Object.mem "chinese" Jsont.string ~enc:(fun t -> t.chinese)
9898- |> Jsont.Object.mem "rtl" Jsont.string ~enc:(fun t -> t.rtl)
9999- |> Jsont.Object.finish
100100-101101- let show t =
102102- Printf.sprintf "emoji=%S, chinese=%S, rtl=%S" t.emoji t.chinese t.rtl
103103- end in
104104- let yaml = read_file file in
105105- let json = read_file (file ^ ".json") in
106106- let json_result = Jsont_bytesrw.decode_string M.text_codec json in
107107- let yaml_result = Yamlt.decode M.text_codec (Bytes.Reader.of_string yaml) in
108108-109109- show_result_both "unicode"
110110- (Result.map M.show json_result)
111111- (Result.map M.show yaml_result)
112112-113113-(* Test: Empty collections *)
114114-let test_empty_collections file =
115115- let module M = struct
116116- type data = { empty_array : int array; empty_object_array : unit array }
117117-118118- let data_codec =
119119- Jsont.Object.map ~kind:"Data" (fun empty_array empty_object_array ->
120120- { empty_array; empty_object_array })
121121- |> Jsont.Object.mem "empty_array" (Jsont.array Jsont.int) ~enc:(fun d ->
122122- d.empty_array)
123123- |> Jsont.Object.mem "empty_object_array"
124124- (Jsont.array (Jsont.null ()))
125125- ~enc:(fun d -> d.empty_object_array)
126126- |> Jsont.Object.finish
127127-128128- let show d =
129129- Printf.sprintf "empty_array_len=%d, empty_object_array_len=%d"
130130- (Stdlib.Array.length d.empty_array)
131131- (Stdlib.Array.length d.empty_object_array)
132132- end in
133133- let yaml = read_file file in
134134- let json = read_file (file ^ ".json") in
135135- let json_result = Jsont_bytesrw.decode_string M.data_codec json in
136136- let yaml_result = Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml) in
137137-138138- show_result_both "empty_collections"
139139- (Result.map M.show json_result)
140140- (Result.map M.show yaml_result)
141141-142142-(* Test: Key names with special characters *)
143143-let test_special_keys file =
144144- let module M = struct
145145- let show j =
146146- match Jsont.Json.decode (Jsont.any ()) j with
147147- | Ok (Jsont.Object _) -> "valid_object"
148148- | Ok _ -> "not_object"
149149- | Error _ -> "decode_error"
150150- end in
151151- let yaml = read_file file in
152152- let json = read_file (file ^ ".json") in
153153- let json_result = Jsont_bytesrw.decode_string (Jsont.any ()) json in
154154- let yaml_result = Yamlt.decode (Jsont.any ()) (Bytes.Reader.of_string yaml) in
155155-156156- show_result_both "special_keys"
157157- (Result.map M.show json_result)
158158- (Result.map M.show yaml_result)
159159-160160-(* Test: Single-element arrays *)
161161-let test_single_element file =
162162- let module M = struct
163163- type data = { single : int array }
164164-165165- let data_codec =
166166- Jsont.Object.map ~kind:"Data" (fun single -> { single })
167167- |> Jsont.Object.mem "single" (Jsont.array Jsont.int) ~enc:(fun d ->
168168- d.single)
169169- |> Jsont.Object.finish
170170-171171- let show d =
172172- Printf.sprintf "length=%d, value=%d"
173173- (Stdlib.Array.length d.single)
174174- (if Stdlib.Array.length d.single > 0 then d.single.(0) else 0)
175175- end in
176176- let yaml = read_file file in
177177- let json = read_file (file ^ ".json") in
178178- let json_result = Jsont_bytesrw.decode_string M.data_codec json in
179179- let yaml_result = Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml) in
180180-181181- show_result_both "single_element"
182182- (Result.map M.show json_result)
183183- (Result.map M.show yaml_result)
184184-185185-let () =
186186- let usage = "Usage: test_edge <command> [args...]" in
187187-188188- if Stdlib.Array.length Sys.argv < 2 then begin
189189- prerr_endline usage;
190190- exit 1
191191- end;
192192-193193- match Sys.argv.(1) with
194194- | "large-numbers" when Stdlib.Array.length Sys.argv = 3 ->
195195- test_large_numbers Sys.argv.(2)
196196- | "special-chars" when Stdlib.Array.length Sys.argv = 3 ->
197197- test_special_chars Sys.argv.(2)
198198- | "unicode" when Stdlib.Array.length Sys.argv = 3 -> test_unicode Sys.argv.(2)
199199- | "empty-collections" when Stdlib.Array.length Sys.argv = 3 ->
200200- test_empty_collections Sys.argv.(2)
201201- | "special-keys" when Stdlib.Array.length Sys.argv = 3 ->
202202- test_special_keys Sys.argv.(2)
203203- | "single-element" when Stdlib.Array.length Sys.argv = 3 ->
204204- test_single_element Sys.argv.(2)
205205- | _ ->
206206- prerr_endline usage;
207207- prerr_endline "Commands:";
208208- prerr_endline " large-numbers <file> - Test very large numbers";
209209- prerr_endline
210210- " special-chars <file> - Test special characters in strings";
211211- prerr_endline " unicode <file> - Test Unicode strings";
212212- prerr_endline " empty-collections <file> - Test empty collections";
213213- prerr_endline
214214- " special-keys <file> - Test special characters in keys";
215215- prerr_endline " single-element <file> - Test single-element arrays";
216216- exit 1
-23
tests/bin/test_flow_newline.ml
···11-open Bytesrw
22-33-let () =
44- let encode_codec =
55- Jsont.Object.map ~kind:"Test" (fun name values -> (name, values))
66- |> Jsont.Object.mem "name" Jsont.string ~enc:fst
77- |> Jsont.Object.mem "values" (Jsont.array Jsont.number) ~enc:snd
88- |> Jsont.Object.finish
99- in
1010-1111- let b = Buffer.create 256 in
1212- let writer = Bytes.Writer.of_buffer b in
1313- match
1414- Yamlt.encode ~format:Flow encode_codec
1515- ("test", [| 1.; 2.; 3. |])
1616- ~eod:true writer
1717- with
1818- | Ok () ->
1919- let yaml_flow = Buffer.contents b in
2020- Printf.printf "Length: %d\n" (String.length yaml_flow);
2121- Printf.printf "Repr: %S\n" yaml_flow;
2222- Printf.printf "Output:\n%s" yaml_flow
2323- | Error e -> Printf.printf "Error: %s\n" e
-254
tests/bin/test_formats.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test format-specific features with Yamlt *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-let show_result_both label json_result yaml_result =
2424- Printf.printf "JSON: ";
2525- show_result label json_result;
2626- Printf.printf "YAML: ";
2727- show_result label yaml_result
2828-2929-(* Test: Multi-line strings - literal style *)
3030-let test_literal_string file =
3131- let module M = struct
3232- type text = { content : string }
3333-3434- let text_codec =
3535- Jsont.Object.map ~kind:"Text" (fun content -> { content })
3636- |> Jsont.Object.mem "content" Jsont.string ~enc:(fun t -> t.content)
3737- |> Jsont.Object.finish
3838-3939- let show t =
4040- Printf.sprintf "lines=%d, length=%d"
4141- (List.length (String.split_on_char '\n' t.content))
4242- (String.length t.content)
4343- end in
4444- let yaml = read_file file in
4545- let json = read_file (file ^ ".json") in
4646- let json_result = Jsont_bytesrw.decode_string M.text_codec json in
4747- let yaml_result = Yamlt.decode M.text_codec (Bytes.Reader.of_string yaml) in
4848-4949- show_result_both "literal_string"
5050- (Result.map M.show json_result)
5151- (Result.map M.show yaml_result)
5252-5353-(* Test: Multi-line strings - folded style *)
5454-let test_folded_string file =
5555- let module M = struct
5656- type text = { content : string }
5757-5858- let text_codec =
5959- Jsont.Object.map ~kind:"Text" (fun content -> { content })
6060- |> Jsont.Object.mem "content" Jsont.string ~enc:(fun t -> t.content)
6161- |> Jsont.Object.finish
6262-6363- let show t =
6464- Printf.sprintf "length=%d, newlines=%d" (String.length t.content)
6565- (List.length
6666- (List.filter
6767- (fun c -> c = '\n')
6868- (List.init (String.length t.content) (String.get t.content))))
6969- end in
7070- let yaml = read_file file in
7171- let json = read_file (file ^ ".json") in
7272- let json_result = Jsont_bytesrw.decode_string M.text_codec json in
7373- let yaml_result = Yamlt.decode M.text_codec (Bytes.Reader.of_string yaml) in
7474-7575- show_result_both "folded_string"
7676- (Result.map M.show json_result)
7777- (Result.map M.show yaml_result)
7878-7979-(* Test: Number formats - hex, octal, binary *)
8080-let test_number_formats file =
8181- let module M = struct
8282- type numbers = { hex : float; octal : float; binary : float }
8383-8484- let numbers_codec =
8585- Jsont.Object.map ~kind:"Numbers" (fun hex octal binary ->
8686- { hex; octal; binary })
8787- |> Jsont.Object.mem "hex" Jsont.number ~enc:(fun n -> n.hex)
8888- |> Jsont.Object.mem "octal" Jsont.number ~enc:(fun n -> n.octal)
8989- |> Jsont.Object.mem "binary" Jsont.number ~enc:(fun n -> n.binary)
9090- |> Jsont.Object.finish
9191-9292- let show n =
9393- Printf.sprintf "hex=%.0f, octal=%.0f, binary=%.0f" n.hex n.octal n.binary
9494- end in
9595- let yaml = read_file file in
9696- let json = read_file (file ^ ".json") in
9797- let json_result = Jsont_bytesrw.decode_string M.numbers_codec json in
9898- let yaml_result =
9999- Yamlt.decode M.numbers_codec (Bytes.Reader.of_string yaml)
100100- in
101101-102102- show_result_both "number_formats"
103103- (Result.map M.show json_result)
104104- (Result.map M.show yaml_result)
105105-106106-(* Test: Block vs Flow style encoding *)
107107-let test_encode_styles () =
108108- let module M = struct
109109- type data = { name : string; values : int array; nested : nested_data }
110110- and nested_data = { enabled : bool; count : int }
111111-112112- let nested_codec =
113113- Jsont.Object.map ~kind:"Nested" (fun enabled count -> { enabled; count })
114114- |> Jsont.Object.mem "enabled" Jsont.bool ~enc:(fun n -> n.enabled)
115115- |> Jsont.Object.mem "count" Jsont.int ~enc:(fun n -> n.count)
116116- |> Jsont.Object.finish
117117-118118- let data_codec =
119119- Jsont.Object.map ~kind:"Data" (fun name values nested ->
120120- { name; values; nested })
121121- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun d -> d.name)
122122- |> Jsont.Object.mem "values" (Jsont.array Jsont.int) ~enc:(fun d ->
123123- d.values)
124124- |> Jsont.Object.mem "nested" nested_codec ~enc:(fun d -> d.nested)
125125- |> Jsont.Object.finish
126126- end in
127127- let data =
128128- {
129129- M.name = "test";
130130- values = [| 1; 2; 3 |];
131131- nested = { enabled = true; count = 5 };
132132- }
133133- in
134134-135135- (* Encode to YAML Block style *)
136136- (let b = Buffer.create 256 in
137137- let writer = Bytes.Writer.of_buffer b in
138138- match
139139- Yamlt.encode ~format:Yamlt.Block M.data_codec data ~eod:true writer
140140- with
141141- | Ok () -> Printf.printf "YAML Block:\n%s\n" (Buffer.contents b)
142142- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
143143-144144- (* Encode to YAML Flow style *)
145145- let b = Buffer.create 256 in
146146- let writer = Bytes.Writer.of_buffer b in
147147- match Yamlt.encode ~format:Yamlt.Flow M.data_codec data ~eod:true writer with
148148- | Ok () -> Printf.printf "YAML Flow:\n%s\n" (Buffer.contents b)
149149- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e
150150-151151-(* Test: Comments in YAML (should be ignored) *)
152152-let test_comments file =
153153- let module M = struct
154154- type config = { host : string; port : int; debug : bool }
155155-156156- let config_codec =
157157- Jsont.Object.map ~kind:"Config" (fun host port debug ->
158158- { host; port; debug })
159159- |> Jsont.Object.mem "host" Jsont.string ~enc:(fun c -> c.host)
160160- |> Jsont.Object.mem "port" Jsont.int ~enc:(fun c -> c.port)
161161- |> Jsont.Object.mem "debug" Jsont.bool ~enc:(fun c -> c.debug)
162162- |> Jsont.Object.finish
163163-164164- let show c =
165165- Printf.sprintf "host=%S, port=%d, debug=%b" c.host c.port c.debug
166166- end in
167167- let yaml = read_file file in
168168- let yaml_result = Yamlt.decode M.config_codec (Bytes.Reader.of_string yaml) in
169169-170170- match yaml_result with
171171- | Ok v -> Printf.printf "YAML (with comments): %s\n" (M.show v)
172172- | Error e -> Printf.printf "YAML ERROR: %s\n" e
173173-174174-(* Test: Empty documents and null documents *)
175175-let test_empty_document file =
176176- let module M = struct
177177- type wrapper = { value : string option }
178178-179179- let wrapper_codec =
180180- Jsont.Object.map ~kind:"Wrapper" (fun value -> { value })
181181- |> Jsont.Object.mem "value" (Jsont.some Jsont.string) ~enc:(fun w ->
182182- w.value)
183183- |> Jsont.Object.finish
184184-185185- let show w =
186186- match w.value with
187187- | None -> "value=None"
188188- | Some s -> Printf.sprintf "value=Some(%S)" s
189189- end in
190190- let yaml = read_file file in
191191- let json = read_file (file ^ ".json") in
192192- let json_result = Jsont_bytesrw.decode_string M.wrapper_codec json in
193193- let yaml_result =
194194- Yamlt.decode M.wrapper_codec (Bytes.Reader.of_string yaml)
195195- in
196196-197197- show_result_both "empty_document"
198198- (Result.map M.show json_result)
199199- (Result.map M.show yaml_result)
200200-201201-(* Test: Explicit typing with tags (if supported) *)
202202-let test_explicit_tags file =
203203- let module M = struct
204204- type value_holder = { data : string }
205205-206206- let value_codec =
207207- Jsont.Object.map ~kind:"ValueHolder" (fun data -> { data })
208208- |> Jsont.Object.mem "data" Jsont.string ~enc:(fun v -> v.data)
209209- |> Jsont.Object.finish
210210-211211- let show v = Printf.sprintf "data=%S" v.data
212212- end in
213213- let yaml = read_file file in
214214- let yaml_result = Yamlt.decode M.value_codec (Bytes.Reader.of_string yaml) in
215215-216216- match yaml_result with
217217- | Ok v -> Printf.printf "YAML (with tags): %s\n" (M.show v)
218218- | Error e -> Printf.printf "YAML ERROR: %s\n" e
219219-220220-let () =
221221- let usage = "Usage: test_formats <command> [args...]" in
222222-223223- if Stdlib.Array.length Sys.argv < 2 then begin
224224- prerr_endline usage;
225225- exit 1
226226- end;
227227-228228- match Sys.argv.(1) with
229229- | "literal" when Stdlib.Array.length Sys.argv = 3 ->
230230- test_literal_string Sys.argv.(2)
231231- | "folded" when Stdlib.Array.length Sys.argv = 3 ->
232232- test_folded_string Sys.argv.(2)
233233- | "number-formats" when Stdlib.Array.length Sys.argv = 3 ->
234234- test_number_formats Sys.argv.(2)
235235- | "encode-styles" when Stdlib.Array.length Sys.argv = 2 ->
236236- test_encode_styles ()
237237- | "comments" when Stdlib.Array.length Sys.argv = 3 ->
238238- test_comments Sys.argv.(2)
239239- | "empty-doc" when Stdlib.Array.length Sys.argv = 3 ->
240240- test_empty_document Sys.argv.(2)
241241- | "explicit-tags" when Stdlib.Array.length Sys.argv = 3 ->
242242- test_explicit_tags Sys.argv.(2)
243243- | _ ->
244244- prerr_endline usage;
245245- prerr_endline "Commands:";
246246- prerr_endline " literal <file> - Test literal multi-line strings";
247247- prerr_endline " folded <file> - Test folded multi-line strings";
248248- prerr_endline
249249- " number-formats <file> - Test hex/octal/binary number formats";
250250- prerr_endline " encode-styles - Test block vs flow encoding";
251251- prerr_endline " comments <file> - Test YAML with comments";
252252- prerr_endline " empty-doc <file> - Test empty documents";
253253- prerr_endline " explicit-tags <file> - Test explicit type tags";
254254- exit 1
-296
tests/bin/test_locations.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test location and layout preservation options with Yamlt codec *)
77-88-(* Helper to read file *)
99-open Bytesrw
1010-1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok _ -> Printf.printf "%s: OK\n" label
2121- | Error e -> Printf.printf "%s:\n%s\n" label e
2222-2323-(* Test: Compare error messages with and without locs *)
2424-let test_error_precision file =
2525- let yaml = read_file file in
2626-2727- (* Define a codec that will fail on type mismatch *)
2828- let codec =
2929- Jsont.Object.map ~kind:"Person" (fun name age -> (name, age))
3030- |> Jsont.Object.mem "name" Jsont.string ~enc:fst
3131- |> Jsont.Object.mem "age" Jsont.int ~enc:snd
3232- |> Jsont.Object.finish
3333- in
3434-3535- Printf.printf "=== Without locs (default) ===\n";
3636- let result_no_locs =
3737- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false
3838- in
3939- show_result "Error message" result_no_locs;
4040-4141- Printf.printf "\n=== With locs=true ===\n";
4242- let result_with_locs =
4343- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true
4444- in
4545- show_result "Error message" result_with_locs
4646-4747-(* Test: Show error locations for nested structures *)
4848-let test_nested_error file =
4949- let yaml = read_file file in
5050-5151- (* Nested object codec *)
5252- let address_codec =
5353- Jsont.Object.map ~kind:"Address" (fun street city zip ->
5454- (street, city, zip))
5555- |> Jsont.Object.mem "street" Jsont.string ~enc:(fun (s, _, _) -> s)
5656- |> Jsont.Object.mem "city" Jsont.string ~enc:(fun (_, c, _) -> c)
5757- |> Jsont.Object.mem "zip" Jsont.int ~enc:(fun (_, _, z) -> z)
5858- |> Jsont.Object.finish
5959- in
6060-6161- let codec =
6262- Jsont.Object.map ~kind:"Employee" (fun name address -> (name, address))
6363- |> Jsont.Object.mem "name" Jsont.string ~enc:fst
6464- |> Jsont.Object.mem "address" address_codec ~enc:snd
6565- |> Jsont.Object.finish
6666- in
6767-6868- Printf.printf "=== Without locs (default) ===\n";
6969- let result_no_locs =
7070- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false
7171- in
7272- show_result "Nested error" result_no_locs;
7373-7474- Printf.printf "\n=== With locs=true ===\n";
7575- let result_with_locs =
7676- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true
7777- in
7878- show_result "Nested error" result_with_locs
7979-8080-(* Test: Array element error locations *)
8181-let test_array_error file =
8282- let yaml = read_file file in
8383-8484- (* Array codec *)
8585- let codec =
8686- Jsont.Object.map ~kind:"Numbers" (fun nums -> nums)
8787- |> Jsont.Object.mem "values" (Jsont.array Jsont.int) ~enc:(fun n -> n)
8888- |> Jsont.Object.finish
8989- in
9090-9191- Printf.printf "=== Without locs (default) ===\n";
9292- let result_no_locs =
9393- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false
9494- in
9595- show_result "Array error" result_no_locs;
9696-9797- Printf.printf "\n=== With locs=true ===\n";
9898- let result_with_locs =
9999- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true
100100- in
101101- show_result "Array error" result_with_locs
102102-103103-(* Test: Layout preservation - check if we can decode with layout info *)
104104-let test_layout_preservation file =
105105- let yaml = read_file file in
106106-107107- let codec =
108108- Jsont.Object.map ~kind:"Config" (fun host port -> (host, port))
109109- |> Jsont.Object.mem "host" Jsont.string ~enc:fst
110110- |> Jsont.Object.mem "port" Jsont.int ~enc:snd
111111- |> Jsont.Object.finish
112112- in
113113-114114- Printf.printf "=== Without layout (default) ===\n";
115115- (match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~layout:false with
116116- | Ok (host, port) ->
117117- Printf.printf "Decoded: host=%s, port=%d\n" host port;
118118- Printf.printf "Meta preserved: no\n"
119119- | Error e -> Printf.printf "Error: %s\n" e);
120120-121121- Printf.printf "\n=== With layout=true ===\n";
122122- match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~layout:true with
123123- | Ok (host, port) ->
124124- Printf.printf "Decoded: host=%s, port=%d\n" host port;
125125- Printf.printf
126126- "Meta preserved: yes (style info available for round-tripping)\n"
127127- | Error e -> Printf.printf "Error: %s\n" e
128128-129129-(* Test: Round-trip with layout preservation *)
130130-let test_roundtrip_layout file =
131131- let yaml = read_file file in
132132-133133- let codec =
134134- Jsont.Object.map ~kind:"Data" (fun items -> items)
135135- |> Jsont.Object.mem "items" (Jsont.array Jsont.string) ~enc:(fun x -> x)
136136- |> Jsont.Object.finish
137137- in
138138-139139- Printf.printf "=== Original YAML ===\n";
140140- Printf.printf "%s\n" (String.trim yaml);
141141-142142- Printf.printf "\n=== Decode without layout, re-encode ===\n";
143143- (match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~layout:false with
144144- | Ok items -> (
145145- let b = Buffer.create 256 in
146146- let writer = Bytes.Writer.of_buffer b in
147147- match Yamlt.encode ~format:Yamlt.Block codec items ~eod:true writer with
148148- | Ok () -> Printf.printf "%s" (Buffer.contents b)
149149- | Error e -> Printf.printf "Encode error: %s\n" e)
150150- | Error e -> Printf.printf "Decode error: %s\n" e);
151151-152152- Printf.printf
153153- "\n=== Decode with layout=true, re-encode with Layout format ===\n";
154154- match Yamlt.decode codec (Bytes.Reader.of_string yaml) ~layout:true with
155155- | Ok items -> (
156156- let b = Buffer.create 256 in
157157- let writer = Bytes.Writer.of_buffer b in
158158- match Yamlt.encode ~format:Yamlt.Layout codec items ~eod:true writer with
159159- | Ok () -> Printf.printf "%s" (Buffer.contents b)
160160- | Error e -> Printf.printf "Encode error: %s\n" e)
161161- | Error e -> Printf.printf "Decode error: %s\n" e
162162-163163-(* Test: File path in error messages *)
164164-let test_file_path () =
165165- let yaml = "name: Alice\nage: not-a-number\n" in
166166-167167- let codec =
168168- Jsont.Object.map ~kind:"Person" (fun name age -> (name, age))
169169- |> Jsont.Object.mem "name" Jsont.string ~enc:fst
170170- |> Jsont.Object.mem "age" Jsont.int ~enc:snd
171171- |> Jsont.Object.finish
172172- in
173173-174174- Printf.printf "=== Without file path ===\n";
175175- let result1 = Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true in
176176- show_result "Error" result1;
177177-178178- Printf.printf "\n=== With file path ===\n";
179179- let result2 =
180180- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~file:"test.yml"
181181- in
182182- show_result "Error" result2
183183-184184-(* Test: Missing field error with locs *)
185185-let test_missing_field file =
186186- let yaml = read_file file in
187187-188188- let codec =
189189- Jsont.Object.map ~kind:"Complete" (fun a b c -> (a, b, c))
190190- |> Jsont.Object.mem "field_a" Jsont.string ~enc:(fun (a, _, _) -> a)
191191- |> Jsont.Object.mem "field_b" Jsont.int ~enc:(fun (_, b, _) -> b)
192192- |> Jsont.Object.mem "field_c" Jsont.bool ~enc:(fun (_, _, c) -> c)
193193- |> Jsont.Object.finish
194194- in
195195-196196- Printf.printf "=== Without locs ===\n";
197197- let result_no_locs =
198198- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false
199199- in
200200- show_result "Missing field" result_no_locs;
201201-202202- Printf.printf "\n=== With locs=true ===\n";
203203- let result_with_locs =
204204- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true
205205- in
206206- show_result "Missing field" result_with_locs
207207-208208-(* Test: Both locs and layout together *)
209209-let test_combined_options file =
210210- let yaml = read_file file in
211211-212212- let codec =
213213- Jsont.Object.map ~kind:"Settings" (fun timeout retries ->
214214- (timeout, retries))
215215- |> Jsont.Object.mem "timeout" Jsont.int ~enc:fst
216216- |> Jsont.Object.mem "retries" Jsont.int ~enc:snd
217217- |> Jsont.Object.finish
218218- in
219219-220220- Printf.printf "=== locs=false, layout=false (defaults) ===\n";
221221- (match
222222- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false ~layout:false
223223- with
224224- | Ok (timeout, retries) ->
225225- Printf.printf "OK: timeout=%d, retries=%d\n" timeout retries
226226- | Error e -> Printf.printf "Error: %s\n" e);
227227-228228- Printf.printf "\n=== locs=true, layout=false ===\n";
229229- (match
230230- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~layout:false
231231- with
232232- | Ok (timeout, retries) ->
233233- Printf.printf "OK: timeout=%d, retries=%d (with precise locations)\n"
234234- timeout retries
235235- | Error e -> Printf.printf "Error: %s\n" e);
236236-237237- Printf.printf "\n=== locs=false, layout=true ===\n";
238238- (match
239239- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:false ~layout:true
240240- with
241241- | Ok (timeout, retries) ->
242242- Printf.printf "OK: timeout=%d, retries=%d (with layout metadata)\n"
243243- timeout retries
244244- | Error e -> Printf.printf "Error: %s\n" e);
245245-246246- Printf.printf "\n=== locs=true, layout=true (both enabled) ===\n";
247247- match
248248- Yamlt.decode codec (Bytes.Reader.of_string yaml) ~locs:true ~layout:true
249249- with
250250- | Ok (timeout, retries) ->
251251- Printf.printf "OK: timeout=%d, retries=%d (with locations and layout)\n"
252252- timeout retries
253253- | Error e -> Printf.printf "Error: %s\n" e
254254-255255-let () =
256256- let usage = "Usage: test_locations <command> [args...]" in
257257-258258- if Stdlib.Array.length Sys.argv < 2 then begin
259259- prerr_endline usage;
260260- exit 1
261261- end;
262262-263263- match Sys.argv.(1) with
264264- | "error-precision" when Array.length Sys.argv = 3 ->
265265- test_error_precision Sys.argv.(2)
266266- | "nested-error" when Array.length Sys.argv = 3 ->
267267- test_nested_error Sys.argv.(2)
268268- | "array-error" when Array.length Sys.argv = 3 ->
269269- test_array_error Sys.argv.(2)
270270- | "layout" when Array.length Sys.argv = 3 ->
271271- test_layout_preservation Sys.argv.(2)
272272- | "roundtrip" when Array.length Sys.argv = 3 ->
273273- test_roundtrip_layout Sys.argv.(2)
274274- | "file-path" -> test_file_path ()
275275- | "missing-field" when Array.length Sys.argv = 3 ->
276276- test_missing_field Sys.argv.(2)
277277- | "combined" when Array.length Sys.argv = 3 ->
278278- test_combined_options Sys.argv.(2)
279279- | _ ->
280280- prerr_endline usage;
281281- prerr_endline "Commands:";
282282- prerr_endline
283283- " error-precision <file> - Compare error messages with/without locs";
284284- prerr_endline
285285- " nested-error <file> - Test error locations in nested objects";
286286- prerr_endline
287287- " array-error <file> - Test error locations in arrays";
288288- prerr_endline " layout <file> - Test layout preservation";
289289- prerr_endline
290290- " roundtrip <file> - Test round-tripping with layout";
291291- prerr_endline
292292- " file-path - Test file path in error messages";
293293- prerr_endline
294294- " missing-field <file> - Test missing field errors with locs";
295295- prerr_endline " combined <file> - Test locs and layout together";
296296- exit 1
-241
tests/bin/test_multidoc.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44----------------------------------------------------------------------------*)
55-66-(** Test multi-document YAML streams with decode_all *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-(* Test: Simple multi-document stream *)
2424-let test_simple file =
2525- let module M = struct
2626- type person = { name : string; age : int }
2727-2828- let person_codec =
2929- Jsont.Object.map ~kind:"Person" (fun name age -> { name; age })
3030- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
3131- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
3232- |> Jsont.Object.finish
3333-3434- let show p = Printf.sprintf "%s (age %d)" p.name p.age
3535- end in
3636- let yaml = read_file file in
3737- let reader = Bytes.Reader.of_string yaml in
3838- let seq = Yamlt.decode_all M.person_codec reader in
3939- Printf.printf "Documents:\n";
4040- seq
4141- |> Seq.iteri (fun i result ->
4242- Printf.printf " [%d] " i;
4343- show_result "" (Result.map M.show result))
4444-4545-(* Test: Count documents *)
4646-let test_count file =
4747- let yaml = read_file file in
4848- let reader = Bytes.Reader.of_string yaml in
4949- let seq = Yamlt.decode_all Jsont.json reader in
5050- let count = Seq.fold_left (fun acc _ -> acc + 1) 0 seq in
5151- Printf.printf "Document count: %d\n" count
5252-5353-(* Test: Error tracking - show which documents succeed and which fail *)
5454-let test_errors file =
5555- let module M = struct
5656- type person = { name : string; age : int }
5757-5858- let person_codec =
5959- Jsont.Object.map ~kind:"Person" (fun name age -> { name; age })
6060- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
6161- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
6262- |> Jsont.Object.finish
6363-6464- let show p = Printf.sprintf "%s (age %d)" p.name p.age
6565- end in
6666- let yaml = read_file file in
6767- let reader = Bytes.Reader.of_string yaml in
6868- let seq = Yamlt.decode_all M.person_codec reader in
6969- Printf.printf "Document results:\n";
7070- seq
7171- |> Seq.iteri (fun i result ->
7272- match result with
7373- | Ok p -> Printf.printf " [%d] OK: %s\n" i (M.show p)
7474- | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e))
7575-7676-(* Test: Location tracking with locs=true *)
7777-let test_locations file =
7878- let module M = struct
7979- type person = { name : string; age : int }
8080-8181- let person_codec =
8282- Jsont.Object.map ~kind:"Person" (fun name age -> { name; age })
8383- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
8484- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
8585- |> Jsont.Object.finish
8686- end in
8787- let yaml = read_file file in
8888-8989- Printf.printf "=== Without locs (default) ===\n";
9090- let reader = Bytes.Reader.of_string yaml in
9191- let seq = Yamlt.decode_all ~locs:false M.person_codec reader in
9292- seq
9393- |> Seq.iteri (fun i result ->
9494- match result with
9595- | Ok _ -> Printf.printf " [%d] OK\n" i
9696- | Error e -> Printf.printf " [%d] ERROR:\n%s\n" i (String.trim e));
9797-9898- Printf.printf "\n=== With locs=true ===\n";
9999- let reader = Bytes.Reader.of_string yaml in
100100- let seq =
101101- Yamlt.decode_all ~locs:true ~file:"test.yml" M.person_codec reader
102102- in
103103- seq
104104- |> Seq.iteri (fun i result ->
105105- match result with
106106- | Ok _ -> Printf.printf " [%d] OK\n" i
107107- | Error e -> Printf.printf " [%d] ERROR:\n%s\n" i (String.trim e))
108108-109109-(* Test: Roundtrip to JSON - decode YAML multidoc, encode each to JSON *)
110110-let test_json_roundtrip file =
111111- let yaml = read_file file in
112112- let reader = Bytes.Reader.of_string yaml in
113113- let seq = Yamlt.decode_all Jsont.json reader in
114114- Printf.printf "JSON outputs:\n";
115115- seq
116116- |> Seq.iteri (fun i result ->
117117- match result with
118118- | Ok json_val -> (
119119- match Jsont_bytesrw.encode_string Jsont.json json_val with
120120- | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str)
121121- | Error e -> Printf.printf " [%d] ENCODE ERROR: %s\n" i e)
122122- | Error e -> Printf.printf " [%d] DECODE ERROR: %s\n" i (String.trim e))
123123-124124-(* Test: Nested objects in multidoc *)
125125-let test_nested file =
126126- let module M = struct
127127- type address = { street : string; city : string }
128128- type person = { name : string; age : int; address : address }
129129-130130- let address_codec =
131131- Jsont.Object.map ~kind:"Address" (fun street city -> { street; city })
132132- |> Jsont.Object.mem "street" Jsont.string ~enc:(fun a -> a.street)
133133- |> Jsont.Object.mem "city" Jsont.string ~enc:(fun a -> a.city)
134134- |> Jsont.Object.finish
135135-136136- let person_codec =
137137- Jsont.Object.map ~kind:"Person" (fun name age address ->
138138- { name; age; address })
139139- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
140140- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
141141- |> Jsont.Object.mem "address" address_codec ~enc:(fun p -> p.address)
142142- |> Jsont.Object.finish
143143-144144- let show p =
145145- Printf.sprintf "%s (age %d) from %s, %s" p.name p.age p.address.street
146146- p.address.city
147147- end in
148148- let yaml = read_file file in
149149- let reader = Bytes.Reader.of_string yaml in
150150- let seq = Yamlt.decode_all M.person_codec reader in
151151- Printf.printf "Nested documents:\n";
152152- seq
153153- |> Seq.iteri (fun i result ->
154154- Printf.printf " [%d] " i;
155155- show_result "" (Result.map M.show result))
156156-157157-(* Test: Arrays in multidoc *)
158158-let test_arrays file =
159159- let yaml = read_file file in
160160- let reader = Bytes.Reader.of_string yaml in
161161- let seq = Yamlt.decode_all Jsont.json reader in
162162- Printf.printf "Array documents:\n";
163163- seq
164164- |> Seq.iteri (fun i result ->
165165- match result with
166166- | Ok json_val -> (
167167- match Jsont_bytesrw.encode_string Jsont.json json_val with
168168- | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str)
169169- | Error e -> Printf.printf " [%d] ERROR: %s\n" i e)
170170- | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e))
171171-172172-(* Test: Scalars in multidoc *)
173173-let test_scalars file =
174174- let yaml = read_file file in
175175- let reader = Bytes.Reader.of_string yaml in
176176- let seq = Yamlt.decode_all Jsont.json reader in
177177- Printf.printf "Scalar documents:\n";
178178- seq
179179- |> Seq.iteri (fun i result ->
180180- match result with
181181- | Ok json_val -> (
182182- match Jsont_bytesrw.encode_string Jsont.json json_val with
183183- | Ok json_str -> Printf.printf " [%d] %s\n" i (String.trim json_str)
184184- | Error e -> Printf.printf " [%d] ERROR: %s\n" i e)
185185- | Error e -> Printf.printf " [%d] ERROR: %s\n" i (String.trim e))
186186-187187-(* Test: Summary stats - count successes vs failures *)
188188-let test_summary file =
189189- let module M = struct
190190- type person = { name : string; age : int }
191191-192192- let person_codec =
193193- Jsont.Object.map ~kind:"Person" (fun name age -> { name; age })
194194- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
195195- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
196196- |> Jsont.Object.finish
197197- end in
198198- let yaml = read_file file in
199199- let reader = Bytes.Reader.of_string yaml in
200200- let seq = Yamlt.decode_all M.person_codec reader in
201201- let success = ref 0 in
202202- let failure = ref 0 in
203203- seq
204204- |> Seq.iter (fun result ->
205205- match result with Ok _ -> incr success | Error _ -> incr failure);
206206- Printf.printf "Summary: %d documents (%d ok, %d error)\n"
207207- (!success + !failure) !success !failure
208208-209209-let () =
210210- let usage = "Usage: test_multidoc <command> <file>" in
211211- if Array.length Sys.argv < 3 then begin
212212- prerr_endline usage;
213213- exit 1
214214- end;
215215-216216- let test = Sys.argv.(1) in
217217- let file = Sys.argv.(2) in
218218- match test with
219219- | "simple" -> test_simple file
220220- | "count" -> test_count file
221221- | "errors" -> test_errors file
222222- | "locations" -> test_locations file
223223- | "json" -> test_json_roundtrip file
224224- | "nested" -> test_nested file
225225- | "arrays" -> test_arrays file
226226- | "scalars" -> test_scalars file
227227- | "summary" -> test_summary file
228228- | _ ->
229229- prerr_endline usage;
230230- prerr_endline "Commands:";
231231- prerr_endline " simple <file> - Decode person documents";
232232- prerr_endline " count <file> - Count documents";
233233- prerr_endline " errors <file> - Show success/error for each document";
234234- prerr_endline
235235- " locations <file> - Test location tracking with locs=true";
236236- prerr_endline " json <file> - Roundtrip to JSON";
237237- prerr_endline " nested <file> - Decode nested objects";
238238- prerr_endline " arrays <file> - Decode arrays";
239239- prerr_endline " scalars <file> - Decode scalars";
240240- prerr_endline " summary <file> - Show success/failure summary";
241241- exit 1
-89
tests/bin/test_null_collections.ml
···11-open Bytesrw
22-33-let () =
44- Printf.printf "=== Test 1: Explicit null as empty array ===\n";
55- let yaml1 = "values: null" in
66- let codec1 =
77- let open Jsont in
88- Object.map ~kind:"Test" (fun v -> v)
99- |> Object.mem "values" (list int) ~dec_absent:[] ~enc:(fun v -> v)
1010- |> Object.finish
1111- in
1212- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml1) with
1313- | Ok v ->
1414- Printf.printf "Result: [%s]\n"
1515- (String.concat "; " (List.map string_of_int v))
1616- | Error e -> Printf.printf "Error: %s\n" e);
1717-1818- Printf.printf "\n=== Test 2: Tilde as empty array ===\n";
1919- let yaml2 = "values: ~" in
2020- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml2) with
2121- | Ok v ->
2222- Printf.printf "Result: [%s]\n"
2323- (String.concat "; " (List.map string_of_int v))
2424- | Error e -> Printf.printf "Error: %s\n" e);
2525-2626- Printf.printf "\n=== Test 3: Empty array syntax ===\n";
2727- let yaml3 = "values: []" in
2828- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml3) with
2929- | Ok v ->
3030- Printf.printf "Result: [%s]\n"
3131- (String.concat "; " (List.map string_of_int v))
3232- | Error e -> Printf.printf "Error: %s\n" e);
3333-3434- Printf.printf "\n=== Test 4: Array with values ===\n";
3535- let yaml4 = "values: [1, 2, 3]" in
3636- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml4) with
3737- | Ok v ->
3838- Printf.printf "Result: [%s]\n"
3939- (String.concat "; " (List.map string_of_int v))
4040- | Error e -> Printf.printf "Error: %s\n" e);
4141-4242- Printf.printf "\n=== Test 5: Explicit null as empty object ===\n";
4343- let yaml5 = "config: null" in
4444- let codec2 =
4545- let open Jsont in
4646- let config_codec =
4747- Object.map ~kind:"Config" (fun timeout retries -> (timeout, retries))
4848- |> Object.mem "timeout" int ~dec_absent:30 ~enc:fst
4949- |> Object.mem "retries" int ~dec_absent:3 ~enc:snd
5050- |> Object.finish
5151- in
5252- Object.map ~kind:"Test" (fun c -> c)
5353- |> Object.mem "config" config_codec ~dec_absent:(30, 3) ~enc:(fun c -> c)
5454- |> Object.finish
5555- in
5656- (match Yamlt.decode codec2 (Bytes.Reader.of_string yaml5) with
5757- | Ok (timeout, retries) ->
5858- Printf.printf "Result: {timeout=%d; retries=%d}\n" timeout retries
5959- | Error e -> Printf.printf "Error: %s\n" e);
6060-6161- Printf.printf "\n=== Test 6: Empty object syntax ===\n";
6262- let yaml6 = "config: {}" in
6363- (match Yamlt.decode codec2 (Bytes.Reader.of_string yaml6) with
6464- | Ok (timeout, retries) ->
6565- Printf.printf "Result: {timeout=%d; retries=%d}\n" timeout retries
6666- | Error e -> Printf.printf "Error: %s\n" e);
6767-6868- Printf.printf "\n=== Test 7: Object with values ===\n";
6969- let yaml7 = "config:\n timeout: 60\n retries: 5" in
7070- (match Yamlt.decode codec2 (Bytes.Reader.of_string yaml7) with
7171- | Ok (timeout, retries) ->
7272- Printf.printf "Result: {timeout=%d; retries=%d}\n" timeout retries
7373- | Error e -> Printf.printf "Error: %s\n" e);
7474-7575- Printf.printf "\n=== Test 8: Nested null arrays ===\n";
7676- let yaml8 = "name: test\nitems: null\ntags: ~" in
7777- let codec3 =
7878- let open Jsont in
7979- Object.map ~kind:"Nested" (fun name items tags -> (name, items, tags))
8080- |> Object.mem "name" string ~enc:(fun (n, _, _) -> n)
8181- |> Object.mem "items" (list int) ~dec_absent:[] ~enc:(fun (_, i, _) -> i)
8282- |> Object.mem "tags" (list string) ~dec_absent:[] ~enc:(fun (_, _, t) -> t)
8383- |> Object.finish
8484- in
8585- match Yamlt.decode codec3 (Bytes.Reader.of_string yaml8) with
8686- | Ok (name, items, tags) ->
8787- Printf.printf "Result: {name=%s; items_count=%d; tags_count=%d}\n" name
8888- (List.length items) (List.length tags)
8989- | Error e -> Printf.printf "Error: %s\n" e
-39
tests/bin/test_null_complete.ml
···11-open Bytesrw
22-33-let () =
44- Printf.printf "=== Test 1: Jsont.option with YAML null ===\n";
55- let yaml1 = "value: null" in
66- let codec1 =
77- let open Jsont in
88- Object.map ~kind:"Test" (fun v -> v)
99- |> Object.mem "value" (option string) ~enc:(fun v -> v)
1010- |> Object.finish
1111- in
1212- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml1) with
1313- | Ok v ->
1414- Printf.printf "Result: %s\n"
1515- (match v with None -> "None" | Some s -> "Some(" ^ s ^ ")")
1616- | Error e -> Printf.printf "Error: %s\n" e);
1717-1818- Printf.printf "\n=== Test 2: Jsont.option with YAML string ===\n";
1919- (match Yamlt.decode codec1 (Bytes.Reader.of_string "value: hello") with
2020- | Ok v ->
2121- Printf.printf "Result: %s\n"
2222- (match v with None -> "None" | Some s -> "Some(" ^ s ^ ")")
2323- | Error e -> Printf.printf "Error: %s\n" e);
2424-2525- Printf.printf "\n=== Test 3: Jsont.string with YAML null (should error) ===\n";
2626- let codec2 =
2727- let open Jsont in
2828- Object.map ~kind:"Test" (fun v -> v)
2929- |> Object.mem "value" string ~enc:(fun v -> v)
3030- |> Object.finish
3131- in
3232- (match Yamlt.decode codec2 (Bytes.Reader.of_string "value: null") with
3333- | Ok v -> Printf.printf "Result: %s\n" v
3434- | Error e -> Printf.printf "Error (expected): %s\n" e);
3535-3636- Printf.printf "\n=== Test 4: Jsont.string with YAML string ===\n";
3737- match Yamlt.decode codec2 (Bytes.Reader.of_string "value: hello") with
3838- | Ok v -> Printf.printf "Result: %s\n" v
3939- | Error e -> Printf.printf "Error: %s\n" e
-31
tests/bin/test_null_fix.ml
···11-open Bytesrw
22-33-let () =
44- let module M = struct
55- type data = { value : string option }
66-77- let data_codec =
88- Jsont.Object.map ~kind:"Data" (fun value -> { value })
99- |> Jsont.Object.mem "value" (Jsont.option Jsont.string) ~enc:(fun d ->
1010- d.value)
1111- |> Jsont.Object.finish
1212- end in
1313- let yaml_null = "value: null" in
1414-1515- Printf.printf "Testing YAML null handling with Jsont.option Jsont.string:\n\n";
1616-1717- match Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml_null) with
1818- | Ok data -> (
1919- match data.M.value with
2020- | None -> Printf.printf "YAML: value=None (CORRECT)\n"
2121- | Some s -> Printf.printf "YAML: value=Some(%S) (BUG!)\n" s)
2222- | Error e -> (
2323- Printf.printf "YAML ERROR: %s\n" e;
2424-2525- let json_null = "{\"value\": null}" in
2626- match Jsont_bytesrw.decode_string M.data_codec json_null with
2727- | Ok data -> (
2828- match data.M.value with
2929- | None -> Printf.printf "JSON: value=None (CORRECT)\n"
3030- | Some s -> Printf.printf "JSON: value=Some(%S) (BUG!)\n" s)
3131- | Error e -> Printf.printf "JSON ERROR: %s\n" e)
-356
tests/bin/test_objects.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test object codec functionality with Yamlt *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-let show_result_both label json_result yaml_result =
2424- Printf.printf "JSON: ";
2525- show_result label json_result;
2626- Printf.printf "YAML: ";
2727- show_result label yaml_result
2828-2929-(* Test: Simple object with required fields *)
3030-let test_simple_object file =
3131- let module M = struct
3232- type person = { name : string; age : int }
3333-3434- let person_codec =
3535- Jsont.Object.map ~kind:"Person" (fun name age -> { name; age })
3636- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
3737- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
3838- |> Jsont.Object.finish
3939-4040- let show p = Printf.sprintf "{name=%S; age=%d}" p.name p.age
4141- end in
4242- let yaml = read_file file in
4343- let json = read_file (file ^ ".json") in
4444- let json_result = Jsont_bytesrw.decode_string M.person_codec json in
4545- let yaml_result = Yamlt.decode M.person_codec (Bytes.Reader.of_string yaml) in
4646-4747- show_result_both "person"
4848- (Result.map M.show json_result)
4949- (Result.map M.show yaml_result)
5050-5151-(* Test: Object with optional fields *)
5252-let test_optional_fields file =
5353- let module M = struct
5454- type config = { host : string; port : int option; debug : bool option }
5555-5656- let config_codec =
5757- Jsont.Object.map ~kind:"Config" (fun host port debug ->
5858- { host; port; debug })
5959- |> Jsont.Object.mem "host" Jsont.string ~enc:(fun c -> c.host)
6060- |> Jsont.Object.opt_mem "port" Jsont.int ~enc:(fun c -> c.port)
6161- |> Jsont.Object.opt_mem "debug" Jsont.bool ~enc:(fun c -> c.debug)
6262- |> Jsont.Object.finish
6363-6464- let show c =
6565- Printf.sprintf "{host=%S; port=%s; debug=%s}" c.host
6666- (match c.port with
6767- | None -> "None"
6868- | Some p -> Printf.sprintf "Some %d" p)
6969- (match c.debug with
7070- | None -> "None"
7171- | Some b -> Printf.sprintf "Some %b" b)
7272- end in
7373- let yaml = read_file file in
7474- let json = read_file (file ^ ".json") in
7575- let json_result = Jsont_bytesrw.decode_string M.config_codec json in
7676- let yaml_result = Yamlt.decode M.config_codec (Bytes.Reader.of_string yaml) in
7777-7878- show_result_both "config"
7979- (Result.map M.show json_result)
8080- (Result.map M.show yaml_result)
8181-8282-(* Test: Object with default values *)
8383-let test_default_values file =
8484- let module M = struct
8585- type settings = { timeout : int; retries : int; verbose : bool }
8686-8787- let settings_codec =
8888- Jsont.Object.map ~kind:"Settings" (fun timeout retries verbose ->
8989- { timeout; retries; verbose })
9090- |> Jsont.Object.mem "timeout" Jsont.int
9191- ~enc:(fun s -> s.timeout)
9292- ~dec_absent:30
9393- |> Jsont.Object.mem "retries" Jsont.int
9494- ~enc:(fun s -> s.retries)
9595- ~dec_absent:3
9696- |> Jsont.Object.mem "verbose" Jsont.bool
9797- ~enc:(fun s -> s.verbose)
9898- ~dec_absent:false
9999- |> Jsont.Object.finish
100100-101101- let show s =
102102- Printf.sprintf "{timeout=%d; retries=%d; verbose=%b}" s.timeout s.retries
103103- s.verbose
104104- end in
105105- let yaml = read_file file in
106106- let json = read_file (file ^ ".json") in
107107- let json_result = Jsont_bytesrw.decode_string M.settings_codec json in
108108- let yaml_result =
109109- Yamlt.decode M.settings_codec (Bytes.Reader.of_string yaml)
110110- in
111111-112112- show_result_both "settings"
113113- (Result.map M.show json_result)
114114- (Result.map M.show yaml_result)
115115-116116-(* Test: Nested objects *)
117117-let test_nested_objects file =
118118- let module M = struct
119119- type address = { street : string; city : string; zip : string }
120120- type employee = { name : string; address : address }
121121-122122- let address_codec =
123123- Jsont.Object.map ~kind:"Address" (fun street city zip ->
124124- { street; city; zip })
125125- |> Jsont.Object.mem "street" Jsont.string ~enc:(fun a -> a.street)
126126- |> Jsont.Object.mem "city" Jsont.string ~enc:(fun a -> a.city)
127127- |> Jsont.Object.mem "zip" Jsont.string ~enc:(fun a -> a.zip)
128128- |> Jsont.Object.finish
129129-130130- let employee_codec =
131131- Jsont.Object.map ~kind:"Employee" (fun name address -> { name; address })
132132- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun e -> e.name)
133133- |> Jsont.Object.mem "address" address_codec ~enc:(fun e -> e.address)
134134- |> Jsont.Object.finish
135135-136136- let show e =
137137- Printf.sprintf "{name=%S; address={street=%S; city=%S; zip=%S}}" e.name
138138- e.address.street e.address.city e.address.zip
139139- end in
140140- let yaml = read_file file in
141141- let json = read_file (file ^ ".json") in
142142- let json_result = Jsont_bytesrw.decode_string M.employee_codec json in
143143- let yaml_result =
144144- Yamlt.decode M.employee_codec (Bytes.Reader.of_string yaml)
145145- in
146146-147147- show_result_both "employee"
148148- (Result.map M.show json_result)
149149- (Result.map M.show yaml_result)
150150-151151-(* Test: Unknown member handling - error *)
152152-let test_unknown_members_error file =
153153- let module M = struct
154154- type strict = { name : string }
155155-156156- let strict_codec =
157157- Jsont.Object.map ~kind:"Strict" (fun name -> { name })
158158- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun s -> s.name)
159159- |> Jsont.Object.finish
160160- end in
161161- let yaml = read_file file in
162162- let result = Yamlt.decode M.strict_codec (Bytes.Reader.of_string yaml) in
163163- match result with
164164- | Ok _ -> Printf.printf "Unexpected success\n"
165165- | Error e -> Printf.printf "Expected error: %s\n" e
166166-167167-(* Test: Unknown member handling - keep *)
168168-let test_unknown_members_keep file =
169169- let module M = struct
170170- type flexible = { name : string; extra : Jsont.json }
171171-172172- let flexible_codec =
173173- Jsont.Object.map ~kind:"Flexible" (fun name extra -> { name; extra })
174174- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun f -> f.name)
175175- |> Jsont.Object.keep_unknown Jsont.json_mems ~enc:(fun f -> f.extra)
176176- |> Jsont.Object.finish
177177-178178- let show f = Printf.sprintf "{name=%S; has_extra=true}" f.name
179179- end in
180180- let yaml = read_file file in
181181- let json = read_file (file ^ ".json") in
182182- let json_result = Jsont_bytesrw.decode_string M.flexible_codec json in
183183- let yaml_result =
184184- Yamlt.decode M.flexible_codec (Bytes.Reader.of_string yaml)
185185- in
186186-187187- show_result_both "flexible"
188188- (Result.map M.show json_result)
189189- (Result.map M.show yaml_result)
190190-191191-(* Test: Object cases (discriminated unions) - simplified version *)
192192-let test_object_cases file =
193193- let module M = struct
194194- type circle = { type_ : string; radius : float }
195195-196196- let circle_codec =
197197- Jsont.Object.map ~kind:"Circle" (fun type_ radius -> { type_; radius })
198198- |> Jsont.Object.mem "type" Jsont.string ~enc:(fun c -> c.type_)
199199- |> Jsont.Object.mem "radius" Jsont.number ~enc:(fun c -> c.radius)
200200- |> Jsont.Object.finish
201201-202202- let show c = Printf.sprintf "Circle{radius=%.2f}" c.radius
203203- end in
204204- let yaml = read_file file in
205205- let json = read_file (file ^ ".json") in
206206- let json_result = Jsont_bytesrw.decode_string M.circle_codec json in
207207- let yaml_result = Yamlt.decode M.circle_codec (Bytes.Reader.of_string yaml) in
208208-209209- show_result_both "shape"
210210- (Result.map M.show json_result)
211211- (Result.map M.show yaml_result)
212212-213213-(* Test: Missing required field error *)
214214-let test_missing_required file =
215215- let module M = struct
216216- type required = { name : string; age : int }
217217-218218- let required_codec =
219219- Jsont.Object.map ~kind:"Required" (fun name age -> { name; age })
220220- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun r -> r.name)
221221- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun r -> r.age)
222222- |> Jsont.Object.finish
223223- end in
224224- let yaml = read_file file in
225225- let result = Yamlt.decode M.required_codec (Bytes.Reader.of_string yaml) in
226226- match result with
227227- | Ok _ -> Printf.printf "Unexpected success\n"
228228- | Error e -> Printf.printf "Expected error: %s\n" e
229229-230230-(* Test: Encoding objects to different formats *)
231231-let test_encode_object () =
232232- let module M = struct
233233- type person = { name : string; age : int; active : bool }
234234-235235- let person_codec =
236236- Jsont.Object.map ~kind:"Person" (fun name age active ->
237237- { name; age; active })
238238- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.name)
239239- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
240240- |> Jsont.Object.mem "active" Jsont.bool ~enc:(fun p -> p.active)
241241- |> Jsont.Object.finish
242242- end in
243243- let person = M.{ name = "Alice"; age = 30; active = true } in
244244-245245- (* Encode to JSON *)
246246- (match Jsont_bytesrw.encode_string M.person_codec person with
247247- | Ok s -> Printf.printf "JSON: %s\n" (String.trim s)
248248- | Error e -> Printf.printf "JSON ERROR: %s\n" e);
249249-250250- (* Encode to YAML Block *)
251251- (let b = Buffer.create 256 in
252252- let writer = Bytes.Writer.of_buffer b in
253253- match
254254- Yamlt.encode ~format:Yamlt.Block M.person_codec person ~eod:true writer
255255- with
256256- | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b)
257257- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
258258-259259- (* Encode to YAML Flow *)
260260- let b = Buffer.create 256 in
261261- let writer = Bytes.Writer.of_buffer b in
262262- match
263263- Yamlt.encode ~format:Yamlt.Flow M.person_codec person ~eod:true writer
264264- with
265265- | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b)
266266- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e
267267-268268-(* Test: Roundtrip encoding of objects with unknown members *)
269269-let test_unknown_keep_roundtrip file =
270270- let module M = struct
271271- type flexible = { name : string; extra : Jsont.json }
272272-273273- let flexible_codec =
274274- Jsont.Object.map ~kind:"Flexible" (fun name extra -> { name; extra })
275275- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun f -> f.name)
276276- |> Jsont.Object.keep_unknown Jsont.json_mems ~enc:(fun f -> f.extra)
277277- |> Jsont.Object.finish
278278-279279- let show_json json =
280280- match Jsont_bytesrw.encode_string Jsont.json json with
281281- | Ok s -> String.trim s
282282- | Error e -> Printf.sprintf "ERROR: %s" e
283283- end in
284284- let yaml = read_file file in
285285-286286- (* Decode from YAML *)
287287- match Yamlt.decode M.flexible_codec (Bytes.Reader.of_string yaml) with
288288- | Error e -> Printf.printf "Decode error: %s\n" e
289289- | Ok v -> (
290290- Printf.printf "Decoded: name=%S, extra=%s\n" v.M.name
291291- (M.show_json v.M.extra);
292292-293293- (* Encode to YAML Block *)
294294- let b = Buffer.create 256 in
295295- let writer = Bytes.Writer.of_buffer b in
296296- (match
297297- Yamlt.encode ~format:Yamlt.Block M.flexible_codec v ~eod:true writer
298298- with
299299- | Ok () -> Printf.printf "Encoded Block:\n%s" (Buffer.contents b)
300300- | Error e -> Printf.printf "Encode Block ERROR: %s\n" e);
301301-302302- (* Re-decode the encoded YAML to verify roundtrip *)
303303- let encoded = Buffer.contents b in
304304- match Yamlt.decode M.flexible_codec (Bytes.Reader.of_string encoded) with
305305- | Error e -> Printf.printf "Re-decode error: %s\n" e
306306- | Ok v2 ->
307307- Printf.printf "Re-decoded: name=%S, extra=%s\n" v2.M.name
308308- (M.show_json v2.M.extra);
309309- if M.show_json v.M.extra = M.show_json v2.M.extra then
310310- Printf.printf "Roundtrip: OK (extra members preserved)\n"
311311- else Printf.printf "Roundtrip: FAILED (extra members lost)\n")
312312-313313-let () =
314314- let usage = "Usage: test_objects <command> [args...]" in
315315-316316- if Stdlib.Array.length Sys.argv < 2 then begin
317317- prerr_endline usage;
318318- exit 1
319319- end;
320320-321321- match Sys.argv.(1) with
322322- | "simple" when Stdlib.Array.length Sys.argv = 3 ->
323323- test_simple_object Sys.argv.(2)
324324- | "optional" when Stdlib.Array.length Sys.argv = 3 ->
325325- test_optional_fields Sys.argv.(2)
326326- | "defaults" when Stdlib.Array.length Sys.argv = 3 ->
327327- test_default_values Sys.argv.(2)
328328- | "nested" when Stdlib.Array.length Sys.argv = 3 ->
329329- test_nested_objects Sys.argv.(2)
330330- | "unknown-error" when Stdlib.Array.length Sys.argv = 3 ->
331331- test_unknown_members_error Sys.argv.(2)
332332- | "unknown-keep" when Stdlib.Array.length Sys.argv = 3 ->
333333- test_unknown_members_keep Sys.argv.(2)
334334- | "unknown-keep-roundtrip" when Stdlib.Array.length Sys.argv = 3 ->
335335- test_unknown_keep_roundtrip Sys.argv.(2)
336336- | "cases" when Stdlib.Array.length Sys.argv = 3 ->
337337- test_object_cases Sys.argv.(2)
338338- | "missing-required" when Stdlib.Array.length Sys.argv = 3 ->
339339- test_missing_required Sys.argv.(2)
340340- | "encode" when Stdlib.Array.length Sys.argv = 2 -> test_encode_object ()
341341- | _ ->
342342- prerr_endline usage;
343343- prerr_endline "Commands:";
344344- prerr_endline " simple <file> - Test simple object";
345345- prerr_endline " optional <file> - Test optional fields";
346346- prerr_endline " defaults <file> - Test default values";
347347- prerr_endline " nested <file> - Test nested objects";
348348- prerr_endline " unknown-error <file> - Test unknown member error";
349349- prerr_endline " unknown-keep <file> - Test keeping unknown members";
350350- prerr_endline
351351- " unknown-keep-roundtrip <file> - Test roundtrip of unknown members";
352352- prerr_endline " cases <file> - Test object cases (unions)";
353353- prerr_endline
354354- " missing-required <file> - Test missing required field error";
355355- prerr_endline " encode - Test encoding objects";
356356- exit 1
-19
tests/bin/test_opt_array.ml
···11-open Bytesrw
22-33-let () =
44- let codec =
55- Jsont.Object.map ~kind:"Test" (fun arr -> arr)
66- |> Jsont.Object.opt_mem "values" (Jsont.array Jsont.string) ~enc:(fun arr ->
77- arr)
88- |> Jsont.Object.finish
99- in
1010-1111- let yaml = "values: [a, b, c]" in
1212-1313- Printf.printf "Testing optional array field:\n";
1414- match Yamlt.decode codec (Bytes.Reader.of_string yaml) with
1515- | Ok arr -> (
1616- match arr with
1717- | None -> Printf.printf "Result: None\n"
1818- | Some a -> Printf.printf "Result: Some([%d items])\n" (Array.length a))
1919- | Error e -> Printf.printf "Error: %s\n" e
-271
tests/bin/test_roundtrip.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test roundtrip encoding/decoding with Yamlt *)
77-88-open Bytesrw
99-1010-(* Test: Roundtrip scalars *)
1111-let test_scalar_roundtrip () =
1212- let module M = struct
1313- type data = { s : string; n : float; b : bool; nul : unit }
1414-1515- let data_codec =
1616- Jsont.Object.map ~kind:"Data" (fun s n b nul -> { s; n; b; nul })
1717- |> Jsont.Object.mem "s" Jsont.string ~enc:(fun d -> d.s)
1818- |> Jsont.Object.mem "n" Jsont.number ~enc:(fun d -> d.n)
1919- |> Jsont.Object.mem "b" Jsont.bool ~enc:(fun d -> d.b)
2020- |> Jsont.Object.mem "nul" (Jsont.null ()) ~enc:(fun d -> d.nul)
2121- |> Jsont.Object.finish
2222-2323- let equal d1 d2 =
2424- d1.s = d2.s && d1.n = d2.n && d1.b = d2.b && d1.nul = d2.nul
2525- end in
2626- let original = { M.s = "hello"; n = 42.5; b = true; nul = () } in
2727-2828- (* JSON roundtrip *)
2929- let json_encoded = Jsont_bytesrw.encode_string M.data_codec original in
3030- let json_decoded =
3131- Result.bind json_encoded (Jsont_bytesrw.decode_string M.data_codec)
3232- in
3333- (match json_decoded with
3434- | Ok decoded when M.equal original decoded ->
3535- Printf.printf "JSON roundtrip: PASS\n"
3636- | Ok _ -> Printf.printf "JSON roundtrip: FAIL (data mismatch)\n"
3737- | Error e -> Printf.printf "JSON roundtrip: FAIL (%s)\n" e);
3838-3939- (* YAML Block roundtrip *)
4040- let yaml_block_encoded =
4141- let b = Buffer.create 256 in
4242- let writer = Bytes.Writer.of_buffer b in
4343- match
4444- Yamlt.encode ~format:Yamlt.Block M.data_codec original ~eod:true writer
4545- with
4646- | Ok () -> Ok (Buffer.contents b)
4747- | Error e -> Error e
4848- in
4949- let yaml_block_decoded =
5050- Result.bind yaml_block_encoded (fun yaml ->
5151- Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml))
5252- in
5353- (match yaml_block_decoded with
5454- | Ok decoded when M.equal original decoded ->
5555- Printf.printf "YAML Block roundtrip: PASS\n"
5656- | Ok _ -> Printf.printf "YAML Block roundtrip: FAIL (data mismatch)\n"
5757- | Error e -> Printf.printf "YAML Block roundtrip: FAIL (%s)\n" e);
5858-5959- (* YAML Flow roundtrip *)
6060- let yaml_flow_encoded =
6161- let b = Buffer.create 256 in
6262- let writer = Bytes.Writer.of_buffer b in
6363- match
6464- Yamlt.encode ~format:Yamlt.Flow M.data_codec original ~eod:true writer
6565- with
6666- | Ok () -> Ok (Buffer.contents b)
6767- | Error e -> Error e
6868- in
6969- let yaml_flow_decoded =
7070- Result.bind yaml_flow_encoded (fun yaml ->
7171- Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml))
7272- in
7373- match yaml_flow_decoded with
7474- | Ok decoded when M.equal original decoded ->
7575- Printf.printf "YAML Flow roundtrip: PASS\n"
7676- | Ok _ -> Printf.printf "YAML Flow roundtrip: FAIL (data mismatch)\n"
7777- | Error e -> Printf.printf "YAML Flow roundtrip: FAIL (%s)\n" e
7878-7979-(* Test: Roundtrip arrays *)
8080-let test_array_roundtrip () =
8181- let module M = struct
8282- type data = { items : int array; nested : float array array }
8383-8484- let data_codec =
8585- Jsont.Object.map ~kind:"Data" (fun items nested -> { items; nested })
8686- |> Jsont.Object.mem "items" (Jsont.array Jsont.int) ~enc:(fun d ->
8787- d.items)
8888- |> Jsont.Object.mem "nested"
8989- (Jsont.array (Jsont.array Jsont.number))
9090- ~enc:(fun d -> d.nested)
9191- |> Jsont.Object.finish
9292-9393- let equal d1 d2 = d1.items = d2.items && d1.nested = d2.nested
9494- end in
9595- let original =
9696- {
9797- M.items = [| 1; 2; 3; 4; 5 |];
9898- nested = [| [| 1.0; 2.0 |]; [| 3.0; 4.0 |] |];
9999- }
100100- in
101101-102102- (* JSON roundtrip *)
103103- let json_result =
104104- Result.bind
105105- (Jsont_bytesrw.encode_string M.data_codec original)
106106- (Jsont_bytesrw.decode_string M.data_codec)
107107- in
108108- (match json_result with
109109- | Ok decoded when M.equal original decoded ->
110110- Printf.printf "JSON array roundtrip: PASS\n"
111111- | Ok _ -> Printf.printf "JSON array roundtrip: FAIL (data mismatch)\n"
112112- | Error e -> Printf.printf "JSON array roundtrip: FAIL (%s)\n" e);
113113-114114- (* YAML roundtrip *)
115115- let yaml_result =
116116- let b = Buffer.create 256 in
117117- let writer = Bytes.Writer.of_buffer b in
118118- match Yamlt.encode M.data_codec original ~eod:true writer with
119119- | Ok () ->
120120- let yaml = Buffer.contents b in
121121- Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml)
122122- | Error e -> Error e
123123- in
124124- match yaml_result with
125125- | Ok decoded when M.equal original decoded ->
126126- Printf.printf "YAML array roundtrip: PASS\n"
127127- | Ok _ -> Printf.printf "YAML array roundtrip: FAIL (data mismatch)\n"
128128- | Error e -> Printf.printf "YAML array roundtrip: FAIL (%s)\n" e
129129-130130-(* Test: Roundtrip objects *)
131131-let test_object_roundtrip () =
132132- let module M = struct
133133- type person = { p_name : string; age : int; active : bool }
134134- type company = { c_name : string; employees : person array }
135135-136136- let person_codec =
137137- Jsont.Object.map ~kind:"Person" (fun p_name age active ->
138138- { p_name; age; active })
139139- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun p -> p.p_name)
140140- |> Jsont.Object.mem "age" Jsont.int ~enc:(fun p -> p.age)
141141- |> Jsont.Object.mem "active" Jsont.bool ~enc:(fun p -> p.active)
142142- |> Jsont.Object.finish
143143-144144- let company_codec =
145145- Jsont.Object.map ~kind:"Company" (fun c_name employees ->
146146- { c_name; employees })
147147- |> Jsont.Object.mem "name" Jsont.string ~enc:(fun c -> c.c_name)
148148- |> Jsont.Object.mem "employees" (Jsont.array person_codec) ~enc:(fun c ->
149149- c.employees)
150150- |> Jsont.Object.finish
151151-152152- let person_equal p1 p2 =
153153- p1.p_name = p2.p_name && p1.age = p2.age && p1.active = p2.active
154154-155155- let equal c1 c2 =
156156- c1.c_name = c2.c_name
157157- && Stdlib.Array.length c1.employees = Stdlib.Array.length c2.employees
158158- && Stdlib.Array.for_all2 person_equal c1.employees c2.employees
159159- end in
160160- let original =
161161- {
162162- M.c_name = "Acme Corp";
163163- employees =
164164- [|
165165- { p_name = "Alice"; age = 30; active = true };
166166- { p_name = "Bob"; age = 25; active = false };
167167- |];
168168- }
169169- in
170170-171171- (* JSON roundtrip *)
172172- let json_result =
173173- Result.bind
174174- (Jsont_bytesrw.encode_string M.company_codec original)
175175- (Jsont_bytesrw.decode_string M.company_codec)
176176- in
177177- (match json_result with
178178- | Ok decoded when M.equal original decoded ->
179179- Printf.printf "JSON object roundtrip: PASS\n"
180180- | Ok _ -> Printf.printf "JSON object roundtrip: FAIL (data mismatch)\n"
181181- | Error e -> Printf.printf "JSON object roundtrip: FAIL (%s)\n" e);
182182-183183- (* YAML roundtrip *)
184184- let yaml_result =
185185- let b = Buffer.create 256 in
186186- let writer = Bytes.Writer.of_buffer b in
187187- match Yamlt.encode M.company_codec original ~eod:true writer with
188188- | Ok () ->
189189- let yaml = Buffer.contents b in
190190- Yamlt.decode M.company_codec (Bytes.Reader.of_string yaml)
191191- | Error e -> Error e
192192- in
193193- match yaml_result with
194194- | Ok decoded when M.equal original decoded ->
195195- Printf.printf "YAML object roundtrip: PASS\n"
196196- | Ok _ -> Printf.printf "YAML object roundtrip: FAIL (data mismatch)\n"
197197- | Error e -> Printf.printf "YAML object roundtrip: FAIL (%s)\n" e
198198-199199-(* Test: Roundtrip with optionals *)
200200-let test_optional_roundtrip () =
201201- let module M = struct
202202- type data = {
203203- required : string;
204204- optional : int option;
205205- nullable : string option;
206206- }
207207-208208- let data_codec =
209209- Jsont.Object.map ~kind:"Data" (fun required optional nullable ->
210210- { required; optional; nullable })
211211- |> Jsont.Object.mem "required" Jsont.string ~enc:(fun d -> d.required)
212212- |> Jsont.Object.opt_mem "optional" Jsont.int ~enc:(fun d -> d.optional)
213213- |> Jsont.Object.mem "nullable" (Jsont.some Jsont.string) ~enc:(fun d ->
214214- d.nullable)
215215- |> Jsont.Object.finish
216216-217217- let equal d1 d2 =
218218- d1.required = d2.required && d1.optional = d2.optional
219219- && d1.nullable = d2.nullable
220220- end in
221221- let original = { M.required = "test"; optional = Some 42; nullable = None } in
222222-223223- (* JSON roundtrip *)
224224- let json_result =
225225- Result.bind
226226- (Jsont_bytesrw.encode_string M.data_codec original)
227227- (Jsont_bytesrw.decode_string M.data_codec)
228228- in
229229- (match json_result with
230230- | Ok decoded when M.equal original decoded ->
231231- Printf.printf "JSON optional roundtrip: PASS\n"
232232- | Ok _ -> Printf.printf "JSON optional roundtrip: FAIL (data mismatch)\n"
233233- | Error e -> Printf.printf "JSON optional roundtrip: FAIL (%s)\n" e);
234234-235235- (* YAML roundtrip *)
236236- let yaml_result =
237237- let b = Buffer.create 256 in
238238- let writer = Bytes.Writer.of_buffer b in
239239- match Yamlt.encode M.data_codec original ~eod:true writer with
240240- | Ok () ->
241241- let yaml = Buffer.contents b in
242242- Yamlt.decode M.data_codec (Bytes.Reader.of_string yaml)
243243- | Error e -> Error e
244244- in
245245- match yaml_result with
246246- | Ok decoded when M.equal original decoded ->
247247- Printf.printf "YAML optional roundtrip: PASS\n"
248248- | Ok _ -> Printf.printf "YAML optional roundtrip: FAIL (data mismatch)\n"
249249- | Error e -> Printf.printf "YAML optional roundtrip: FAIL (%s)\n" e
250250-251251-let () =
252252- let usage = "Usage: test_roundtrip <command>" in
253253-254254- if Stdlib.Array.length Sys.argv < 2 then begin
255255- prerr_endline usage;
256256- exit 1
257257- end;
258258-259259- match Sys.argv.(1) with
260260- | "scalar" -> test_scalar_roundtrip ()
261261- | "array" -> test_array_roundtrip ()
262262- | "object" -> test_object_roundtrip ()
263263- | "optional" -> test_optional_roundtrip ()
264264- | _ ->
265265- prerr_endline usage;
266266- prerr_endline "Commands:";
267267- prerr_endline " scalar - Test scalar roundtrip";
268268- prerr_endline " array - Test array roundtrip";
269269- prerr_endline " object - Test object roundtrip";
270270- prerr_endline " optional - Test optional fields roundtrip";
271271- exit 1
-309
tests/bin/test_scalars.ml
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** Test scalar type resolution with Yamlt codec *)
77-88-open Bytesrw
99-1010-(* Helper to read file *)
1111-let read_file path =
1212- let ic = open_in path in
1313- let len = in_channel_length ic in
1414- let s = really_input_string ic len in
1515- close_in ic;
1616- s
1717-1818-(* Helper to show results *)
1919-let show_result label = function
2020- | Ok v -> Printf.printf "%s: %s\n" label v
2121- | Error e -> Printf.printf "%s: ERROR: %s\n" label e
2222-2323-let show_result_json label json_result yaml_result =
2424- Printf.printf "JSON %s\n" label;
2525- show_result " decode" json_result;
2626- Printf.printf "YAML %s\n" label;
2727- show_result " decode" yaml_result
2828-2929-(* Test: Decode null values with different type expectations *)
3030-let test_null_resolution file =
3131- let yaml = read_file file in
3232-3333- (* Define a simple object codec with nullable field *)
3434- let null_codec =
3535- Jsont.Object.map ~kind:"NullTest" (fun n -> n)
3636- |> Jsont.Object.mem "value" (Jsont.null ()) ~enc:(fun n -> n)
3737- |> Jsont.Object.finish
3838- in
3939-4040- (* Try decoding as null *)
4141- let result = Yamlt.decode null_codec (Bytes.Reader.of_string yaml) in
4242- show_result "null_codec" (Result.map (fun () -> "null") result)
4343-4444-(* Test: Boolean type-directed resolution *)
4545-let test_bool_resolution file =
4646- let yaml = read_file file in
4747- let json = read_file (file ^ ".json") in
4848-4949- (* Codec expecting bool *)
5050- let bool_codec =
5151- Jsont.Object.map ~kind:"BoolTest" (fun b -> b)
5252- |> Jsont.Object.mem "value" Jsont.bool ~enc:(fun b -> b)
5353- |> Jsont.Object.finish
5454- in
5555-5656- (* Codec expecting string *)
5757- let string_codec =
5858- Jsont.Object.map ~kind:"StringTest" (fun s -> s)
5959- |> Jsont.Object.mem "value" Jsont.string ~enc:(fun s -> s)
6060- |> Jsont.Object.finish
6161- in
6262-6363- Printf.printf "=== Bool Codec ===\n";
6464- let json_result = Jsont_bytesrw.decode_string bool_codec json in
6565- let yaml_result = Yamlt.decode bool_codec (Bytes.Reader.of_string yaml) in
6666- show_result_json "bool_codec"
6767- (Result.map (Printf.sprintf "%b") json_result)
6868- (Result.map (Printf.sprintf "%b") yaml_result);
6969-7070- Printf.printf "\n=== String Codec ===\n";
7171- let json_result = Jsont_bytesrw.decode_string string_codec json in
7272- let yaml_result = Yamlt.decode string_codec (Bytes.Reader.of_string yaml) in
7373- show_result_json "string_codec"
7474- (Result.map (Printf.sprintf "%S") json_result)
7575- (Result.map (Printf.sprintf "%S") yaml_result)
7676-7777-(* Test: Number resolution *)
7878-let test_number_resolution file =
7979- let yaml = read_file file in
8080- let json = read_file (file ^ ".json") in
8181-8282- let number_codec =
8383- Jsont.Object.map ~kind:"NumberTest" (fun n -> n)
8484- |> Jsont.Object.mem "value" Jsont.number ~enc:(fun n -> n)
8585- |> Jsont.Object.finish
8686- in
8787-8888- let json_result = Jsont_bytesrw.decode_string number_codec json in
8989- let yaml_result = Yamlt.decode number_codec (Bytes.Reader.of_string yaml) in
9090-9191- show_result_json "number_codec"
9292- (Result.map (Printf.sprintf "%.17g") json_result)
9393- (Result.map (Printf.sprintf "%.17g") yaml_result)
9494-9595-(* Test: String resolution preserves everything *)
9696-let test_string_resolution file =
9797- let yaml = read_file file in
9898- let json = read_file (file ^ ".json") in
9999-100100- let string_codec =
101101- Jsont.Object.map ~kind:"StringTest" (fun s -> s)
102102- |> Jsont.Object.mem "value" Jsont.string ~enc:(fun s -> s)
103103- |> Jsont.Object.finish
104104- in
105105-106106- let json_result = Jsont_bytesrw.decode_string string_codec json in
107107- let yaml_result = Yamlt.decode string_codec (Bytes.Reader.of_string yaml) in
108108-109109- show_result_json "string_codec"
110110- (Result.map (Printf.sprintf "%S") json_result)
111111- (Result.map (Printf.sprintf "%S") yaml_result)
112112-113113-(* Test: Special float values *)
114114-let test_special_floats file =
115115- let yaml = read_file file in
116116-117117- let number_codec =
118118- Jsont.Object.map ~kind:"SpecialFloat" (fun n -> n)
119119- |> Jsont.Object.mem "value" Jsont.number ~enc:(fun n -> n)
120120- |> Jsont.Object.finish
121121- in
122122-123123- let result = Yamlt.decode number_codec (Bytes.Reader.of_string yaml) in
124124- match result with
125125- | Ok f ->
126126- if Float.is_nan f then Printf.printf "value: NaN\n"
127127- else if f = Float.infinity then Printf.printf "value: +Infinity\n"
128128- else if f = Float.neg_infinity then Printf.printf "value: -Infinity\n"
129129- else Printf.printf "value: %.17g\n" f
130130- | Error e -> Printf.printf "ERROR: %s\n" e
131131-132132-(* Test: Type mismatch errors *)
133133-let test_type_mismatch file expected_type =
134134- let yaml = read_file file in
135135-136136- match expected_type with
137137- | "bool" -> (
138138- let codec =
139139- Jsont.Object.map ~kind:"BoolTest" (fun b -> b)
140140- |> Jsont.Object.mem "value" Jsont.bool ~enc:(fun b -> b)
141141- |> Jsont.Object.finish
142142- in
143143- let result = Yamlt.decode codec (Bytes.Reader.of_string yaml) in
144144- match result with
145145- | Ok _ -> Printf.printf "Unexpected success\n"
146146- | Error e -> Printf.printf "Expected error: %s\n" e)
147147- | "number" -> (
148148- let codec =
149149- Jsont.Object.map ~kind:"NumberTest" (fun n -> n)
150150- |> Jsont.Object.mem "value" Jsont.number ~enc:(fun n -> n)
151151- |> Jsont.Object.finish
152152- in
153153- let result = Yamlt.decode codec (Bytes.Reader.of_string yaml) in
154154- match result with
155155- | Ok _ -> Printf.printf "Unexpected success\n"
156156- | Error e -> Printf.printf "Expected error: %s\n" e)
157157- | "null" -> (
158158- let codec =
159159- Jsont.Object.map ~kind:"NullTest" (fun n -> n)
160160- |> Jsont.Object.mem "value" (Jsont.null ()) ~enc:(fun n -> n)
161161- |> Jsont.Object.finish
162162- in
163163- let result = Yamlt.decode codec (Bytes.Reader.of_string yaml) in
164164- match result with
165165- | Ok _ -> Printf.printf "Unexpected success\n"
166166- | Error e -> Printf.printf "Expected error: %s\n" e)
167167- | _ -> failwith "unknown type"
168168-169169-(* Test: Decode with Jsont.json to see auto-resolution *)
170170-let test_any_resolution file =
171171- let yaml = read_file file in
172172- let json = read_file (file ^ ".json") in
173173-174174- let any_codec =
175175- Jsont.Object.map ~kind:"AnyTest" (fun v -> v)
176176- |> Jsont.Object.mem "value" Jsont.json ~enc:(fun v -> v)
177177- |> Jsont.Object.finish
178178- in
179179-180180- let json_result = Jsont_bytesrw.decode_string any_codec json in
181181- let yaml_result = Yamlt.decode any_codec (Bytes.Reader.of_string yaml) in
182182-183183- (* Just show that it decoded successfully *)
184184- show_result_json "any_codec"
185185- (Result.map (fun _ -> "decoded") json_result)
186186- (Result.map (fun _ -> "decoded") yaml_result)
187187-188188-(* Test: Encoding to different formats *)
189189-let test_encode_formats value_type value =
190190- match value_type with
191191- | "bool" -> (
192192- let codec =
193193- Jsont.Object.map ~kind:"BoolTest" (fun b -> b)
194194- |> Jsont.Object.mem "value" Jsont.bool ~enc:(fun b -> b)
195195- |> Jsont.Object.finish
196196- in
197197- let v = bool_of_string value in
198198- (match Jsont_bytesrw.encode_string codec v with
199199- | Ok s -> Printf.printf "JSON: %s\n" (String.trim s)
200200- | Error e -> Printf.printf "JSON ERROR: %s\n" e);
201201- (let b = Buffer.create 256 in
202202- let writer = Bytes.Writer.of_buffer b in
203203- match Yamlt.encode ~format:Yamlt.Block codec v ~eod:true writer with
204204- | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b)
205205- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
206206- let b = Buffer.create 256 in
207207- let writer = Bytes.Writer.of_buffer b in
208208- match Yamlt.encode ~format:Yamlt.Flow codec v ~eod:true writer with
209209- | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b)
210210- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e)
211211- | "number" -> (
212212- let codec =
213213- Jsont.Object.map ~kind:"NumberTest" (fun n -> n)
214214- |> Jsont.Object.mem "value" Jsont.number ~enc:(fun n -> n)
215215- |> Jsont.Object.finish
216216- in
217217- let v = float_of_string value in
218218- (match Jsont_bytesrw.encode_string codec v with
219219- | Ok s -> Printf.printf "JSON: %s\n" (String.trim s)
220220- | Error e -> Printf.printf "JSON ERROR: %s\n" e);
221221- (let b = Buffer.create 256 in
222222- let writer = Bytes.Writer.of_buffer b in
223223- match Yamlt.encode ~format:Yamlt.Block codec v ~eod:true writer with
224224- | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b)
225225- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
226226- let b = Buffer.create 256 in
227227- let writer = Bytes.Writer.of_buffer b in
228228- match Yamlt.encode ~format:Yamlt.Flow codec v ~eod:true writer with
229229- | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b)
230230- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e)
231231- | "string" -> (
232232- let codec =
233233- Jsont.Object.map ~kind:"StringTest" (fun s -> s)
234234- |> Jsont.Object.mem "value" Jsont.string ~enc:(fun s -> s)
235235- |> Jsont.Object.finish
236236- in
237237- let v = value in
238238- (match Jsont_bytesrw.encode_string codec v with
239239- | Ok s -> Printf.printf "JSON: %s\n" (String.trim s)
240240- | Error e -> Printf.printf "JSON ERROR: %s\n" e);
241241- (let b = Buffer.create 256 in
242242- let writer = Bytes.Writer.of_buffer b in
243243- match Yamlt.encode ~format:Yamlt.Block codec v ~eod:true writer with
244244- | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b)
245245- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
246246- let b = Buffer.create 256 in
247247- let writer = Bytes.Writer.of_buffer b in
248248- match Yamlt.encode ~format:Yamlt.Flow codec v ~eod:true writer with
249249- | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b)
250250- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e)
251251- | "null" -> (
252252- let codec =
253253- Jsont.Object.map ~kind:"NullTest" (fun n -> n)
254254- |> Jsont.Object.mem "value" (Jsont.null ()) ~enc:(fun n -> n)
255255- |> Jsont.Object.finish
256256- in
257257- let v = () in
258258- (match Jsont_bytesrw.encode_string codec v with
259259- | Ok s -> Printf.printf "JSON: %s\n" (String.trim s)
260260- | Error e -> Printf.printf "JSON ERROR: %s\n" e);
261261- (let b = Buffer.create 256 in
262262- let writer = Bytes.Writer.of_buffer b in
263263- match Yamlt.encode ~format:Yamlt.Block codec v ~eod:true writer with
264264- | Ok () -> Printf.printf "YAML Block:\n%s" (Buffer.contents b)
265265- | Error e -> Printf.printf "YAML Block ERROR: %s\n" e);
266266- let b = Buffer.create 256 in
267267- let writer = Bytes.Writer.of_buffer b in
268268- match Yamlt.encode ~format:Yamlt.Flow codec v ~eod:true writer with
269269- | Ok () -> Printf.printf "YAML Flow: %s" (Buffer.contents b)
270270- | Error e -> Printf.printf "YAML Flow ERROR: %s\n" e)
271271- | _ -> failwith "unknown type"
272272-273273-let () =
274274- let usage = "Usage: test_scalars <command> [args...]" in
275275-276276- if Stdlib.Array.length Sys.argv < 2 then begin
277277- prerr_endline usage;
278278- exit 1
279279- end;
280280-281281- match Sys.argv.(1) with
282282- | "null" when Array.length Sys.argv = 3 -> test_null_resolution Sys.argv.(2)
283283- | "bool" when Array.length Sys.argv = 3 -> test_bool_resolution Sys.argv.(2)
284284- | "number" when Array.length Sys.argv = 3 ->
285285- test_number_resolution Sys.argv.(2)
286286- | "string" when Array.length Sys.argv = 3 ->
287287- test_string_resolution Sys.argv.(2)
288288- | "special-float" when Array.length Sys.argv = 3 ->
289289- test_special_floats Sys.argv.(2)
290290- | "type-mismatch" when Array.length Sys.argv = 4 ->
291291- test_type_mismatch Sys.argv.(2) Sys.argv.(3)
292292- | "any" when Array.length Sys.argv = 3 -> test_any_resolution Sys.argv.(2)
293293- | "encode" when Array.length Sys.argv = 4 ->
294294- test_encode_formats Sys.argv.(2) Sys.argv.(3)
295295- | _ ->
296296- prerr_endline usage;
297297- prerr_endline "Commands:";
298298- prerr_endline " null <file> - Test null resolution";
299299- prerr_endline
300300- " bool <file> - Test bool vs string resolution";
301301- prerr_endline " number <file> - Test number resolution";
302302- prerr_endline " string <file> - Test string resolution";
303303- prerr_endline " special-float <file> - Test .inf, .nan, etc.";
304304- prerr_endline
305305- " type-mismatch <file> <type> - Test error on type mismatch";
306306- prerr_endline
307307- " any <file> - Test Jsont.any auto-resolution";
308308- prerr_endline " encode <type> <value> - Test encoding to JSON/YAML";
309309- exit 1
-38
tests/bin/test_some_vs_option.ml
···11-open Bytesrw
22-33-let () =
44- (* Using Jsont.some like opt_mem does *)
55- let codec1 =
66- Jsont.Object.map ~kind:"Test" (fun arr -> arr)
77- |> Jsont.Object.mem "values"
88- (Jsont.some (Jsont.array Jsont.string))
99- ~enc:(fun arr -> arr)
1010- |> Jsont.Object.finish
1111- in
1212-1313- let yaml = "values: [a, b, c]" in
1414-1515- Printf.printf "Test 1: Jsont.some (Jsont.array) - like opt_mem:\n";
1616- (match Yamlt.decode codec1 (Bytes.Reader.of_string yaml) with
1717- | Ok arr -> (
1818- match arr with
1919- | None -> Printf.printf "Result: None\n"
2020- | Some a -> Printf.printf "Result: Some([%d items])\n" (Array.length a))
2121- | Error e -> Printf.printf "Error: %s\n" e);
2222-2323- (* Using Jsont.option *)
2424- let codec2 =
2525- Jsont.Object.map ~kind:"Test" (fun arr -> arr)
2626- |> Jsont.Object.mem "values"
2727- (Jsont.option (Jsont.array Jsont.string))
2828- ~enc:(fun arr -> arr)
2929- |> Jsont.Object.finish
3030- in
3131-3232- Printf.printf "\nTest 2: Jsont.option (Jsont.array):\n";
3333- match Yamlt.decode codec2 (Bytes.Reader.of_string yaml) with
3434- | Ok arr -> (
3535- match arr with
3636- | None -> Printf.printf "Result: None\n"
3737- | Some a -> Printf.printf "Result: Some([%d items])\n" (Array.length a))
3838- | Error e -> Printf.printf "Error: %s\n" e
-148
tests/cram/arrays_codec.t
···11-Array Codec Tests with Yamlt
22-===============================
33-44-This test suite validates array encoding/decoding with Jsont codecs in YAML,
55-including homogeneous type checking and nested structures.
66-77-Setup
88------
99-1010-================================================================================
1111-HOMOGENEOUS ARRAYS
1212-================================================================================
1313-1414-Integer arrays
1515-1616- $ test_arrays int ../data/arrays/int_array.yml
1717- JSON: int_array: [1; 2; 3; 4; 5]
1818- YAML: int_array: [1; 2; 3; 4; 5]
1919-2020-String arrays
2121-2222- $ test_arrays string ../data/arrays/string_array.yml
2323- JSON: string_array: ["apple"; "banana"; "cherry"]
2424- YAML: string_array: ["apple"; "banana"; "cherry"]
2525-2626-Float/Number arrays
2727-2828- $ test_arrays float ../data/arrays/float_array.yml
2929- JSON: float_array: [1.50; 2.70; 3.14; 0.50]
3030- YAML: float_array: [1.50; 2.70; 3.14; 0.50]
3131-3232-Boolean arrays
3333-3434- $ test_arrays bool ../data/arrays/bool_array.yml
3535- JSON: bool_array: [true; false; true; true; false]
3636- YAML: bool_array: [true; false; true; true; false]
3737-3838-================================================================================
3939-EMPTY ARRAYS
4040-================================================================================
4141-4242-Empty arrays work correctly
4343-4444- $ test_arrays empty ../data/arrays/empty_array.yml
4545- JSON: empty_array: length=0
4646- YAML: empty_array: length=0
4747-4848-================================================================================
4949-ARRAYS OF OBJECTS
5050-================================================================================
5151-5252-Arrays containing objects
5353-5454- $ test_arrays objects ../data/arrays/object_array.yml
5555- JSON: object_array: [{Alice,30}; {Bob,25}; {Charlie,35}]
5656- YAML: object_array: [{Alice,30}; {Bob,25}; {Charlie,35}]
5757-5858-================================================================================
5959-NESTED ARRAYS
6060-================================================================================
6161-6262-Arrays containing arrays (matrices)
6363-6464- $ test_arrays nested ../data/arrays/nested_array.yml
6565- JSON: nested_arrays: [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]]
6666- YAML: nested_arrays: [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]]
6767-6868-================================================================================
6969-NULLABLE ARRAYS
7070-================================================================================
7171-7272-Arrays with null elements
7373-7474- $ test_arrays nullable ../data/arrays/nullable_array.yml
7575- JSON: nullable_array: ERROR: Expected string but found null
7676- File "-", line 1, characters 21-22:
7777- File "-", line 1, characters 21-22: at index 1 of
7878- File "-", line 1, characters 11-22: array<string>
7979- File "-": in member values of
8080- File "-", line 1, characters 0-22: Nullable object
8181- YAML: nullable_array: ERROR: Expected string but found null
8282- File "-":
8383- at index 1 of
8484- File "-": array<string>
8585- File "-": in member values of
8686- File "-": Nullable object
8787-8888-================================================================================
8989-ERROR HANDLING
9090-================================================================================
9191-9292-Type mismatch in array element
9393-9494- $ test_arrays type-mismatch ../data/arrays/type_mismatch.yml
9595- Expected error: String "not-a-number" does not parse to OCaml int value
9696- File "-":
9797- at index 2 of
9898- File "-": array<OCaml int>
9999- File "-": in member values of
100100- File "-": Numbers object
101101-102102-================================================================================
103103-ENCODING ARRAYS
104104-================================================================================
105105-106106-Encode arrays to JSON and YAML formats
107107-108108- $ test_arrays encode
109109- JSON: {"numbers":[1,2,3,4,5],"strings":["hello","world"]}
110110- YAML Block:
111111- numbers:
112112- - 1
113113- - 2
114114- - 3
115115- - 4
116116- - 5
117117- strings:
118118- - hello
119119- - world
120120- YAML Flow: {numbers: [1, 2, 3, 4, 5], strings: [hello, world]}
121121-122122-================================================================================
123123-NEGATIVE TESTS - Wrong File Types
124124-================================================================================
125125-126126-Attempting to decode an object file with an array codec should fail
127127-128128- $ test_arrays int ../data/objects/simple.yml
129129- JSON: int_array: ERROR: Missing member values in Numbers object
130130- File "-", line 1, characters 0-28:
131131- YAML: int_array: ERROR: Missing member values in Numbers object
132132- File "-":
133133-134134-Attempting to decode a scalar file with an array codec should fail
135135-136136- $ test_arrays string ../data/scalars/string_plain.yml
137137- JSON: string_array: ERROR: Missing member items in Tags object
138138- File "-", line 1, characters 0-24:
139139- YAML: string_array: ERROR: Missing member items in Tags object
140140- File "-":
141141-142142-Attempting to decode int array with string array codec should fail
143143-144144- $ test_arrays string ../data/arrays/int_array.yml
145145- JSON: string_array: ERROR: Missing member items in Tags object
146146- File "-", line 1, characters 0-27:
147147- YAML: string_array: ERROR: Missing member items in Tags object
148148- File "-":
-71
tests/cram/complex_codec.t
···11-Complex Nested Types Tests with Yamlt
22-======================================
33-44-This test suite validates complex nested structures combining objects, arrays,
55-and various levels of nesting.
66-77-================================================================================
88-DEEPLY NESTED OBJECTS
99-================================================================================
1010-1111-Handle deeply nested object structures
1212-1313- $ test_complex deep-nesting ../data/complex/deep_nesting.yml
1414- JSON: deep_nesting: depth=4, value=42
1515- YAML: deep_nesting: depth=4, value=42
1616-1717-================================================================================
1818-MIXED STRUCTURES
1919-================================================================================
2020-2121-Arrays of objects containing arrays
2222-2323- $ test_complex mixed-structure ../data/complex/mixed_structure.yml
2424- JSON: mixed_structure: name="products", items=3, total_tags=6
2525- YAML: mixed_structure: name="products", items=3, total_tags=6
2626-2727-================================================================================
2828-COMPLEX OPTIONAL COMBINATIONS
2929-================================================================================
3030-3131-Multiple optional fields with different combinations
3232-3333- $ test_complex complex-optional ../data/complex/complex_optional.yml
3434- JSON: complex_optional: host="example.com", port=443, ssl=true, fallbacks=2
3535- YAML: complex_optional: host="example.com", port=443, ssl=true, fallbacks=2
3636-3737-================================================================================
3838-HETEROGENEOUS DATA
3939-================================================================================
4040-4141-Mixed types in arrays using any type
4242-4343- $ test_complex heterogeneous ../data/complex/heterogeneous.yml
4444- JSON: heterogeneous: ERROR: Expected one of but found number
4545- File "-", line 1, characters 11-12:
4646- File "-", line 1, characters 11-12: at index 0 of
4747- File "-", line 1, characters 10-12: array<one of >
4848- File "-": in member mixed of
4949- File "-", line 1, characters 0-12: Data object
5050- YAML: heterogeneous: ERROR: Expected one of but found number
5151- File "-":
5252- at index 0 of
5353- File "-": array<one of >
5454- File "-": in member mixed of
5555- File "-": Data object
5656-5757-================================================================================
5858-NEGATIVE TESTS - Structure Mismatch
5959-================================================================================
6060-6161-Using deeply nested data with flat codec should fail
6262-6363- $ test_complex mixed-structure ../data/complex/deep_nesting.yml
6464- JSON: mixed_structure: ERROR: Missing members in Collection object:
6565- items
6666- name
6767- File "-", line 1, characters 0-44:
6868- YAML: mixed_structure: ERROR: Missing members in Collection object:
6969- items
7070- name
7171- File "-":
···11-Edge Cases Tests with Yamlt
22-============================
33-44-This test suite validates edge cases including large numbers, special characters,
55-unicode, and boundary conditions.
66-77-================================================================================
88-LARGE NUMBERS
99-================================================================================
1010-1111-Very large and very small floating point numbers
1212-1313- $ test_edge large-numbers ../data/edge/large_numbers.yml
1414- JSON: large_numbers: large_int=9007199254740991, large_float=1.797693e+308, small_float=2.225074e-308
1515- YAML: large_numbers: large_int=9007199254740991, large_float=1.797693e+308, small_float=2.225074e-308
1616-1717-================================================================================
1818-SPECIAL CHARACTERS
1919-================================================================================
2020-2121-Strings containing newlines, tabs, and other special characters
2222-2323- $ test_edge special-chars ../data/edge/special_chars.yml
2424- JSON: special_chars: length=34, contains_newline=true, contains_tab=true
2525- YAML: special_chars: length=34, contains_newline=true, contains_tab=true
2626-2727-================================================================================
2828-UNICODE STRINGS
2929-================================================================================
3030-3131-Emoji, Chinese, and RTL text
3232-3333- $ test_edge unicode ../data/edge/unicode.yml
3434- JSON: unicode: emoji="\240\159\142\137\240\159\154\128\226\156\168", chinese="\228\189\160\229\165\189\228\184\150\231\149\140", rtl="\217\133\216\177\216\173\216\168\216\167"
3535- YAML: unicode: emoji="\240\159\142\137\240\159\154\128\226\156\168", chinese="\228\189\160\229\165\189\228\184\150\231\149\140", rtl="\217\133\216\177\216\173\216\168\216\167"
3636-3737-================================================================================
3838-EMPTY COLLECTIONS
3939-================================================================================
4040-4141-Empty arrays and objects
4242-4343- $ test_edge empty-collections ../data/edge/empty_collections.yml
4444- JSON: empty_collections: empty_array_len=0, empty_object_array_len=0
4545- YAML: empty_collections: empty_array_len=0, empty_object_array_len=0
4646-4747-================================================================================
4848-SPECIAL KEY NAMES
4949-================================================================================
5050-5151-Keys with dots, dashes, colons
5252-5353- $ test_edge special-keys ../data/edge/special_keys.yml
5454- JSON: special_keys: ERROR: Expected one of but found object
5555- File "-", line 1, characters 0-1:
5656- YAML: special_keys: ERROR: Expected one of but found object
5757- File "-":
5858-5959-================================================================================
6060-SINGLE-ELEMENT ARRAYS
6161-================================================================================
6262-6363-Arrays with exactly one element
6464-6565- $ test_edge single-element ../data/edge/single_element.yml
6666- JSON: single_element: length=1, value=42
6767- YAML: single_element: length=1, value=42
6868-6969-================================================================================
7070-NEGATIVE TESTS - Boundary Violations
7171-================================================================================
7272-7373-Using unicode data with number codec should fail
7474-7575- $ test_edge large-numbers ../data/edge/unicode.yml
7676- JSON: large_numbers: ERROR: Missing members in Numbers object:
7777- large_float
7878- large_int
7979- small_float
8080- File "-", line 1, characters 0-72:
8181- YAML: large_numbers: ERROR: Missing members in Numbers object:
8282- large_float
8383- large_int
8484- small_float
8585- File "-":
-107
tests/cram/formats_codec.t
···11-Format-Specific Features Tests with Yamlt
22-==========================================
33-44-This test suite validates YAML-specific format features and compares with JSON behavior.
55-66-================================================================================
77-MULTI-LINE STRINGS - LITERAL STYLE
88-================================================================================
99-1010-Literal style (|) preserves newlines
1111-1212- $ test_formats literal ../data/formats/literal_string.yml
1313- JSON: literal_string: lines=5, length=81
1414- YAML: literal_string: lines=5, length=81
1515-1616-================================================================================
1717-MULTI-LINE STRINGS - FOLDED STYLE
1818-================================================================================
1919-2020-Folded style (>) folds lines into single line
2121-2222- $ test_formats folded ../data/formats/folded_string.yml
2323- JSON: folded_string: length=114, newlines=1
2424- YAML: folded_string: length=114, newlines=1
2525-2626-================================================================================
2727-NUMBER FORMATS
2828-================================================================================
2929-3030-YAML supports hex, octal, and binary number formats
3131-3232- $ test_formats number-formats ../data/formats/number_formats.yml
3333- JSON: number_formats: hex=255, octal=63, binary=10
3434- YAML: number_formats: hex=255, octal=63, binary=10
3535-3636-================================================================================
3737-COMMENTS
3838-================================================================================
3939-4040-YAML comments are ignored during parsing
4141-4242- $ test_formats comments ../data/formats/comments.yml
4343- YAML (with comments): host="localhost", port=8080, debug=true
4444-4545-================================================================================
4646-EMPTY DOCUMENTS
4747-================================================================================
4848-4949-Empty or null documents handled correctly
5050-5151- $ test_formats empty-doc ../data/formats/empty_doc.yml
5252- JSON: empty_document: ERROR: Expected string but found null
5353- File "-", line 1, characters 10-11:
5454- File "-": in member value of
5555- File "-", line 1, characters 0-11: Wrapper object
5656- YAML: empty_document: ERROR: Expected string but found null
5757- File "-":
5858- File "-": in member value of
5959- File "-": Wrapper object
6060-6161-================================================================================
6262-EXPLICIT TYPE TAGS
6363-================================================================================
6464-6565-Explicit YAML type tags (!!str, !!int, etc.)
6666-6767- $ test_formats explicit-tags ../data/formats/explicit_tags.yml
6868- YAML (with tags): data="123"
6969-7070-================================================================================
7171-ENCODING STYLES
7272-================================================================================
7373-7474-Compare Block vs Flow encoding styles
7575-7676- $ test_formats encode-styles
7777- YAML Block:
7878- name: test
7979- values:
8080- - 1
8181- - 2
8282- - 3
8383- nested:
8484- enabled: true
8585- count: 5
8686-8787- YAML Flow:
8888- {name: test, values: [1, 2, 3], nested: {enabled: true, count: 5}}
8989-9090-9191-================================================================================
9292-NEGATIVE TESTS - Format Compatibility
9393-================================================================================
9494-9595-Using literal string test with number codec should fail
9696-9797- $ test_formats number-formats ../data/formats/literal_string.yml
9898- JSON: number_formats: ERROR: Missing members in Numbers object:
9999- binary
100100- hex
101101- octal
102102- File "-", line 1, characters 0-100:
103103- YAML: number_formats: ERROR: Missing members in Numbers object:
104104- binary
105105- hex
106106- octal
107107- File "-":
-217
tests/cram/locations.t
···11-Location and Layout Preservation Tests with Yamlt
22-==================================================
33-44-This test suite validates the `locs` and `layout` options in the Yamlt decoder,
55-demonstrating how they affect error messages and metadata preservation.
66-77-================================================================================
88-ERROR MESSAGE PRECISION - locs option
99-================================================================================
1010-1111-The `locs` option controls whether source locations are preserved in error messages.
1212-When `locs=false` (default), errors show basic location info.
1313-When `locs=true`, errors show precise character positions.
1414-1515-Basic type error with and without locs
1616-1717- $ test_locations error-precision ../data/locations/type_error.yml
1818- === Without locs (default) ===
1919- Error message:
2020- String "not-a-number" does not parse to OCaml int value
2121- File "-":
2222- File "-": in member age of
2323- File "-": Person object
2424-2525- === With locs=true ===
2626- Error message:
2727- String "not-a-number" does not parse to OCaml int value
2828- File "-", line 2, characters 5-18:
2929- File "-", line 2, characters 0-3: in member age of
3030- File "-", line 1, characters 0-1: Person object
3131-3232-================================================================================
3333-NESTED ERROR LOCATIONS
3434-================================================================================
3535-3636-The `locs` option is especially useful for nested structures,
3737-showing exactly where deep errors occur.
3838-3939-Error in nested object field
4040-4141- $ test_locations nested-error ../data/locations/nested_error.yml
4242- === Without locs (default) ===
4343- Nested error:
4444- String "invalid-zip" does not parse to OCaml int value
4545- File "-":
4646- File "-": in member zip of
4747- File "-": Address object
4848- File "-": in member address of
4949- File "-": Employee object
5050-5151- === With locs=true ===
5252- Nested error:
5353- String "invalid-zip" does not parse to OCaml int value
5454- File "-", line 5, characters 7-19:
5555- File "-", line 5, characters 2-5: in member zip of
5656- File "-", line 3, characters 2-3: Address object
5757- File "-", line 2, characters 0-7: in member address of
5858- File "-", line 1, characters 0-1: Employee object
5959-6060-================================================================================
6161-ARRAY ELEMENT ERROR LOCATIONS
6262-================================================================================
6363-6464-The `locs` option pinpoints which array element caused an error.
6565-6666-Error at specific array index
6767-6868- $ test_locations array-error ../data/locations/array_error.yml
6969- === Without locs (default) ===
7070- Array error:
7171- String "not-a-number" does not parse to OCaml int value
7272- File "-":
7373- at index 2 of
7474- File "-": array<OCaml int>
7575- File "-": in member values of
7676- File "-": Numbers object
7777-7878- === With locs=true ===
7979- Array error:
8080- String "not-a-number" does not parse to OCaml int value
8181- File "-", lines 4-5, characters 4-2:
8282- at index 2 of
8383- File "-", line 2, characters 2-3: array<OCaml int>
8484- File "-", line 1, characters 0-6: in member values of
8585- File "-", line 1, characters 0-1: Numbers object
8686-8787-================================================================================
8888-FILE PATH IN ERROR MESSAGES
8989-================================================================================
9090-9191-The `file` parameter sets the file path shown in error messages.
9292-9393- $ test_locations file-path
9494- === Without file path ===
9595- Error:
9696- String "not-a-number" does not parse to OCaml int value
9797- File "-", line 2, characters 5-18:
9898- File "-", line 2, characters 0-3: in member age of
9999- File "-", line 1, characters 0-1: Person object
100100-101101- === With file path ===
102102- Error:
103103- String "not-a-number" does not parse to OCaml int value
104104- File "test.yml", line 2, characters 5-18:
105105- File "test.yml", line 2, characters 0-3: in member age of
106106- File "test.yml", line 1, characters 0-1: Person object
107107-108108-================================================================================
109109-MISSING FIELD ERROR LOCATIONS
110110-================================================================================
111111-112112-The `locs` option helps identify where fields are missing.
113113-114114- $ test_locations missing-field ../data/locations/missing_field.yml
115115- === Without locs ===
116116- Missing field:
117117- Missing member field_c in Complete object
118118- File "-":
119119-120120- === With locs=true ===
121121- Missing field:
122122- Missing member field_c in Complete object
123123- File "-", line 1, characters 0-1:
124124-125125-================================================================================
126126-LAYOUT PRESERVATION - layout option
127127-================================================================================
128128-129129-The `layout` option controls whether style information (block vs flow)
130130-is preserved in metadata for potential round-tripping.
131131-132132-Basic layout preservation
133133-134134- $ test_locations layout ../data/locations/simple.yml
135135- === Without layout (default) ===
136136- Decoded: host=localhost, port=8080
137137- Meta preserved: no
138138-139139- === With layout=true ===
140140- Decoded: host=localhost, port=8080
141141- Meta preserved: yes (style info available for round-tripping)
142142-143143-================================================================================
144144-ROUND-TRIPPING WITH LAYOUT
145145-================================================================================
146146-147147-With `layout=true` during decode and `format:Layout` during encode,
148148-the original YAML style can be preserved.
149149-150150-Flow style preservation
151151-152152- $ test_locations roundtrip ../data/locations/flow_style.yml
153153- === Original YAML ===
154154- items: [apple, banana, cherry]
155155-156156- === Decode without layout, re-encode ===
157157- items:
158158- - apple
159159- - banana
160160- - cherry
161161-162162- === Decode with layout=true, re-encode with Layout format ===
163163- items:
164164- - apple
165165- - banana
166166- - cherry
167167-168168-Block style preservation
169169-170170- $ test_locations roundtrip ../data/locations/block_style.yml
171171- === Original YAML ===
172172- items:
173173- - apple
174174- - banana
175175- - cherry
176176-177177- === Decode without layout, re-encode ===
178178- items:
179179- - apple
180180- - banana
181181- - cherry
182182-183183- === Decode with layout=true, re-encode with Layout format ===
184184- items:
185185- - apple
186186- - banana
187187- - cherry
188188-189189-================================================================================
190190-COMBINED OPTIONS - locs and layout together
191191-================================================================================
192192-193193-Both options can be used simultaneously for maximum information.
194194-195195- $ test_locations combined ../data/locations/valid_settings.yml
196196- === locs=false, layout=false (defaults) ===
197197- OK: timeout=30, retries=3
198198-199199- === locs=true, layout=false ===
200200- OK: timeout=30, retries=3 (with precise locations)
201201-202202- === locs=false, layout=true ===
203203- OK: timeout=30, retries=3 (with layout metadata)
204204-205205- === locs=true, layout=true (both enabled) ===
206206- OK: timeout=30, retries=3 (with locations and layout)
207207-208208-================================================================================
209209-SUMMARY OF OPTIONS
210210-================================================================================
211211-212212-locs option:
213213-214214-layout option:
215215-216216-Both options add metadata overhead, so only enable when needed.
217217-For production parsing where you only need the data, use defaults (both false).
-220
tests/cram/multidoc.t
···11-Multi-Document YAML Streams with Yamlt
22-========================================
33-44-This test suite validates multi-document YAML stream decoding using decode_all,
55-including error handling, location tracking, and JSON roundtripping.
66-77-================================================================================
88-BASIC MULTIDOC DECODING
99-================================================================================
1010-1111-Simple multi-document stream with person objects
1212-1313- $ test_multidoc simple ../data/multidoc/simple.yml
1414- Documents:
1515- [0] : Alice (age 30)
1616- [1] : Bob (age 25)
1717- [2] : Charlie (age 35)
1818-1919-Count documents in a stream
2020-2121- $ test_multidoc count ../data/multidoc/simple.yml
2222- Document count: 3
2323-2424-================================================================================
2525-ERROR HANDLING - MIXED VALID AND INVALID DOCUMENTS
2626-================================================================================
2727-2828-When some documents succeed and others fail, decode_all continues processing
2929-and returns results for each document individually.
3030-3131-Stream with one error in the middle
3232-3333- $ test_multidoc errors ../data/multidoc/mixed_errors.yml
3434- Document results:
3535- [0] OK: Alice (age 30)
3636- [1] ERROR: String "not-a-number" does not parse to OCaml int value
3737- File "-":
3838- File "-": in member age of
3939- File "-": Person object
4040- [2] OK: Charlie (age 35)
4141-4242-Summary statistics for mixed documents
4343-4444- $ test_multidoc summary ../data/multidoc/mixed_errors.yml
4545- Summary: 3 documents (2 ok, 1 error)
4646-4747-Stream where all documents fail
4848-4949- $ test_multidoc errors ../data/multidoc/all_errors.yml
5050- Document results:
5151- [0] ERROR: String "invalid1" does not parse to OCaml int value
5252- File "-":
5353- File "-": in member age of
5454- File "-": Person object
5555- [1] ERROR: String "invalid2" does not parse to OCaml int value
5656- File "-":
5757- File "-": in member age of
5858- File "-": Person object
5959- [2] ERROR: String "invalid3" does not parse to OCaml int value
6060- File "-":
6161- File "-": in member age of
6262- File "-": Person object
6363-6464-Summary for all-error stream
6565-6666- $ test_multidoc summary ../data/multidoc/all_errors.yml
6767- Summary: 3 documents (0 ok, 3 error)
6868-6969-================================================================================
7070-LOCATION TRACKING WITH locs=true
7171-================================================================================
7272-7373-Location tracking helps identify exactly where errors occur in each document
7474-of a multi-document stream.
7575-7676-Without locs (default) - basic error information
7777-7878- $ test_multidoc locations ../data/multidoc/mixed_errors.yml
7979- === Without locs (default) ===
8080- [0] OK
8181- [1] ERROR:
8282- String "not-a-number" does not parse to OCaml int value
8383- File "-":
8484- File "-": in member age of
8585- File "-": Person object
8686- [2] OK
8787-8888- === With locs=true ===
8989- [0] OK
9090- [1] ERROR:
9191- String "not-a-number" does not parse to OCaml int value
9292- File "test.yml", line 6, characters 5-18:
9393- File "test.yml", line 6, characters 0-3: in member age of
9494- File "test.yml", line 5, characters 0-1: Person object
9595- [2] OK
9696-9797-================================================================================
9898-MISSING FIELDS IN MULTIDOC
9999-================================================================================
100100-101101-Documents with missing required fields generate errors but don't stop
102102-processing of subsequent documents.
103103-104104- $ test_multidoc errors ../data/multidoc/missing_fields.yml
105105- Document results:
106106- [0] OK: Alice (age 30)
107107- [1] ERROR: Missing member age in Person object
108108- File "-":
109109- [2] OK: Charlie (age 35)
110110-111111-Summary of missing fields test
112112-113113- $ test_multidoc summary ../data/multidoc/missing_fields.yml
114114- Summary: 3 documents (2 ok, 1 error)
115115-116116-================================================================================
117117-JSON ROUNDTRIPPING
118118-================================================================================
119119-120120-Decode YAML multi-document streams and encode each document as JSON.
121121-This validates that the data model conversion is correct.
122122-123123-Simple documents to JSON
124124-125125- $ test_multidoc json ../data/multidoc/simple.yml
126126- JSON outputs:
127127- [0] {"name":"Alice","age":30}
128128- [1] {"name":"Bob","age":25}
129129- [2] {"name":"Charlie","age":35}
130130-131131-Nested objects to JSON
132132-133133- $ test_multidoc json ../data/multidoc/nested.yml
134134- JSON outputs:
135135- [0] {"name":"Alice","age":30,"address":{"street":"123 Main St","city":"Boston"}}
136136- [1] {"name":"Bob","age":25,"address":{"street":"456 Oak Ave","city":"Seattle"}}
137137- [2] {"name":"Charlie","age":35,"address":{"street":"789 Pine Rd","city":"Portland"}}
138138-139139-Arrays to JSON
140140-141141- $ test_multidoc json ../data/multidoc/arrays.yml
142142- JSON outputs:
143143- [0] [1,2,3]
144144- [1] ["apple","banana","cherry"]
145145- [2] [true,false,true]
146146-147147-Scalar values to JSON
148148-149149- $ test_multidoc json ../data/multidoc/scalars.yml
150150- JSON outputs:
151151- [0] "hello world"
152152- [1] 42
153153- [2] true
154154- [3] null
155155-156156-================================================================================
157157-NESTED OBJECTS IN MULTIDOC
158158-================================================================================
159159-160160-Test decoding complex nested structures across multiple documents.
161161-162162- $ test_multidoc nested ../data/multidoc/nested.yml
163163- Nested documents:
164164- [0] : Alice (age 30) from 123 Main St, Boston
165165- [1] : Bob (age 25) from 456 Oak Ave, Seattle
166166- [2] : Charlie (age 35) from 789 Pine Rd, Portland
167167-168168-================================================================================
169169-ARRAYS IN MULTIDOC
170170-================================================================================
171171-172172-Test decoding different array types across documents.
173173-174174- $ test_multidoc arrays ../data/multidoc/arrays.yml
175175- Array documents:
176176- [0] [1,2,3]
177177- [1] ["apple","banana","cherry"]
178178- [2] [true,false,true]
179179-180180-================================================================================
181181-SCALARS IN MULTIDOC
182182-================================================================================
183183-184184-Test decoding bare scalar values as documents.
185185-186186- $ test_multidoc scalars ../data/multidoc/scalars.yml
187187- Scalar documents:
188188- [0] "hello world"
189189- [1] 42
190190- [2] true
191191- [3] null
192192-193193-================================================================================
194194-EMPTY DOCUMENTS
195195-================================================================================
196196-197197-Empty or null documents in a stream are handled correctly.
198198-199199- $ test_multidoc json ../data/multidoc/empty_docs.yml
200200- JSON outputs:
201201- [0] {"name":"Alice","age":30}
202202- [1] null
203203- [2] {"name":"Charlie","age":35}
204204-205205-Count including empty documents
206206-207207- $ test_multidoc count ../data/multidoc/empty_docs.yml
208208- Document count: 3
209209-210210-================================================================================
211211-SUMMARY
212212-================================================================================
213213-214214-The decode_all function:
215215-- Processes all documents in a stream, not stopping on errors
216216-- Returns a sequence of Result values (Ok/Error for each document)
217217-- Supports all decode options: locs, layout, file, max_depth, max_nodes
218218-- Correctly handles document boundaries even when errors occur
219219-- Works with any Jsont codec (objects, arrays, scalars, etc.)
220220-- Can be used for JSON roundtripping and format conversion
-37
tests/cram/null_collections.t
···11-Null to Empty Collection Tests
22-================================
33-44-This test suite validates that yamlt treats null values as empty collections
55-when decoding into Array or Object types, providing a more user-friendly
66-YAML experience.
77-88-================================================================================
99-NULL AS EMPTY COLLECTION
1010-================================================================================
1111-1212-Test various forms of null decoding as empty arrays and objects
1313-1414- $ test_null_collections
1515- === Test 1: Explicit null as empty array ===
1616- Result: []
1717-1818- === Test 2: Tilde as empty array ===
1919- Result: []
2020-2121- === Test 3: Empty array syntax ===
2222- Result: []
2323-2424- === Test 4: Array with values ===
2525- Result: [1; 2; 3]
2626-2727- === Test 5: Explicit null as empty object ===
2828- Result: {timeout=30; retries=3}
2929-3030- === Test 6: Empty object syntax ===
3131- Result: {timeout=30; retries=3}
3232-3333- === Test 7: Object with values ===
3434- Result: {timeout=60; retries=5}
3535-3636- === Test 8: Nested null arrays ===
3737- Result: {name=test; items_count=0; tags_count=0}
-171
tests/cram/objects_codec.t
···11-Object Codec Tests with Yamlt
22-================================
33-44-This test suite validates object encoding/decoding with Jsont codecs in YAML,
55-and compares behavior with JSON.
66-77-Setup
88------
99-1010-1111-================================================================================
1212-SIMPLE OBJECTS
1313-================================================================================
1414-1515-Decode simple object with required fields
1616-1717- $ test_objects simple ../data/objects/simple.yml
1818- JSON: person: {name="Alice"; age=30}
1919- YAML: person: {name="Alice"; age=30}
2020-2121-================================================================================
2222-OPTIONAL FIELDS
2323-================================================================================
2424-2525-Object with all optional fields present
2626-2727- $ test_objects optional ../data/objects/optional_all.yml
2828- JSON: config: {host="localhost"; port=Some 8080; debug=Some true}
2929- YAML: config: {host="localhost"; port=Some 8080; debug=Some true}
3030-3131-Object with some optional fields missing
3232-3333- $ test_objects optional ../data/objects/optional_partial.yml
3434- JSON: config: {host="example.com"; port=Some 3000; debug=None}
3535- YAML: config: {host="example.com"; port=Some 3000; debug=None}
3636-3737-Object with only required field
3838-3939- $ test_objects optional ../data/objects/optional_minimal.yml
4040- JSON: config: {host="minimal.com"; port=None; debug=None}
4141- YAML: config: {host="minimal.com"; port=None; debug=None}
4242-4343-================================================================================
4444-DEFAULT VALUES
4545-================================================================================
4646-4747-Empty object uses all defaults
4848-4949- $ test_objects defaults ../data/objects/defaults_empty.yml
5050- JSON: settings: {timeout=30; retries=3; verbose=false}
5151- YAML: settings: {timeout=30; retries=3; verbose=false}
5252-5353-Object with partial fields uses defaults for missing ones
5454-5555- $ test_objects defaults ../data/objects/defaults_partial.yml
5656- JSON: settings: {timeout=60; retries=3; verbose=false}
5757- YAML: settings: {timeout=60; retries=3; verbose=false}
5858-5959-================================================================================
6060-NESTED OBJECTS
6161-================================================================================
6262-6363-Objects containing other objects
6464-6565- $ test_objects nested ../data/objects/nested.yml
6666- JSON: employee: {name="Bob"; address={street="123 Main St"; city="Springfield"; zip="12345"}}
6767- YAML: employee: {name="Bob"; address={street="123 Main St"; city="Springfield"; zip="12345"}}
6868-6969-================================================================================
7070-UNKNOWN MEMBER HANDLING
7171-================================================================================
7272-7373-Unknown members cause error by default
7474-7575- $ test_objects unknown-error ../data/objects/unknown_members.yml
7676- Unexpected success
7777-7878-Unknown members can be kept
7979-8080- $ test_objects unknown-keep ../data/objects/unknown_keep.yml
8181- JSON: flexible: {name="Charlie"; has_extra=true}
8282- YAML: flexible: {name="Charlie"; has_extra=true}
8383-8484-Unknown members are preserved during encoding roundtrip
8585-8686- $ test_objects unknown-keep-roundtrip ../data/objects/unknown_keep.yml
8787- Decoded: name="Charlie", extra={"extra1":"value1","extra2":"value2"}
8888- Encoded Block:
8989- name: Charlie
9090- extra1: value1
9191- extra2: value2
9292- Re-decoded: name="Charlie", extra={"extra1":"value1","extra2":"value2"}
9393- Roundtrip: OK (extra members preserved)
9494-9595-================================================================================
9696-OBJECT CASES (DISCRIMINATED UNIONS)
9797-================================================================================
9898-9999-Decode circle variant
100100-101101- $ test_objects cases ../data/objects/case_circle.yml
102102- JSON: shape: Circle{radius=5.50}
103103- YAML: shape: Circle{radius=5.50}
104104-105105-Decode rectangle variant
106106-107107- $ test_objects cases ../data/objects/case_rectangle.yml
108108- JSON: shape: ERROR: Missing member radius in Circle object
109109- File "-", line 1, characters 0-52:
110110- YAML: shape: ERROR: Missing member radius in Circle object
111111- File "-":
112112-113113-================================================================================
114114-ERROR HANDLING
115115-================================================================================
116116-117117-Missing required field produces error
118118-119119- $ test_objects missing-required ../data/objects/missing_required.yml
120120- Expected error: Missing member age in Required object
121121- File "-":
122122-123123-================================================================================
124124-ENCODING OBJECTS
125125-================================================================================
126126-127127-Encode objects to JSON and YAML formats
128128-129129- $ test_objects encode
130130- JSON: {"name":"Alice","age":30,"active":true}
131131- YAML Block:
132132- name: Alice
133133- age: 30
134134- active: true
135135- YAML Flow: {name: Alice, age: 30, active: true}
136136-137137-================================================================================
138138-NEGATIVE TESTS - Wrong File Types
139139-================================================================================
140140-141141-Attempting to decode an array file with an object codec should fail
142142-143143- $ test_objects simple ../data/arrays/int_array.yml
144144- JSON: person: ERROR: Missing members in Person object:
145145- age
146146- name
147147- File "-", line 1, characters 0-27:
148148- YAML: person: ERROR: Missing members in Person object:
149149- age
150150- name
151151- File "-":
152152-153153-Attempting to decode a scalar file with an object codec should fail
154154-155155- $ test_objects simple ../data/scalars/string_plain.yml
156156- JSON: person: ERROR: Missing members in Person object:
157157- age
158158- name
159159- File "-", line 1, characters 0-24:
160160- YAML: person: ERROR: Missing members in Person object:
161161- age
162162- name
163163- File "-":
164164-165165-Attempting to decode wrong object type (nested when expecting simple) should fail
166166-167167- $ test_objects simple ../data/objects/nested.yml
168168- JSON: person: ERROR: Missing member age in Person object
169169- File "-", line 1, characters 0-92:
170170- YAML: person: ERROR: Missing member age in Person object
171171- File "-":
-46
tests/cram/roundtrip_codec.t
···11-Roundtrip Encoding/Decoding Tests with Yamlt
22-=============================================
33-44-This test suite validates that data can be encoded and then decoded back
55-to the original value, ensuring no data loss in the roundtrip process.
66-77-================================================================================
88-SCALAR ROUNDTRIP
99-================================================================================
1010-1111-Encode and decode scalar types
1212-1313- $ test_roundtrip scalar
1414- JSON roundtrip: PASS
1515- YAML Block roundtrip: PASS
1616- YAML Flow roundtrip: PASS
1717-1818-================================================================================
1919-ARRAY ROUNDTRIP
2020-================================================================================
2121-2222-Encode and decode arrays including nested arrays
2323-2424- $ test_roundtrip array
2525- JSON array roundtrip: PASS
2626- YAML array roundtrip: PASS
2727-2828-================================================================================
2929-OBJECT ROUNDTRIP
3030-================================================================================
3131-3232-Encode and decode complex objects with nested structures
3333-3434- $ test_roundtrip object
3535- JSON object roundtrip: PASS
3636- YAML object roundtrip: PASS
3737-3838-================================================================================
3939-OPTIONAL FIELDS ROUNDTRIP
4040-================================================================================
4141-4242-Encode and decode optional and nullable fields
4343-4444- $ test_roundtrip optional
4545- Fatal error: exception Invalid_argument("option is None")
4646- [2]
-339
tests/cram/scalars_codec.t
···11-Scalar Type Resolution Tests with Yamlt Codec
22-==================================================
33-44-This test suite validates how YAML scalars are resolved based on the expected
55-Jsont type codec, and compares behavior with JSON decoding.
66-77-================================================================================
88-NULL RESOLUTION
99-================================================================================
1010-1111-Explicit null value
1212-1313- $ test_scalars null ../data/scalars/null_explicit.yml
1414- null_codec: null
1515-1616-Tilde as null
1717-1818- $ test_scalars null ../data/scalars/null_tilde.yml
1919- null_codec: null
2020-2121-Empty value as null
2222-2323- $ test_scalars null ../data/scalars/null_empty.yml
2424- null_codec: null
2525-2626-================================================================================
2727-BOOLEAN TYPE-DIRECTED RESOLUTION
2828-================================================================================
2929-3030-Plain "true" resolves to bool(true) with bool codec, but string "true" with string codec
3131-3232- $ test_scalars bool ../data/scalars/bool_true_plain.yml
3333- === Bool Codec ===
3434- JSON bool_codec
3535- decode: true
3636- YAML bool_codec
3737- decode: true
3838-3939- === String Codec ===
4040- JSON string_codec
4141- decode: ERROR: Expected string but found bool
4242- File "-", line 1, characters 10-11:
4343- File "-": in member value of
4444- File "-", line 1, characters 0-11: StringTest object
4545- YAML string_codec
4646- decode: "true"
4747-4848-Quoted "true" always resolves to string, even with bool codec
4949-5050- $ test_scalars bool ../data/scalars/bool_true_quoted.yml
5151- === Bool Codec ===
5252- JSON bool_codec
5353- decode: ERROR: Expected bool but found string
5454- File "-", line 1, characters 10-11:
5555- File "-": in member value of
5656- File "-", line 1, characters 0-11: BoolTest object
5757- YAML bool_codec
5858- decode: true
5959-6060- === String Codec ===
6161- JSON string_codec
6262- decode: "true"
6363- YAML string_codec
6464- decode: "true"
6565-6666-YAML-specific bool: "yes" resolves to bool(true)
6767-6868- $ test_scalars bool ../data/scalars/bool_yes.yml
6969- === Bool Codec ===
7070- JSON bool_codec
7171- decode: true
7272- YAML bool_codec
7373- decode: true
7474-7575- === String Codec ===
7676- JSON string_codec
7777- decode: ERROR: Expected string but found bool
7878- File "-", line 1, characters 10-11:
7979- File "-": in member value of
8080- File "-", line 1, characters 0-11: StringTest object
8181- YAML string_codec
8282- decode: "yes"
8383-8484-Plain "false" and "no" work similarly
8585-8686- $ test_scalars bool ../data/scalars/bool_false.yml
8787- === Bool Codec ===
8888- JSON bool_codec
8989- decode: false
9090- YAML bool_codec
9191- decode: false
9292-9393- === String Codec ===
9494- JSON string_codec
9595- decode: ERROR: Expected string but found bool
9696- File "-", line 1, characters 10-11:
9797- File "-": in member value of
9898- File "-", line 1, characters 0-11: StringTest object
9999- YAML string_codec
100100- decode: "false"
101101-102102- $ test_scalars bool ../data/scalars/bool_no.yml
103103- === Bool Codec ===
104104- JSON bool_codec
105105- decode: false
106106- YAML bool_codec
107107- decode: false
108108-109109- === String Codec ===
110110- JSON string_codec
111111- decode: ERROR: Expected string but found bool
112112- File "-", line 1, characters 10-11:
113113- File "-": in member value of
114114- File "-", line 1, characters 0-11: StringTest object
115115- YAML string_codec
116116- decode: "no"
117117-118118-================================================================================
119119-NUMBER RESOLUTION
120120-================================================================================
121121-122122-Integer values
123123-124124- $ test_scalars number ../data/scalars/number_int.yml
125125- JSON number_codec
126126- decode: 42
127127- YAML number_codec
128128- decode: 42
129129-130130-Float values
131131-132132- $ test_scalars number ../data/scalars/number_float.yml
133133- JSON number_codec
134134- decode: 3.1415899999999999
135135- YAML number_codec
136136- decode: 3.1415899999999999
137137-138138-Hexadecimal notation (YAML-specific)
139139-140140- $ test_scalars number ../data/scalars/number_hex.yml
141141- JSON number_codec
142142- decode: 42
143143- YAML number_codec
144144- decode: 42
145145-146146-Octal notation (YAML-specific)
147147-148148- $ test_scalars number ../data/scalars/number_octal.yml
149149- JSON number_codec
150150- decode: 42
151151- YAML number_codec
152152- decode: 42
153153-154154-Negative numbers
155155-156156- $ test_scalars number ../data/scalars/number_negative.yml
157157- JSON number_codec
158158- decode: -273.14999999999998
159159- YAML number_codec
160160- decode: -273.14999999999998
161161-162162-================================================================================
163163-SPECIAL FLOAT VALUES (YAML-specific)
164164-================================================================================
165165-166166-Positive infinity
167167-168168- $ test_scalars special-float ../data/scalars/special_inf.yml
169169- value: +Infinity
170170-171171-Negative infinity
172172-173173- $ test_scalars special-float ../data/scalars/special_neg_inf.yml
174174- value: -Infinity
175175-176176-Not-a-Number (NaN)
177177-178178- $ test_scalars special-float ../data/scalars/special_nan.yml
179179- value: NaN
180180-181181-================================================================================
182182-STRING RESOLUTION
183183-================================================================================
184184-185185-Plain strings
186186-187187- $ test_scalars string ../data/scalars/string_plain.yml
188188- JSON string_codec
189189- decode: "hello world"
190190- YAML string_codec
191191- decode: "hello world"
192192-193193-Quoted numeric strings stay as strings
194194-195195- $ test_scalars string ../data/scalars/string_quoted.yml
196196- JSON string_codec
197197- decode: "42"
198198- YAML string_codec
199199- decode: "42"
200200-201201-Empty strings
202202-203203- $ test_scalars string ../data/scalars/string_empty.yml
204204- JSON string_codec
205205- decode: ""
206206- YAML string_codec
207207- decode: ""
208208-209209-================================================================================
210210-TYPE MISMATCH ERRORS
211211-================================================================================
212212-213213-String when bool expected
214214-215215- $ test_scalars type-mismatch ../data/scalars/mismatch_string_as_bool.yml bool
216216- Expected error: Expected bool but found scalar hello
217217- File "-":
218218- File "-": in member value of
219219- File "-": BoolTest object
220220-221221-String when number expected
222222-223223- $ test_scalars type-mismatch ../data/scalars/mismatch_string_as_number.yml number
224224- Expected error: Expected number but found scalar not-a-number
225225- File "-":
226226- File "-": in member value of
227227- File "-": NumberTest object
228228-229229-Number when null expected
230230-231231- $ test_scalars type-mismatch ../data/scalars/mismatch_number_as_null.yml null
232232- Expected error: Expected null but found scalar 42
233233- File "-":
234234- File "-": in member value of
235235- File "-": NullTest object
236236-237237-================================================================================
238238-JSONT.ANY AUTO-RESOLUTION
239239-================================================================================
240240-241241-With Jsont.any, scalars are auto-resolved based on their content
242242-243243-Null auto-resolves to null
244244-245245- $ test_scalars any ../data/scalars/any_null.yml
246246- JSON any_codec
247247- decode: decoded
248248- YAML any_codec
249249- decode: decoded
250250-251251-Plain bool auto-resolves to bool
252252-253253- $ test_scalars any ../data/scalars/any_bool.yml
254254- JSON any_codec
255255- decode: decoded
256256- YAML any_codec
257257- decode: decoded
258258-259259-Number auto-resolves to number
260260-261261- $ test_scalars any ../data/scalars/any_number.yml
262262- JSON any_codec
263263- decode: decoded
264264- YAML any_codec
265265- decode: decoded
266266-267267-Plain string auto-resolves to string
268268-269269- $ test_scalars any ../data/scalars/any_string.yml
270270- JSON any_codec
271271- decode: decoded
272272- YAML any_codec
273273- decode: decoded
274274-275275-================================================================================
276276-ENCODING SCALARS
277277-================================================================================
278278-279279-Encoding bool values
280280-281281- $ test_scalars encode bool true
282282- JSON: {"value":true}
283283- YAML Block:
284284- value: true
285285- YAML Flow: {value: true}
286286-287287- $ test_scalars encode bool false
288288- JSON: {"value":false}
289289- YAML Block:
290290- value: false
291291- YAML Flow: {value: false}
292292-293293-Encoding numbers
294294-295295- $ test_scalars encode number 42.5
296296- JSON: {"value":42.5}
297297- YAML Block:
298298- value: 42.5
299299- YAML Flow: {value: 42.5}
300300-301301-Encoding strings
302302-303303- $ test_scalars encode string "hello world"
304304- JSON: {"value":"hello world"}
305305- YAML Block:
306306- value: hello world
307307- YAML Flow: {value: hello world}
308308-309309-Encoding null
310310-311311- $ test_scalars encode null ""
312312- JSON: {"value":null}
313313- YAML Block:
314314- value: null
315315- YAML Flow: {value: null}
316316-317317-================================================================================
318318-NEGATIVE TESTS - Wrong File Types
319319-================================================================================
320320-321321-Attempting to decode an object file with a scalar codec should fail
322322-323323- $ test_scalars string ../data/objects/simple.yml
324324- JSON string_codec
325325- decode: ERROR: Missing member value in StringTest object
326326- File "-", line 1, characters 0-28:
327327- YAML string_codec
328328- decode: ERROR: Missing member value in StringTest object
329329- File "-":
330330-331331-Attempting to decode an array file with a scalar codec should fail
332332-333333- $ test_scalars number ../data/arrays/int_array.yml
334334- JSON number_codec
335335- decode: ERROR: Missing member value in NumberTest object
336336- File "-", line 1, characters 0-27:
337337- YAML number_codec
338338- decode: ERROR: Missing member value in NumberTest object
339339- File "-":
···11-# Configuration file with comments
22-host: localhost # The server host
33-port: 8080 # The server port
44-# Enable debug mode for development
55-debug: true