1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
(* Js_of_ocaml tests
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2022 Hugo Heuzard
* Laboratoire PPS - CNRS Université Paris Diderot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
open Js_of_ocaml
let round_trip x =
let s = Json.output x in
let s1 = Js.to_bytestring s in
let s2 =
let old = Json.use_native_stringify () in
Json.set_use_native_stringify false;
let s = Json.output x in
Json.set_use_native_stringify old;
Js.to_bytestring s
in
Printf.printf "%s\n" s1;
if s1 <> s2 then Printf.printf "Json.output mismatch: %s vs %s\n" s1 s2;
(* Other direction of the round-trip (unmarshalling from JSON) is only
available with js_of_ocaml *)
match Sys.backend_type with
| Other "js_of_ocaml" when Json.use_native_stringify () ->
let y = Json.unsafe_input s in
if not (x = y) then Printf.printf "not invariant by round-trip\n"
| _ -> ()
let%expect_test _ =
round_trip 123L;
[%expect {|
[255,123,0,0] |}];
round_trip "asd";
[%expect {|
"asd" |}];
round_trip "\000\255\254";
[%expect {| "\u0000" |}];
round_trip (2, 3);
round_trip (2., 3.);
round_trip (2.2, 3.3);
[%expect {|
[0,2,3]
[0,2,3]
[0,2.2,3.3]
|}];
round_trip [| 1.; 2.; 3. |];
[%expect {| [254,1,2,3] |}];
round_trip 2n;
[%expect {| 2 |}]
let%expect_test "JavaScript object" =
let x =
object%js
val x = 1
val y = Js.string "abcd"
end
in
let s = Json.output x in
let s1 = Js.to_string s in
Printf.printf "%s\n" s1;
[%expect {| {"x":1,"y":"abcd"} |}]
|