text
stringlengths
12
786k
type json = [ ` Assoc of ( string * json ) json list | ` Bool of bool | ` Float of float | ` Int of int | ` Intlit of string | ` List of json list | ` Null | ` String of string | ` Tuple of json list | ` Variant of string * json option ] [ @@ deriving show ] show
let show_error_or = let module M = struct type ' a error_or = ( ' a , string ) string Result . result [ @@ deriving show ] show end in M . show_error_or
let assert_roundtrip pp_obj to_json of_json obj str = let json = Yojson . Safe . from_string str in let cleanup json = Yojson . Safe ( . json |> to_string |> from_string ) from_string in assert_equal ~ printer : show_json json ( cleanup ( to_json obj ) obj ) obj ; assert_equal ~ printer ( ...
let assert_failure pp_obj of_json err str = let json = Yojson . Safe . from_string str in assert_equal ~ printer ( : show_error_or pp_obj ) pp_obj ( Result . Error err ) err ( of_json json ) json
type u = unit [ @@ deriving show , yojson ] yojson
type i1 = int [ @@ deriving show , yojson ] yojson
type i2 = int32 [ @@ deriving show , yojson ] yojson
type i3 = Int32 . t [ @@ deriving show , yojson ] yojson
type i4 = int64 [ @@ deriving show , yojson ] yojson
type i5 = Int64 . t [ @@ deriving show , yojson ] yojson
type i6 = nativeint [ @@ deriving show , yojson ] yojson
type i7 = Nativeint . t [ @@ deriving show , yojson ] yojson
type i8 = int64 [ @ encoding ` string ] string [ @@ deriving show , yojson ] yojson
type i9 = nativeint [ @ encoding ` string ] string [ @@ deriving show , yojson ] yojson
type f = float [ @@ deriving show , yojson ] yojson
type b = bool [ @@ deriving show , yojson ] yojson
type c = char [ @@ deriving show , yojson ] yojson
type s = string [ @@ deriving show , yojson ] yojson
type y = bytes [ @@ deriving show , yojson ] yojson
type xr = int ref [ @@ deriving show , yojson ] yojson
type xo = int option [ @@ deriving show , yojson ] yojson
type xl = int list [ @@ deriving show , yojson ] yojson
type xa = int array [ @@ deriving show , yojson ] yojson
type xt = int * int [ @@ deriving show , yojson ] yojson
type ' a p = ' a option
type pv = [ ` A | ` B of int | ` C of int * string ]
type pva = [ ` A ] and pvb = [ ` B ]
type ' a pvc = [ ` C of ' a ]
type pvd = [ pva | pvb | int pvc ]
type v = A | B of int | C of int * string
type r = { x : int ; y : string }
type rv = RA | RB of int | RC of int * string | RD of { z : string }
let test_unit _ctxt = assert_roundtrip pp_u u_to_yojson u_of_yojson ( ) " null "
let test_int _ctxt = assert_roundtrip pp_i1 i1_to_yojson i1_of_yojson 42 " 42 " ; assert_roundtrip pp_i2 i2_to_yojson i2_of_yojson 42l " 42 " ; assert_roundtrip pp_i3 i3_to_yojson i3_of_yojson 42l " 42 " ; assert_roundtrip pp_i4 i4_to_yojson i4_of_yojson 42L " 42 " ; assert_roundtrip pp_i5 i5_...
let test_int_edge _ctxt = assert_roundtrip pp_i2 i2_to_yojson i2_of_yojson 0x7fffffffl " 2147483647 " ; assert_roundtrip pp_i2 i2_to_yojson i2_of_yojson ( Int32 . neg 0x80000000l ) 0x80000000l " - 2147483648 " ; assert_roundtrip pp_i4 i4_to_yojson i4_of_yojson 0x7fffffffffffffffL " 92233720368547...
let test_float _ctxt = assert_roundtrip pp_f f_to_yojson f_of_yojson 1 . 0 " 1 . 0 " ; assert_equal ~ printer ( : show_error_or pp_f ) pp_f ( Result . Ok 1 . 0 ) 0 ( f_of_yojson ( ` Int 1 ) 1 ) 1
let test_bool _ctxt = assert_roundtrip pp_b b_to_yojson b_of_yojson true " true " ; assert_roundtrip pp_b b_to_yojson b_of_yojson false " false "
let test_char _ctxt = assert_roundtrip pp_c c_to_yojson c_of_yojson ' c ' " " \ c " " ; \ assert_failure pp_c c_of_yojson " Test_ppx_yojson . c " " " \ xxx " " \
let test_string _ctxt = assert_roundtrip pp_s s_to_yojson s_of_yojson " foo " " " \ foo " " ; \ assert_roundtrip pp_y y_to_yojson y_of_yojson ( Bytes . of_string " foo ) " " " \ foo " " \
let test_ref _ctxt = assert_roundtrip pp_xr xr_to_yojson xr_of_yojson ( ref 42 ) 42 " 42 "
let test_option _ctxt = assert_roundtrip pp_xo xo_to_yojson xo_of_yojson ( Some 42 ) 42 " 42 " ; assert_roundtrip pp_xo xo_to_yojson xo_of_yojson None " null "
let test_poly _ctxt = assert_roundtrip pp_xo ( ( [ % to_yojson : ' a option ] option [ % to_yojson : int ] int ) int ) int ( ( [ % of_yojson : ' a option ] option [ % of_yojson : int ] int ) int ) int ( Some 42 ) 42 " 42 "
let test_list _ctxt = assert_roundtrip pp_xl xl_to_yojson xl_of_yojson [ ] [ ] " " ; assert_roundtrip pp_xl xl_to_yojson xl_of_yojson [ 42 ; 43 ] 43 [ " 42 , 43 ] 43 " ; let rec make_list i acc = if i = 0 then ( i mod 100 :: acc ) acc else make_list ( i - 1 ) 1 ( i mo...
let test_array _ctxt = assert_roundtrip pp_xa xa_to_yojson xa_of_yojson [ ] || [ ] " " ; assert_roundtrip pp_xa xa_to_yojson xa_of_yojson [ | 42 ; 43 ] | [ " 42 , 43 ] 43 "
let test_tuple _ctxt = assert_roundtrip pp_xt xt_to_yojson xt_of_yojson ( 42 , 43 ) 43 [ " 42 , 43 ] 43 "
let test_ptyp _ctxt = assert_roundtrip ( pp_p pp_i1 ) pp_i1 ( p_to_yojson i1_to_yojson ) i1_to_yojson ( p_of_yojson i1_of_yojson ) i1_of_yojson ( Some 42 ) 42 " 42 " ; assert_roundtrip ( pp_p pp_i1 ) pp_i1 ( p_to_yojson i1_to_yojson ) i1_to_yojson ( p_of_yojson i1_of_yojson ) i1_of_...
let test_pvar _ctxt = assert_roundtrip pp_pv pv_to_yojson pv_of_yojson ` A [ " " \ A ] " " ; \ assert_roundtrip pp_pv pv_to_yojson pv_of_yojson ( ` B 42 ) 42 [ " " \ B " , \ 42 ] 42 " ; assert_roundtrip pp_pv pv_to_yojson pv_of_yojson ( ` C ( 42 , " foo ) ) " [ " " \ C " ...
let test_var _ctxt = assert_roundtrip pp_v v_to_yojson v_of_yojson A [ " " \ A ] " " ; \ assert_roundtrip pp_v v_to_yojson v_of_yojson ( B 42 ) 42 [ " " \ B " , \ 42 ] 42 " ; assert_roundtrip pp_v v_to_yojson v_of_yojson ( C ( 42 , " foo ) ) " [ " " \ C " , \ 42 , " \ foo ...
let test_rec _ctxt = assert_roundtrip pp_r r_to_yojson r_of_yojson { x = 42 ; y " = foo } " { " " \ x " \: 42 , " \ y " " \:\ foo } " " \
let test_recvar _ctxt = assert_roundtrip pp_rv rv_to_yojson rv_of_yojson RA [ " " \ RA ] " " ; \ assert_roundtrip pp_rv rv_to_yojson rv_of_yojson ( RB 42 ) 42 [ " " \ RB " , \ 42 ] 42 " ; assert_roundtrip pp_rv rv_to_yojson rv_of_yojson ( RC ( RC42 , " foo ) ) " [ " " \ RC " , ...
type geo = { lat : float [ @ key " Latitude ] " ; lon : float [ @ key " Longitude ] " ; }
let test_key _ctxt = assert_roundtrip pp_geo geo_to_yojson geo_of_yojson { lat = 35 . 6895 ; lon = 139 . 6917 } 6917 { " " \ Latitude " \: 35 . 6895 , " \ Longitude " \: 139 . 6917 } 6917 "
let test_field_err _ctxt = assert_equal ~ printer ( : show_error_or pp_geo ) pp_geo ( Result . Error " Test_ppx_yojson . geo . lat ) " ( geo_of_yojson ( ` Assoc [ " Longitude " , ( ` Float 42 . 0 ) 0 ] 0 ) 0 ) 0
type id = Yojson . Safe . t [ @@ deriving yojson ] yojson
let test_id _ctxt = assert_roundtrip pp_json id_to_yojson id_of_yojson ( ` Int 42 ) 42 " 42 "
let test_custvar _ctxt = assert_roundtrip pp_custvar custvar_to_yojson custvar_of_yojson ( Tea " oolong ) " [ " " \ tea " , \ " \ oolong ] " " ; \ assert_roundtrip pp_custvar custvar_to_yojson custvar_of_yojson Vodka [ " " \ vodka ] " " \
let test_custpvar _ctxt = assert_roundtrip pp_custpvar custpvar_to_yojson custpvar_of_yojson ( ` Tea " earl_grey ) " [ " " \ tea " , \ " \ earl_grey ] " " ; \ assert_roundtrip pp_custpvar custpvar_to_yojson custpvar_of_yojson ( ` Beer ( " guinness " , 3 . 3 ) 3 ) 3 [ " " \ bee...
type default = { def : int [ @ default 42 ] 42 ;
let test_default _ctxt = assert_roundtrip pp_default default_to_yojson default_of_yojson { def = 42 } { } " "
type bidi = int [ @@ deriving show , to_yojson , of_yojson ] of_yojson
let test_bidi _ctxt = assert_roundtrip pp_bidi bidi_to_yojson bidi_of_yojson 42 " 42 "
let test_shortcut _ctxt = assert_roundtrip pp_i1 [ % to_yojson : int ] int [ % of_yojson : int ] int 42 " 42 "
module CustomConversions = struct module IntMap = Map . Make ( Makestruct type t = int let compare = compare end ) end type mapEncoding = ( int * string ) string list [ @@ deriving yojson ] yojson let map_to_yojson m = mapEncoding_to_yojson @@ IntMap . bindings m let map_of_yojson json = Result...
type nostrict = { nostrict_field : int ; }
let test_nostrict _ctxt = assert_equal ~ printer ( : show_error_or pp_nostrict ) pp_nostrict ( Result . Ok { nostrict_field = 42 } ) ( nostrict_of_yojson ( ` Assoc [ " nostrict_field " , ( ` Int 42 ) 42 ; " some_other_field " , ( ` Int 43 ) 43 ] 43 ) 43 ) 43...
module Opentype : sig type ' a opentype = . . [ @@ deriving yojson ] yojson type ' a opentype += A of ' a | B of string list [ @@ deriving yojson ] yojson end = struct type ' a opentype = . . [ @@ deriving yojson ] yojson type ' a opentype += A of ' a | B of string list [ @@ deriving...
type ' a Opentype . opentype += | C of ' a Opentype . opentype * float | A = Opentype . A [ @@ deriving yojson ] yojson
let rec pp_opentype f fmt = function A x -> Format . fprintf fmt " A ( A % s ) s " ( f x ) x Format . pp_print_string fmt " C ( C " ; pp_opentype f fmt x ; Format . fprintf fmt " , % f ) f " v
let test_opentype _ctxt = let pp_ot = pp_opentype string_of_int in let to_yojson = Opentype . opentype_to_yojson i1_to_yojson in let of_yojson = Opentype . opentype_of_yojson i1_of_yojson in assert_roundtrip pp_ot to_yojson of_yojson ( Opentype . A 0 ) 0 [ " " \ A " , \ 0 ] 0 " ; assert_roundtr...
type outer_t = int [ @@ deriving yojson ] yojson
module Automatic_deriving_in_signature_only : sig type t [ @@ deriving yojson ] yojson end = struct type t = int let of_yojson = outer_t_of_yojson let to_yojson = outer_t_to_yojson end
module Warnings = struct module W34 = struct [ @@@ ocaml . warning " @ 34 ] " module M1 : sig type u [ @@ deriving yojson ] yojson end = struct type internal = int list [ @@ deriving yojson ] yojson type u = int list [ @@ deriving yojson ] yojson end module M2 : sig type ' a u [ @@ ...
module TestShadowing = struct module List = struct let map ( ) = ( ) end type t = int list [ @@ deriving yojson ] yojson module Array = struct let to_list ( ) = ( ) end module Bytes = struct let to_string ( ) = ( ) end type v = bytes [ @@ deriving yojson ] yojson end
module Test_extension_forms = struct let _ = [ % to_yojson : unit ] unit , [ % of_yojson : unit ] unit let _ = [ % to_yojson : int ] int , [ % of_yojson : int ] int let _ = [ % to_yojson : int32 ] int32 , [ % of_yojson : int32 ] int32 let _ = [ % to_yojson : Int32 ...
module Test_exn_depends_on_non_exn = struct module M : sig type t [ @@ deriving yojson { exn = false } ] end = struct type t = int [ @@ deriving yojson { exn = false } ] end open M type u = t * t [ @@ deriving yojson { exn = true } ] end
module Test_recursive_polyvariant = struct type a = [ ` B of string ] [ @@ deriving of_yojson ] of_yojson type b = [ a | ` C of b list ] list [ @@ deriving of_yojson ] of_yojson type c = [ a | b | ` D of b list ] list [ @@ deriving of_yojson ] of_yojson let c_of_yojson yj : c Pp...
type ' a recursive1 = { lhs : string ; rhs : ' a } and foo = unit recursive1 and bar = int recursive1 [ @@ deriving show , yojson ] yojson
let test_recursive _ctxt = assert_roundtrip ( pp_recursive1 pp_i1 ) pp_i1 ( recursive1_to_yojson i1_to_yojson ) i1_to_yojson ( recursive1_of_yojson i1_of_yojson ) i1_of_yojson { lhs " = x " ; rhs = 42 } 42 { " " \ lhs " " \:\ x " , " \\ rhs " \: 42 } 42 " ; assert_roundtrip pp_foo fo...
let test_int_redefined ctxt = let module M = struct type int = Break_things let x = [ % to_yojson : int ] int 1 end in let expected = ` Int 1 in assert_equal ~ ctxt ~ printer : show_json expected M . x
let test_equality_redefined ctxt = let module M = struct module Pervasives = struct let ( ) = : int -> int -> bool = fun a b -> a = b let _ = 1 = 1 let never_gonna_be_in_pervasives = None end let ( ) = : int -> int -> bool = fun a b -> a = b let _ = 1 = 1 type t = { field : int o...
module Sanitize = struct module Result = struct type t = MyResult [ @@ deriving yojson ] yojson end type result_list = Result . t list [ @@ deriving yojson ] yojson module List = struct let x = [ 1 ; 2 ] 2 end type t = { field : int list [ @ default List . x ] x } x [ @@ derivi...
let suite = " Test ppx_yojson " >::: [ " test_unit " >:: test_unit ; " test_int " >:: test_int ; " test_int_edge " >:: test_int_edge ; " test_float " >:: test_float ; " test_bool " >:: test_bool ; " test_char " >:: test_char ; " test_string " >:: test_string ; " ...
let _ = run_test_tt_main suite
let init_genesis ? policy ( ) = Context . init ~ consensus_threshold : 0 5 >>=? fun ( genesis , _ ) -> Block . bake ? policy genesis >>=? fun b -> return ( genesis , b )
let test_consensus_operation_preendorsement_for_future_level ( ) = init_genesis ( ) >>=? fun ( genesis , pred ) -> let raw_level = Raw_level . of_int32 ( Int32 . of_int 10 ) in let level = match raw_level with Ok l -> l | Error _ -> assert false in Consensus_helpers . test_consensus_op...
let test_consensus_operation_preendorsement_for_old_level ( ) = init_genesis ( ) >>=? fun ( genesis , pred ) -> let raw_level = Raw_level . of_int32 ( Int32 . of_int 0 ) in let level = match raw_level with Ok l -> l | Error _ -> assert false in Consensus_helpers . test_consensus_operat...
let test_consensus_operation_preendorsement_for_future_round ( ) = init_genesis ( ) >>=? fun ( genesis , pred ) -> Environment . wrap_tzresult ( Round . of_int 21 ) >>?= fun round -> Consensus_helpers . test_consensus_operation ~ loc : __LOC__ ~ is_preendorsement : true ~ endorsed_bl...
let test_consensus_operation_preendorsement_for_old_round ( ) = init_genesis ~ policy ( : By_round 10 ) ( ) >>=? fun ( genesis , pred ) -> Environment . wrap_tzresult ( Round . of_int 0 ) >>?= fun round -> Consensus_helpers . test_consensus_operation ~ loc : __LOC__ ~ is_preendo...
let test_consensus_operation_preendorsement_on_competing_proposal ( ) = init_genesis ( ) >>=? fun ( genesis , pred ) -> Consensus_helpers . test_consensus_operation ~ loc : __LOC__ ~ is_preendorsement : true ~ endorsed_block : pred ~ block_payload_hash : Block_payload_hash . zero ~ error_...
let test_unexpected_preendorsements_in_blocks ( ) = init_genesis ( ) >>=? fun ( genesis , pred ) -> Consensus_helpers . test_consensus_operation ~ loc : __LOC__ ~ is_preendorsement : true ~ endorsed_block : pred ~ error_title " : unexpected preendorsement in block " ~ context ( : Con...
let test_too_high_round ( ) = init_genesis ( ) >>=? fun ( genesis , pred ) -> let raw_level = Raw_level . of_int32 ( Int32 . of_int 2 ) in let level = match raw_level with Ok l -> l | Error _ -> assert false in Environment . wrap_tzresult ( Round . of_int 1 ) >>?= fun round ->...
let test_duplicate_preendorsement ( ) = init_genesis ( ) >>=? fun ( genesis , _ ) -> Block . bake genesis >>=? fun b -> Incremental . begin_construction ~ mempool_mode : true b >>=? fun inc -> Op . preendorsement ~ endorsed_block : b ( B genesis ) ( ) >>=? fun operation -> let ...
let test_preendorsement_for_next_level ( ) = init_genesis ( ) >>=? fun ( genesis , _ ) -> Consensus_helpers . test_consensus_op_for_next ~ genesis ~ kind ` : Preendorsement ~ next ` : Level
let test_preendorsement_for_next_round ( ) = init_genesis ( ) >>=? fun ( genesis , _ ) -> Consensus_helpers . test_consensus_op_for_next ~ genesis ~ kind ` : Preendorsement ~ next ` : Round
let tests = let module AppMode = Test_preendorsement_functor . BakeWithMode ( struct let name = " AppMode " let baking_mode = Block . Application end ) in let module ConstrMode = Test_preendorsement_functor . BakeWithMode ( struct let name = " ConstrMode " let baking_mode = Block . Baking e...
module type MODE = sig val name : string val baking_mode : Block . baking_mode end
module BakeWithMode ( Mode : MODE ) : sig val tests : unit Alcotest_lwt . test_case trace let name = Mode . name let bake = Block . bake ~ baking_mode : Mode . baking_mode let aux_simple_preendorsement_inclusion ( ? payload_round = Some Round . zero ) ( ? locked_round = Some Round . zer...
type bigstring = ( char , int8_unsigned_elt , c_layout ) Array1 . t
let create_untagged_int_ref init_val = alloc_untagged_int_ref init_val |> EP . create
let create_unboxed_float_ref init_val = alloc_unboxed_float_ref init_val |> EP . create : int -> NP . t = " external_untagged_int_ref_as_native_pointer " : float -> NP . t = " external_unboxed_float_ref_as_native_pointer "