text
stringlengths
0
601k
module Recursive_with_indirect_base_case = Recursive_with_indirect_base_case let module Recursive_with_indirect_base_case ' = struct type t = Recursive_with_indirect_base_case . t = { children : t list } [ @@ deriving compare , hash , sexp_of ] let examples = List . init 3 ~ f ( : fun n -> { children = List . init n ~ ...
module Mutually_recursive = Mutually_recursive let module Mutually_recursive ' = struct type expr = Mutually_recursive . expr = | Constant of int64 | Operator of op | Application of expr * args and op = [ ` plus | ` minus | ` abs ] and args = expr list [ @@ deriving compare , hash , sexp_of ] end in test ~ shrinker ` :...
module Extensions = Extensions let module Extensions ' = struct type t = [ ` A | ` B of bool * unit option ] [ @@ deriving compare , enumerate , sexp_of ] end in test ( module Extensions ) ( m_all ( module Extensions ' ) ) ; [ % expect { | ( generator exhaustive ) ( observer transparent ) ( shrinker ( ( ( B ( false ( (...
module Escaped = Escaped let module Escaped ' = struct type t = int * char * bool option [ @@ deriving compare , sexp_of ] let examples = List . concat_map [ 1 ; 2 ] ~ f ( : fun int -> List . concat_map [ ' a ' ; ' b ' ] ~ f ( : fun string -> List . concat_map [ None ; Some true ] ~ f ( : fun bool_option -> [ int , str...
module Wildcard = Wildcard let module Instance = struct include Wildcard ( ( val m_bool ) ) let compare = [ % compare : bool list ] let sexp_of_t = [ % sexp_of : bool list ] let examples = let module T = ( val m_list m_bool ) in T . examples ; ; end in test ~ cr : Comment ( module Instance ) ( module Instance ) ; [ % e...
module Attribute_override = Attribute_override let module Attribute_override ' = struct type t = Attribute_override . t = | Null | Text of string | Number of float [ @@ deriving compare , sexp_of ] let examples = [ Null ; Text " a " ; Number 1 . ] end in test ( module Attribute_override ) ( module Attribute_override ' ...
module Attribute_override_recursive = Attribute_override_recursive let module Attribute_override_recursive ' = struct type t = Attribute_override_recursive . t = | Leaf | Node1 of t * int64 * t | Node2 of t * int64 * t * int64 * t [ @@ deriving compare , sexp_of ] let examples = [ Leaf ; Node1 ( Leaf , 0L , Leaf ) ; No...
module Deriving_from_wildcard = Deriving_from_wildcard let module Transparent = struct type t = int Deriving_from_wildcard . transparent [ @@ deriving quickcheck ] let compare = [ % compare : string ] let sexp_of_t = [ % sexp_of : string ] let examples = [ " " ; " a " ] end in test ( module Transparent ) ( module Trans...
let ( ) = ( match % pcre " " % with _ -> ( ) ) ; ( match % pcre " " % with s -> assert ( s = " " ) ) ; % ( function % pcre _ -> ( ) ) " " ; % ( function % pcre s -> assert ( s = " " ) ) % " " %
let test1 = ( function % pcre | { ( |^?< k . ) >*: ( *?< v . ) } >+?$| -> ` Attr ( k , v ) | { |^# ( ?< comment . ) } >+$| -> ` Comment comment | { ( ( |^?< last [ ] ) { >@% 2 } ) { 0 , 2 } } $| -> ` Even_sigils last | { [ ] } |^@%| -> ` Odd_sigils | _ -> ` Unknown )
let ( ) = assert ( test1 " x : 1 " = ` Attr ( " x " , Some " 1 " ) ) ; assert ( test1 " # Kommentar " = ` Comment " Kommentar " ) ; assert ( test1 " " = ` Even_sigils None ) ; assert ( test1 " " %%%@ = ` Even_sigils ( Some " " ) ) ; @ assert ( test1 " " %%@ = ` Odd_sigils )
let last_elt s = let n = String . length s in assert ( s . [ n - 1 ] = ' ; ' ) ; let i = try String . rindex_from s ( n - 2 ) ' ; ' + 1 with Not_found -> 0 in String . sub s i ( n - i - 1 )
let rec test2 s = ( match % pcre s with | { } |^<>$| -> assert ( s = " " ) <> | { ( |^<?< x [ ] ) } >^<>+>$| -> assert ( s = " " < ^ x ^ " " ) > | { ( |^<?< x [ ] ) ( >^<>+><?< y [ ] ) } >^<>+>$| -> assert ( s = " " < ^ x ^ " " >< ^ y ^ " " ) > | { ( ( |^?< elt [ ; ] ) ; ) } >^<>+$| -> assert ( elt = last_elt s ) | { [...
let test3 s = ( match % pcre s with | { | no ( is ) ( ( e ) ) ( ?< is > is ) ( g ( oo ) d ) } | -> assert ( is = " is " ) | { |?< s ' ( ) [ >& a - zA - Z0 - 9_ ] ( ; ) } -+| -> let i , j = String . index s ' ' , & String . rindex s ' ; ' in assert ( s ' = String . sub s i ( j - i + 1 ) ) | { | m ( o ) + re re ( gular )...
let test4 = function % pcre | { ( |?< x [ ] [ [ >-+?: digit ] ] . [ [ :+: digit ] ] ) } :*| -> [ x ] | { ( |?< x ( > abc ) ) [ [ : space ] ] ( :*?< y ( > xyz ) ) } | -> [ x ; y ] | _ -> assert false
let test5 = function % pcre | { . ( |^?< x . ) } >+| -> ( match % pcre x with | { . ( |^?< y . ) } >+| -> ( match % pcre y with | { . ( |^?< z . ) } >+| -> ( x , y , z ) | _ -> assert false ) | _ -> assert false ) | _ -> assert false
let ( ) = test2 " " ; <> test2 " < a " ; > test2 " < ab " ; > test2 " < a >< b " ; > test2 " < ab >< cde " ; > test2 " a ; " ; test2 " a ; b ; c ; d ; " ; test2 " < a ; b " ; > test2 " Xx { { -- a ; b ; c ; } } -- yY . " ; test2 " one " ; test2 " two " ; test3 " - + & nbsp ; + " ; - test3 " catch - all " ; assert ( tes...
module F ( M : Map . OrderedType ) = struct let f x = ( match % pcre x with | { ( |#?< space >\ s ) ( ??< comment . ) } >*| -> Some ( space <> None , comment ) | _ -> None ) end
let r = ref false | { } |^$| -> r := true | _ -> assert false )
module Util = struct [ %% declare_roles a , b , c , d ] [ %% declare_labels msg , left , right , middle , ping , pong , fini ] end
let test_projection_success ( ) = assert_equal ( ) @@ ignore @@ extract @@ ( a --> b ) msg finish ; assert_equal ~ msg " : simple " ( ) @@ ignore @@ extract @@ [ % choice_at a ( [ % choice_at a ( ( a --> b ) left finish , ( a --> b ) right finish ) ] , [ % choice_at a ( ( a --> b ) middle finish , [ % choice_at a ( ( a...
let test_failfast_bottom ( ) = let bottom ( ) = loop_with [ a ; b ; c ] ( fun t -> t ) in assert_raises UnguardedLoop ~ msg " : bottom " ( fun _ -> ignore @@ extract @@ bottom ( ) ) ; assert_raises UnguardedLoop ~ msg " : bottom after comm " ( fun _ -> ignore @@ extract @@ ( a --> b ) msg @@ bottom ( ) ) ; assert_raise...
let test_failfast_loop_roles ( ) = assert_raises UnguardedLoop ~ msg " : declared roles absent in the loop " ( fun _ -> ignore @@ extract @@ loop_with [ a ; b ; c ] @@ fun t -> [ % choice_at c ( ( c --> b ) left t ) ( ( c --> b ) right t ) ] ) ; assert_raises UnguardedLoop ~ msg " : declared roles absent in the loop 2 ...
let test_loop_merge ( ) = assert_raises UnguardedLoop ~ msg " : loop merge " ( fun _ -> ignore @@ extract @@ let g = [ % choice_at a ( ( a --> b ) left @@ loop_with [ a ; b ; c ] ( fun t -> [ % choice_at a ( ( a --> b ) left t , ( a --> b ) right t ) ] ) , ( a --> b ) right @@ ( a --> b ) left @@ ( b --> c ) left finis...
let suite = " Protcol combinators expected " >::: [ " test_failfast_loop_roles " >:: test_failfast_loop_roles ; " test_loop_merge " >:: test_loop_merge ; ] ; ;
let _results = run_test_tt_main suite in ( )
let test re s = match Tyre . exec re s with | Ok b -> b | Error e -> Format . eprintf " Error : % a . " @ Tyre . pp_error e ; assert false
let ( ) %%% a b = assert ( a %% b )
let ( ) = ( function % tyre _ -> true ) %%% " " ; % ( function % tyre s -> s = " " ) % " " %%%%
type t = [ | ` Attr of string * string option | ` Comment of string | ` Even_sigils of string option | ` Odd_sigils | ` Id of string * int * string | ` Unknown ]
let test1 : t Tyre . re = ( function % tyre | { ( |^?< k . ) >*: ( *?< v . ) } >+?$| -> ` Attr ( k , v ) | { |^# ( ?< comment . ) } >+$| -> ` Comment comment | { ( |^?< sigil ( [ ] { >@% 2 } ) ) } +?$| -> ` Even_sigils sigil | { [ ] } |^@%| -> ` Odd_sigils | { ( |^?< id [ > a - z ] ) ( +?& num : Tyre . pos_int ) ( ?< s...
let ( ) = assert ( test1 %% " x : 1 " = ` Attr ( " x " , Some " 1 " ) ) ; assert ( test1 %% " # Kommentar " = ` Comment " Kommentar " ) ; assert ( test1 %% " " = ` Even_sigils None ) ; assert ( test1 %% " " %%%@ = ` Even_sigils ( Some " " ) ) ; %%%@ assert ( test1 %% " " %%@ = ` Odd_sigils ) ; assert ( test1 %% " abc42...
let concat_seq sep seq = let rec f seq = match seq ( ) with | Seq . Nil -> " " | Cons ( s , seq ) -> s ^ sep ^ f seq in f seq
let test2 = function % tyre | { } |^<>$| -> ( ) = " " <> | { ( |^<?< x [ ] ) } >^<>+>$| -> fun s -> s = " " < ^ x ^ " " > | { ( |^<?< x [ ] ) ( >^<>+><?< y [ ] ) } >^<>+>$| -> fun s -> s = " " < ^ x ^ " " >< ^ y ^ " " > | { ( ( |^?< elt [ ; ] ) ; ) } >^<>*$| -> fun s -> concat_seq " ; " elt = s | { ( |^?< a > one ) ( |...
let ( ) %%%% re s = ( re %% s ) s
let ( ) = assert ( test2 " " ) ; %%%%<> assert ( test2 " %%%%< a " ) ; > assert ( test2 " %%%%< ab " ) ; > assert ( test2 " %%%%< a >< b " ) ; > assert ( test2 " %%%%< ab >< cde " ) ; > assert ( test2 " %%%% a ; " ) ; assert ( test2 " %%%% a ; b ; c ; d ; " ) ; assert ( test2 " %%%%< a ; b " ) ; > assert ( test2 " %%%%...
module F ( M : Map . OrderedType ) = struct let f = function % tyre | { ( |#?< space >\ s ) ( ??< comment . ) } >*| -> Some ( space <> None , comment ) | _ -> None end
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 ( : show_error_or pp_obj ) pp_ob...
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_to_yojson i5_of_yojson 42L...
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 " 9223372036854775807 " ; assert_roundt...
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 mod 100 :: acc ) acc in let lst = make_list ( 500_000 ...
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_yojson None " null "
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 " , \ 42 , " \ foo ] " " ; \ assert_roundtrip pp_p...
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 " , \ 42 , " \ foo ] " " ; \ assert_roundtrip ...
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 [ " " \ beer " , \ " \ guinness " , \ 3 . 3 ] 3 " ; as...
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 ( . match mapEncoding_of...