Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_inline_test.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342openPpxlibopenStdppxopenAst_builder.Default(* Generated code should depend on the environment in scope as little as
possible. E.g. rather than [foo = []] do [match foo with [] ->], to eliminate the
use of [=]. It is especially important to not use polymorphic comparisons, since we
are moving more and more to code that doesn't have them in scope. *)typemaybe_drop=|Keep|Drop_with_deadcode|Dropletmaybe_drop_mode=refKeepletset_default_maybe_dropx=maybe_drop_mode:=xlet()=Driver.add_arg"-inline-test-drop"(Unit(fun()->maybe_drop_mode:=Drop))~doc:" Drop unit tests";Driver.add_arg"-inline-test-drop-with-deadcode"(Unit(fun()->maybe_drop_mode:=Drop_with_deadcode))~doc:" Drop unit tests by wrapping them inside deadcode to prevent unused variable \
warnings.";;let()=Driver.Cookies.add_simple_handler"inline-test"Ast_pattern.(pexp_ident(lident__'))~f:(function|None->()|Someid->(matchid.txtwith|"drop"->maybe_drop_mode:=Drop|"drop_with_deadcode"->maybe_drop_mode:=Drop_with_deadcode|s->Location.raise_errorf~loc:id.loc"invalid 'inline-test' cookie (%s), expected one of: drop, \
drop_with_deadcode"s));;(* Same as above, but for the Dune setting *)let()=Driver.Cookies.add_simple_handler"inline_tests"Ast_pattern.(estring__')~f:(function|None->()|Someid->(matchid.txtwith|"enabled"->maybe_drop_mode:=Keep|"disabled"->maybe_drop_mode:=Drop|"ignored"->maybe_drop_mode:=Drop_with_deadcode|s->Location.raise_errorf~loc:id.loc"invalid 'inline_tests' cookie (%s), expected one of: enabled, disabled or \
ignored"s));;letmaybe_droploccode=match!maybe_drop_modewith|Keep->[%strlet()=[%ecode]]|Drop_with_deadcode->[%strlet()=iffalsethen[%ecode]else()]|Drop->Attribute.explicitly_drop#expressioncode;[%str];;letrecshort_desc_of_expr~max_lene=matche.pexp_descwith|Pexp_let(_,_,e)|Pexp_letmodule(_,_,e)->short_desc_of_expr~max_lene|_->lets=Pprintast.string_of_expressioneinletres=ifString.lengths>=max_lenthen(lets_short=String.subs~pos:0~len:(max_len-5)ins_short^"[...]")elsesinString.mapres~f:(function|'\n'->' '|c->c);;letdescr~(loc:Location.t)?(inner_loc=loc)e_optid_opt=letfilename=loc.loc_start.pos_fnameinletline=loc.loc_start.pos_lnuminletstart_pos=loc.loc_start.pos_cnum-loc.loc_start.pos_bolinletend_pos=inner_loc.Location.loc_end.pos_cnum-loc.loc_start.pos_bolinletdescr=matchid_optwith|`Literalid->estring~locid|`Expre->e|`None->estring~loc(matche_optwith|None->""|Somee->"<<"^short_desc_of_expr~max_len:50e^">>")in(pexp_lazy~locdescr,estring~locfilename,eint~locline,eint~locstart_pos,eint~locend_pos);;letapply_to_descrlid~loc?inner_loce_optid_opttagsmore_arg=letdescr,filename,line,start_pos,end_pos=descr~loc?inner_loce_optid_optinletexpr=pexp_apply~loc(evar~loc("Ppx_inline_test_lib."^lid))[Labelled"config",[%expr(moduleInline_test_config)];Labelled"descr",descr;Labelled"tags",elist~loc(List.map~f:(estring~loc)tags);Labelled"filename",filename;Labelled"line_number",line;Labelled"start_pos",start_pos;Labelled"end_pos",end_pos;Nolabel,more_arg]inmaybe_droplocexpr;;letcan_use_test_extensions()=match!maybe_drop_mode,Ppx_inline_test_libname.get()with|Keep,None->false|(Drop|Drop_with_deadcode),_|_,Some_->true;;(* Set to [true] when we see a [let%test] or [let%expect_test] etc extension. *)moduleHas_tests=Driver.Create_file_property(structletname="ppx_inline_test.has_tests"end)(structtypet=boollett_of_sexp=Sexplib0.Sexp_conv.bool_of_sexpletsexp_of_t=Sexplib0.Sexp_conv.sexp_of_boolend)letall_tags=["no-js";"js-only";"64-bits-only";"32-bits-only";"fast-flambda";"fast-flambda2";"x-library-inlining-sensitive";"not-on-el7";"not-on-el8";"disabled"];;letvalidate_tagtag=ifnot(List.mem~set:all_tagstag)thenError(Spellcheck.spellcheckall_tagstag)elseOk();;letvalidate_extension_point_exn~name_of_ppx_rewriter~loc~tags=Has_tests.settrue;ifnot(can_use_test_extensions())thenLocation.raise_errorf~loc"%s: extension is disabled because the tests would be ignored (the build system \
didn't pass -inline-test-lib. With jenga or dune, this usually happens when \
writing tests in files that are part of an executable stanza, but only library \
stanzas support inline tests)"name_of_ppx_rewriter;List.itertags~f:(funtag->matchvalidate_tagtagwith|Ok()->()|Errorhint->lethint=matchhintwith|None->""|Somehint->"\n"^hintinLocation.raise_errorf~loc"%s: %S is not a valid tag for inline tests.%s"name_of_ppx_rewritertaghint);;letname_of_ppx_rewriter="ppx_inline_test"letexpand_test~loc~path:_~name:id~tagse=letloc={locwithloc_ghost=true}invalidate_extension_point_exn~name_of_ppx_rewriter~loc~tags;apply_to_descr"test"~loc(Somee)idtags[%exprfun()->[%ee]];;letexpand_test_unit~loc~path:_~name:id~tagse=letloc={locwithloc_ghost=true}invalidate_extension_point_exn~name_of_ppx_rewriter~loc~tags;(* The "; ()" bit is there to breaks tail call optimization, for better backtraces. *)apply_to_descr"test_unit"~loc(Somee)idtags[%exprfun()->[%ee];()];;letexpand_test_module~loc~path:_~name:id~tagsm=letloc={locwithloc_ghost=true}invalidate_extension_point_exn~name_of_ppx_rewriter~loc~tags;apply_to_descr"test_module"~loc~inner_loc:m.pmod_locNoneidtags(pexp_fun~locNolabelNone(punit~loc)(pexp_letmodule~loc(Located.mk~loc(Some"M"))m(eunit~loc)));;moduleE=structopenAst_patternlettags=Attribute.declare"tags"Attribute.Context.pattern(single_expr_payload(pexp_tuple(many(estring__))|||map(estring__)~f:(funfx->f[x])))(funx->x);;letlist_of_option=function|None->[]|Somex->x;;letopt_name()=map(pstring__)~f:(funfx->f(`Literalx))|||mapppat_any~f:(funf->f`None)|||map(ppat_extension(extension(cst~to_string:Fn.id"name")(single_expr_payload__)))~f:(funfe->f(`Expre));;letopt_name_and_exprexpr=pstr(pstr_valuenonrecursive(value_binding~pat:(map(Attribute.patterntags(opt_name()))~f:(funfattributesname_opt->f~name:name_opt~tags:(list_of_optionattributes)))~expr^::nil)^::nil);;lettest=Extension.declare_inline"inline_test.test"Extension.Context.structure_item(opt_name_and_expr__)expand_test;;lettest_unit=Extension.declare_inline"inline_test.test_unit"Extension.Context.structure_item(opt_name_and_expr__)expand_test_unit;;lettest_module=Extension.declare_inline"inline_test.test_module"Extension.Context.structure_item(opt_name_and_expr(pexp_pack__))expand_test_module;;letall=[test;test_unit;test_module]endlettags=E.tagslet()=Driver.V2.register_transformation"inline-test"~extensions:E.all~enclose_impl:(functxtloc->matchloc,Ppx_inline_test_libname.get()with|None,_|_,None->[],[]|Someloc,Some(libname,partition_opt)->letpartition=matchpartition_optwith|None->Stdlib.Filename.basename(Expansion_context.Base.input_namectxt)|Somep->pinletloc={locwithloc_ghost=true}in(* See comment in benchmark_accumulator.ml *)letheader=letloc={locwithloc_end=loc.loc_start}inmaybe_droploc[%exprPpx_inline_test_lib.set_lib_and_partition[%eestring~loclibname][%eestring~locpartition]]andfooter=letloc={locwithloc_start=loc.loc_end}inmaybe_droploc[%exprPpx_inline_test_lib.unset_lib[%eestring~loclibname]]inheader,footer);;