123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196openExpect_test_common.StdopenBaseopenPpxlibopenAst_builder.Defaultletlifter~loc=objectinherit[expression]Lifter.liftinheritPpxlib_metaquot_lifters.expression_lifterslocmethodfilenamefile_name=eapply~loc(evar~loc"Expect_test_common.Std.File.Name.of_string")[estring~loc(File.Name.to_stringfile_name)]end;;letlift_location~locof_loc=(lifter~loc)#locationof_locletlift_expectation~locexpect=letexp=(lifter~loc)#rawexpectin(* the constraint is added in order to avoid warnings if the code
is compiled with -principal. *)[%expr([%eexp]:stringExpect_test_common.Std.Expectation.t)];;leteoption~locx=matchxwith|None->pexp_construct~loc(Located.mk~loc(lident"None"))None|Somee->pexp_construct~loc(Located.mk~loc(lident"Some"))(Somee);;letestring_option~locx=eoption~loc(Option.mapx~f:(estring~loc))(* Grab a list of all the output expressions *)letcollect_expectations=objectinherit[(Location.t*Expectation.Raw.t)list]Ast_traverse.foldassupermethod!expressionexpracc=matchExpect_extension.match_expectationexprwith|None->super#expressionexpracc|Someext->assert_no_attributesexpr.pexp_attributes;(expr.pexp_loc,ext)::accend;;letreplace_expects=objectinheritAst_traverse.mapassupermethod!expression({pexp_attributes;pexp_loc;_}asexpr)=matchExpect_extension.match_expectationexprwith|None->super#expressionexpr|Someext->letf_var=matchext.bodywith|Exact_|Pretty_|Unreachable->"Expect_test_collector.save_output"|Output->"Expect_test_collector.save_and_return_output"inletloc={pexp_locwithloc_end=pexp_loc.loc_start}inletexpr=[%expr[%eevar~locf_var][%elift_location~locext.extid_location]]in{exprwithpexp_attributes}end;;letfile_digest=letcache=Hashtbl.create(moduleString)~size:32infunfname->Hashtbl.find_or_addcachefname~default:(fun()->Caml.Digest.filefname|>Caml.Digest.to_hex);;letrewrite_test_body~descr~tags~uncaught_exnpstr_locbody=letloc=pstr_locinletexpectations=List.map(collect_expectations#expressionbody[])~f:(fun(loc,expect_extension)->lift_expectation~locexpect_extension)|>elist~locinletuncaught_exn=Option.mapuncaught_exn~f:(fun(loc,expectation)->lift_expectation~locexpectation)|>eoption~locinletbody=replace_expects#expressionbodyinletabsolute_filename=Ppx_here_expander.expand_filenamepstr_loc.loc_start.pos_fnameinlethash=file_digestloc.loc_start.pos_fnamein[%exprletmoduleExpect_test_collector=Expect_test_collector.Make(Expect_test_config)inExpect_test_collector.run~file_digest:(Expect_test_common.Std.File.Digest.of_string[%eestring~lochash])~location:[%elift_location~loc(Ppx_expect_payload.transl_locpstr_loc)]~absolute_filename:[%eestring~locabsolute_filename]~description:[%eestring_option~locdescr]~tags:[%eelist~loc(List.maptags~f:(estring~loc))]~expectations:[%eexpectations]~uncaught_exn_expectation:[%euncaught_exn]~inline_test_config:(moduleInline_test_config)(fun()->[%ebody])];;moduleP=structopenAst_patternletuncaught_exn=Attribute.declare_with_name_loc"@expect.uncaught_exn"Attribute.Context.value_binding(map1'(Ppx_expect_payload.pattern())~f:(funlocx->loc,x))(fun~name_loc(loc,x)->loc,Ppx_expect_payload.makex~kind:Normal~extension_id_loc:name_loc);;letopt_name()=map(pstring__)~f:(funfx->f(Somex))|||mapppat_any~f:(funf->fNone);;letpattern()=pstr(pstr_valuenonrecursive(Attribute.patternuncaught_exn(value_binding~pat:(map(Attribute.patternPpx_inline_test.tags(opt_name()))~f:(funfattributesname_opt->f~name:name_opt~tags:(matchattributeswith|None->[]|Somex->x)))~expr:__)^::nil)^::nil);;end(* Set to [true] when we see a [%expect_test] extension *)moduleHas_tests=Driver.Create_file_property(structletname="ppx_expect.has_tests"end)(Bool)letexpect_test=Extension.declare_inline"expect_test"Structure_item(P.pattern())(fun~loc~path:_uncaught_exn~name~tagscode->Has_tests.settrue;Ppx_inline_test.validate_extension_point_exn~name_of_ppx_rewriter:"ppx_expect"~loc~tags;rewrite_test_body~descr:name~tags~uncaught_exnloccode|>Ppx_inline_test.maybe_droploc);;let()=Driver.register_transformation"expect_test"~rules:[Context_free.Rule.extensionexpect_test]~enclose_impl:(funwhole_loc->matchwhole_loc,Ppx_inline_test_libname.get()with|None,_|_,None->[],[]|Someloc,Some_->letmaybe_drop=Ppx_inline_test.maybe_dropinletabsolute_filename=Ppx_here_expander.expand_filenameloc.loc_start.pos_fnameinletheader=letloc={locwithloc_end=loc.loc_start}inmaybe_droploc[%exprExpect_test_collector.Current_file.set~absolute_filename:[%eestring~locabsolute_filename]]andfooter=letloc={locwithloc_start=loc.loc_end}inmaybe_droploc[%exprExpect_test_collector.Current_file.unset()]inheader,footer);;