Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file oUnit.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462(***********************************************************************)(* The OUnit library *)(* *)(* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 *)(* Maas-Maarten Zeeman. *)(*
The package OUnit is copyright by Maas-Maarten Zeeman.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this document and the OUnit software ("the Software"), to
deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
The Software is provided ``as is'', without warranty of any kind,
express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose and noninfringement.
In no event shall Maas-Maarten Zeeman be liable for any claim, damages
or other liability, whether in an action of contract, tort or
otherwise, arising from, out of or in connection with the Software or
the use or other dealings in the software.
*)(***********************************************************************)(* pad: just harmonized some APIs regarding the 'msg' label *)letbracketset_upftear_down()=letfixture=set_up()intryffixture;tear_downfixturewithe->tear_downfixture;raiseeexceptionSkipofstringletskip_ifbmsg=ifbthenraise(Skipmsg)exceptionTodoofstringlettodomsg=raise(Todomsg)letassert_failuremsg=failwith("OUnit: "^msg)letassert_bool~msgb=ifnotbthenassert_failuremsgletassert_stringstr=ifnot(str="")thenassert_failurestrletassert_equal?(cmp=(=))?printer?msgexpectedactual=(* pad: better to use dump by default *)letp=Dumper.dumpinletget_error_string_=matchprinter,msgwithNone,None->(Format.sprintf"expected: %s but got: %s"(pexpected)(pactual))|None,Somes->(Format.sprintf"%s\nnot equal, expected: %s but got: %s"s(pexpected)(pactual))|Somep,None->(Format.sprintf"expected: %s but got: %s"(pexpected)(pactual))|Somep,Somes->(Format.sprintf"%s\nexpected: %s but got: %s"s(pexpected)(pactual))inifnot(cmpexpectedactual)thenassert_failure(get_error_string())letraisesf=tryf();Nonewithe->Someeletassert_raises?msgexn(f:unit->'a)=letpexn=Printexc.to_stringinletget_error_string_=letstr=Format.sprintf"expected exception %s, but no exception was raised."(pexnexn)inmatchmsgwithNone->assert_failurestr|Somes->assert_failure(Format.sprintf"%s\n%s"sstr)inmatchraisesfwithNone->assert_failure(get_error_string())|Somee->assert_equal?msg~printer:pexnexne(* Compare floats up to a given relative error *)letcmp_float?(epsilon=0.00001)ab=abs_float(a-.b)<=epsilon*.(abs_floata)||abs_float(a-.b)<=epsilon*.(abs_floatb)(* Now some handy shorthands *)let(@?)msga=assert_boolmsga(* The type of test function *)typetest_fun=unit->unit(* The type of tests *)typetest=TestCaseoftest_fun|TestListoftestlist|TestLabelofstring*test(* Some shorthands which allows easy test construction *)let(>:)st=TestLabel(s,t)(* infix *)let(>::)sf=TestLabel(s,TestCase(f))(* infix *)let(>:::)sl=TestLabel(s,TestList(l))(* infix *)(* Utility function to manipulate test *)letrectest_decorategtst=matchtstwith|TestCasef->TestCase(gf)|TestListtst_lst->TestList(List.map(test_decorateg)tst_lst)|TestLabel(str,tst)->TestLabel(str,test_decorategtst)(* Return the number of available tests *)letrectest_case_counttest=matchtestwithTestCase_->1|TestLabel(_,t)->test_case_countt|TestListl->List.fold_left(funct->c+test_case_countt)0ltypenode=ListItemofint|Labelofstringtypepath=nodelistletstring_of_nodenode=matchnodewithListItemn->(string_of_intn)|Labels->sletstring_of_pathpath=List.fold_left(funal->ifa=""thenlelsel^":"^a)""(List.mapstring_of_nodepath)(* Some helper function, they are generally applicable *)(* Applies function f in turn to each element in list. Function f takes
one element, and integer indicating its location in the list *)letmapifl=letrecrmapicntl=matchlwith[]->[]|h::t->(fhcnt)::(rmapi(cnt+1)t)inrmapi0lletfold_leftifaccul=letrecrfold_lefticntaccupl=matchlwith[]->accup|h::t->rfold_lefti(cnt+1)(faccuphcnt)tinrfold_lefti0accul(* Returns all possible paths in the test. The order is from test case
to root
*)lettest_case_pathstest=letrectcpspathtest=matchtestwithTestCase_->[path]|TestListtests->List.concat(mapi(funti->tcps((ListItemi)::path)t)tests)|TestLabel(l,t)->tcps((Labell)::path)tintcps[]test(* Test filtering with their path *)moduleSetTestPath=Set.Make(String)lettest_filteronlytest=letset_test=List.fold_left(funststr->SetTestPath.addstrst)SetTestPath.emptyonlyinletfoldifacclst=List.fold_left(fun(i,acc)e->letnacc=fiaccein(i+1),nacc)acclstinletrecfilter_testpathtst=ifSetTestPath.mem(string_of_pathpath)set_testthen(Sometst)else(matchtstwith|TestCase_->None|TestListtst_lst->let(_,ntst_lst)=foldi(funintst_lsttst->letnntst_lst=matchfilter_test((ListItemi)::path)tstwith|Sometst->tst::ntst_lst|None->ntst_lstinnntst_lst)(0,[])tst_lstinifntst_lst=[]thenNoneelseSome(TestListntst_lst)|TestLabel(lbl,tst)->letntst=filter_test((Labellbl)::path)tstinmatchntstwith|Sometst->Some(TestLabel(lbl,tst))|None->None)infilter_test[]test(* The possible test results *)typetest_result=RSuccessofpath|RFailureofpath*string|RErrorofpath*string|RSkipofpath*string|RTodoofpath*stringletis_success=functionRSuccess_->true|RFailure_|RError_|RSkip_|RTodo_->falseletis_failure=functionRFailure_->true|RSuccess_|RError_|RSkip_|RTodo_->falseletis_error=functionRError_->true|RSuccess_|RFailure_|RSkip_|RTodo_->falseletis_skip=functionRSkip_->true|RSuccess_|RFailure_|RError_|RTodo_->falseletis_todo=functionRTodo_->true|RSuccess_|RFailure_|RError_|RSkip_->falseletresult_flavour=functionRError_->"Error"|RFailure_->"Failure"|RSuccess_->"Success"|RSkip_->"Skip"|RTodo_->"Todo"letresult_path=functionRSuccesspath|RError(path,_)|RFailure(path,_)|RSkip(path,_)|RTodo(path,_)->pathletresult_msg=functionRSuccess_->"Success"|RError(_,msg)|RFailure(_,msg)|RSkip(_,msg)|RTodo(_,msg)->msg(* Returns true if the result list contains successes only *)letrecwas_successfulresults=matchresultswith[]->true|RSuccess_::t|RSkip_::t->was_successfult|RFailure_::_|RError_::_|RTodo_::_->false(* Events which can happen during testing *)typetest_event=EStartofpath|EEndofpath|EResultoftest_result(* Run all tests, report starts, errors, failures, and return the results *)letperform_testreporttest=letrun_test_casefpath=tryf();RSuccesspathwithFailures->RFailure(path,s)|Skips->RSkip(path,s)|Todos->RTodo(path,s)|s->RError(path,(Printexc.to_strings^" "^Printexc.get_backtrace()))inletrecrun_testpathresultstest=matchtestwithTestCase(f)->report(EStartpath);letresult=run_test_casefpathinreport(EResultresult);report(EEndpath);result::results|TestList(tests)->fold_lefti(funresultstcnt->run_test((ListItemcnt)::path)resultst)resultstests|TestLabel(label,t)->run_test((Labellabel)::path)resultstinrun_test[][]test(* Function which runs the given function and returns the running time
of the function, and the original result in a tuple *)lettime_funfxy=letbegin_time=Unix.gettimeofday()in(Unix.gettimeofday()-.begin_time,fxy)(* A simple (currently too simple) text based test runner *)letrun_test_tt?(verbose=false)test=letprintf=Format.printfinletseparator1="======================================================================"inletseparator2="----------------------------------------------------------------------"inletstring_of_result=functionRSuccess_->ifverbosethen"ok\n"else"."|RFailure(_,_)->ifverbosethen"FAIL\n"else"F"|RError(_,_)->ifverbosethen"ERROR\n"else"E"|RSkip(_,_)->ifverbosethen"SKIP\n"else"S"|RTodo(_,_)->ifverbosethen"TODO\n"else"T"inletreport_event=functionEStartp->ifverbosethenprintf"%s ... "(string_of_pathp)|EEnd_->()|EResultresult->printf"%s@?"(string_of_resultresult);inletprint_result_listresults=List.iter(funresult->printf"%s\n%s: %s\n\n%s\n%s\n"separator1(result_flavourresult)(string_of_path(result_pathresult))(result_msgresult)separator2)resultsin(* Now start the test *)letrunning_time,results=time_funperform_testreport_eventtestinleterrors=List.filteris_errorresultsinletfailures=List.filteris_failureresultsinletskips=List.filteris_skipresultsinlettodos=List.filteris_todoresultsinifnotverbosethenprintf"\n";(* Print test report *)print_result_listerrors;print_result_listfailures;printf"Ran: %d tests in: %.2f seconds.\n"(List.lengthresults)running_time;(* Print final verdict *)ifwas_successfulresultsthen(ifskips=[]thenprintf"OK"elseprintf"OK: Cases: %d Skip: %d\n"(test_case_counttest)(List.lengthskips))elseprintf"FAILED: Cases: %d Tried: %d Errors: %d Failures: %d Skip:%d Todo:%d\n"(test_case_counttest)(List.lengthresults)(List.lengtherrors)(List.lengthfailures)(List.lengthskips)(List.lengthtodos);(* Return the results possibly for further processing *)results(* Call this one from you test suites *)letrun_test_tt_mainsuite=letverbose=reffalseinletonly_test=ref[]inArg.parse(Arg.align[("-verbose",Arg.Setverbose," Run the test in verbose mode.");("-only-test",Arg.String(funstr->only_test:=str::!only_test),"path Run only the selected test");])(funx->raise(Arg.Bad("Bad argument : "^x)))("usage: "^Sys.argv.(0)^" [-verbose] [-only-test path]*");letnsuite=if!only_test=[]then(suite)else(matchtest_filter!only_testsuitewith|Sometst->tst|None->failwith("Filtering test "^(String.concat", "!only_test)^" lead to no test"))inletresult=run_test_tt~verbose:!verbosensuiteinifnot(was_successfulresult)thenexit1elseresult