···1111module Qmp = Qemu.Qmp
12121313let parse_json s =
1414- match Json.Value.of_string s with Ok json -> json | Error e -> failwith e
1414+ match Json.Value.of_string s with
1515+ | Ok json -> json
1616+ | Error e -> failwith (Json.Error.to_string e)
15171618let check_message_ok msg json =
1719 match Qmp.Message.of_string json with
···278280 let cmd = Qmp.Command.make ~id:"cookie#1" "query-name" in
279281 let json = Qmp.Command.to_json cmd in
280282 (* Roundtrip through Command codec *)
281281- match Json.Value.to_string json with
282282- | Ok s -> (
283283- match Json.of_string Qmp.Command.jsont s with
284284- | Ok cmd' ->
285285- (* Encode again and check it still contains our id *)
286286- let json' = Qmp.Command.to_json cmd' in
287287- let s' = Json.Value.to_string json' e in
288288- (* Both serializations should match *)
289289- Alcotest.(check string) "roundtrip" s s'
290290- | Error e -> Alcotest.failf "decode: %s" e)
291291- | Error e -> Alcotest.fail e
283283+ let s = Json.Value.to_string json in
284284+ match Json.of_string Qmp.Command.jsont s with
285285+ | Ok cmd' ->
286286+ (* Encode again and check it still contains our id *)
287287+ let json' = Qmp.Command.to_json cmd' in
288288+ let s' = Json.Value.to_string json' in
289289+ Alcotest.(check string) "roundtrip" s s'
290290+ | Error e -> Alcotest.failf "decode: %s" (Json.Error.to_string e)
292291293292let test_command_roundtrip () =
294293 let cmds =
···309308 in
310309 List.iter
311310 (fun cmd ->
312312- let json = Qmp.Command.to_json cmd in
313313- match Json.Value.to_string json with
314314- | Ok s -> (
315315- match Json.of_string Qmp.Command.jsont s with
316316- | Ok _ -> ()
317317- | Error e -> Alcotest.failf "roundtrip decode: %s" e)
318318- | Error e -> Alcotest.failf "roundtrip encode: %s" e)
311311+ let s = Json.Value.to_string (Qmp.Command.to_json cmd) in
312312+ match Json.of_string Qmp.Command.jsont s with
313313+ | Ok _ -> ()
314314+ | Error e ->
315315+ Alcotest.failf "roundtrip decode: %s" (Json.Error.to_string e))
319316 cmds
320317321318(* {1 Status parsing tests} *)
+13-12
test/test_qmp_protocol.ml
···1818module Qmp = Qemu.Qmp
19192020let parse_json s =
2121- match Json.Value.of_string s with Ok json -> json | Error e -> failwith e
2121+ match Json.Value.of_string s with
2222+ | Ok json -> json
2323+ | Error e -> failwith (Json.Error.to_string e)
22242325let encode_json json = Json.Value.to_string json
24262527(* {1 Version codec tests} *)
26282729let test_version_make_roundtrip () =
2828- (* make -> jsont encode -> jsont decode -> check fields *)
3030+ (* make -> json encode -> json decode -> check fields *)
2931 let v = Qmp.Version.make 9 0 0 "v9.0.0" in
3030- let json = Json.encode Qmp.Version.jsont v in
3131- match json with
3232+ match Json.encode Qmp.Version.jsont v with
3333+ | Error e -> Alcotest.failf "encode: %s" (Json.Error.to_string e)
3234 | Ok json -> (
3335 match Json.decode Qmp.Version.jsont json with
3436 | Ok v' ->
···3638 Alcotest.(check int) "minor" 0 v'.qemu.minor;
3739 Alcotest.(check int) "micro" 0 v'.qemu.micro;
3840 Alcotest.(check string) "package" "v9.0.0" v'.package
3939- | Error e -> Alcotest.failf "decode: %s" e)
4040- | Error e -> Alcotest.failf "encode: %s" e
4141+ | Error e -> Alcotest.failf "decode: %s" (Json.Error.to_string e))
41424243let test_version_high_numbers () =
4344 let v = Qmp.Version.make 99 12 3 "custom-build-2025" in
···5657 Alcotest.(check int) "minor" 2 v.qemu.minor;
5758 Alcotest.(check int) "micro" 5 v.qemu.micro;
5859 Alcotest.(check string) "package" "Debian 1:7.2+dfsg-7" v.package
5959- | Error e -> Alcotest.failf "decode: %s" e
6060+ | Error e -> Alcotest.failf "decode: %s" (Json.Error.to_string e)
60616162let test_version_extra_fields_ignored () =
6263 (* Jsont skip_unknown should tolerate extra fields *)
···6869 | Ok v ->
6970 Alcotest.(check int) "major" 8 v.qemu.major;
7071 Alcotest.(check string) "package" "" v.package
7171- | Error e -> Alcotest.failf "decode: %s" e
7272+ | Error e -> Alcotest.failf "decode: %s" (Json.Error.to_string e)
72737374(* {1 Greeting tests -- QEMU 9.0 spec vector} *)
7475···160161 (* Roundtrip through codec *)
161162 match Json.of_string Qmp.Command.jsont s with
162163 | Ok _ -> ()
163163- | Error e -> Alcotest.failf "roundtrip: %s" e
164164+ | Error e -> Alcotest.failf "roundtrip: %s" (Json.Error.to_string e)
164165165166let test_command_with_id_args () =
166167 let args = parse_json {|{"protocol": "tcp", "hostname": "localhost"}|} in
···169170 let s = encode_json json in
170171 match Json.of_string Qmp.Command.jsont s with
171172 | Ok _ -> ()
172172- | Error e -> Alcotest.failf "roundtrip: %s" e
173173+ | Error e -> Alcotest.failf "roundtrip: %s" (Json.Error.to_string e)
173174174175let test_command_blockdev_add () =
175176 (* More complex arguments -- blockdev-add with nested config *)
···182183 let s = encode_json json in
183184 match Json.of_string Qmp.Command.jsont s with
184185 | Ok _ -> ()
185185- | Error e -> Alcotest.failf "roundtrip: %s" e
186186+ | Error e -> Alcotest.failf "roundtrip: %s" (Json.Error.to_string e)
186187187188(* {1 Status run_state string roundtrip} *)
188189···302303 | Ok ts ->
303304 Alcotest.(check int) "seconds" 1267040730 ts.seconds;
304305 Alcotest.(check int) "microseconds" 682951 ts.microseconds
305305- | Error e -> Alcotest.failf "decode: %s" e
306306+ | Error e -> Alcotest.failf "decode: %s" (Json.Error.to_string e)
306307307308(* {1 Event data extraction} *)
308309