text stringlengths 0 601k |
|---|
module String : sig type t = string val to_string : ' a -> ' a end } ] | |
module P = Pair ( Int ) ( Pair ( String ) ( Int ) ) [ %% expect { | { ( " P " , module ) -> { . < 18 > ( " t " , type ) -> . < 5 ; > ( " to_string " , value ) -> . < 6 ; > } ; } |
module P : sig type t = Int . t * Pair ( String ) ( Int ) . t val to_string : Int . t * Pair ( String ) ( Int ) . t -> string end } ] ; ; | [ %% expect { | { } } ] | |
let rec srec = fun [ [ ] -> [ : ] : | [ h :: t ] -> [ : ` h ; srec t ] : ] in srec ; |
type d_op_t = [ DOP_le | DOP_lt | DOP_eq | DOP_ne | DOP_gt | DOP_ge ] ; |
type d_version_t = [ DVERSION of string ] ; |
type dexpr = [ DE_uident of Ploc . t and string | DE_version_op of Ploc . t and ( Ploc . t * d_op_t ) and ( Ploc . t * d_version_t ) | DE_parens of Ploc . t and dexpr and Ploc . t | DE_not of Ploc . t and dexpr | DE_and of dexpr and Ploc . t and dexpr | DE_or of dexpr and Ploc . t and dexpr ] ; |
type base_token = ( Ploc . t * ( string * string ) ) ; |
type ifdef_t = [ DEF_ifdef of bool and Ploc . t and dexpr and Ploc . t and tokens and else_t and Ploc . t ] DEF_none ] ; |
module PA = struct parser [ [ : ` ( loc , ( " " , " " ) ) < ] : -> ( loc , DOP_lt ) | [ : ` ( loc , ( " " , " " ) ) <= ] : -> ( loc , DOP_le ) | [ : ` ( loc , ( " " , " " ) ) = ] : -> ( loc , DOP_eq ) | [ : ` ( loc , ( " " , " " ) ) <> ] : -> ( loc , DOP_ne ) | [ : ` ( loc , ( " " , " " ) ) > ] : -> ( loc , DOP_gt ) | ... |
module PP = struct let s = match d_op with [ DOP_le -> " " <= | DOP_lt -> " " < | DOP_eq -> " " = | DOP_ne -> " " <> | DOP_gt -> " " > | DOP_ge -> " " >= ] in [ : ` ( loc , ( " " , s ) ) ] : ; fun [ DE_uident loc id -> [ : ` ( loc , ( " UIDENT " , id ) ) ] : | DE_version_op loc1 loc_op loc_vers -> [ : ` ( loc1 , ( " UI... |
module DorT = struct let rec trec = parser [ [ : d = PA . pa_ifdef ; strm ] : -> [ : ` L d ; trec strm ] : | [ : ` t ; strm ] : -> [ : ` R t ; trec strm ] : | [ : ] : -> [ : ] : ] in trec ; let rec trec = parser [ [ : ` R tok ; strm ] : -> [ : ` tok ; trec strm ] : | [ : ` L def ; strm ] : -> [ : PP . pp_def def ; trec... |
module Testables = struct type nested_struct_record = { message : string } [ @@ deriving show , eq ] type test_struct_record = { comment : string ; decimal : float ; falseVar : bool ; trueVal : bool ; int32 : Int32 . t ; long : Int64 . t ; nested : nested_struct_record ; bools : bool list } [ @@ deriving show , eq ] le... |
module Utils = struct let test_transport ( ) = let buffer = Buffer . create 1024 in new Test_transport . t ( buffer , buffer ) let make_test_cases test_name speed cases case_name test = cases |> List . map ( fun case -> Alcotest . test_case ( case_name test_name case ) speed ( test case ) ) end |
let test_round_trip protocol_factory ( ) = let trans = Utils . test_transport ( ) in let proto = protocol_factory trans in let nested_struct = new Test_types . nestedStruct in nested_struct # set_message " Hello world " ; ! let test_struct = new Test_types . testStruct in test_struct # set_comment " No comment . " ; te... |
module TestReadWriteInt ( I : TCompactProtocol . IntSig ) = struct module RWI = TCompactProtocol . ReadWriteInt ( I ) open RWI module Testables = struct let pp = fun fmt t -> Format . fprintf fmt " % s " @@ I . to_string t let int_testable = Alcotest . testable pp I . equal end let test_round_trip_varint test_number ( ... |
let all = [ Alcotest . test_case " Round trip a struct using TCompactProtocol " ` Quick @@ test_round_trip ( fun trans -> new TCompactProtocol . t trans ) ; Alcotest . test_case " Round trip a struct using TBinaryProtocol " ` Quick @@ test_round_trip ( fun trans -> new TBinaryProtocol . t trans ) ] @ Utils . make_test_... |
module Map = Map . Make ( Int32 ) |
let succ n = if Compare . Int32 . equal n Int32 . max_int then invalid_arg " round_repr . succ : cannot apply succ to maximum round value " else Int32 . succ n |
let pp fmt i = Format . fprintf fmt " % ld " i |
type error += Negative_round of int |
type error += Round_overflow of int |
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : negative_round " ~ title " : Negative round " ~ description " : Round cannot be built out of negative integers . " ~ pp ( : fun ppf i -> Format . fprintf ppf " Negative round cannot be built out of negative integers ( % Ld ) " i ) ( obj1 ( req... |
let of_int32 i = if i >= 0l then Ok i else error ( Negative_round ( Int32 . to_int i ) ) [ @@ inline ] |
let pred r = let p = Int32 . pred r in of_int32 p |
let of_int i = if Compare . Int . ( i < 0 ) then error ( Negative_round i ) else let i32 = Int32 . of_int i in if Compare . Int . ( Int32 . to_int i32 = i ) then Ok i32 else error ( Round_overflow i ) |
let to_int i32 = let i = Int32 . to_int i32 in if Int32 . ( equal ( of_int i ) i32 ) then ok i else error ( Round_overflow i ) |
let to_int32 t = t [ @@ inline ] |
let to_slot round ~ committee_size = to_int round >>? fun r -> let slot = r mod committee_size in Slot_repr . of_int slot |
let encoding = Data_encoding . conv_with_guard ( fun i -> i ) ( fun i -> match of_int32 i with | Ok _ as res -> res | Error _ -> Error " Round_repr . encoding : negative round " ) Data_encoding . int32 |
module Durations = struct type t = { first_round_duration : Period_repr . t ; delay_increment_per_round : Period_repr . t ; } type error += | Non_increasing_rounds of { increment : Period_repr . t } | Round_durations_must_be_at_least_one_second of { round : Period_repr . t } let ( ) = register_error_kind ` Permanent ~ ... |
type error += Round_too_high of int32 |
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : round_too_high " ~ title " : round too high " ~ description " : block round too high . " ~ pp ( : fun ppf round -> Format . fprintf ppf " Block round is too high : % ld " round ) ( obj1 ( req " level_offset_too_high " int32 ) ) ( function Roun... |
let raw_level_offset_of_round round_durations ~ round = if Compare . Int32 . ( round = zero ) then ok Int64 . zero else let sum_durations = let Durations . { first_round_duration ; delay_increment_per_round } = round_durations in let roundz = Int64 . of_int32 round in let m = Z . of_int64 Int64 . ( div ( mul roundz ( p... |
type error += Level_offset_too_high of Period_repr . t |
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : level_offset_too_high " ~ title " : level offset too high " ~ description " : The block ' s level offset is too high . " ~ pp ( : fun ppf offset -> Format . fprintf ppf " The block ' s level offset is too high : % a " Period_repr . pp offset )... |
type round_and_offset = { round : int32 ; offset : Period_repr . t } |
let round_and_offset round_durations ~ level_offset = let level_offset_in_seconds = Period_repr . to_seconds level_offset in let overflow_bound = Int64 . shift_right Int64 . max_int 10 in if Compare . Int64 . ( overflow_bound < level_offset_in_seconds ) then error ( Level_offset_too_high level_offset ) else let Duratio... |
let timestamp_of_round round_durations ~ predecessor_timestamp ~ predecessor_round ~ round = let pred_round_duration = Durations . round_duration round_durations predecessor_round in Time_repr . ( predecessor_timestamp +? pred_round_duration ) >>? fun start_of_current_level -> raw_level_offset_of_round round_durations ... |
let timestamp_of_another_round_same_level round_durations ~ current_timestamp ~ current_round ~ considered_round = raw_level_offset_of_round round_durations ~ round : considered_round >>? fun target_offset -> raw_level_offset_of_round round_durations ~ round : current_round >>? fun current_offset -> ok @@ Time_repr . o... |
type error += | Round_of_past_timestamp of { provided_timestamp : Time . t ; predecessor_timestamp : Time . t ; predecessor_round : t ; } |
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : round_of_past_timestamp " ~ title " : Round_of_timestamp for past timestamp " ~ description " : Provided timestamp is before the expected level start . " ~ pp ( : fun ppf ( provided_ts , predecessor_ts , round ) -> Format . fprintf ppf " Provi... |
let round_of_timestamp round_durations ~ predecessor_timestamp ~ predecessor_round ~ timestamp = let round_duration = Durations . round_duration round_durations predecessor_round in Time_repr . ( predecessor_timestamp +? round_duration ) >>? fun start_of_current_level -> Period_repr . of_seconds ( Time_repr . diff time... |
let level_offset_of_round round_durations ~ round = raw_level_offset_of_round round_durations ~ round >>? fun offset -> ok ( Period_repr . of_seconds_exn offset ) |
module Internals_for_test = struct type round_and_offset_raw = { round : round ; offset : Period_repr . t } let round_and_offset round_durations ~ level_offset = round_and_offset round_durations ~ level_offset >|? fun v -> { round = v . round ; offset = v . offset } end |
let slist t = Alcotest . slist t compare |
let params = slist Alcotest ( . pair string string ) string |
let matches_t : Route . matches Alcotest . testable = ( module struct type t = Route . matches let equal r1 r2 = r1 . Route . splat = r2 . Route . splat && r1 . Route . params = r2 . Route . params let pp f t = Sexp . pp_hum f ( Route . sexp_of_matches t ) t end ) end ; ; |
let match_get_params route url = match Route . match_url route url with | None -> None | Some { Route . params ; _ } -> Some params ; ; |
let string_of_match = function | None -> " None " | Some m -> let open Sexp_conv in Sexp . to_string_hum ( sexp_of_list ( sexp_of_pair sexp_of_string sexp_of_string ) sexp_of_string m ) m ; ; |
let simple_route1 ( ) = let r = Route . of_string " / test /: id " in Alcotest ( . check ( option params ) params " no match " None ( match_get_params r " / test / blerg / 123 ) ) " ; Alcotest ( . check ( option params ) params " match " ( match_get_params r " / test / 123 ) " ( Some [ " id " , " 123 " ] ) ) ; ; |
let simple_route2 ( ) = let r = Route . of_string " / test /: format /: name " in let m = match_get_params r " / test / json / bar " in Alcotest ( . check ( option params ) params " " m ( Some [ " format " , " json " ; " name " , " bar " ] ) ) ; ; |
let simple_route3 ( ) = let r = Route . of_string " / test /: format /: name " in let m = Route . match_url r " / test / bar " in Alcotest ( . check ( option matches_t ) matches_t " unexpected match " None m ) m ; ; |
let route_no_slash ( ) = let r = Route . of_string " / xxx /: title " in let m = Route . match_url r " / xxx . . " // in Alcotest ( . check ( option matches_t ) matches_t " unexpected match " None m ) m ; ; |
let splat_route1 ( ) = let r = Route . of_string " / test /*/: id " in let matches = Route . match_url r " / test / splat / 123 " in Alcotest ( . check ( option matches_t ) matches_t " matches " ( Some { Route . params = [ " id " , " 123 " ] ; splat = [ " splat " ] } ) matches ) matches ; ; |
let splat_route2 ( ) = let r = Route . of_string " " /* in let m = Route . match_url r " / abc / 123 " in Alcotest ( . check ( option matches_t ) matches_t " unexpected match " None m ) m ; ; |
let splat_route3 ( ) = let r = Route . of_string " " /*/*/* in let matches = Route . match_url r " / test / splat / 123 " in Alcotest ( . check ( option matches_t ) matches_t " matches " ( Some { Route . params = [ ] ; splat = [ " test " ; " splat " ; " 123 " ] } ) matches ) matches ; ; |
let test_match_2_params ( ) = let r = Route . of_string " / xxx /: x /: y " in let m = match_get_params r " / xxx / 123 / 456 " in Alcotest ( . check ( option params ) params " " ( Some [ " x " , " 123 " ; " y " , " 456 " ] ) m ) m ; ; |
let test_match_no_param ( ) = let r = Route . of_string " / version " in let m1 , m2 = Route ( . match_url r " / version " , match_url r " / tt ) " in match m1 , m2 with | Some _ , None -> ( ) | _ , _ -> Alcotest . fail " bad match " ; ; |
let test_empty_route ( ) = let r = Route . of_string " " / in let m s = match Route . match_url r s with | None -> false | Some _ -> true in let m1 , m2 = m " " , / m " / testing " in Alcotest ( . check bool " match ' / ' " true m1 ) m1 ; Alcotest ( . check bool " not match ' / testing ' " false m2 ) m2 ; ; |
let printer x = x |
let str_t s = s |> Route . of_string |> Route . to_string |
let string_convert_1 ( ) = Alcotest ( . check string " " " " / ( str_t ) ) " " / |
let string_convert_2 ( ) = Alcotest ( . check string " " " / one /: two " ( str_t " / one /: two ) ) " |
let string_convert_3 ( ) = Alcotest ( . check string " " " / one / two /*/ three " ( str_t " / one / two /*/ three ) ) " ; ; |
let escape_param_1 ( ) = let r = Route . of_string " /: pp " /* in let matches = Route . match_url r " /% 23 /% 23a " in Alcotest ( . check ( option matches_t ) matches_t " matches " ( Some { Route . params = [ " pp " , " " # ] ; splat = [ " # a " ] } ) matches ) matches ; ; |
let empty_route ( ) = let r = Route . of_string " " in Alcotest ( . check ( option matches_t ) matches_t " " ( Some { Route . params = [ ] ; splat = [ ] } ) ( Route . match_url r ) ) " " ; ; |
let test_double_splat ( ) = let r = Route . of_string " " /** in let matching_urls = [ " / test " , [ " test " ] ; " " , / [ ] ; " / user / 123 / foo / bar " , [ " user " ; " 123 " ; " foo " ; " bar " ] ] in matching_urls |> List . iter ( fun ( u , splat ) splat -> Alcotest ( . check ( option matches_t ) matches_t " ma... |
let test_double_splat_escape ( ) = let r = Route . of_string " " /** in let matches = Route . match_url r " /% 23 /% 23a " in Alcotest ( . check ( option matches_t ) matches_t " matches " ( Some { Route . params = [ ] ; splat = [ " " ; # " # a " ] } ) matches ) matches ; ; |
let test_query_params_dont_impact_match ( ) = let r2 = Route . of_string " / foo /: message " in Alcotest ( . check ( option params ) params " " ( match_get_params r2 " / foo / bar ) " ( Some [ " message " , " bar " ] ) ) ; Alcotest ( . check ( option params ) params " " ( match_get_params r2 " / foo / bar ? key = 12 )... |
let ( ) = Alcotest . run " Route " [ ( " match " , [ " test match no param " , ` Quick , test_match_no_param ; " test match 1 " , ` Quick , simple_route1 ; " test match 2 " , ` Quick , simple_route2 ; " test match 3 " , ` Quick , simple_route3 ; " test match 2 params " , ` Quick , test_match_2_params ] ) ; ( " splat " ... |
module Dream = struct include Dream let path = path [ @ ocaml . warning " - 3 " ] end |
let ( ) = ignore Initialize . require |
let path request = Dream . path request |> String . concat " " / |> fun path -> " " / ^ path |
let show_tokens route = try Dream__server . Router . parse route |> List . map ( function | Dream__server . Router . Literal s -> Printf . sprintf " % S " s | Dream__server . Router . Param s -> Printf . sprintf " :% S " s | Dream__server . Router . Wildcard s -> Printf . sprintf " *% S " s ) |> String . concat " ; " |... |
let show ( ? prefix = " " ) / ( ? method_ = ` GET ) target router = try Dream . request ~ method_ ~ target " " |> Dream . test ~ prefix router |> fun response -> let body = Dream . client_stream response |> Obj . magic |> Dream_pure . Stream . read_until_close |> Lwt_main . run in let status = Dream . status response i... |
let expand_router_str ~ loc router_id module_names error_handler = let open Ast_builder . Default in let routes_list = List . map ~ f ( : fun module_name -> pexp_ident ~ loc { txt = Ldot ( module_name , " routes " ) ; loc = loc } ) module_names in [ % stri let [ % p pvar router_id ~ loc ] = Ocamlapi_router . create ( L... |
let router_str_pattern = let open Ast_pattern in let id = ppat_var __ in let module_names = pexp_construct __ none |> elist in let error_handler = __ ^:: nil in let router_binding = value_binding ~ pat : id ~ expr ( : module_names ^:: error_handler |> pexp_tuple ) ^:: nil in pstr_value nonrecursive router_binding ^:: n... |
let router_extension_str = Extension . declare name Extension . Context . structure_item router_str_pattern ( fun ~ loc ~ path : _ router_id module_names error_handler -> expand_router_str ~ loc router_id module_names error_handler ) |
let expand_router_str = Context_free . Rule . extension router_extension_str |
let expand_router ~ loc router_id module_names error_handler exp = let open Ast_builder . Default in match module_names with | [ ] -> failwith " Cannot create a router with empty list of modules " | _ :: _ -> let routes_list = List . map ~ f ( : fun module_name -> pexp_ident ~ loc { txt = Ldot ( module_name , " routes ... |
let router_pattern = let open Ast_pattern in let id = ppat_var __ in let module_names = pexp_construct __ none |> elist in let error_handler = __ ^:: nil in pexp_let nonrecursive ( value_binding ~ pat : id ~ expr ( : module_names ^:: error_handler |> pexp_tuple ) ^:: nil ) __ |> single_expr_payload |
let router_extension = Extension . declare name Extension . Context . expression router_pattern ( fun ~ loc ~ path : _ router_id module_names exp -> expand_router ~ loc router_id module_names exp ) |
let expand_router = Context_free . Rule . extension router_extension |
let register ( ) = Driver . register_transformation name ~ rules [ : expand_router ; expand_router_str ] |
module Make_with_config ( Req : Cohttp . S . Request ) ( B : S . Body ) ( Resp : Cohttp . S . Response ) ( IO : S . Io ) ( Config : S . Router_config with type req := Req . t and type body := B . t and type resp := Resp . t and type ' a io := ' a IO . t ) = struct include Config type callback = ? vars : string String .... |
module Make ( Req : Cohttp . S . Request ) ( B : S . Body ) ( Resp : Cohttp . S . Response ) ( IO : S . Io ) = struct module Default_config = struct let default_exn_handler ? vars : _ _ = IO . return ( Resp . make ~ status ` : Internal_server_error ( ) , B . empty ) let default_fallback ? vars : _ _req _body = IO . ret... |
let name = " Route tests " |
let failure_callback msg = fun _ -> assert_failure msg |
let variable_set lst = match String . Table . of_alist lst with | ` Duplicate_key key -> failwith ( sprintf " Duplicate key in test input % s " key ) | ` Ok set -> set |
let simple_success_callback ? vars : _ _ _ = respond_success_string " Success " |
let validate_vars_callback expected ? vars ( : vars = String . Table . create ( ) ) _ _ = assert_equal ~ cmp ( : fun t1 t2 -> String . Table . equal t1 t2 String . equal ) expected vars ; respond_success_string " Success " |
let localhost path = dummy_host ^ path |> Uri . of_string |
let tests = [ " Simple route test " >:: ( fun _ -> let route = dummy_path , [ ` GET , simple_success_callback ] in let router = Router . create_exn [ route ] in let req = Request . make ~ meth ` : GET ( localhost dummy_path ) in let body = Body . empty in Validators . dispatch_and_validate Router . dispatch router req ... |
let meth_callback_pair = let open Ast_pattern in let meth = alt ( pexp_variant ( string " Other " ) ( some ( estring __ ) ) ) ( pexp_variant __ none ) in let callback = __ in meth ^:: callback ^:: nil |> pexp_tuple |
let expand_meth_callback _meth_name _callback_expr = ( ) |
let route_pattern = let open Ast_pattern in let route_binding = value_binding ~ pat ( : pstring __ ) ~ expr ( : elist __ ) ^:: nil in let route_extension = extension ( string " route " ) ( pstr_value nonrecursive route_binding ^:: nil |> pstr ) in pstr_extension route_extension nil |
let expand_route_pattern ~ loc acc path meth_callback_pairs = let open Ast_builder . Default in let template_id = id_from_template path in List . iter meth_callback_pairs ~ f ( : fun meth_callback -> Ast_pattern . parse meth_callback_pair loc meth_callback ~ on_error ( : fun ( ) -> failwith " Some informative message "... |
let visit_route_extension ~ loc ~ on_error ext acc = Ast_pattern . parse route_pattern loc ~ on_error ext ( expand_route_pattern ~ loc acc ) |
let route_nodes_expander = object ( self ) inherit [ string list ] Ast_traverse . fold_map as _super method ! structure s acc = let s ' , ids = List . fold_right s ~ f ( : fun s_i ( s ' , ids ) -> let s_i ' , ids ' = self # structure_item s_i ids in s_i ' :: s ' , ids ' ) ~ init ( [ ] , : [ ] ) in match ids with | [ ] ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.