You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doctest.cpp 160KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866
  1. #if defined(DOCTEST_CONFIG_IMPLEMENT) || !defined(DOCTEST_SINGLE_HEADER)
  2. #ifndef DOCTEST_SINGLE_HEADER
  3. #include "doctest_fwd.h"
  4. #endif // DOCTEST_SINGLE_HEADER
  5. DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wunused-macros")
  6. #ifndef DOCTEST_LIBRARY_IMPLEMENTATION
  7. #define DOCTEST_LIBRARY_IMPLEMENTATION
  8. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  9. DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
  10. DOCTEST_CLANG_SUPPRESS_WARNING("-Wunknown-pragmas")
  11. DOCTEST_CLANG_SUPPRESS_WARNING("-Wpadded")
  12. DOCTEST_CLANG_SUPPRESS_WARNING("-Wweak-vtables")
  13. DOCTEST_CLANG_SUPPRESS_WARNING("-Wglobal-constructors")
  14. DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
  15. DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-prototypes")
  16. DOCTEST_CLANG_SUPPRESS_WARNING("-Wsign-conversion")
  17. DOCTEST_CLANG_SUPPRESS_WARNING("-Wshorten-64-to-32")
  18. DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-variable-declarations")
  19. DOCTEST_CLANG_SUPPRESS_WARNING("-Wswitch")
  20. DOCTEST_CLANG_SUPPRESS_WARNING("-Wswitch-enum")
  21. DOCTEST_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default")
  22. DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-noreturn")
  23. DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-local-typedef")
  24. DOCTEST_CLANG_SUPPRESS_WARNING("-Wdisabled-macro-expansion")
  25. DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-braces")
  26. DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-field-initializers")
  27. DOCTEST_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
  28. DOCTEST_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic")
  29. DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-member-function")
  30. DOCTEST_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path")
  31. DOCTEST_GCC_SUPPRESS_WARNING_PUSH
  32. DOCTEST_GCC_SUPPRESS_WARNING("-Wunknown-pragmas")
  33. DOCTEST_GCC_SUPPRESS_WARNING("-Wpragmas")
  34. DOCTEST_GCC_SUPPRESS_WARNING("-Wconversion")
  35. DOCTEST_GCC_SUPPRESS_WARNING("-Weffc++")
  36. DOCTEST_GCC_SUPPRESS_WARNING("-Wsign-conversion")
  37. DOCTEST_GCC_SUPPRESS_WARNING("-Wstrict-overflow")
  38. DOCTEST_GCC_SUPPRESS_WARNING("-Wstrict-aliasing")
  39. DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-field-initializers")
  40. DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-braces")
  41. DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-declarations")
  42. DOCTEST_GCC_SUPPRESS_WARNING("-Wswitch")
  43. DOCTEST_GCC_SUPPRESS_WARNING("-Wswitch-enum")
  44. DOCTEST_GCC_SUPPRESS_WARNING("-Wswitch-default")
  45. DOCTEST_GCC_SUPPRESS_WARNING("-Wunsafe-loop-optimizations")
  46. DOCTEST_GCC_SUPPRESS_WARNING("-Wold-style-cast")
  47. DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-local-typedefs")
  48. DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast")
  49. DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-function")
  50. DOCTEST_GCC_SUPPRESS_WARNING("-Wmultiple-inheritance")
  51. DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept")
  52. DOCTEST_GCC_SUPPRESS_WARNING("-Wsuggest-attribute")
  53. DOCTEST_MSVC_SUPPRESS_WARNING_PUSH
  54. DOCTEST_MSVC_SUPPRESS_WARNING(4616) // invalid compiler warning
  55. DOCTEST_MSVC_SUPPRESS_WARNING(4619) // invalid compiler warning
  56. DOCTEST_MSVC_SUPPRESS_WARNING(4996) // The compiler encountered a deprecated declaration
  57. DOCTEST_MSVC_SUPPRESS_WARNING(4267) // 'var' : conversion from 'x' to 'y', possible loss of data
  58. DOCTEST_MSVC_SUPPRESS_WARNING(4706) // assignment within conditional expression
  59. DOCTEST_MSVC_SUPPRESS_WARNING(4512) // 'class' : assignment operator could not be generated
  60. DOCTEST_MSVC_SUPPRESS_WARNING(4127) // conditional expression is constant
  61. DOCTEST_MSVC_SUPPRESS_WARNING(4530) // C++ exception handler used, but unwind semantics not enabled
  62. DOCTEST_MSVC_SUPPRESS_WARNING(4577) // 'noexcept' used with no exception handling mode specified
  63. DOCTEST_MSVC_SUPPRESS_WARNING(4774) // format string expected in argument is not a string literal
  64. DOCTEST_MSVC_SUPPRESS_WARNING(4365) // conversion from 'int' to 'unsigned', signed/unsigned mismatch
  65. DOCTEST_MSVC_SUPPRESS_WARNING(4820) // padding in structs
  66. DOCTEST_MSVC_SUPPRESS_WARNING(4640) // construction of local static object is not thread-safe
  67. DOCTEST_MSVC_SUPPRESS_WARNING(5039) // pointer to potentially throwing function passed to extern C
  68. DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation stuff
  69. DOCTEST_MSVC_SUPPRESS_WARNING(4626) // assignment operator was implicitly defined as deleted
  70. DOCTEST_MSVC_SUPPRESS_WARNING(5027) // move assignment operator was implicitly defined as deleted
  71. DOCTEST_MSVC_SUPPRESS_WARNING(5026) // move constructor was implicitly defined as deleted
  72. DOCTEST_MSVC_SUPPRESS_WARNING(4625) // copy constructor was implicitly defined as deleted
  73. DOCTEST_MSVC_SUPPRESS_WARNING(4800) // forcing value to bool 'true' or 'false' (performance warning)
  74. // static analysis
  75. DOCTEST_MSVC_SUPPRESS_WARNING(26439) // This kind of function may not throw. Declare it 'noexcept'
  76. DOCTEST_MSVC_SUPPRESS_WARNING(26495) // Always initialize a member variable
  77. DOCTEST_MSVC_SUPPRESS_WARNING(26451) // Arithmetic overflow ...
  78. DOCTEST_MSVC_SUPPRESS_WARNING(26444) // Avoid unnamed objects with custom construction and dtor...
  79. DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum'
  80. DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
  81. // required includes - will go only in one translation unit!
  82. #include <ctime>
  83. #include <cmath>
  84. #include <climits>
  85. // borland (Embarcadero) compiler requires math.h and not cmath - https://github.com/onqtam/doctest/pull/37
  86. #ifdef __BORLANDC__
  87. #include <math.h>
  88. #endif // __BORLANDC__
  89. #include <new>
  90. #include <cstdio>
  91. #include <cstdlib>
  92. #include <cstring>
  93. #include <limits>
  94. #include <utility>
  95. #include <fstream>
  96. #include <sstream>
  97. #include <iostream>
  98. #include <algorithm>
  99. #include <iomanip>
  100. #include <vector>
  101. #include <atomic>
  102. #include <mutex>
  103. #include <set>
  104. #include <map>
  105. #include <exception>
  106. #include <stdexcept>
  107. #include <csignal>
  108. #include <cfloat>
  109. #include <cctype>
  110. #include <cstdint>
  111. #ifdef DOCTEST_PLATFORM_MAC
  112. #include <sys/types.h>
  113. #include <unistd.h>
  114. #include <sys/sysctl.h>
  115. #endif // DOCTEST_PLATFORM_MAC
  116. #ifdef DOCTEST_PLATFORM_WINDOWS
  117. // defines for a leaner windows.h
  118. #ifndef WIN32_LEAN_AND_MEAN
  119. #define WIN32_LEAN_AND_MEAN
  120. #endif // WIN32_LEAN_AND_MEAN
  121. #ifndef NOMINMAX
  122. #define NOMINMAX
  123. #endif // NOMINMAX
  124. // not sure what AfxWin.h is for - here I do what Catch does
  125. #ifdef __AFXDLL
  126. #include <AfxWin.h>
  127. #else
  128. #include <windows.h>
  129. #endif
  130. #include <io.h>
  131. #else // DOCTEST_PLATFORM_WINDOWS
  132. #include <sys/time.h>
  133. #include <unistd.h>
  134. #endif // DOCTEST_PLATFORM_WINDOWS
  135. // this is a fix for https://github.com/onqtam/doctest/issues/348
  136. // https://mail.gnome.org/archives/xml/2012-January/msg00000.html
  137. #if !defined(HAVE_UNISTD_H) && !defined(STDOUT_FILENO)
  138. #define STDOUT_FILENO fileno(stdout)
  139. #endif // HAVE_UNISTD_H
  140. DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
  141. // counts the number of elements in a C array
  142. #define DOCTEST_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
  143. #ifdef DOCTEST_CONFIG_DISABLE
  144. #define DOCTEST_BRANCH_ON_DISABLED(if_disabled, if_not_disabled) if_disabled
  145. #else // DOCTEST_CONFIG_DISABLE
  146. #define DOCTEST_BRANCH_ON_DISABLED(if_disabled, if_not_disabled) if_not_disabled
  147. #endif // DOCTEST_CONFIG_DISABLE
  148. #ifndef DOCTEST_CONFIG_OPTIONS_PREFIX
  149. #define DOCTEST_CONFIG_OPTIONS_PREFIX "dt-"
  150. #endif
  151. #ifndef DOCTEST_THREAD_LOCAL
  152. #define DOCTEST_THREAD_LOCAL thread_local
  153. #endif
  154. #ifndef DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES
  155. #define DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES 32
  156. #endif
  157. #ifndef DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE
  158. #define DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE 64
  159. #endif
  160. #ifdef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  161. #define DOCTEST_OPTIONS_PREFIX_DISPLAY DOCTEST_CONFIG_OPTIONS_PREFIX
  162. #else
  163. #define DOCTEST_OPTIONS_PREFIX_DISPLAY ""
  164. #endif
  165. #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
  166. #define DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
  167. #endif
  168. namespace doctest {
  169. bool is_running_in_test = false;
  170. namespace {
  171. using namespace detail;
  172. // case insensitive strcmp
  173. int stricmp(const char* a, const char* b) {
  174. for(;; a++, b++) {
  175. const int d = tolower(*a) - tolower(*b);
  176. if(d != 0 || !*a)
  177. return d;
  178. }
  179. }
  180. template <typename T>
  181. String fpToString(T value, int precision) {
  182. std::ostringstream oss;
  183. oss << std::setprecision(precision) << std::fixed << value;
  184. std::string d = oss.str();
  185. size_t i = d.find_last_not_of('0');
  186. if(i != std::string::npos && i != d.size() - 1) {
  187. if(d[i] == '.')
  188. i++;
  189. d = d.substr(0, i + 1);
  190. }
  191. return d.c_str();
  192. }
  193. struct Endianness
  194. {
  195. enum Arch
  196. {
  197. Big,
  198. Little
  199. };
  200. static Arch which() {
  201. int x = 1;
  202. // casting any data pointer to char* is allowed
  203. auto ptr = reinterpret_cast<char*>(&x);
  204. if(*ptr)
  205. return Little;
  206. return Big;
  207. }
  208. };
  209. } // namespace
  210. namespace detail {
  211. void my_memcpy(void* dest, const void* src, unsigned num) { memcpy(dest, src, num); }
  212. String rawMemoryToString(const void* object, unsigned size) {
  213. // Reverse order for little endian architectures
  214. int i = 0, end = static_cast<int>(size), inc = 1;
  215. if(Endianness::which() == Endianness::Little) {
  216. i = end - 1;
  217. end = inc = -1;
  218. }
  219. unsigned const char* bytes = static_cast<unsigned const char*>(object);
  220. std::ostringstream oss;
  221. oss << "0x" << std::setfill('0') << std::hex;
  222. for(; i != end; i += inc)
  223. oss << std::setw(2) << static_cast<unsigned>(bytes[i]);
  224. return oss.str().c_str();
  225. }
  226. DOCTEST_THREAD_LOCAL std::ostringstream g_oss; // NOLINT(cert-err58-cpp)
  227. std::ostream* getTlsOss() {
  228. g_oss.clear(); // there shouldn't be anything worth clearing in the flags
  229. g_oss.str(""); // the slow way of resetting a string stream
  230. //g_oss.seekp(0); // optimal reset - as seen here: https://stackoverflow.com/a/624291/3162383
  231. return &g_oss;
  232. }
  233. String getTlsOssResult() {
  234. //g_oss << std::ends; // needed - as shown here: https://stackoverflow.com/a/624291/3162383
  235. return g_oss.str().c_str();
  236. }
  237. #ifndef DOCTEST_CONFIG_DISABLE
  238. namespace timer_large_integer
  239. {
  240. #if defined(DOCTEST_PLATFORM_WINDOWS)
  241. typedef ULONGLONG type;
  242. #else // DOCTEST_PLATFORM_WINDOWS
  243. using namespace std;
  244. typedef uint64_t type;
  245. #endif // DOCTEST_PLATFORM_WINDOWS
  246. }
  247. typedef timer_large_integer::type ticks_t;
  248. #ifdef DOCTEST_CONFIG_GETCURRENTTICKS
  249. ticks_t getCurrentTicks() { return DOCTEST_CONFIG_GETCURRENTTICKS(); }
  250. #elif defined(DOCTEST_PLATFORM_WINDOWS)
  251. ticks_t getCurrentTicks() {
  252. static LARGE_INTEGER hz = {0}, hzo = {0};
  253. if(!hz.QuadPart) {
  254. QueryPerformanceFrequency(&hz);
  255. QueryPerformanceCounter(&hzo);
  256. }
  257. LARGE_INTEGER t;
  258. QueryPerformanceCounter(&t);
  259. return ((t.QuadPart - hzo.QuadPart) * LONGLONG(1000000)) / hz.QuadPart;
  260. }
  261. #else // DOCTEST_PLATFORM_WINDOWS
  262. ticks_t getCurrentTicks() {
  263. timeval t;
  264. gettimeofday(&t, nullptr);
  265. return static_cast<ticks_t>(t.tv_sec) * 1000000 + static_cast<ticks_t>(t.tv_usec);
  266. }
  267. #endif // DOCTEST_PLATFORM_WINDOWS
  268. struct Timer
  269. {
  270. void start() { m_ticks = getCurrentTicks(); }
  271. unsigned int getElapsedMicroseconds() const {
  272. return static_cast<unsigned int>(getCurrentTicks() - m_ticks);
  273. }
  274. //unsigned int getElapsedMilliseconds() const {
  275. // return static_cast<unsigned int>(getElapsedMicroseconds() / 1000);
  276. //}
  277. double getElapsedSeconds() const { return static_cast<double>(getCurrentTicks() - m_ticks) / 1000000.0; }
  278. private:
  279. ticks_t m_ticks = 0;
  280. };
  281. #ifdef DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
  282. template <typename T>
  283. using AtomicOrMultiLaneAtomic = std::atomic<T>;
  284. #else // DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
  285. // Provides a multilane implementation of an atomic variable that supports add, sub, load,
  286. // store. Instead of using a single atomic variable, this splits up into multiple ones,
  287. // each sitting on a separate cache line. The goal is to provide a speedup when most
  288. // operations are modifying. It achieves this with two properties:
  289. //
  290. // * Multiple atomics are used, so chance of congestion from the same atomic is reduced.
  291. // * Each atomic sits on a separate cache line, so false sharing is reduced.
  292. //
  293. // The disadvantage is that there is a small overhead due to the use of TLS, and load/store
  294. // is slower because all atomics have to be accessed.
  295. template <typename T>
  296. class MultiLaneAtomic
  297. {
  298. struct CacheLineAlignedAtomic
  299. {
  300. std::atomic<T> atomic{};
  301. char padding[DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE - sizeof(std::atomic<T>)];
  302. };
  303. CacheLineAlignedAtomic m_atomics[DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES];
  304. static_assert(sizeof(CacheLineAlignedAtomic) == DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE,
  305. "guarantee one atomic takes exactly one cache line");
  306. public:
  307. T operator++() DOCTEST_NOEXCEPT { return fetch_add(1) + 1; }
  308. T operator++(int) DOCTEST_NOEXCEPT { return fetch_add(1); }
  309. T fetch_add(T arg, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT {
  310. return myAtomic().fetch_add(arg, order);
  311. }
  312. T fetch_sub(T arg, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT {
  313. return myAtomic().fetch_sub(arg, order);
  314. }
  315. operator T() const DOCTEST_NOEXCEPT { return load(); }
  316. T load(std::memory_order order = std::memory_order_seq_cst) const DOCTEST_NOEXCEPT {
  317. auto result = T();
  318. for(auto const& c : m_atomics) {
  319. result += c.atomic.load(order);
  320. }
  321. return result;
  322. }
  323. T operator=(T desired) DOCTEST_NOEXCEPT {
  324. store(desired);
  325. return desired;
  326. }
  327. void store(T desired, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT {
  328. // first value becomes desired", all others become 0.
  329. for(auto& c : m_atomics) {
  330. c.atomic.store(desired, order);
  331. desired = {};
  332. }
  333. }
  334. private:
  335. // Each thread has a different atomic that it operates on. If more than NumLanes threads
  336. // use this, some will use the same atomic. So performance will degrate a bit, but still
  337. // everything will work.
  338. //
  339. // The logic here is a bit tricky. The call should be as fast as possible, so that there
  340. // is minimal to no overhead in determining the correct atomic for the current thread.
  341. //
  342. // 1. A global static counter laneCounter counts continuously up.
  343. // 2. Each successive thread will use modulo operation of that counter so it gets an atomic
  344. // assigned in a round-robin fashion.
  345. // 3. This tlsLaneIdx is stored in the thread local data, so it is directly available with
  346. // little overhead.
  347. std::atomic<T>& myAtomic() DOCTEST_NOEXCEPT {
  348. static std::atomic<size_t> laneCounter;
  349. DOCTEST_THREAD_LOCAL size_t tlsLaneIdx =
  350. laneCounter++ % DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES;
  351. return m_atomics[tlsLaneIdx].atomic;
  352. }
  353. };
  354. template <typename T>
  355. using AtomicOrMultiLaneAtomic = MultiLaneAtomic<T>;
  356. #endif // DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
  357. // this holds both parameters from the command line and runtime data for tests
  358. struct ContextState : ContextOptions, TestRunStats, CurrentTestCaseStats
  359. {
  360. AtomicOrMultiLaneAtomic<int> numAssertsCurrentTest_atomic;
  361. AtomicOrMultiLaneAtomic<int> numAssertsFailedCurrentTest_atomic;
  362. std::vector<std::vector<String>> filters = decltype(filters)(9); // 9 different filters
  363. std::vector<IReporter*> reporters_currently_used;
  364. assert_handler ah = nullptr;
  365. Timer timer;
  366. std::vector<String> stringifiedContexts; // logging from INFO() due to an exception
  367. // stuff for subcases
  368. std::vector<SubcaseSignature> subcasesStack;
  369. std::set<decltype(subcasesStack)> subcasesPassed;
  370. int subcasesCurrentMaxLevel;
  371. bool should_reenter;
  372. std::atomic<bool> shouldLogCurrentException;
  373. void resetRunData() {
  374. numTestCases = 0;
  375. numTestCasesPassingFilters = 0;
  376. numTestSuitesPassingFilters = 0;
  377. numTestCasesFailed = 0;
  378. numAsserts = 0;
  379. numAssertsFailed = 0;
  380. numAssertsCurrentTest = 0;
  381. numAssertsFailedCurrentTest = 0;
  382. }
  383. void finalizeTestCaseData() {
  384. seconds = timer.getElapsedSeconds();
  385. // update the non-atomic counters
  386. numAsserts += numAssertsCurrentTest_atomic;
  387. numAssertsFailed += numAssertsFailedCurrentTest_atomic;
  388. numAssertsCurrentTest = numAssertsCurrentTest_atomic;
  389. numAssertsFailedCurrentTest = numAssertsFailedCurrentTest_atomic;
  390. if(numAssertsFailedCurrentTest)
  391. failure_flags |= TestCaseFailureReason::AssertFailure;
  392. if(Approx(currentTest->m_timeout).epsilon(DBL_EPSILON) != 0 &&
  393. Approx(seconds).epsilon(DBL_EPSILON) > currentTest->m_timeout)
  394. failure_flags |= TestCaseFailureReason::Timeout;
  395. if(currentTest->m_should_fail) {
  396. if(failure_flags) {
  397. failure_flags |= TestCaseFailureReason::ShouldHaveFailedAndDid;
  398. } else {
  399. failure_flags |= TestCaseFailureReason::ShouldHaveFailedButDidnt;
  400. }
  401. } else if(failure_flags && currentTest->m_may_fail) {
  402. failure_flags |= TestCaseFailureReason::CouldHaveFailedAndDid;
  403. } else if(currentTest->m_expected_failures > 0) {
  404. if(numAssertsFailedCurrentTest == currentTest->m_expected_failures) {
  405. failure_flags |= TestCaseFailureReason::FailedExactlyNumTimes;
  406. } else {
  407. failure_flags |= TestCaseFailureReason::DidntFailExactlyNumTimes;
  408. }
  409. }
  410. bool ok_to_fail = (TestCaseFailureReason::ShouldHaveFailedAndDid & failure_flags) ||
  411. (TestCaseFailureReason::CouldHaveFailedAndDid & failure_flags) ||
  412. (TestCaseFailureReason::FailedExactlyNumTimes & failure_flags);
  413. // if any subcase has failed - the whole test case has failed
  414. if(failure_flags && !ok_to_fail)
  415. numTestCasesFailed++;
  416. }
  417. };
  418. ContextState* g_cs = nullptr;
  419. // used to avoid locks for the debug output
  420. // TODO: figure out if this is indeed necessary/correct - seems like either there still
  421. // could be a race or that there wouldn't be a race even if using the context directly
  422. DOCTEST_THREAD_LOCAL bool g_no_colors;
  423. #endif // DOCTEST_CONFIG_DISABLE
  424. } // namespace detail
  425. void String::setOnHeap() { *reinterpret_cast<unsigned char*>(&buf[last]) = 128; }
  426. void String::setLast(unsigned in) { buf[last] = char(in); }
  427. void String::copy(const String& other) {
  428. using namespace std;
  429. if(other.isOnStack()) {
  430. memcpy(buf, other.buf, len);
  431. } else {
  432. setOnHeap();
  433. data.size = other.data.size;
  434. data.capacity = data.size + 1;
  435. data.ptr = new char[data.capacity];
  436. memcpy(data.ptr, other.data.ptr, data.size + 1);
  437. }
  438. }
  439. String::String() {
  440. buf[0] = '\0';
  441. setLast();
  442. }
  443. String::~String() {
  444. if(!isOnStack())
  445. delete[] data.ptr;
  446. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  447. }
  448. String::String(const char* in)
  449. : String(in, strlen(in)) {}
  450. String::String(const char* in, unsigned in_size) {
  451. using namespace std;
  452. if(in_size <= last) {
  453. memcpy(buf, in, in_size);
  454. buf[in_size] = '\0';
  455. setLast(last - in_size);
  456. } else {
  457. setOnHeap();
  458. data.size = in_size;
  459. data.capacity = data.size + 1;
  460. data.ptr = new char[data.capacity];
  461. memcpy(data.ptr, in, in_size);
  462. data.ptr[in_size] = '\0';
  463. }
  464. }
  465. String::String(const String& other) { copy(other); }
  466. String& String::operator=(const String& other) {
  467. if(this != &other) {
  468. if(!isOnStack())
  469. delete[] data.ptr;
  470. copy(other);
  471. }
  472. return *this;
  473. }
  474. String& String::operator+=(const String& other) {
  475. const unsigned my_old_size = size();
  476. const unsigned other_size = other.size();
  477. const unsigned total_size = my_old_size + other_size;
  478. using namespace std;
  479. if(isOnStack()) {
  480. if(total_size < len) {
  481. // append to the current stack space
  482. memcpy(buf + my_old_size, other.c_str(), other_size + 1);
  483. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  484. setLast(last - total_size);
  485. } else {
  486. // alloc new chunk
  487. char* temp = new char[total_size + 1];
  488. // copy current data to new location before writing in the union
  489. memcpy(temp, buf, my_old_size); // skip the +1 ('\0') for speed
  490. // update data in union
  491. setOnHeap();
  492. data.size = total_size;
  493. data.capacity = data.size + 1;
  494. data.ptr = temp;
  495. // transfer the rest of the data
  496. memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
  497. }
  498. } else {
  499. if(data.capacity > total_size) {
  500. // append to the current heap block
  501. data.size = total_size;
  502. memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
  503. } else {
  504. // resize
  505. data.capacity *= 2;
  506. if(data.capacity <= total_size)
  507. data.capacity = total_size + 1;
  508. // alloc new chunk
  509. char* temp = new char[data.capacity];
  510. // copy current data to new location before releasing it
  511. memcpy(temp, data.ptr, my_old_size); // skip the +1 ('\0') for speed
  512. // release old chunk
  513. delete[] data.ptr;
  514. // update the rest of the union members
  515. data.size = total_size;
  516. data.ptr = temp;
  517. // transfer the rest of the data
  518. memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
  519. }
  520. }
  521. return *this;
  522. }
  523. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  524. String String::operator+(const String& other) const { return String(*this) += other; }
  525. String::String(String&& other) {
  526. using namespace std;
  527. memcpy(buf, other.buf, len);
  528. other.buf[0] = '\0';
  529. other.setLast();
  530. }
  531. String& String::operator=(String&& other) {
  532. using namespace std;
  533. if(this != &other) {
  534. if(!isOnStack())
  535. delete[] data.ptr;
  536. memcpy(buf, other.buf, len);
  537. other.buf[0] = '\0';
  538. other.setLast();
  539. }
  540. return *this;
  541. }
  542. char String::operator[](unsigned i) const {
  543. return const_cast<String*>(this)->operator[](i); // NOLINT
  544. }
  545. char& String::operator[](unsigned i) {
  546. if(isOnStack())
  547. return reinterpret_cast<char*>(buf)[i];
  548. return data.ptr[i];
  549. }
  550. DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wmaybe-uninitialized")
  551. unsigned String::size() const {
  552. if(isOnStack())
  553. return last - (unsigned(buf[last]) & 31); // using "last" would work only if "len" is 32
  554. return data.size;
  555. }
  556. DOCTEST_GCC_SUPPRESS_WARNING_POP
  557. unsigned String::capacity() const {
  558. if(isOnStack())
  559. return len;
  560. return data.capacity;
  561. }
  562. int String::compare(const char* other, bool no_case) const {
  563. if(no_case)
  564. return doctest::stricmp(c_str(), other);
  565. return std::strcmp(c_str(), other);
  566. }
  567. int String::compare(const String& other, bool no_case) const {
  568. return compare(other.c_str(), no_case);
  569. }
  570. // clang-format off
  571. bool operator==(const String& lhs, const String& rhs) { return lhs.compare(rhs) == 0; }
  572. bool operator!=(const String& lhs, const String& rhs) { return lhs.compare(rhs) != 0; }
  573. bool operator< (const String& lhs, const String& rhs) { return lhs.compare(rhs) < 0; }
  574. bool operator> (const String& lhs, const String& rhs) { return lhs.compare(rhs) > 0; }
  575. bool operator<=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) < 0 : true; }
  576. bool operator>=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) > 0 : true; }
  577. // clang-format on
  578. std::ostream& operator<<(std::ostream& s, const String& in) { return s << in.c_str(); }
  579. namespace {
  580. void color_to_stream(std::ostream&, Color::Enum) DOCTEST_BRANCH_ON_DISABLED({}, ;)
  581. } // namespace
  582. namespace Color {
  583. std::ostream& operator<<(std::ostream& s, Color::Enum code) {
  584. color_to_stream(s, code);
  585. return s;
  586. }
  587. } // namespace Color
  588. // clang-format off
  589. const char* assertString(assertType::Enum at) {
  590. DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4062) // enum 'x' in switch of enum 'y' is not handled
  591. switch(at) { //!OCLINT missing default in switch statements
  592. case assertType::DT_WARN : return "WARN";
  593. case assertType::DT_CHECK : return "CHECK";
  594. case assertType::DT_REQUIRE : return "REQUIRE";
  595. case assertType::DT_WARN_FALSE : return "WARN_FALSE";
  596. case assertType::DT_CHECK_FALSE : return "CHECK_FALSE";
  597. case assertType::DT_REQUIRE_FALSE : return "REQUIRE_FALSE";
  598. case assertType::DT_WARN_THROWS : return "WARN_THROWS";
  599. case assertType::DT_CHECK_THROWS : return "CHECK_THROWS";
  600. case assertType::DT_REQUIRE_THROWS : return "REQUIRE_THROWS";
  601. case assertType::DT_WARN_THROWS_AS : return "WARN_THROWS_AS";
  602. case assertType::DT_CHECK_THROWS_AS : return "CHECK_THROWS_AS";
  603. case assertType::DT_REQUIRE_THROWS_AS : return "REQUIRE_THROWS_AS";
  604. case assertType::DT_WARN_THROWS_WITH : return "WARN_THROWS_WITH";
  605. case assertType::DT_CHECK_THROWS_WITH : return "CHECK_THROWS_WITH";
  606. case assertType::DT_REQUIRE_THROWS_WITH : return "REQUIRE_THROWS_WITH";
  607. case assertType::DT_WARN_THROWS_WITH_AS : return "WARN_THROWS_WITH_AS";
  608. case assertType::DT_CHECK_THROWS_WITH_AS : return "CHECK_THROWS_WITH_AS";
  609. case assertType::DT_REQUIRE_THROWS_WITH_AS : return "REQUIRE_THROWS_WITH_AS";
  610. case assertType::DT_WARN_NOTHROW : return "WARN_NOTHROW";
  611. case assertType::DT_CHECK_NOTHROW : return "CHECK_NOTHROW";
  612. case assertType::DT_REQUIRE_NOTHROW : return "REQUIRE_NOTHROW";
  613. case assertType::DT_WARN_EQ : return "WARN_EQ";
  614. case assertType::DT_CHECK_EQ : return "CHECK_EQ";
  615. case assertType::DT_REQUIRE_EQ : return "REQUIRE_EQ";
  616. case assertType::DT_WARN_NE : return "WARN_NE";
  617. case assertType::DT_CHECK_NE : return "CHECK_NE";
  618. case assertType::DT_REQUIRE_NE : return "REQUIRE_NE";
  619. case assertType::DT_WARN_GT : return "WARN_GT";
  620. case assertType::DT_CHECK_GT : return "CHECK_GT";
  621. case assertType::DT_REQUIRE_GT : return "REQUIRE_GT";
  622. case assertType::DT_WARN_LT : return "WARN_LT";
  623. case assertType::DT_CHECK_LT : return "CHECK_LT";
  624. case assertType::DT_REQUIRE_LT : return "REQUIRE_LT";
  625. case assertType::DT_WARN_GE : return "WARN_GE";
  626. case assertType::DT_CHECK_GE : return "CHECK_GE";
  627. case assertType::DT_REQUIRE_GE : return "REQUIRE_GE";
  628. case assertType::DT_WARN_LE : return "WARN_LE";
  629. case assertType::DT_CHECK_LE : return "CHECK_LE";
  630. case assertType::DT_REQUIRE_LE : return "REQUIRE_LE";
  631. case assertType::DT_WARN_UNARY : return "WARN_UNARY";
  632. case assertType::DT_CHECK_UNARY : return "CHECK_UNARY";
  633. case assertType::DT_REQUIRE_UNARY : return "REQUIRE_UNARY";
  634. case assertType::DT_WARN_UNARY_FALSE : return "WARN_UNARY_FALSE";
  635. case assertType::DT_CHECK_UNARY_FALSE : return "CHECK_UNARY_FALSE";
  636. case assertType::DT_REQUIRE_UNARY_FALSE : return "REQUIRE_UNARY_FALSE";
  637. }
  638. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  639. return "";
  640. }
  641. // clang-format on
  642. const char* failureString(assertType::Enum at) {
  643. if(at & assertType::is_warn) //!OCLINT bitwise operator in conditional
  644. return "WARNING";
  645. if(at & assertType::is_check) //!OCLINT bitwise operator in conditional
  646. return "ERROR";
  647. if(at & assertType::is_require) //!OCLINT bitwise operator in conditional
  648. return "FATAL ERROR";
  649. return "";
  650. }
  651. DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wnull-dereference")
  652. DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wnull-dereference")
  653. // depending on the current options this will remove the path of filenames
  654. const char* skipPathFromFilename(const char* file) {
  655. #ifndef DOCTEST_CONFIG_DISABLE
  656. if(getContextOptions()->no_path_in_filenames) {
  657. auto back = std::strrchr(file, '\\');
  658. auto forward = std::strrchr(file, '/');
  659. if(back || forward) {
  660. if(back > forward)
  661. forward = back;
  662. return forward + 1;
  663. }
  664. }
  665. #endif // DOCTEST_CONFIG_DISABLE
  666. return file;
  667. }
  668. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  669. DOCTEST_GCC_SUPPRESS_WARNING_POP
  670. bool SubcaseSignature::operator<(const SubcaseSignature& other) const {
  671. if(m_line != other.m_line)
  672. return m_line < other.m_line;
  673. if(std::strcmp(m_file, other.m_file) != 0)
  674. return std::strcmp(m_file, other.m_file) < 0;
  675. return m_name.compare(other.m_name) < 0;
  676. }
  677. IContextScope::IContextScope() = default;
  678. IContextScope::~IContextScope() = default;
  679. #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  680. String toString(char* in) { return toString(static_cast<const char*>(in)); }
  681. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  682. String toString(const char* in) { return String("\"") + (in ? in : "{null string}") + "\""; }
  683. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  684. String toString(bool in) { return in ? "true" : "false"; }
  685. String toString(float in) { return fpToString(in, 5) + "f"; }
  686. String toString(double in) { return fpToString(in, 10); }
  687. String toString(double long in) { return fpToString(in, 15); }
  688. #define DOCTEST_TO_STRING_OVERLOAD(type, fmt) \
  689. String toString(type in) { \
  690. char buf[64]; \
  691. std::sprintf(buf, fmt, in); \
  692. return buf; \
  693. }
  694. DOCTEST_TO_STRING_OVERLOAD(char, "%d")
  695. DOCTEST_TO_STRING_OVERLOAD(char signed, "%d")
  696. DOCTEST_TO_STRING_OVERLOAD(char unsigned, "%u")
  697. DOCTEST_TO_STRING_OVERLOAD(int short, "%d")
  698. DOCTEST_TO_STRING_OVERLOAD(int short unsigned, "%u")
  699. DOCTEST_TO_STRING_OVERLOAD(int, "%d")
  700. DOCTEST_TO_STRING_OVERLOAD(unsigned, "%u")
  701. DOCTEST_TO_STRING_OVERLOAD(int long, "%ld")
  702. DOCTEST_TO_STRING_OVERLOAD(int long unsigned, "%lu")
  703. DOCTEST_TO_STRING_OVERLOAD(int long long, "%lld")
  704. DOCTEST_TO_STRING_OVERLOAD(int long long unsigned, "%llu")
  705. String toString(std::nullptr_t) { return "NULL"; }
  706. #if DOCTEST_MSVC >= DOCTEST_COMPILER(19, 20, 0)
  707. // see this issue on why this is needed: https://github.com/onqtam/doctest/issues/183
  708. String toString(const std::string& in) { return in.c_str(); }
  709. #endif // VS 2019
  710. Approx::Approx(double value)
  711. : m_epsilon(static_cast<double>(std::numeric_limits<float>::epsilon()) * 100)
  712. , m_scale(1.0)
  713. , m_value(value) {}
  714. Approx Approx::operator()(double value) const {
  715. Approx approx(value);
  716. approx.epsilon(m_epsilon);
  717. approx.scale(m_scale);
  718. return approx;
  719. }
  720. Approx& Approx::epsilon(double newEpsilon) {
  721. m_epsilon = newEpsilon;
  722. return *this;
  723. }
  724. Approx& Approx::scale(double newScale) {
  725. m_scale = newScale;
  726. return *this;
  727. }
  728. bool operator==(double lhs, const Approx& rhs) {
  729. // Thanks to Richard Harris for his help refining this formula
  730. return std::fabs(lhs - rhs.m_value) <
  731. rhs.m_epsilon * (rhs.m_scale + std::max<double>(std::fabs(lhs), std::fabs(rhs.m_value)));
  732. }
  733. bool operator==(const Approx& lhs, double rhs) { return operator==(rhs, lhs); }
  734. bool operator!=(double lhs, const Approx& rhs) { return !operator==(lhs, rhs); }
  735. bool operator!=(const Approx& lhs, double rhs) { return !operator==(rhs, lhs); }
  736. bool operator<=(double lhs, const Approx& rhs) { return lhs < rhs.m_value || lhs == rhs; }
  737. bool operator<=(const Approx& lhs, double rhs) { return lhs.m_value < rhs || lhs == rhs; }
  738. bool operator>=(double lhs, const Approx& rhs) { return lhs > rhs.m_value || lhs == rhs; }
  739. bool operator>=(const Approx& lhs, double rhs) { return lhs.m_value > rhs || lhs == rhs; }
  740. bool operator<(double lhs, const Approx& rhs) { return lhs < rhs.m_value && lhs != rhs; }
  741. bool operator<(const Approx& lhs, double rhs) { return lhs.m_value < rhs && lhs != rhs; }
  742. bool operator>(double lhs, const Approx& rhs) { return lhs > rhs.m_value && lhs != rhs; }
  743. bool operator>(const Approx& lhs, double rhs) { return lhs.m_value > rhs && lhs != rhs; }
  744. String toString(const Approx& in) {
  745. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  746. return String("Approx( ") + doctest::toString(in.m_value) + " )";
  747. }
  748. const ContextOptions* getContextOptions() { return DOCTEST_BRANCH_ON_DISABLED(nullptr, g_cs); }
  749. } // namespace doctest
  750. #ifdef DOCTEST_CONFIG_DISABLE
  751. namespace doctest {
  752. Context::Context(int, const char* const*) {}
  753. Context::~Context() = default;
  754. void Context::applyCommandLine(int, const char* const*) {}
  755. void Context::addFilter(const char*, const char*) {}
  756. void Context::clearFilters() {}
  757. void Context::setOption(const char*, int) {}
  758. void Context::setOption(const char*, const char*) {}
  759. bool Context::shouldExit() { return false; }
  760. void Context::setAsDefaultForAssertsOutOfTestCases() {}
  761. void Context::setAssertHandler(detail::assert_handler) {}
  762. int Context::run() { return 0; }
  763. IReporter::~IReporter() = default;
  764. int IReporter::get_num_active_contexts() { return 0; }
  765. const IContextScope* const* IReporter::get_active_contexts() { return nullptr; }
  766. int IReporter::get_num_stringified_contexts() { return 0; }
  767. const String* IReporter::get_stringified_contexts() { return nullptr; }
  768. int registerReporter(const char*, int, IReporter*) { return 0; }
  769. } // namespace doctest
  770. #else // DOCTEST_CONFIG_DISABLE
  771. #if !defined(DOCTEST_CONFIG_COLORS_NONE)
  772. #if !defined(DOCTEST_CONFIG_COLORS_WINDOWS) && !defined(DOCTEST_CONFIG_COLORS_ANSI)
  773. #ifdef DOCTEST_PLATFORM_WINDOWS
  774. #define DOCTEST_CONFIG_COLORS_WINDOWS
  775. #else // linux
  776. #define DOCTEST_CONFIG_COLORS_ANSI
  777. #endif // platform
  778. #endif // DOCTEST_CONFIG_COLORS_WINDOWS && DOCTEST_CONFIG_COLORS_ANSI
  779. #endif // DOCTEST_CONFIG_COLORS_NONE
  780. namespace doctest_detail_test_suite_ns {
  781. // holds the current test suite
  782. doctest::detail::TestSuite& getCurrentTestSuite() {
  783. static doctest::detail::TestSuite data{};
  784. return data;
  785. }
  786. } // namespace doctest_detail_test_suite_ns
  787. namespace doctest {
  788. namespace {
  789. // the int (priority) is part of the key for automatic sorting - sadly one can register a
  790. // reporter with a duplicate name and a different priority but hopefully that won't happen often :|
  791. typedef std::map<std::pair<int, String>, reporterCreatorFunc> reporterMap;
  792. reporterMap& getReporters() {
  793. static reporterMap data;
  794. return data;
  795. }
  796. reporterMap& getListeners() {
  797. static reporterMap data;
  798. return data;
  799. }
  800. } // namespace
  801. namespace detail {
  802. #define DOCTEST_ITERATE_THROUGH_REPORTERS(function, ...) \
  803. for(auto& curr_rep : g_cs->reporters_currently_used) \
  804. curr_rep->function(__VA_ARGS__)
  805. bool checkIfShouldThrow(assertType::Enum at) {
  806. if(at & assertType::is_require) //!OCLINT bitwise operator in conditional
  807. return true;
  808. if((at & assertType::is_check) //!OCLINT bitwise operator in conditional
  809. && getContextOptions()->abort_after > 0 &&
  810. (g_cs->numAssertsFailed + g_cs->numAssertsFailedCurrentTest_atomic) >=
  811. getContextOptions()->abort_after)
  812. return true;
  813. return false;
  814. }
  815. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  816. DOCTEST_NORETURN void throwException() {
  817. g_cs->shouldLogCurrentException = false;
  818. throw TestFailureException();
  819. } // NOLINT(cert-err60-cpp)
  820. #else // DOCTEST_CONFIG_NO_EXCEPTIONS
  821. void throwException() {}
  822. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  823. } // namespace detail
  824. namespace {
  825. using namespace detail;
  826. // matching of a string against a wildcard mask (case sensitivity configurable) taken from
  827. // https://www.codeproject.com/Articles/1088/Wildcard-string-compare-globbing
  828. int wildcmp(const char* str, const char* wild, bool caseSensitive) {
  829. const char* cp = str;
  830. const char* mp = wild;
  831. while((*str) && (*wild != '*')) {
  832. if((caseSensitive ? (*wild != *str) : (tolower(*wild) != tolower(*str))) &&
  833. (*wild != '?')) {
  834. return 0;
  835. }
  836. wild++;
  837. str++;
  838. }
  839. while(*str) {
  840. if(*wild == '*') {
  841. if(!*++wild) {
  842. return 1;
  843. }
  844. mp = wild;
  845. cp = str + 1;
  846. } else if((caseSensitive ? (*wild == *str) : (tolower(*wild) == tolower(*str))) ||
  847. (*wild == '?')) {
  848. wild++;
  849. str++;
  850. } else {
  851. wild = mp; //!OCLINT parameter reassignment
  852. str = cp++; //!OCLINT parameter reassignment
  853. }
  854. }
  855. while(*wild == '*') {
  856. wild++;
  857. }
  858. return !*wild;
  859. }
  860. //// C string hash function (djb2) - taken from http://www.cse.yorku.ca/~oz/hash.html
  861. //unsigned hashStr(unsigned const char* str) {
  862. // unsigned long hash = 5381;
  863. // char c;
  864. // while((c = *str++))
  865. // hash = ((hash << 5) + hash) + c; // hash * 33 + c
  866. // return hash;
  867. //}
  868. // checks if the name matches any of the filters (and can be configured what to do when empty)
  869. bool matchesAny(const char* name, const std::vector<String>& filters, bool matchEmpty,
  870. bool caseSensitive) {
  871. if(filters.empty() && matchEmpty)
  872. return true;
  873. for(auto& curr : filters)
  874. if(wildcmp(name, curr.c_str(), caseSensitive))
  875. return true;
  876. return false;
  877. }
  878. } // namespace
  879. namespace detail {
  880. Subcase::Subcase(const String& name, const char* file, int line)
  881. : m_signature({name, file, line}) {
  882. auto* s = g_cs;
  883. // check subcase filters
  884. if(s->subcasesStack.size() < size_t(s->subcase_filter_levels)) {
  885. if(!matchesAny(m_signature.m_name.c_str(), s->filters[6], true, s->case_sensitive))
  886. return;
  887. if(matchesAny(m_signature.m_name.c_str(), s->filters[7], false, s->case_sensitive))
  888. return;
  889. }
  890. // if a Subcase on the same level has already been entered
  891. if(s->subcasesStack.size() < size_t(s->subcasesCurrentMaxLevel)) {
  892. s->should_reenter = true;
  893. return;
  894. }
  895. // push the current signature to the stack so we can check if the
  896. // current stack + the current new subcase have been traversed
  897. s->subcasesStack.push_back(m_signature);
  898. if(s->subcasesPassed.count(s->subcasesStack) != 0) {
  899. // pop - revert to previous stack since we've already passed this
  900. s->subcasesStack.pop_back();
  901. return;
  902. }
  903. s->subcasesCurrentMaxLevel = s->subcasesStack.size();
  904. m_entered = true;
  905. DOCTEST_ITERATE_THROUGH_REPORTERS(subcase_start, m_signature);
  906. }
  907. DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4996) // std::uncaught_exception is deprecated in C++17
  908. DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
  909. DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
  910. Subcase::~Subcase() {
  911. if(m_entered) {
  912. // only mark the subcase stack as passed if no subcases have been skipped
  913. if(g_cs->should_reenter == false)
  914. g_cs->subcasesPassed.insert(g_cs->subcasesStack);
  915. g_cs->subcasesStack.pop_back();
  916. #if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)
  917. if(std::uncaught_exceptions() > 0
  918. #else
  919. if(std::uncaught_exception()
  920. #endif
  921. && g_cs->shouldLogCurrentException) {
  922. DOCTEST_ITERATE_THROUGH_REPORTERS(
  923. test_case_exception, {"exception thrown in subcase - will translate later "
  924. "when the whole test case has been exited (cannot "
  925. "translate while there is an active exception)",
  926. false});
  927. g_cs->shouldLogCurrentException = false;
  928. }
  929. DOCTEST_ITERATE_THROUGH_REPORTERS(subcase_end, DOCTEST_EMPTY);
  930. }
  931. }
  932. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  933. DOCTEST_GCC_SUPPRESS_WARNING_POP
  934. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  935. Subcase::operator bool() const { return m_entered; }
  936. Result::Result(bool passed, const String& decomposition)
  937. : m_passed(passed)
  938. , m_decomp(decomposition) {}
  939. ExpressionDecomposer::ExpressionDecomposer(assertType::Enum at)
  940. : m_at(at) {}
  941. TestSuite& TestSuite::operator*(const char* in) {
  942. m_test_suite = in;
  943. // clear state
  944. m_description = nullptr;
  945. m_skip = false;
  946. m_no_breaks = false;
  947. m_no_output = false;
  948. m_may_fail = false;
  949. m_should_fail = false;
  950. m_expected_failures = 0;
  951. m_timeout = 0;
  952. return *this;
  953. }
  954. TestCase::TestCase(funcType test, const char* file, unsigned line, const TestSuite& test_suite,
  955. const char* type, int template_id) {
  956. m_file = file;
  957. m_line = line;
  958. m_name = nullptr; // will be later overridden in operator*
  959. m_test_suite = test_suite.m_test_suite;
  960. m_description = test_suite.m_description;
  961. m_skip = test_suite.m_skip;
  962. m_no_breaks = test_suite.m_no_breaks;
  963. m_no_output = test_suite.m_no_output;
  964. m_may_fail = test_suite.m_may_fail;
  965. m_should_fail = test_suite.m_should_fail;
  966. m_expected_failures = test_suite.m_expected_failures;
  967. m_timeout = test_suite.m_timeout;
  968. m_test = test;
  969. m_type = type;
  970. m_template_id = template_id;
  971. }
  972. TestCase::TestCase(const TestCase& other)
  973. : TestCaseData() {
  974. *this = other;
  975. }
  976. DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(26434) // hides a non-virtual function
  977. DOCTEST_MSVC_SUPPRESS_WARNING(26437) // Do not slice
  978. TestCase& TestCase::operator=(const TestCase& other) {
  979. static_cast<TestCaseData&>(*this) = static_cast<const TestCaseData&>(other);
  980. m_test = other.m_test;
  981. m_type = other.m_type;
  982. m_template_id = other.m_template_id;
  983. m_full_name = other.m_full_name;
  984. if(m_template_id != -1)
  985. m_name = m_full_name.c_str();
  986. return *this;
  987. }
  988. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  989. TestCase& TestCase::operator*(const char* in) {
  990. m_name = in;
  991. // make a new name with an appended type for templated test case
  992. if(m_template_id != -1) {
  993. m_full_name = String(m_name) + m_type;
  994. // redirect the name to point to the newly constructed full name
  995. m_name = m_full_name.c_str();
  996. }
  997. return *this;
  998. }
  999. bool TestCase::operator<(const TestCase& other) const {
  1000. // this will be used only to differentiate between test cases - not relevant for sorting
  1001. if(m_line != other.m_line)
  1002. return m_line < other.m_line;
  1003. const int name_cmp = strcmp(m_name, other.m_name);
  1004. if(name_cmp != 0)
  1005. return name_cmp < 0;
  1006. const int file_cmp = m_file.compare(other.m_file);
  1007. if(file_cmp != 0)
  1008. return file_cmp < 0;
  1009. return m_template_id < other.m_template_id;
  1010. }
  1011. // all the registered tests
  1012. std::set<TestCase>& getRegisteredTests() {
  1013. static std::set<TestCase> data;
  1014. return data;
  1015. }
  1016. } // namespace detail
  1017. namespace {
  1018. using namespace detail;
  1019. // for sorting tests by file/line
  1020. bool fileOrderComparator(const TestCase* lhs, const TestCase* rhs) {
  1021. // this is needed because MSVC gives different case for drive letters
  1022. // for __FILE__ when evaluated in a header and a source file
  1023. const int res = lhs->m_file.compare(rhs->m_file, bool(DOCTEST_MSVC));
  1024. if(res != 0)
  1025. return res < 0;
  1026. if(lhs->m_line != rhs->m_line)
  1027. return lhs->m_line < rhs->m_line;
  1028. return lhs->m_template_id < rhs->m_template_id;
  1029. }
  1030. // for sorting tests by suite/file/line
  1031. bool suiteOrderComparator(const TestCase* lhs, const TestCase* rhs) {
  1032. const int res = std::strcmp(lhs->m_test_suite, rhs->m_test_suite);
  1033. if(res != 0)
  1034. return res < 0;
  1035. return fileOrderComparator(lhs, rhs);
  1036. }
  1037. // for sorting tests by name/suite/file/line
  1038. bool nameOrderComparator(const TestCase* lhs, const TestCase* rhs) {
  1039. const int res = std::strcmp(lhs->m_name, rhs->m_name);
  1040. if(res != 0)
  1041. return res < 0;
  1042. return suiteOrderComparator(lhs, rhs);
  1043. }
  1044. #ifdef DOCTEST_CONFIG_COLORS_WINDOWS
  1045. HANDLE g_stdoutHandle;
  1046. WORD g_origFgAttrs;
  1047. WORD g_origBgAttrs;
  1048. bool g_attrsInitted = false;
  1049. int colors_init() {
  1050. if(!g_attrsInitted) {
  1051. g_stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  1052. g_attrsInitted = true;
  1053. CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  1054. GetConsoleScreenBufferInfo(g_stdoutHandle, &csbiInfo);
  1055. g_origFgAttrs = csbiInfo.wAttributes & ~(BACKGROUND_GREEN | BACKGROUND_RED |
  1056. BACKGROUND_BLUE | BACKGROUND_INTENSITY);
  1057. g_origBgAttrs = csbiInfo.wAttributes & ~(FOREGROUND_GREEN | FOREGROUND_RED |
  1058. FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  1059. }
  1060. return 0;
  1061. }
  1062. int dumy_init_console_colors = colors_init();
  1063. #endif // DOCTEST_CONFIG_COLORS_WINDOWS
  1064. DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
  1065. void color_to_stream(std::ostream& s, Color::Enum code) {
  1066. static_cast<void>(s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS
  1067. static_cast<void>(code); // for DOCTEST_CONFIG_COLORS_NONE
  1068. #ifdef DOCTEST_CONFIG_COLORS_ANSI
  1069. if(g_no_colors ||
  1070. (isatty(STDOUT_FILENO) == false && getContextOptions()->force_colors == false))
  1071. return;
  1072. auto col = "";
  1073. // clang-format off
  1074. switch(code) { //!OCLINT missing break in switch statement / unnecessary default statement in covered switch statement
  1075. case Color::Red: col = "[0;31m"; break;
  1076. case Color::Green: col = "[0;32m"; break;
  1077. case Color::Blue: col = "[0;34m"; break;
  1078. case Color::Cyan: col = "[0;36m"; break;
  1079. case Color::Yellow: col = "[0;33m"; break;
  1080. case Color::Grey: col = "[1;30m"; break;
  1081. case Color::LightGrey: col = "[0;37m"; break;
  1082. case Color::BrightRed: col = "[1;31m"; break;
  1083. case Color::BrightGreen: col = "[1;32m"; break;
  1084. case Color::BrightWhite: col = "[1;37m"; break;
  1085. case Color::Bright: // invalid
  1086. case Color::None:
  1087. case Color::White:
  1088. default: col = "[0m";
  1089. }
  1090. // clang-format on
  1091. s << "\033" << col;
  1092. #endif // DOCTEST_CONFIG_COLORS_ANSI
  1093. #ifdef DOCTEST_CONFIG_COLORS_WINDOWS
  1094. if(g_no_colors ||
  1095. (isatty(fileno(stdout)) == false && getContextOptions()->force_colors == false))
  1096. return;
  1097. #define DOCTEST_SET_ATTR(x) SetConsoleTextAttribute(g_stdoutHandle, x | g_origBgAttrs)
  1098. // clang-format off
  1099. switch (code) {
  1100. case Color::White: DOCTEST_SET_ATTR(FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); break;
  1101. case Color::Red: DOCTEST_SET_ATTR(FOREGROUND_RED); break;
  1102. case Color::Green: DOCTEST_SET_ATTR(FOREGROUND_GREEN); break;
  1103. case Color::Blue: DOCTEST_SET_ATTR(FOREGROUND_BLUE); break;
  1104. case Color::Cyan: DOCTEST_SET_ATTR(FOREGROUND_BLUE | FOREGROUND_GREEN); break;
  1105. case Color::Yellow: DOCTEST_SET_ATTR(FOREGROUND_RED | FOREGROUND_GREEN); break;
  1106. case Color::Grey: DOCTEST_SET_ATTR(0); break;
  1107. case Color::LightGrey: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY); break;
  1108. case Color::BrightRed: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_RED); break;
  1109. case Color::BrightGreen: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_GREEN); break;
  1110. case Color::BrightWhite: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); break;
  1111. case Color::None:
  1112. case Color::Bright: // invalid
  1113. default: DOCTEST_SET_ATTR(g_origFgAttrs);
  1114. }
  1115. // clang-format on
  1116. #endif // DOCTEST_CONFIG_COLORS_WINDOWS
  1117. }
  1118. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  1119. std::vector<const IExceptionTranslator*>& getExceptionTranslators() {
  1120. static std::vector<const IExceptionTranslator*> data;
  1121. return data;
  1122. }
  1123. String translateActiveException() {
  1124. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  1125. String res;
  1126. auto& translators = getExceptionTranslators();
  1127. for(auto& curr : translators)
  1128. if(curr->translate(res))
  1129. return res;
  1130. // clang-format off
  1131. DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wcatch-value")
  1132. try {
  1133. throw;
  1134. } catch(std::exception& ex) {
  1135. return ex.what();
  1136. } catch(std::string& msg) {
  1137. return msg.c_str();
  1138. } catch(const char* msg) {
  1139. return msg;
  1140. } catch(...) {
  1141. return "unknown exception";
  1142. }
  1143. DOCTEST_GCC_SUPPRESS_WARNING_POP
  1144. // clang-format on
  1145. #else // DOCTEST_CONFIG_NO_EXCEPTIONS
  1146. return "";
  1147. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  1148. }
  1149. } // namespace
  1150. namespace detail {
  1151. // used by the macros for registering tests
  1152. int regTest(const TestCase& tc) {
  1153. getRegisteredTests().insert(tc);
  1154. return 0;
  1155. }
  1156. // sets the current test suite
  1157. int setTestSuite(const TestSuite& ts) {
  1158. doctest_detail_test_suite_ns::getCurrentTestSuite() = ts;
  1159. return 0;
  1160. }
  1161. #ifdef DOCTEST_IS_DEBUGGER_ACTIVE
  1162. bool isDebuggerActive() { return DOCTEST_IS_DEBUGGER_ACTIVE(); }
  1163. #else // DOCTEST_IS_DEBUGGER_ACTIVE
  1164. #ifdef DOCTEST_PLATFORM_LINUX
  1165. class ErrnoGuard {
  1166. public:
  1167. ErrnoGuard() : m_oldErrno(errno) {}
  1168. ~ErrnoGuard() { errno = m_oldErrno; }
  1169. private:
  1170. int m_oldErrno;
  1171. };
  1172. // See the comments in Catch2 for the reasoning behind this implementation:
  1173. // https://github.com/catchorg/Catch2/blob/v2.13.1/include/internal/catch_debugger.cpp#L79-L102
  1174. bool isDebuggerActive() {
  1175. ErrnoGuard guard;
  1176. std::ifstream in("/proc/self/status");
  1177. for(std::string line; std::getline(in, line);) {
  1178. static const int PREFIX_LEN = 11;
  1179. if(line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0) {
  1180. return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0';
  1181. }
  1182. }
  1183. return false;
  1184. }
  1185. #elif defined(DOCTEST_PLATFORM_MAC)
  1186. // The following function is taken directly from the following technical note:
  1187. // https://developer.apple.com/library/archive/qa/qa1361/_index.html
  1188. // Returns true if the current process is being debugged (either
  1189. // running under the debugger or has a debugger attached post facto).
  1190. bool isDebuggerActive() {
  1191. int mib[4];
  1192. kinfo_proc info;
  1193. size_t size;
  1194. // Initialize the flags so that, if sysctl fails for some bizarre
  1195. // reason, we get a predictable result.
  1196. info.kp_proc.p_flag = 0;
  1197. // Initialize mib, which tells sysctl the info we want, in this case
  1198. // we're looking for information about a specific process ID.
  1199. mib[0] = CTL_KERN;
  1200. mib[1] = KERN_PROC;
  1201. mib[2] = KERN_PROC_PID;
  1202. mib[3] = getpid();
  1203. // Call sysctl.
  1204. size = sizeof(info);
  1205. if(sysctl(mib, DOCTEST_COUNTOF(mib), &info, &size, 0, 0) != 0) {
  1206. std::cerr << "\nCall to sysctl failed - unable to determine if debugger is active **\n";
  1207. return false;
  1208. }
  1209. // We're being debugged if the P_TRACED flag is set.
  1210. return ((info.kp_proc.p_flag & P_TRACED) != 0);
  1211. }
  1212. #elif DOCTEST_MSVC || defined(__MINGW32__) || defined(__MINGW64__)
  1213. bool isDebuggerActive() { return ::IsDebuggerPresent() != 0; }
  1214. #else
  1215. bool isDebuggerActive() { return false; }
  1216. #endif // Platform
  1217. #endif // DOCTEST_IS_DEBUGGER_ACTIVE
  1218. void registerExceptionTranslatorImpl(const IExceptionTranslator* et) {
  1219. if(std::find(getExceptionTranslators().begin(), getExceptionTranslators().end(), et) ==
  1220. getExceptionTranslators().end())
  1221. getExceptionTranslators().push_back(et);
  1222. }
  1223. #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1224. void toStream(std::ostream* s, char* in) { *s << in; }
  1225. void toStream(std::ostream* s, const char* in) { *s << in; }
  1226. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1227. void toStream(std::ostream* s, bool in) { *s << std::boolalpha << in << std::noboolalpha; }
  1228. void toStream(std::ostream* s, float in) { *s << in; }
  1229. void toStream(std::ostream* s, double in) { *s << in; }
  1230. void toStream(std::ostream* s, double long in) { *s << in; }
  1231. void toStream(std::ostream* s, char in) { *s << in; }
  1232. void toStream(std::ostream* s, char signed in) { *s << in; }
  1233. void toStream(std::ostream* s, char unsigned in) { *s << in; }
  1234. void toStream(std::ostream* s, int short in) { *s << in; }
  1235. void toStream(std::ostream* s, int short unsigned in) { *s << in; }
  1236. void toStream(std::ostream* s, int in) { *s << in; }
  1237. void toStream(std::ostream* s, int unsigned in) { *s << in; }
  1238. void toStream(std::ostream* s, int long in) { *s << in; }
  1239. void toStream(std::ostream* s, int long unsigned in) { *s << in; }
  1240. void toStream(std::ostream* s, int long long in) { *s << in; }
  1241. void toStream(std::ostream* s, int long long unsigned in) { *s << in; }
  1242. DOCTEST_THREAD_LOCAL std::vector<IContextScope*> g_infoContexts; // for logging with INFO()
  1243. ContextScopeBase::ContextScopeBase() {
  1244. g_infoContexts.push_back(this);
  1245. }
  1246. DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4996) // std::uncaught_exception is deprecated in C++17
  1247. DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
  1248. DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
  1249. // destroy cannot be inlined into the destructor because that would mean calling stringify after
  1250. // ContextScope has been destroyed (base class destructors run after derived class destructors).
  1251. // Instead, ContextScope calls this method directly from its destructor.
  1252. void ContextScopeBase::destroy() {
  1253. #if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)
  1254. if(std::uncaught_exceptions() > 0) {
  1255. #else
  1256. if(std::uncaught_exception()) {
  1257. #endif
  1258. std::ostringstream s;
  1259. this->stringify(&s);
  1260. g_cs->stringifiedContexts.push_back(s.str().c_str());
  1261. }
  1262. g_infoContexts.pop_back();
  1263. }
  1264. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  1265. DOCTEST_GCC_SUPPRESS_WARNING_POP
  1266. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  1267. } // namespace detail
  1268. namespace {
  1269. using namespace detail;
  1270. #if !defined(DOCTEST_CONFIG_POSIX_SIGNALS) && !defined(DOCTEST_CONFIG_WINDOWS_SEH)
  1271. struct FatalConditionHandler
  1272. {
  1273. static void reset() {}
  1274. static void allocateAltStackMem() {}
  1275. static void freeAltStackMem() {}
  1276. };
  1277. #else // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH
  1278. void reportFatal(const std::string&);
  1279. #ifdef DOCTEST_PLATFORM_WINDOWS
  1280. struct SignalDefs
  1281. {
  1282. DWORD id;
  1283. const char* name;
  1284. };
  1285. // There is no 1-1 mapping between signals and windows exceptions.
  1286. // Windows can easily distinguish between SO and SigSegV,
  1287. // but SigInt, SigTerm, etc are handled differently.
  1288. SignalDefs signalDefs[] = {
  1289. {static_cast<DWORD>(EXCEPTION_ILLEGAL_INSTRUCTION),
  1290. "SIGILL - Illegal instruction signal"},
  1291. {static_cast<DWORD>(EXCEPTION_STACK_OVERFLOW), "SIGSEGV - Stack overflow"},
  1292. {static_cast<DWORD>(EXCEPTION_ACCESS_VIOLATION),
  1293. "SIGSEGV - Segmentation violation signal"},
  1294. {static_cast<DWORD>(EXCEPTION_INT_DIVIDE_BY_ZERO), "Divide by zero error"},
  1295. };
  1296. struct FatalConditionHandler
  1297. {
  1298. static LONG CALLBACK handleException(PEXCEPTION_POINTERS ExceptionInfo) {
  1299. // Multiple threads may enter this filter/handler at once. We want the error message to be printed on the
  1300. // console just once no matter how many threads have crashed.
  1301. static std::mutex mutex;
  1302. static bool execute = true;
  1303. {
  1304. std::lock_guard<std::mutex> lock(mutex);
  1305. if(execute) {
  1306. bool reported = false;
  1307. for(size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) {
  1308. if(ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) {
  1309. reportFatal(signalDefs[i].name);
  1310. reported = true;
  1311. break;
  1312. }
  1313. }
  1314. if(reported == false)
  1315. reportFatal("Unhandled SEH exception caught");
  1316. if(isDebuggerActive() && !g_cs->no_breaks)
  1317. DOCTEST_BREAK_INTO_DEBUGGER();
  1318. }
  1319. execute = false;
  1320. }
  1321. std::exit(EXIT_FAILURE);
  1322. }
  1323. static void allocateAltStackMem() {}
  1324. static void freeAltStackMem() {}
  1325. FatalConditionHandler() {
  1326. isSet = true;
  1327. // 32k seems enough for doctest to handle stack overflow,
  1328. // but the value was found experimentally, so there is no strong guarantee
  1329. guaranteeSize = 32 * 1024;
  1330. // Register an unhandled exception filter
  1331. previousTop = SetUnhandledExceptionFilter(handleException);
  1332. // Pass in guarantee size to be filled
  1333. SetThreadStackGuarantee(&guaranteeSize);
  1334. // On Windows uncaught exceptions from another thread, exceptions from
  1335. // destructors, or calls to std::terminate are not a SEH exception
  1336. // The terminal handler gets called when:
  1337. // - std::terminate is called FROM THE TEST RUNNER THREAD
  1338. // - an exception is thrown from a destructor FROM THE TEST RUNNER THREAD
  1339. original_terminate_handler = std::get_terminate();
  1340. std::set_terminate([]() DOCTEST_NOEXCEPT {
  1341. reportFatal("Terminate handler called");
  1342. if(isDebuggerActive() && !g_cs->no_breaks)
  1343. DOCTEST_BREAK_INTO_DEBUGGER();
  1344. std::exit(EXIT_FAILURE); // explicitly exit - otherwise the SIGABRT handler may be called as well
  1345. });
  1346. // SIGABRT is raised when:
  1347. // - std::terminate is called FROM A DIFFERENT THREAD
  1348. // - an exception is thrown from a destructor FROM A DIFFERENT THREAD
  1349. // - an uncaught exception is thrown FROM A DIFFERENT THREAD
  1350. prev_sigabrt_handler = std::signal(SIGABRT, [](int signal) DOCTEST_NOEXCEPT {
  1351. if(signal == SIGABRT) {
  1352. reportFatal("SIGABRT - Abort (abnormal termination) signal");
  1353. if(isDebuggerActive() && !g_cs->no_breaks)
  1354. DOCTEST_BREAK_INTO_DEBUGGER();
  1355. std::exit(EXIT_FAILURE);
  1356. }
  1357. });
  1358. // The following settings are taken from google test, and more
  1359. // specifically from UnitTest::Run() inside of gtest.cc
  1360. // the user does not want to see pop-up dialogs about crashes
  1361. prev_error_mode_1 = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
  1362. SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
  1363. // This forces the abort message to go to stderr in all circumstances.
  1364. prev_error_mode_2 = _set_error_mode(_OUT_TO_STDERR);
  1365. // In the debug version, Visual Studio pops up a separate dialog
  1366. // offering a choice to debug the aborted program - we want to disable that.
  1367. prev_abort_behavior = _set_abort_behavior(0x0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
  1368. // In debug mode, the Windows CRT can crash with an assertion over invalid
  1369. // input (e.g. passing an invalid file descriptor). The default handling
  1370. // for these assertions is to pop up a dialog and wait for user input.
  1371. // Instead ask the CRT to dump such assertions to stderr non-interactively.
  1372. prev_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  1373. prev_report_file = _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  1374. }
  1375. static void reset() {
  1376. if(isSet) {
  1377. // Unregister handler and restore the old guarantee
  1378. SetUnhandledExceptionFilter(previousTop);
  1379. SetThreadStackGuarantee(&guaranteeSize);
  1380. std::set_terminate(original_terminate_handler);
  1381. std::signal(SIGABRT, prev_sigabrt_handler);
  1382. SetErrorMode(prev_error_mode_1);
  1383. _set_error_mode(prev_error_mode_2);
  1384. _set_abort_behavior(prev_abort_behavior, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
  1385. static_cast<void>(_CrtSetReportMode(_CRT_ASSERT, prev_report_mode));
  1386. static_cast<void>(_CrtSetReportFile(_CRT_ASSERT, prev_report_file));
  1387. isSet = false;
  1388. }
  1389. }
  1390. ~FatalConditionHandler() { reset(); }
  1391. private:
  1392. static UINT prev_error_mode_1;
  1393. static int prev_error_mode_2;
  1394. static unsigned int prev_abort_behavior;
  1395. static int prev_report_mode;
  1396. static _HFILE prev_report_file;
  1397. static void (*prev_sigabrt_handler)(int);
  1398. static std::terminate_handler original_terminate_handler;
  1399. static bool isSet;
  1400. static ULONG guaranteeSize;
  1401. static LPTOP_LEVEL_EXCEPTION_FILTER previousTop;
  1402. };
  1403. UINT FatalConditionHandler::prev_error_mode_1;
  1404. int FatalConditionHandler::prev_error_mode_2;
  1405. unsigned int FatalConditionHandler::prev_abort_behavior;
  1406. int FatalConditionHandler::prev_report_mode;
  1407. _HFILE FatalConditionHandler::prev_report_file;
  1408. void (*FatalConditionHandler::prev_sigabrt_handler)(int);
  1409. std::terminate_handler FatalConditionHandler::original_terminate_handler;
  1410. bool FatalConditionHandler::isSet = false;
  1411. ULONG FatalConditionHandler::guaranteeSize = 0;
  1412. LPTOP_LEVEL_EXCEPTION_FILTER FatalConditionHandler::previousTop = nullptr;
  1413. #else // DOCTEST_PLATFORM_WINDOWS
  1414. struct SignalDefs
  1415. {
  1416. int id;
  1417. const char* name;
  1418. };
  1419. SignalDefs signalDefs[] = {{SIGINT, "SIGINT - Terminal interrupt signal"},
  1420. {SIGILL, "SIGILL - Illegal instruction signal"},
  1421. {SIGFPE, "SIGFPE - Floating point error signal"},
  1422. {SIGSEGV, "SIGSEGV - Segmentation violation signal"},
  1423. {SIGTERM, "SIGTERM - Termination request signal"},
  1424. {SIGABRT, "SIGABRT - Abort (abnormal termination) signal"}};
  1425. struct FatalConditionHandler
  1426. {
  1427. static bool isSet;
  1428. static struct sigaction oldSigActions[DOCTEST_COUNTOF(signalDefs)];
  1429. static stack_t oldSigStack;
  1430. static size_t altStackSize;
  1431. static char* altStackMem;
  1432. static void handleSignal(int sig) {
  1433. const char* name = "<unknown signal>";
  1434. for(std::size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) {
  1435. SignalDefs& def = signalDefs[i];
  1436. if(sig == def.id) {
  1437. name = def.name;
  1438. break;
  1439. }
  1440. }
  1441. reset();
  1442. reportFatal(name);
  1443. raise(sig);
  1444. }
  1445. static void allocateAltStackMem() {
  1446. altStackMem = new char[altStackSize];
  1447. }
  1448. static void freeAltStackMem() {
  1449. delete[] altStackMem;
  1450. }
  1451. FatalConditionHandler() {
  1452. isSet = true;
  1453. stack_t sigStack;
  1454. sigStack.ss_sp = altStackMem;
  1455. sigStack.ss_size = altStackSize;
  1456. sigStack.ss_flags = 0;
  1457. sigaltstack(&sigStack, &oldSigStack);
  1458. struct sigaction sa = {};
  1459. sa.sa_handler = handleSignal; // NOLINT
  1460. sa.sa_flags = SA_ONSTACK;
  1461. for(std::size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) {
  1462. sigaction(signalDefs[i].id, &sa, &oldSigActions[i]);
  1463. }
  1464. }
  1465. ~FatalConditionHandler() { reset(); }
  1466. static void reset() {
  1467. if(isSet) {
  1468. // Set signals back to previous values -- hopefully nobody overwrote them in the meantime
  1469. for(std::size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) {
  1470. sigaction(signalDefs[i].id, &oldSigActions[i], nullptr);
  1471. }
  1472. // Return the old stack
  1473. sigaltstack(&oldSigStack, nullptr);
  1474. isSet = false;
  1475. }
  1476. }
  1477. };
  1478. bool FatalConditionHandler::isSet = false;
  1479. struct sigaction FatalConditionHandler::oldSigActions[DOCTEST_COUNTOF(signalDefs)] = {};
  1480. stack_t FatalConditionHandler::oldSigStack = {};
  1481. size_t FatalConditionHandler::altStackSize = 4 * SIGSTKSZ;
  1482. char* FatalConditionHandler::altStackMem = nullptr;
  1483. #endif // DOCTEST_PLATFORM_WINDOWS
  1484. #endif // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH
  1485. } // namespace
  1486. namespace {
  1487. using namespace detail;
  1488. #ifdef DOCTEST_PLATFORM_WINDOWS
  1489. #define DOCTEST_OUTPUT_DEBUG_STRING(text) ::OutputDebugStringA(text)
  1490. #else
  1491. // TODO: integration with XCode and other IDEs
  1492. #define DOCTEST_OUTPUT_DEBUG_STRING(text) // NOLINT(clang-diagnostic-unused-macros)
  1493. #endif // Platform
  1494. void addAssert(assertType::Enum at) {
  1495. if((at & assertType::is_warn) == 0) //!OCLINT bitwise operator in conditional
  1496. g_cs->numAssertsCurrentTest_atomic++;
  1497. }
  1498. void addFailedAssert(assertType::Enum at) {
  1499. if((at & assertType::is_warn) == 0) //!OCLINT bitwise operator in conditional
  1500. g_cs->numAssertsFailedCurrentTest_atomic++;
  1501. }
  1502. #if defined(DOCTEST_CONFIG_POSIX_SIGNALS) || defined(DOCTEST_CONFIG_WINDOWS_SEH)
  1503. void reportFatal(const std::string& message) {
  1504. g_cs->failure_flags |= TestCaseFailureReason::Crash;
  1505. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_exception, {message.c_str(), true});
  1506. while(g_cs->subcasesStack.size()) {
  1507. g_cs->subcasesStack.pop_back();
  1508. DOCTEST_ITERATE_THROUGH_REPORTERS(subcase_end, DOCTEST_EMPTY);
  1509. }
  1510. g_cs->finalizeTestCaseData();
  1511. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_end, *g_cs);
  1512. DOCTEST_ITERATE_THROUGH_REPORTERS(test_run_end, *g_cs);
  1513. }
  1514. #endif // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH
  1515. } // namespace
  1516. namespace detail {
  1517. ResultBuilder::ResultBuilder(assertType::Enum at, const char* file, int line, const char* expr,
  1518. const char* exception_type, const char* exception_string) {
  1519. m_test_case = g_cs->currentTest;
  1520. m_at = at;
  1521. m_file = file;
  1522. m_line = line;
  1523. m_expr = expr;
  1524. m_failed = true;
  1525. m_threw = false;
  1526. m_threw_as = false;
  1527. m_exception_type = exception_type;
  1528. m_exception_string = exception_string;
  1529. #if DOCTEST_MSVC
  1530. if(m_expr[0] == ' ') // this happens when variadic macros are disabled under MSVC
  1531. ++m_expr;
  1532. #endif // MSVC
  1533. }
  1534. void ResultBuilder::setResult(const Result& res) {
  1535. m_decomp = res.m_decomp;
  1536. m_failed = !res.m_passed;
  1537. }
  1538. void ResultBuilder::translateException() {
  1539. m_threw = true;
  1540. m_exception = translateActiveException();
  1541. }
  1542. bool ResultBuilder::log() {
  1543. if(m_at & assertType::is_throws) { //!OCLINT bitwise operator in conditional
  1544. m_failed = !m_threw;
  1545. } else if((m_at & assertType::is_throws_as) && (m_at & assertType::is_throws_with)) { //!OCLINT
  1546. m_failed = !m_threw_as || (m_exception != m_exception_string);
  1547. } else if(m_at & assertType::is_throws_as) { //!OCLINT bitwise operator in conditional
  1548. m_failed = !m_threw_as;
  1549. } else if(m_at & assertType::is_throws_with) { //!OCLINT bitwise operator in conditional
  1550. m_failed = m_exception != m_exception_string;
  1551. } else if(m_at & assertType::is_nothrow) { //!OCLINT bitwise operator in conditional
  1552. m_failed = m_threw;
  1553. }
  1554. if(m_exception.size())
  1555. m_exception = String("\"") + m_exception + "\"";
  1556. if(is_running_in_test) {
  1557. addAssert(m_at);
  1558. DOCTEST_ITERATE_THROUGH_REPORTERS(log_assert, *this);
  1559. if(m_failed)
  1560. addFailedAssert(m_at);
  1561. } else if(m_failed) {
  1562. failed_out_of_a_testing_context(*this);
  1563. }
  1564. return m_failed && isDebuggerActive() && !getContextOptions()->no_breaks &&
  1565. (g_cs->currentTest == nullptr || !g_cs->currentTest->m_no_breaks); // break into debugger
  1566. }
  1567. void ResultBuilder::react() const {
  1568. if(m_failed && checkIfShouldThrow(m_at))
  1569. throwException();
  1570. }
  1571. void failed_out_of_a_testing_context(const AssertData& ad) {
  1572. if(g_cs->ah)
  1573. g_cs->ah(ad);
  1574. else
  1575. std::abort();
  1576. }
  1577. void decomp_assert(assertType::Enum at, const char* file, int line, const char* expr,
  1578. Result result) {
  1579. bool failed = !result.m_passed;
  1580. // ###################################################################################
  1581. // IF THE DEBUGGER BREAKS HERE - GO 1 LEVEL UP IN THE CALLSTACK FOR THE FAILING ASSERT
  1582. // THIS IS THE EFFECT OF HAVING 'DOCTEST_CONFIG_SUPER_FAST_ASSERTS' DEFINED
  1583. // ###################################################################################
  1584. DOCTEST_ASSERT_OUT_OF_TESTS(result.m_decomp);
  1585. DOCTEST_ASSERT_IN_TESTS(result.m_decomp);
  1586. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  1587. }
  1588. MessageBuilder::MessageBuilder(const char* file, int line, assertType::Enum severity) {
  1589. m_stream = getTlsOss();
  1590. m_file = file;
  1591. m_line = line;
  1592. m_severity = severity;
  1593. }
  1594. IExceptionTranslator::IExceptionTranslator() = default;
  1595. IExceptionTranslator::~IExceptionTranslator() = default;
  1596. bool MessageBuilder::log() {
  1597. m_string = getTlsOssResult();
  1598. DOCTEST_ITERATE_THROUGH_REPORTERS(log_message, *this);
  1599. const bool isWarn = m_severity & assertType::is_warn;
  1600. // warn is just a message in this context so we don't treat it as an assert
  1601. if(!isWarn) {
  1602. addAssert(m_severity);
  1603. addFailedAssert(m_severity);
  1604. }
  1605. return isDebuggerActive() && !getContextOptions()->no_breaks && !isWarn &&
  1606. (g_cs->currentTest == nullptr || !g_cs->currentTest->m_no_breaks); // break into debugger
  1607. }
  1608. void MessageBuilder::react() {
  1609. if(m_severity & assertType::is_require) //!OCLINT bitwise operator in conditional
  1610. throwException();
  1611. }
  1612. MessageBuilder::~MessageBuilder() = default;
  1613. } // namespace detail
  1614. namespace {
  1615. using namespace detail;
  1616. template <typename Ex>
  1617. DOCTEST_NORETURN void throw_exception(Ex const& e) {
  1618. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  1619. throw e;
  1620. #else // DOCTEST_CONFIG_NO_EXCEPTIONS
  1621. std::cerr << "doctest will terminate because it needed to throw an exception.\n"
  1622. << "The message was: " << e.what() << '\n';
  1623. std::terminate();
  1624. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  1625. }
  1626. #ifndef DOCTEST_INTERNAL_ERROR
  1627. #define DOCTEST_INTERNAL_ERROR(msg) \
  1628. throw_exception(std::logic_error( \
  1629. __FILE__ ":" DOCTEST_TOSTR(__LINE__) ": Internal doctest error: " msg))
  1630. #endif // DOCTEST_INTERNAL_ERROR
  1631. // clang-format off
  1632. // =================================================================================================
  1633. // The following code has been taken verbatim from Catch2/include/internal/catch_xmlwriter.h/cpp
  1634. // This is done so cherry-picking bug fixes is trivial - even the style/formatting is untouched.
  1635. // =================================================================================================
  1636. class XmlEncode {
  1637. public:
  1638. enum ForWhat { ForTextNodes, ForAttributes };
  1639. XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes );
  1640. void encodeTo( std::ostream& os ) const;
  1641. friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode );
  1642. private:
  1643. std::string m_str;
  1644. ForWhat m_forWhat;
  1645. };
  1646. class XmlWriter {
  1647. public:
  1648. class ScopedElement {
  1649. public:
  1650. ScopedElement( XmlWriter* writer );
  1651. ScopedElement( ScopedElement&& other ) DOCTEST_NOEXCEPT;
  1652. ScopedElement& operator=( ScopedElement&& other ) DOCTEST_NOEXCEPT;
  1653. ~ScopedElement();
  1654. ScopedElement& writeText( std::string const& text, bool indent = true );
  1655. template<typename T>
  1656. ScopedElement& writeAttribute( std::string const& name, T const& attribute ) {
  1657. m_writer->writeAttribute( name, attribute );
  1658. return *this;
  1659. }
  1660. private:
  1661. mutable XmlWriter* m_writer = nullptr;
  1662. };
  1663. XmlWriter( std::ostream& os = std::cout );
  1664. ~XmlWriter();
  1665. XmlWriter( XmlWriter const& ) = delete;
  1666. XmlWriter& operator=( XmlWriter const& ) = delete;
  1667. XmlWriter& startElement( std::string const& name );
  1668. ScopedElement scopedElement( std::string const& name );
  1669. XmlWriter& endElement();
  1670. XmlWriter& writeAttribute( std::string const& name, std::string const& attribute );
  1671. XmlWriter& writeAttribute( std::string const& name, const char* attribute );
  1672. XmlWriter& writeAttribute( std::string const& name, bool attribute );
  1673. template<typename T>
  1674. XmlWriter& writeAttribute( std::string const& name, T const& attribute ) {
  1675. std::stringstream rss;
  1676. rss << attribute;
  1677. return writeAttribute( name, rss.str() );
  1678. }
  1679. XmlWriter& writeText( std::string const& text, bool indent = true );
  1680. //XmlWriter& writeComment( std::string const& text );
  1681. //void writeStylesheetRef( std::string const& url );
  1682. //XmlWriter& writeBlankLine();
  1683. void ensureTagClosed();
  1684. private:
  1685. void writeDeclaration();
  1686. void newlineIfNecessary();
  1687. bool m_tagIsOpen = false;
  1688. bool m_needsNewline = false;
  1689. std::vector<std::string> m_tags;
  1690. std::string m_indent;
  1691. std::ostream& m_os;
  1692. };
  1693. // =================================================================================================
  1694. // The following code has been taken verbatim from Catch2/include/internal/catch_xmlwriter.h/cpp
  1695. // This is done so cherry-picking bug fixes is trivial - even the style/formatting is untouched.
  1696. // =================================================================================================
  1697. using uchar = unsigned char;
  1698. namespace {
  1699. size_t trailingBytes(unsigned char c) {
  1700. if ((c & 0xE0) == 0xC0) {
  1701. return 2;
  1702. }
  1703. if ((c & 0xF0) == 0xE0) {
  1704. return 3;
  1705. }
  1706. if ((c & 0xF8) == 0xF0) {
  1707. return 4;
  1708. }
  1709. DOCTEST_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered");
  1710. }
  1711. uint32_t headerValue(unsigned char c) {
  1712. if ((c & 0xE0) == 0xC0) {
  1713. return c & 0x1F;
  1714. }
  1715. if ((c & 0xF0) == 0xE0) {
  1716. return c & 0x0F;
  1717. }
  1718. if ((c & 0xF8) == 0xF0) {
  1719. return c & 0x07;
  1720. }
  1721. DOCTEST_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered");
  1722. }
  1723. void hexEscapeChar(std::ostream& os, unsigned char c) {
  1724. std::ios_base::fmtflags f(os.flags());
  1725. os << "\\x"
  1726. << std::uppercase << std::hex << std::setfill('0') << std::setw(2)
  1727. << static_cast<int>(c);
  1728. os.flags(f);
  1729. }
  1730. } // anonymous namespace
  1731. XmlEncode::XmlEncode( std::string const& str, ForWhat forWhat )
  1732. : m_str( str ),
  1733. m_forWhat( forWhat )
  1734. {}
  1735. void XmlEncode::encodeTo( std::ostream& os ) const {
  1736. // Apostrophe escaping not necessary if we always use " to write attributes
  1737. // (see: https://www.w3.org/TR/xml/#syntax)
  1738. for( std::size_t idx = 0; idx < m_str.size(); ++ idx ) {
  1739. uchar c = m_str[idx];
  1740. switch (c) {
  1741. case '<': os << "&lt;"; break;
  1742. case '&': os << "&amp;"; break;
  1743. case '>':
  1744. // See: https://www.w3.org/TR/xml/#syntax
  1745. if (idx > 2 && m_str[idx - 1] == ']' && m_str[idx - 2] == ']')
  1746. os << "&gt;";
  1747. else
  1748. os << c;
  1749. break;
  1750. case '\"':
  1751. if (m_forWhat == ForAttributes)
  1752. os << "&quot;";
  1753. else
  1754. os << c;
  1755. break;
  1756. default:
  1757. // Check for control characters and invalid utf-8
  1758. // Escape control characters in standard ascii
  1759. // see https://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0
  1760. if (c < 0x09 || (c > 0x0D && c < 0x20) || c == 0x7F) {
  1761. hexEscapeChar(os, c);
  1762. break;
  1763. }
  1764. // Plain ASCII: Write it to stream
  1765. if (c < 0x7F) {
  1766. os << c;
  1767. break;
  1768. }
  1769. // UTF-8 territory
  1770. // Check if the encoding is valid and if it is not, hex escape bytes.
  1771. // Important: We do not check the exact decoded values for validity, only the encoding format
  1772. // First check that this bytes is a valid lead byte:
  1773. // This means that it is not encoded as 1111 1XXX
  1774. // Or as 10XX XXXX
  1775. if (c < 0xC0 ||
  1776. c >= 0xF8) {
  1777. hexEscapeChar(os, c);
  1778. break;
  1779. }
  1780. auto encBytes = trailingBytes(c);
  1781. // Are there enough bytes left to avoid accessing out-of-bounds memory?
  1782. if (idx + encBytes - 1 >= m_str.size()) {
  1783. hexEscapeChar(os, c);
  1784. break;
  1785. }
  1786. // The header is valid, check data
  1787. // The next encBytes bytes must together be a valid utf-8
  1788. // This means: bitpattern 10XX XXXX and the extracted value is sane (ish)
  1789. bool valid = true;
  1790. uint32_t value = headerValue(c);
  1791. for (std::size_t n = 1; n < encBytes; ++n) {
  1792. uchar nc = m_str[idx + n];
  1793. valid &= ((nc & 0xC0) == 0x80);
  1794. value = (value << 6) | (nc & 0x3F);
  1795. }
  1796. if (
  1797. // Wrong bit pattern of following bytes
  1798. (!valid) ||
  1799. // Overlong encodings
  1800. (value < 0x80) ||
  1801. ( value < 0x800 && encBytes > 2) || // removed "0x80 <= value &&" because redundant
  1802. (0x800 < value && value < 0x10000 && encBytes > 3) ||
  1803. // Encoded value out of range
  1804. (value >= 0x110000)
  1805. ) {
  1806. hexEscapeChar(os, c);
  1807. break;
  1808. }
  1809. // If we got here, this is in fact a valid(ish) utf-8 sequence
  1810. for (std::size_t n = 0; n < encBytes; ++n) {
  1811. os << m_str[idx + n];
  1812. }
  1813. idx += encBytes - 1;
  1814. break;
  1815. }
  1816. }
  1817. }
  1818. std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) {
  1819. xmlEncode.encodeTo( os );
  1820. return os;
  1821. }
  1822. XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer )
  1823. : m_writer( writer )
  1824. {}
  1825. XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) DOCTEST_NOEXCEPT
  1826. : m_writer( other.m_writer ){
  1827. other.m_writer = nullptr;
  1828. }
  1829. XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) DOCTEST_NOEXCEPT {
  1830. if ( m_writer ) {
  1831. m_writer->endElement();
  1832. }
  1833. m_writer = other.m_writer;
  1834. other.m_writer = nullptr;
  1835. return *this;
  1836. }
  1837. XmlWriter::ScopedElement::~ScopedElement() {
  1838. if( m_writer )
  1839. m_writer->endElement();
  1840. }
  1841. XmlWriter::ScopedElement& XmlWriter::ScopedElement::writeText( std::string const& text, bool indent ) {
  1842. m_writer->writeText( text, indent );
  1843. return *this;
  1844. }
  1845. XmlWriter::XmlWriter( std::ostream& os ) : m_os( os )
  1846. {
  1847. writeDeclaration();
  1848. }
  1849. XmlWriter::~XmlWriter() {
  1850. while( !m_tags.empty() )
  1851. endElement();
  1852. }
  1853. XmlWriter& XmlWriter::startElement( std::string const& name ) {
  1854. ensureTagClosed();
  1855. newlineIfNecessary();
  1856. m_os << m_indent << '<' << name;
  1857. m_tags.push_back( name );
  1858. m_indent += " ";
  1859. m_tagIsOpen = true;
  1860. return *this;
  1861. }
  1862. XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name ) {
  1863. ScopedElement scoped( this );
  1864. startElement( name );
  1865. return scoped;
  1866. }
  1867. XmlWriter& XmlWriter::endElement() {
  1868. newlineIfNecessary();
  1869. m_indent = m_indent.substr( 0, m_indent.size()-2 );
  1870. if( m_tagIsOpen ) {
  1871. m_os << "/>";
  1872. m_tagIsOpen = false;
  1873. }
  1874. else {
  1875. m_os << m_indent << "</" << m_tags.back() << ">";
  1876. }
  1877. m_os << std::endl;
  1878. m_tags.pop_back();
  1879. return *this;
  1880. }
  1881. XmlWriter& XmlWriter::writeAttribute( std::string const& name, std::string const& attribute ) {
  1882. if( !name.empty() && !attribute.empty() )
  1883. m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"';
  1884. return *this;
  1885. }
  1886. XmlWriter& XmlWriter::writeAttribute( std::string const& name, const char* attribute ) {
  1887. if( !name.empty() && attribute && attribute[0] != '\0' )
  1888. m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"';
  1889. return *this;
  1890. }
  1891. XmlWriter& XmlWriter::writeAttribute( std::string const& name, bool attribute ) {
  1892. m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"';
  1893. return *this;
  1894. }
  1895. XmlWriter& XmlWriter::writeText( std::string const& text, bool indent ) {
  1896. if( !text.empty() ){
  1897. bool tagWasOpen = m_tagIsOpen;
  1898. ensureTagClosed();
  1899. if( tagWasOpen && indent )
  1900. m_os << m_indent;
  1901. m_os << XmlEncode( text );
  1902. m_needsNewline = true;
  1903. }
  1904. return *this;
  1905. }
  1906. //XmlWriter& XmlWriter::writeComment( std::string const& text ) {
  1907. // ensureTagClosed();
  1908. // m_os << m_indent << "<!--" << text << "-->";
  1909. // m_needsNewline = true;
  1910. // return *this;
  1911. //}
  1912. //void XmlWriter::writeStylesheetRef( std::string const& url ) {
  1913. // m_os << "<?xml-stylesheet type=\"text/xsl\" href=\"" << url << "\"?>\n";
  1914. //}
  1915. //XmlWriter& XmlWriter::writeBlankLine() {
  1916. // ensureTagClosed();
  1917. // m_os << '\n';
  1918. // return *this;
  1919. //}
  1920. void XmlWriter::ensureTagClosed() {
  1921. if( m_tagIsOpen ) {
  1922. m_os << ">" << std::endl;
  1923. m_tagIsOpen = false;
  1924. }
  1925. }
  1926. void XmlWriter::writeDeclaration() {
  1927. m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  1928. }
  1929. void XmlWriter::newlineIfNecessary() {
  1930. if( m_needsNewline ) {
  1931. m_os << std::endl;
  1932. m_needsNewline = false;
  1933. }
  1934. }
  1935. // =================================================================================================
  1936. // End of copy-pasted code from Catch
  1937. // =================================================================================================
  1938. // clang-format on
  1939. struct XmlReporter : public IReporter
  1940. {
  1941. XmlWriter xml;
  1942. std::mutex mutex;
  1943. // caching pointers/references to objects of these types - safe to do
  1944. const ContextOptions& opt;
  1945. const TestCaseData* tc = nullptr;
  1946. XmlReporter(const ContextOptions& co)
  1947. : xml(*co.cout)
  1948. , opt(co) {}
  1949. void log_contexts() {
  1950. int num_contexts = get_num_active_contexts();
  1951. if(num_contexts) {
  1952. auto contexts = get_active_contexts();
  1953. std::stringstream ss;
  1954. for(int i = 0; i < num_contexts; ++i) {
  1955. contexts[i]->stringify(&ss);
  1956. xml.scopedElement("Info").writeText(ss.str());
  1957. ss.str("");
  1958. }
  1959. }
  1960. }
  1961. unsigned line(unsigned l) const { return opt.no_line_numbers ? 0 : l; }
  1962. void test_case_start_impl(const TestCaseData& in) {
  1963. bool open_ts_tag = false;
  1964. if(tc != nullptr) { // we have already opened a test suite
  1965. if(std::strcmp(tc->m_test_suite, in.m_test_suite) != 0) {
  1966. xml.endElement();
  1967. open_ts_tag = true;
  1968. }
  1969. }
  1970. else {
  1971. open_ts_tag = true; // first test case ==> first test suite
  1972. }
  1973. if(open_ts_tag) {
  1974. xml.startElement("TestSuite");
  1975. xml.writeAttribute("name", in.m_test_suite);
  1976. }
  1977. tc = &in;
  1978. xml.startElement("TestCase")
  1979. .writeAttribute("name", in.m_name)
  1980. .writeAttribute("filename", skipPathFromFilename(in.m_file.c_str()))
  1981. .writeAttribute("line", line(in.m_line))
  1982. .writeAttribute("description", in.m_description);
  1983. if(Approx(in.m_timeout) != 0)
  1984. xml.writeAttribute("timeout", in.m_timeout);
  1985. if(in.m_may_fail)
  1986. xml.writeAttribute("may_fail", true);
  1987. if(in.m_should_fail)
  1988. xml.writeAttribute("should_fail", true);
  1989. }
  1990. // =========================================================================================
  1991. // WHAT FOLLOWS ARE OVERRIDES OF THE VIRTUAL METHODS OF THE REPORTER INTERFACE
  1992. // =========================================================================================
  1993. void report_query(const QueryData& in) override {
  1994. test_run_start();
  1995. if(opt.list_reporters) {
  1996. for(auto& curr : getListeners())
  1997. xml.scopedElement("Listener")
  1998. .writeAttribute("priority", curr.first.first)
  1999. .writeAttribute("name", curr.first.second);
  2000. for(auto& curr : getReporters())
  2001. xml.scopedElement("Reporter")
  2002. .writeAttribute("priority", curr.first.first)
  2003. .writeAttribute("name", curr.first.second);
  2004. } else if(opt.count || opt.list_test_cases) {
  2005. for(unsigned i = 0; i < in.num_data; ++i) {
  2006. xml.scopedElement("TestCase").writeAttribute("name", in.data[i]->m_name)
  2007. .writeAttribute("testsuite", in.data[i]->m_test_suite)
  2008. .writeAttribute("filename", skipPathFromFilename(in.data[i]->m_file.c_str()))
  2009. .writeAttribute("line", line(in.data[i]->m_line));
  2010. }
  2011. xml.scopedElement("OverallResultsTestCases")
  2012. .writeAttribute("unskipped", in.run_stats->numTestCasesPassingFilters);
  2013. } else if(opt.list_test_suites) {
  2014. for(unsigned i = 0; i < in.num_data; ++i)
  2015. xml.scopedElement("TestSuite").writeAttribute("name", in.data[i]->m_test_suite);
  2016. xml.scopedElement("OverallResultsTestCases")
  2017. .writeAttribute("unskipped", in.run_stats->numTestCasesPassingFilters);
  2018. xml.scopedElement("OverallResultsTestSuites")
  2019. .writeAttribute("unskipped", in.run_stats->numTestSuitesPassingFilters);
  2020. }
  2021. xml.endElement();
  2022. }
  2023. void test_run_start() override {
  2024. // remove .exe extension - mainly to have the same output on UNIX and Windows
  2025. std::string binary_name = skipPathFromFilename(opt.binary_name.c_str());
  2026. #ifdef DOCTEST_PLATFORM_WINDOWS
  2027. if(binary_name.rfind(".exe") != std::string::npos)
  2028. binary_name = binary_name.substr(0, binary_name.length() - 4);
  2029. #endif // DOCTEST_PLATFORM_WINDOWS
  2030. xml.startElement("doctest").writeAttribute("binary", binary_name);
  2031. if(opt.no_version == false)
  2032. xml.writeAttribute("version", DOCTEST_VERSION_STR);
  2033. // only the consequential ones (TODO: filters)
  2034. xml.scopedElement("Options")
  2035. .writeAttribute("order_by", opt.order_by.c_str())
  2036. .writeAttribute("rand_seed", opt.rand_seed)
  2037. .writeAttribute("first", opt.first)
  2038. .writeAttribute("last", opt.last)
  2039. .writeAttribute("abort_after", opt.abort_after)
  2040. .writeAttribute("subcase_filter_levels", opt.subcase_filter_levels)
  2041. .writeAttribute("case_sensitive", opt.case_sensitive)
  2042. .writeAttribute("no_throw", opt.no_throw)
  2043. .writeAttribute("no_skip", opt.no_skip);
  2044. }
  2045. void test_run_end(const TestRunStats& p) override {
  2046. if(tc) // the TestSuite tag - only if there has been at least 1 test case
  2047. xml.endElement();
  2048. xml.scopedElement("OverallResultsAsserts")
  2049. .writeAttribute("successes", p.numAsserts - p.numAssertsFailed)
  2050. .writeAttribute("failures", p.numAssertsFailed);
  2051. xml.startElement("OverallResultsTestCases")
  2052. .writeAttribute("successes",
  2053. p.numTestCasesPassingFilters - p.numTestCasesFailed)
  2054. .writeAttribute("failures", p.numTestCasesFailed);
  2055. if(opt.no_skipped_summary == false)
  2056. xml.writeAttribute("skipped", p.numTestCases - p.numTestCasesPassingFilters);
  2057. xml.endElement();
  2058. xml.endElement();
  2059. }
  2060. void test_case_start(const TestCaseData& in) override {
  2061. test_case_start_impl(in);
  2062. xml.ensureTagClosed();
  2063. }
  2064. void test_case_reenter(const TestCaseData&) override {}
  2065. void test_case_end(const CurrentTestCaseStats& st) override {
  2066. xml.startElement("OverallResultsAsserts")
  2067. .writeAttribute("successes",
  2068. st.numAssertsCurrentTest - st.numAssertsFailedCurrentTest)
  2069. .writeAttribute("failures", st.numAssertsFailedCurrentTest);
  2070. if(opt.duration)
  2071. xml.writeAttribute("duration", st.seconds);
  2072. if(tc->m_expected_failures)
  2073. xml.writeAttribute("expected_failures", tc->m_expected_failures);
  2074. xml.endElement();
  2075. xml.endElement();
  2076. }
  2077. void test_case_exception(const TestCaseException& e) override {
  2078. std::lock_guard<std::mutex> lock(mutex);
  2079. xml.scopedElement("Exception")
  2080. .writeAttribute("crash", e.is_crash)
  2081. .writeText(e.error_string.c_str());
  2082. }
  2083. void subcase_start(const SubcaseSignature& in) override {
  2084. std::lock_guard<std::mutex> lock(mutex);
  2085. xml.startElement("SubCase")
  2086. .writeAttribute("name", in.m_name)
  2087. .writeAttribute("filename", skipPathFromFilename(in.m_file))
  2088. .writeAttribute("line", line(in.m_line));
  2089. xml.ensureTagClosed();
  2090. }
  2091. void subcase_end() override { xml.endElement(); }
  2092. void log_assert(const AssertData& rb) override {
  2093. if(!rb.m_failed && !opt.success)
  2094. return;
  2095. std::lock_guard<std::mutex> lock(mutex);
  2096. xml.startElement("Expression")
  2097. .writeAttribute("success", !rb.m_failed)
  2098. .writeAttribute("type", assertString(rb.m_at))
  2099. .writeAttribute("filename", skipPathFromFilename(rb.m_file))
  2100. .writeAttribute("line", line(rb.m_line));
  2101. xml.scopedElement("Original").writeText(rb.m_expr);
  2102. if(rb.m_threw)
  2103. xml.scopedElement("Exception").writeText(rb.m_exception.c_str());
  2104. if(rb.m_at & assertType::is_throws_as)
  2105. xml.scopedElement("ExpectedException").writeText(rb.m_exception_type);
  2106. if(rb.m_at & assertType::is_throws_with)
  2107. xml.scopedElement("ExpectedExceptionString").writeText(rb.m_exception_string);
  2108. if((rb.m_at & assertType::is_normal) && !rb.m_threw)
  2109. xml.scopedElement("Expanded").writeText(rb.m_decomp.c_str());
  2110. log_contexts();
  2111. xml.endElement();
  2112. }
  2113. void log_message(const MessageData& mb) override {
  2114. std::lock_guard<std::mutex> lock(mutex);
  2115. xml.startElement("Message")
  2116. .writeAttribute("type", failureString(mb.m_severity))
  2117. .writeAttribute("filename", skipPathFromFilename(mb.m_file))
  2118. .writeAttribute("line", line(mb.m_line));
  2119. xml.scopedElement("Text").writeText(mb.m_string.c_str());
  2120. log_contexts();
  2121. xml.endElement();
  2122. }
  2123. void test_case_skipped(const TestCaseData& in) override {
  2124. if(opt.no_skipped_summary == false) {
  2125. test_case_start_impl(in);
  2126. xml.writeAttribute("skipped", "true");
  2127. xml.endElement();
  2128. }
  2129. }
  2130. };
  2131. DOCTEST_REGISTER_REPORTER("xml", 0, XmlReporter);
  2132. void fulltext_log_assert_to_stream(std::ostream& s, const AssertData& rb) {
  2133. if((rb.m_at & (assertType::is_throws_as | assertType::is_throws_with)) ==
  2134. 0) //!OCLINT bitwise operator in conditional
  2135. s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << " ) "
  2136. << Color::None;
  2137. if(rb.m_at & assertType::is_throws) { //!OCLINT bitwise operator in conditional
  2138. s << (rb.m_threw ? "threw as expected!" : "did NOT throw at all!") << "\n";
  2139. } else if((rb.m_at & assertType::is_throws_as) &&
  2140. (rb.m_at & assertType::is_throws_with)) { //!OCLINT
  2141. s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \""
  2142. << rb.m_exception_string << "\", " << rb.m_exception_type << " ) " << Color::None;
  2143. if(rb.m_threw) {
  2144. if(!rb.m_failed) {
  2145. s << "threw as expected!\n";
  2146. } else {
  2147. s << "threw a DIFFERENT exception! (contents: " << rb.m_exception << ")\n";
  2148. }
  2149. } else {
  2150. s << "did NOT throw at all!\n";
  2151. }
  2152. } else if(rb.m_at &
  2153. assertType::is_throws_as) { //!OCLINT bitwise operator in conditional
  2154. s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", "
  2155. << rb.m_exception_type << " ) " << Color::None
  2156. << (rb.m_threw ? (rb.m_threw_as ? "threw as expected!" :
  2157. "threw a DIFFERENT exception: ") :
  2158. "did NOT throw at all!")
  2159. << Color::Cyan << rb.m_exception << "\n";
  2160. } else if(rb.m_at &
  2161. assertType::is_throws_with) { //!OCLINT bitwise operator in conditional
  2162. s << Color::Cyan << assertString(rb.m_at) << "( " << rb.m_expr << ", \""
  2163. << rb.m_exception_string << "\" ) " << Color::None
  2164. << (rb.m_threw ? (!rb.m_failed ? "threw as expected!" :
  2165. "threw a DIFFERENT exception: ") :
  2166. "did NOT throw at all!")
  2167. << Color::Cyan << rb.m_exception << "\n";
  2168. } else if(rb.m_at & assertType::is_nothrow) { //!OCLINT bitwise operator in conditional
  2169. s << (rb.m_threw ? "THREW exception: " : "didn't throw!") << Color::Cyan
  2170. << rb.m_exception << "\n";
  2171. } else {
  2172. s << (rb.m_threw ? "THREW exception: " :
  2173. (!rb.m_failed ? "is correct!\n" : "is NOT correct!\n"));
  2174. if(rb.m_threw)
  2175. s << rb.m_exception << "\n";
  2176. else
  2177. s << " values: " << assertString(rb.m_at) << "( " << rb.m_decomp << " )\n";
  2178. }
  2179. }
  2180. // TODO:
  2181. // - log_message()
  2182. // - respond to queries
  2183. // - honor remaining options
  2184. // - more attributes in tags
  2185. struct JUnitReporter : public IReporter
  2186. {
  2187. XmlWriter xml;
  2188. std::mutex mutex;
  2189. Timer timer;
  2190. std::vector<String> deepestSubcaseStackNames;
  2191. struct JUnitTestCaseData
  2192. {
  2193. static std::string getCurrentTimestamp() {
  2194. // Beware, this is not reentrant because of backward compatibility issues
  2195. // Also, UTC only, again because of backward compatibility (%z is C++11)
  2196. time_t rawtime;
  2197. std::time(&rawtime);
  2198. auto const timeStampSize = sizeof("2017-01-16T17:06:45Z");
  2199. std::tm timeInfo;
  2200. #ifdef DOCTEST_PLATFORM_WINDOWS
  2201. gmtime_s(&timeInfo, &rawtime);
  2202. #else // DOCTEST_PLATFORM_WINDOWS
  2203. gmtime_r(&rawtime, &timeInfo);
  2204. #endif // DOCTEST_PLATFORM_WINDOWS
  2205. char timeStamp[timeStampSize];
  2206. const char* const fmt = "%Y-%m-%dT%H:%M:%SZ";
  2207. std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
  2208. return std::string(timeStamp);
  2209. }
  2210. struct JUnitTestMessage
  2211. {
  2212. JUnitTestMessage(const std::string& _message, const std::string& _type, const std::string& _details)
  2213. : message(_message), type(_type), details(_details) {}
  2214. JUnitTestMessage(const std::string& _message, const std::string& _details)
  2215. : message(_message), type(), details(_details) {}
  2216. std::string message, type, details;
  2217. };
  2218. struct JUnitTestCase
  2219. {
  2220. JUnitTestCase(const std::string& _classname, const std::string& _name)
  2221. : classname(_classname), name(_name), time(0), failures() {}
  2222. std::string classname, name;
  2223. double time;
  2224. std::vector<JUnitTestMessage> failures, errors;
  2225. };
  2226. void add(const std::string& classname, const std::string& name) {
  2227. testcases.emplace_back(classname, name);
  2228. }
  2229. void appendSubcaseNamesToLastTestcase(std::vector<String> nameStack) {
  2230. for(auto& curr: nameStack)
  2231. if(curr.size())
  2232. testcases.back().name += std::string("/") + curr.c_str();
  2233. }
  2234. void addTime(double time) {
  2235. if(time < 1e-4)
  2236. time = 0;
  2237. testcases.back().time = time;
  2238. totalSeconds += time;
  2239. }
  2240. void addFailure(const std::string& message, const std::string& type, const std::string& details) {
  2241. testcases.back().failures.emplace_back(message, type, details);
  2242. ++totalFailures;
  2243. }
  2244. void addError(const std::string& message, const std::string& details) {
  2245. testcases.back().errors.emplace_back(message, details);
  2246. ++totalErrors;
  2247. }
  2248. std::vector<JUnitTestCase> testcases;
  2249. double totalSeconds = 0;
  2250. int totalErrors = 0, totalFailures = 0;
  2251. };
  2252. JUnitTestCaseData testCaseData;
  2253. // caching pointers/references to objects of these types - safe to do
  2254. const ContextOptions& opt;
  2255. const TestCaseData* tc = nullptr;
  2256. JUnitReporter(const ContextOptions& co)
  2257. : xml(*co.cout)
  2258. , opt(co) {}
  2259. unsigned line(unsigned l) const { return opt.no_line_numbers ? 0 : l; }
  2260. // =========================================================================================
  2261. // WHAT FOLLOWS ARE OVERRIDES OF THE VIRTUAL METHODS OF THE REPORTER INTERFACE
  2262. // =========================================================================================
  2263. void report_query(const QueryData&) override {}
  2264. void test_run_start() override {}
  2265. void test_run_end(const TestRunStats& p) override {
  2266. // remove .exe extension - mainly to have the same output on UNIX and Windows
  2267. std::string binary_name = skipPathFromFilename(opt.binary_name.c_str());
  2268. #ifdef DOCTEST_PLATFORM_WINDOWS
  2269. if(binary_name.rfind(".exe") != std::string::npos)
  2270. binary_name = binary_name.substr(0, binary_name.length() - 4);
  2271. #endif // DOCTEST_PLATFORM_WINDOWS
  2272. xml.startElement("testsuites");
  2273. xml.startElement("testsuite").writeAttribute("name", binary_name)
  2274. .writeAttribute("errors", testCaseData.totalErrors)
  2275. .writeAttribute("failures", testCaseData.totalFailures)
  2276. .writeAttribute("tests", p.numAsserts);
  2277. if(opt.no_time_in_output == false) {
  2278. xml.writeAttribute("time", testCaseData.totalSeconds);
  2279. xml.writeAttribute("timestamp", JUnitTestCaseData::getCurrentTimestamp());
  2280. }
  2281. if(opt.no_version == false)
  2282. xml.writeAttribute("doctest_version", DOCTEST_VERSION_STR);
  2283. for(const auto& testCase : testCaseData.testcases) {
  2284. xml.startElement("testcase")
  2285. .writeAttribute("classname", testCase.classname)
  2286. .writeAttribute("name", testCase.name);
  2287. if(opt.no_time_in_output == false)
  2288. xml.writeAttribute("time", testCase.time);
  2289. // This is not ideal, but it should be enough to mimic gtest's junit output.
  2290. xml.writeAttribute("status", "run");
  2291. for(const auto& failure : testCase.failures) {
  2292. xml.scopedElement("failure")
  2293. .writeAttribute("message", failure.message)
  2294. .writeAttribute("type", failure.type)
  2295. .writeText(failure.details, false);
  2296. }
  2297. for(const auto& error : testCase.errors) {
  2298. xml.scopedElement("error")
  2299. .writeAttribute("message", error.message)
  2300. .writeText(error.details);
  2301. }
  2302. xml.endElement();
  2303. }
  2304. xml.endElement();
  2305. xml.endElement();
  2306. }
  2307. void test_case_start(const TestCaseData& in) override {
  2308. testCaseData.add(skipPathFromFilename(in.m_file.c_str()), in.m_name);
  2309. timer.start();
  2310. }
  2311. void test_case_reenter(const TestCaseData& in) override {
  2312. testCaseData.addTime(timer.getElapsedSeconds());
  2313. testCaseData.appendSubcaseNamesToLastTestcase(deepestSubcaseStackNames);
  2314. deepestSubcaseStackNames.clear();
  2315. timer.start();
  2316. testCaseData.add(skipPathFromFilename(in.m_file.c_str()), in.m_name);
  2317. }
  2318. void test_case_end(const CurrentTestCaseStats&) override {
  2319. testCaseData.addTime(timer.getElapsedSeconds());
  2320. testCaseData.appendSubcaseNamesToLastTestcase(deepestSubcaseStackNames);
  2321. deepestSubcaseStackNames.clear();
  2322. }
  2323. void test_case_exception(const TestCaseException& e) override {
  2324. std::lock_guard<std::mutex> lock(mutex);
  2325. testCaseData.addError("exception", e.error_string.c_str());
  2326. }
  2327. void subcase_start(const SubcaseSignature& in) override {
  2328. std::lock_guard<std::mutex> lock(mutex);
  2329. deepestSubcaseStackNames.push_back(in.m_name);
  2330. }
  2331. void subcase_end() override {}
  2332. void log_assert(const AssertData& rb) override {
  2333. if(!rb.m_failed) // report only failures & ignore the `success` option
  2334. return;
  2335. std::lock_guard<std::mutex> lock(mutex);
  2336. std::ostringstream os;
  2337. os << skipPathFromFilename(rb.m_file) << (opt.gnu_file_line ? ":" : "(")
  2338. << line(rb.m_line) << (opt.gnu_file_line ? ":" : "):") << std::endl;
  2339. fulltext_log_assert_to_stream(os, rb);
  2340. log_contexts(os);
  2341. testCaseData.addFailure(rb.m_decomp.c_str(), assertString(rb.m_at), os.str());
  2342. }
  2343. void log_message(const MessageData&) override {}
  2344. void test_case_skipped(const TestCaseData&) override {}
  2345. void log_contexts(std::ostringstream& s) {
  2346. int num_contexts = get_num_active_contexts();
  2347. if(num_contexts) {
  2348. auto contexts = get_active_contexts();
  2349. s << " logged: ";
  2350. for(int i = 0; i < num_contexts; ++i) {
  2351. s << (i == 0 ? "" : " ");
  2352. contexts[i]->stringify(&s);
  2353. s << std::endl;
  2354. }
  2355. }
  2356. }
  2357. };
  2358. DOCTEST_REGISTER_REPORTER("junit", 0, JUnitReporter);
  2359. struct Whitespace
  2360. {
  2361. int nrSpaces;
  2362. explicit Whitespace(int nr)
  2363. : nrSpaces(nr) {}
  2364. };
  2365. std::ostream& operator<<(std::ostream& out, const Whitespace& ws) {
  2366. if(ws.nrSpaces != 0)
  2367. out << std::setw(ws.nrSpaces) << ' ';
  2368. return out;
  2369. }
  2370. struct ConsoleReporter : public IReporter
  2371. {
  2372. std::ostream& s;
  2373. bool hasLoggedCurrentTestStart;
  2374. std::vector<SubcaseSignature> subcasesStack;
  2375. size_t currentSubcaseLevel;
  2376. std::mutex mutex;
  2377. // caching pointers/references to objects of these types - safe to do
  2378. const ContextOptions& opt;
  2379. const TestCaseData* tc;
  2380. ConsoleReporter(const ContextOptions& co)
  2381. : s(*co.cout)
  2382. , opt(co) {}
  2383. ConsoleReporter(const ContextOptions& co, std::ostream& ostr)
  2384. : s(ostr)
  2385. , opt(co) {}
  2386. // =========================================================================================
  2387. // WHAT FOLLOWS ARE HELPERS USED BY THE OVERRIDES OF THE VIRTUAL METHODS OF THE INTERFACE
  2388. // =========================================================================================
  2389. void separator_to_stream() {
  2390. s << Color::Yellow
  2391. << "==============================================================================="
  2392. "\n";
  2393. }
  2394. const char* getSuccessOrFailString(bool success, assertType::Enum at,
  2395. const char* success_str) {
  2396. if(success)
  2397. return success_str;
  2398. return failureString(at);
  2399. }
  2400. Color::Enum getSuccessOrFailColor(bool success, assertType::Enum at) {
  2401. return success ? Color::BrightGreen :
  2402. (at & assertType::is_warn) ? Color::Yellow : Color::Red;
  2403. }
  2404. void successOrFailColoredStringToStream(bool success, assertType::Enum at,
  2405. const char* success_str = "SUCCESS") {
  2406. s << getSuccessOrFailColor(success, at)
  2407. << getSuccessOrFailString(success, at, success_str) << ": ";
  2408. }
  2409. void log_contexts() {
  2410. int num_contexts = get_num_active_contexts();
  2411. if(num_contexts) {
  2412. auto contexts = get_active_contexts();
  2413. s << Color::None << " logged: ";
  2414. for(int i = 0; i < num_contexts; ++i) {
  2415. s << (i == 0 ? "" : " ");
  2416. contexts[i]->stringify(&s);
  2417. s << "\n";
  2418. }
  2419. }
  2420. s << "\n";
  2421. }
  2422. // this was requested to be made virtual so users could override it
  2423. virtual void file_line_to_stream(const char* file, int line,
  2424. const char* tail = "") {
  2425. s << Color::LightGrey << skipPathFromFilename(file) << (opt.gnu_file_line ? ":" : "(")
  2426. << (opt.no_line_numbers ? 0 : line) // 0 or the real num depending on the option
  2427. << (opt.gnu_file_line ? ":" : "):") << tail;
  2428. }
  2429. void logTestStart() {
  2430. if(hasLoggedCurrentTestStart)
  2431. return;
  2432. separator_to_stream();
  2433. file_line_to_stream(tc->m_file.c_str(), tc->m_line, "\n");
  2434. if(tc->m_description)
  2435. s << Color::Yellow << "DESCRIPTION: " << Color::None << tc->m_description << "\n";
  2436. if(tc->m_test_suite && tc->m_test_suite[0] != '\0')
  2437. s << Color::Yellow << "TEST SUITE: " << Color::None << tc->m_test_suite << "\n";
  2438. if(strncmp(tc->m_name, " Scenario:", 11) != 0)
  2439. s << Color::Yellow << "TEST CASE: ";
  2440. s << Color::None << tc->m_name << "\n";
  2441. for(size_t i = 0; i < currentSubcaseLevel; ++i) {
  2442. if(subcasesStack[i].m_name[0] != '\0')
  2443. s << " " << subcasesStack[i].m_name << "\n";
  2444. }
  2445. if(currentSubcaseLevel != subcasesStack.size()) {
  2446. s << Color::Yellow << "\nDEEPEST SUBCASE STACK REACHED (DIFFERENT FROM THE CURRENT ONE):\n" << Color::None;
  2447. for(size_t i = 0; i < subcasesStack.size(); ++i) {
  2448. if(subcasesStack[i].m_name[0] != '\0')
  2449. s << " " << subcasesStack[i].m_name << "\n";
  2450. }
  2451. }
  2452. s << "\n";
  2453. hasLoggedCurrentTestStart = true;
  2454. }
  2455. void printVersion() {
  2456. if(opt.no_version == false)
  2457. s << Color::Cyan << "[doctest] " << Color::None << "doctest version is \""
  2458. << DOCTEST_VERSION_STR << "\"\n";
  2459. }
  2460. void printIntro() {
  2461. printVersion();
  2462. s << Color::Cyan << "[doctest] " << Color::None
  2463. << "run with \"--" DOCTEST_OPTIONS_PREFIX_DISPLAY "help\" for options\n";
  2464. }
  2465. void printHelp() {
  2466. int sizePrefixDisplay = static_cast<int>(strlen(DOCTEST_OPTIONS_PREFIX_DISPLAY));
  2467. printVersion();
  2468. // clang-format off
  2469. s << Color::Cyan << "[doctest]\n" << Color::None;
  2470. s << Color::Cyan << "[doctest] " << Color::None;
  2471. s << "boolean values: \"1/on/yes/true\" or \"0/off/no/false\"\n";
  2472. s << Color::Cyan << "[doctest] " << Color::None;
  2473. s << "filter values: \"str1,str2,str3\" (comma separated strings)\n";
  2474. s << Color::Cyan << "[doctest]\n" << Color::None;
  2475. s << Color::Cyan << "[doctest] " << Color::None;
  2476. s << "filters use wildcards for matching strings\n";
  2477. s << Color::Cyan << "[doctest] " << Color::None;
  2478. s << "something passes a filter if any of the strings in a filter matches\n";
  2479. #ifndef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  2480. s << Color::Cyan << "[doctest]\n" << Color::None;
  2481. s << Color::Cyan << "[doctest] " << Color::None;
  2482. s << "ALL FLAGS, OPTIONS AND FILTERS ALSO AVAILABLE WITH A \"" DOCTEST_CONFIG_OPTIONS_PREFIX "\" PREFIX!!!\n";
  2483. #endif
  2484. s << Color::Cyan << "[doctest]\n" << Color::None;
  2485. s << Color::Cyan << "[doctest] " << Color::None;
  2486. s << "Query flags - the program quits after them. Available:\n\n";
  2487. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "?, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "help, -" DOCTEST_OPTIONS_PREFIX_DISPLAY "h "
  2488. << Whitespace(sizePrefixDisplay*0) << "prints this message\n";
  2489. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "v, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "version "
  2490. << Whitespace(sizePrefixDisplay*1) << "prints the version\n";
  2491. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "c, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "count "
  2492. << Whitespace(sizePrefixDisplay*1) << "prints the number of matching tests\n";
  2493. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "ltc, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "list-test-cases "
  2494. << Whitespace(sizePrefixDisplay*1) << "lists all matching tests by name\n";
  2495. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "lts, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "list-test-suites "
  2496. << Whitespace(sizePrefixDisplay*1) << "lists all matching test suites\n";
  2497. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "lr, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "list-reporters "
  2498. << Whitespace(sizePrefixDisplay*1) << "lists all registered reporters\n\n";
  2499. // ================================================================================== << 79
  2500. s << Color::Cyan << "[doctest] " << Color::None;
  2501. s << "The available <int>/<string> options/filters are:\n\n";
  2502. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "tc, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "test-case=<filters> "
  2503. << Whitespace(sizePrefixDisplay*1) << "filters tests by their name\n";
  2504. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "tce, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "test-case-exclude=<filters> "
  2505. << Whitespace(sizePrefixDisplay*1) << "filters OUT tests by their name\n";
  2506. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "sf, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "source-file=<filters> "
  2507. << Whitespace(sizePrefixDisplay*1) << "filters tests by their file\n";
  2508. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "sfe, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "source-file-exclude=<filters> "
  2509. << Whitespace(sizePrefixDisplay*1) << "filters OUT tests by their file\n";
  2510. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "ts, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "test-suite=<filters> "
  2511. << Whitespace(sizePrefixDisplay*1) << "filters tests by their test suite\n";
  2512. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "tse, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "test-suite-exclude=<filters> "
  2513. << Whitespace(sizePrefixDisplay*1) << "filters OUT tests by their test suite\n";
  2514. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "sc, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "subcase=<filters> "
  2515. << Whitespace(sizePrefixDisplay*1) << "filters subcases by their name\n";
  2516. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "sce, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "subcase-exclude=<filters> "
  2517. << Whitespace(sizePrefixDisplay*1) << "filters OUT subcases by their name\n";
  2518. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "r, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "reporters=<filters> "
  2519. << Whitespace(sizePrefixDisplay*1) << "reporters to use (console is default)\n";
  2520. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "o, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "out=<string> "
  2521. << Whitespace(sizePrefixDisplay*1) << "output filename\n";
  2522. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "ob, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "order-by=<string> "
  2523. << Whitespace(sizePrefixDisplay*1) << "how the tests should be ordered\n";
  2524. s << Whitespace(sizePrefixDisplay*3) << " <string> - [file/suite/name/rand/none]\n";
  2525. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "rs, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "rand-seed=<int> "
  2526. << Whitespace(sizePrefixDisplay*1) << "seed for random ordering\n";
  2527. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "f, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "first=<int> "
  2528. << Whitespace(sizePrefixDisplay*1) << "the first test passing the filters to\n";
  2529. s << Whitespace(sizePrefixDisplay*3) << " execute - for range-based execution\n";
  2530. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "l, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "last=<int> "
  2531. << Whitespace(sizePrefixDisplay*1) << "the last test passing the filters to\n";
  2532. s << Whitespace(sizePrefixDisplay*3) << " execute - for range-based execution\n";
  2533. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "aa, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "abort-after=<int> "
  2534. << Whitespace(sizePrefixDisplay*1) << "stop after <int> failed assertions\n";
  2535. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "scfl,--" DOCTEST_OPTIONS_PREFIX_DISPLAY "subcase-filter-levels=<int> "
  2536. << Whitespace(sizePrefixDisplay*1) << "apply filters for the first <int> levels\n";
  2537. s << Color::Cyan << "\n[doctest] " << Color::None;
  2538. s << "Bool options - can be used like flags and true is assumed. Available:\n\n";
  2539. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "s, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "success=<bool> "
  2540. << Whitespace(sizePrefixDisplay*1) << "include successful assertions in output\n";
  2541. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "cs, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "case-sensitive=<bool> "
  2542. << Whitespace(sizePrefixDisplay*1) << "filters being treated as case sensitive\n";
  2543. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "e, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "exit=<bool> "
  2544. << Whitespace(sizePrefixDisplay*1) << "exits after the tests finish\n";
  2545. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "d, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "duration=<bool> "
  2546. << Whitespace(sizePrefixDisplay*1) << "prints the time duration of each test\n";
  2547. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "nt, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-throw=<bool> "
  2548. << Whitespace(sizePrefixDisplay*1) << "skips exceptions-related assert checks\n";
  2549. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "ne, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-exitcode=<bool> "
  2550. << Whitespace(sizePrefixDisplay*1) << "returns (or exits) always with success\n";
  2551. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "nr, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-run=<bool> "
  2552. << Whitespace(sizePrefixDisplay*1) << "skips all runtime doctest operations\n";
  2553. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "nv, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-version=<bool> "
  2554. << Whitespace(sizePrefixDisplay*1) << "omit the framework version in the output\n";
  2555. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "nc, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-colors=<bool> "
  2556. << Whitespace(sizePrefixDisplay*1) << "disables colors in output\n";
  2557. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "fc, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "force-colors=<bool> "
  2558. << Whitespace(sizePrefixDisplay*1) << "use colors even when not in a tty\n";
  2559. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "nb, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-breaks=<bool> "
  2560. << Whitespace(sizePrefixDisplay*1) << "disables breakpoints in debuggers\n";
  2561. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "ns, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-skip=<bool> "
  2562. << Whitespace(sizePrefixDisplay*1) << "don't skip test cases marked as skip\n";
  2563. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "gfl, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "gnu-file-line=<bool> "
  2564. << Whitespace(sizePrefixDisplay*1) << ":n: vs (n): for line numbers in output\n";
  2565. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "npf, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-path-filenames=<bool> "
  2566. << Whitespace(sizePrefixDisplay*1) << "only filenames and no paths in output\n";
  2567. s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "nln, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "no-line-numbers=<bool> "
  2568. << Whitespace(sizePrefixDisplay*1) << "0 instead of real line numbers in output\n";
  2569. // ================================================================================== << 79
  2570. // clang-format on
  2571. s << Color::Cyan << "\n[doctest] " << Color::None;
  2572. s << "for more information visit the project documentation\n\n";
  2573. }
  2574. void printRegisteredReporters() {
  2575. printVersion();
  2576. auto printReporters = [this] (const reporterMap& reporters, const char* type) {
  2577. if(reporters.size()) {
  2578. s << Color::Cyan << "[doctest] " << Color::None << "listing all registered " << type << "\n";
  2579. for(auto& curr : reporters)
  2580. s << "priority: " << std::setw(5) << curr.first.first
  2581. << " name: " << curr.first.second << "\n";
  2582. }
  2583. };
  2584. printReporters(getListeners(), "listeners");
  2585. printReporters(getReporters(), "reporters");
  2586. }
  2587. void list_query_results() {
  2588. separator_to_stream();
  2589. if(opt.count || opt.list_test_cases) {
  2590. s << Color::Cyan << "[doctest] " << Color::None
  2591. << "unskipped test cases passing the current filters: "
  2592. << g_cs->numTestCasesPassingFilters << "\n";
  2593. } else if(opt.list_test_suites) {
  2594. s << Color::Cyan << "[doctest] " << Color::None
  2595. << "unskipped test cases passing the current filters: "
  2596. << g_cs->numTestCasesPassingFilters << "\n";
  2597. s << Color::Cyan << "[doctest] " << Color::None
  2598. << "test suites with unskipped test cases passing the current filters: "
  2599. << g_cs->numTestSuitesPassingFilters << "\n";
  2600. }
  2601. }
  2602. // =========================================================================================
  2603. // WHAT FOLLOWS ARE OVERRIDES OF THE VIRTUAL METHODS OF THE REPORTER INTERFACE
  2604. // =========================================================================================
  2605. void report_query(const QueryData& in) override {
  2606. if(opt.version) {
  2607. printVersion();
  2608. } else if(opt.help) {
  2609. printHelp();
  2610. } else if(opt.list_reporters) {
  2611. printRegisteredReporters();
  2612. } else if(opt.count || opt.list_test_cases) {
  2613. if(opt.list_test_cases) {
  2614. s << Color::Cyan << "[doctest] " << Color::None
  2615. << "listing all test case names\n";
  2616. separator_to_stream();
  2617. }
  2618. for(unsigned i = 0; i < in.num_data; ++i)
  2619. s << Color::None << in.data[i]->m_name << "\n";
  2620. separator_to_stream();
  2621. s << Color::Cyan << "[doctest] " << Color::None
  2622. << "unskipped test cases passing the current filters: "
  2623. << g_cs->numTestCasesPassingFilters << "\n";
  2624. } else if(opt.list_test_suites) {
  2625. s << Color::Cyan << "[doctest] " << Color::None << "listing all test suites\n";
  2626. separator_to_stream();
  2627. for(unsigned i = 0; i < in.num_data; ++i)
  2628. s << Color::None << in.data[i]->m_test_suite << "\n";
  2629. separator_to_stream();
  2630. s << Color::Cyan << "[doctest] " << Color::None
  2631. << "unskipped test cases passing the current filters: "
  2632. << g_cs->numTestCasesPassingFilters << "\n";
  2633. s << Color::Cyan << "[doctest] " << Color::None
  2634. << "test suites with unskipped test cases passing the current filters: "
  2635. << g_cs->numTestSuitesPassingFilters << "\n";
  2636. }
  2637. }
  2638. void test_run_start() override { printIntro(); }
  2639. void test_run_end(const TestRunStats& p) override {
  2640. separator_to_stream();
  2641. s << std::dec;
  2642. auto totwidth = int(std::ceil(log10((std::max(p.numTestCasesPassingFilters, static_cast<unsigned>(p.numAsserts))) + 1)));
  2643. auto passwidth = int(std::ceil(log10((std::max(p.numTestCasesPassingFilters - p.numTestCasesFailed, static_cast<unsigned>(p.numAsserts - p.numAssertsFailed))) + 1)));
  2644. auto failwidth = int(std::ceil(log10((std::max(p.numTestCasesFailed, static_cast<unsigned>(p.numAssertsFailed))) + 1)));
  2645. const bool anythingFailed = p.numTestCasesFailed > 0 || p.numAssertsFailed > 0;
  2646. s << Color::Cyan << "[doctest] " << Color::None << "test cases: " << std::setw(totwidth)
  2647. << p.numTestCasesPassingFilters << " | "
  2648. << ((p.numTestCasesPassingFilters == 0 || anythingFailed) ? Color::None :
  2649. Color::Green)
  2650. << std::setw(passwidth) << p.numTestCasesPassingFilters - p.numTestCasesFailed << " passed"
  2651. << Color::None << " | " << (p.numTestCasesFailed > 0 ? Color::Red : Color::None)
  2652. << std::setw(failwidth) << p.numTestCasesFailed << " failed" << Color::None << " |";
  2653. if(opt.no_skipped_summary == false) {
  2654. const int numSkipped = p.numTestCases - p.numTestCasesPassingFilters;
  2655. s << " " << (numSkipped == 0 ? Color::None : Color::Yellow) << numSkipped
  2656. << " skipped" << Color::None;
  2657. }
  2658. s << "\n";
  2659. s << Color::Cyan << "[doctest] " << Color::None << "assertions: " << std::setw(totwidth)
  2660. << p.numAsserts << " | "
  2661. << ((p.numAsserts == 0 || anythingFailed) ? Color::None : Color::Green)
  2662. << std::setw(passwidth) << (p.numAsserts - p.numAssertsFailed) << " passed" << Color::None
  2663. << " | " << (p.numAssertsFailed > 0 ? Color::Red : Color::None) << std::setw(failwidth)
  2664. << p.numAssertsFailed << " failed" << Color::None << " |\n";
  2665. s << Color::Cyan << "[doctest] " << Color::None
  2666. << "Status: " << (p.numTestCasesFailed > 0 ? Color::Red : Color::Green)
  2667. << ((p.numTestCasesFailed > 0) ? "FAILURE!" : "SUCCESS!") << Color::None << std::endl;
  2668. }
  2669. void test_case_start(const TestCaseData& in) override {
  2670. hasLoggedCurrentTestStart = false;
  2671. tc = &in;
  2672. subcasesStack.clear();
  2673. currentSubcaseLevel = 0;
  2674. }
  2675. void test_case_reenter(const TestCaseData&) override {
  2676. subcasesStack.clear();
  2677. }
  2678. void test_case_end(const CurrentTestCaseStats& st) override {
  2679. if(tc->m_no_output)
  2680. return;
  2681. // log the preamble of the test case only if there is something
  2682. // else to print - something other than that an assert has failed
  2683. if(opt.duration ||
  2684. (st.failure_flags && st.failure_flags != TestCaseFailureReason::AssertFailure))
  2685. logTestStart();
  2686. if(opt.duration)
  2687. s << Color::None << std::setprecision(6) << std::fixed << st.seconds
  2688. << " s: " << tc->m_name << "\n";
  2689. if(st.failure_flags & TestCaseFailureReason::Timeout)
  2690. s << Color::Red << "Test case exceeded time limit of " << std::setprecision(6)
  2691. << std::fixed << tc->m_timeout << "!\n";
  2692. if(st.failure_flags & TestCaseFailureReason::ShouldHaveFailedButDidnt) {
  2693. s << Color::Red << "Should have failed but didn't! Marking it as failed!\n";
  2694. } else if(st.failure_flags & TestCaseFailureReason::ShouldHaveFailedAndDid) {
  2695. s << Color::Yellow << "Failed as expected so marking it as not failed\n";
  2696. } else if(st.failure_flags & TestCaseFailureReason::CouldHaveFailedAndDid) {
  2697. s << Color::Yellow << "Allowed to fail so marking it as not failed\n";
  2698. } else if(st.failure_flags & TestCaseFailureReason::DidntFailExactlyNumTimes) {
  2699. s << Color::Red << "Didn't fail exactly " << tc->m_expected_failures
  2700. << " times so marking it as failed!\n";
  2701. } else if(st.failure_flags & TestCaseFailureReason::FailedExactlyNumTimes) {
  2702. s << Color::Yellow << "Failed exactly " << tc->m_expected_failures
  2703. << " times as expected so marking it as not failed!\n";
  2704. }
  2705. if(st.failure_flags & TestCaseFailureReason::TooManyFailedAsserts) {
  2706. s << Color::Red << "Aborting - too many failed asserts!\n";
  2707. }
  2708. s << Color::None; // lgtm [cpp/useless-expression]
  2709. }
  2710. void test_case_exception(const TestCaseException& e) override {
  2711. if(tc->m_no_output)
  2712. return;
  2713. logTestStart();
  2714. file_line_to_stream(tc->m_file.c_str(), tc->m_line, " ");
  2715. successOrFailColoredStringToStream(false, e.is_crash ? assertType::is_require :
  2716. assertType::is_check);
  2717. s << Color::Red << (e.is_crash ? "test case CRASHED: " : "test case THREW exception: ")
  2718. << Color::Cyan << e.error_string << "\n";
  2719. int num_stringified_contexts = get_num_stringified_contexts();
  2720. if(num_stringified_contexts) {
  2721. auto stringified_contexts = get_stringified_contexts();
  2722. s << Color::None << " logged: ";
  2723. for(int i = num_stringified_contexts; i > 0; --i) {
  2724. s << (i == num_stringified_contexts ? "" : " ")
  2725. << stringified_contexts[i - 1] << "\n";
  2726. }
  2727. }
  2728. s << "\n" << Color::None;
  2729. }
  2730. void subcase_start(const SubcaseSignature& subc) override {
  2731. std::lock_guard<std::mutex> lock(mutex);
  2732. subcasesStack.push_back(subc);
  2733. ++currentSubcaseLevel;
  2734. hasLoggedCurrentTestStart = false;
  2735. }
  2736. void subcase_end() override {
  2737. std::lock_guard<std::mutex> lock(mutex);
  2738. --currentSubcaseLevel;
  2739. hasLoggedCurrentTestStart = false;
  2740. }
  2741. void log_assert(const AssertData& rb) override {
  2742. if((!rb.m_failed && !opt.success) || tc->m_no_output)
  2743. return;
  2744. std::lock_guard<std::mutex> lock(mutex);
  2745. logTestStart();
  2746. file_line_to_stream(rb.m_file, rb.m_line, " ");
  2747. successOrFailColoredStringToStream(!rb.m_failed, rb.m_at);
  2748. fulltext_log_assert_to_stream(s, rb);
  2749. log_contexts();
  2750. }
  2751. void log_message(const MessageData& mb) override {
  2752. if(tc->m_no_output)
  2753. return;
  2754. std::lock_guard<std::mutex> lock(mutex);
  2755. logTestStart();
  2756. file_line_to_stream(mb.m_file, mb.m_line, " ");
  2757. s << getSuccessOrFailColor(false, mb.m_severity)
  2758. << getSuccessOrFailString(mb.m_severity & assertType::is_warn, mb.m_severity,
  2759. "MESSAGE") << ": ";
  2760. s << Color::None << mb.m_string << "\n";
  2761. log_contexts();
  2762. }
  2763. void test_case_skipped(const TestCaseData&) override {}
  2764. };
  2765. DOCTEST_REGISTER_REPORTER("console", 0, ConsoleReporter);
  2766. #ifdef DOCTEST_PLATFORM_WINDOWS
  2767. struct DebugOutputWindowReporter : public ConsoleReporter
  2768. {
  2769. DOCTEST_THREAD_LOCAL static std::ostringstream oss;
  2770. DebugOutputWindowReporter(const ContextOptions& co)
  2771. : ConsoleReporter(co, oss) {}
  2772. #define DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(func, type, arg) \
  2773. void func(type arg) override { \
  2774. bool with_col = g_no_colors; \
  2775. g_no_colors = false; \
  2776. ConsoleReporter::func(arg); \
  2777. if(oss.tellp() != std::streampos{}) { \
  2778. DOCTEST_OUTPUT_DEBUG_STRING(oss.str().c_str()); \
  2779. oss.str(""); \
  2780. } \
  2781. g_no_colors = with_col; \
  2782. }
  2783. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_run_start, DOCTEST_EMPTY, DOCTEST_EMPTY)
  2784. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_run_end, const TestRunStats&, in)
  2785. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_case_start, const TestCaseData&, in)
  2786. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_case_reenter, const TestCaseData&, in)
  2787. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_case_end, const CurrentTestCaseStats&, in)
  2788. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_case_exception, const TestCaseException&, in)
  2789. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(subcase_start, const SubcaseSignature&, in)
  2790. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(subcase_end, DOCTEST_EMPTY, DOCTEST_EMPTY)
  2791. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(log_assert, const AssertData&, in)
  2792. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(log_message, const MessageData&, in)
  2793. DOCTEST_DEBUG_OUTPUT_REPORTER_OVERRIDE(test_case_skipped, const TestCaseData&, in)
  2794. };
  2795. DOCTEST_THREAD_LOCAL std::ostringstream DebugOutputWindowReporter::oss;
  2796. #endif // DOCTEST_PLATFORM_WINDOWS
  2797. // the implementation of parseOption()
  2798. bool parseOptionImpl(int argc, const char* const* argv, const char* pattern, String* value) {
  2799. // going from the end to the beginning and stopping on the first occurrence from the end
  2800. for(int i = argc; i > 0; --i) {
  2801. auto index = i - 1;
  2802. auto temp = std::strstr(argv[index], pattern);
  2803. if(temp && (value || strlen(temp) == strlen(pattern))) { //!OCLINT prefer early exits and continue
  2804. // eliminate matches in which the chars before the option are not '-'
  2805. bool noBadCharsFound = true;
  2806. auto curr = argv[index];
  2807. while(curr != temp) {
  2808. if(*curr++ != '-') {
  2809. noBadCharsFound = false;
  2810. break;
  2811. }
  2812. }
  2813. if(noBadCharsFound && argv[index][0] == '-') {
  2814. if(value) {
  2815. // parsing the value of an option
  2816. temp += strlen(pattern);
  2817. const unsigned len = strlen(temp);
  2818. if(len) {
  2819. *value = temp;
  2820. return true;
  2821. }
  2822. } else {
  2823. // just a flag - no value
  2824. return true;
  2825. }
  2826. }
  2827. }
  2828. }
  2829. return false;
  2830. }
  2831. // parses an option and returns the string after the '=' character
  2832. bool parseOption(int argc, const char* const* argv, const char* pattern, String* value = nullptr,
  2833. const String& defaultVal = String()) {
  2834. if(value)
  2835. *value = defaultVal;
  2836. #ifndef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  2837. // offset (normally 3 for "dt-") to skip prefix
  2838. if(parseOptionImpl(argc, argv, pattern + strlen(DOCTEST_CONFIG_OPTIONS_PREFIX), value))
  2839. return true;
  2840. #endif // DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  2841. return parseOptionImpl(argc, argv, pattern, value);
  2842. }
  2843. // locates a flag on the command line
  2844. bool parseFlag(int argc, const char* const* argv, const char* pattern) {
  2845. return parseOption(argc, argv, pattern);
  2846. }
  2847. // parses a comma separated list of words after a pattern in one of the arguments in argv
  2848. bool parseCommaSepArgs(int argc, const char* const* argv, const char* pattern,
  2849. std::vector<String>& res) {
  2850. String filtersString;
  2851. if(parseOption(argc, argv, pattern, &filtersString)) {
  2852. // tokenize with "," as a separator
  2853. // cppcheck-suppress strtokCalled
  2854. DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations")
  2855. auto pch = std::strtok(filtersString.c_str(), ","); // modifies the string
  2856. while(pch != nullptr) {
  2857. if(strlen(pch))
  2858. res.push_back(pch);
  2859. // uses the strtok() internal state to go to the next token
  2860. // cppcheck-suppress strtokCalled
  2861. pch = std::strtok(nullptr, ",");
  2862. }
  2863. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  2864. return true;
  2865. }
  2866. return false;
  2867. }
  2868. enum optionType
  2869. {
  2870. option_bool,
  2871. option_int
  2872. };
  2873. // parses an int/bool option from the command line
  2874. bool parseIntOption(int argc, const char* const* argv, const char* pattern, optionType type,
  2875. int& res) {
  2876. String parsedValue;
  2877. if(!parseOption(argc, argv, pattern, &parsedValue))
  2878. return false;
  2879. if(type == 0) {
  2880. // boolean
  2881. const char positive[][5] = {"1", "true", "on", "yes"}; // 5 - strlen("true") + 1
  2882. const char negative[][6] = {"0", "false", "off", "no"}; // 6 - strlen("false") + 1
  2883. // if the value matches any of the positive/negative possibilities
  2884. for(unsigned i = 0; i < 4; i++) {
  2885. if(parsedValue.compare(positive[i], true) == 0) {
  2886. res = 1; //!OCLINT parameter reassignment
  2887. return true;
  2888. }
  2889. if(parsedValue.compare(negative[i], true) == 0) {
  2890. res = 0; //!OCLINT parameter reassignment
  2891. return true;
  2892. }
  2893. }
  2894. } else {
  2895. // integer
  2896. // TODO: change this to use std::stoi or something else! currently it uses undefined behavior - assumes '0' on failed parse...
  2897. int theInt = std::atoi(parsedValue.c_str()); // NOLINT
  2898. if(theInt != 0) {
  2899. res = theInt; //!OCLINT parameter reassignment
  2900. return true;
  2901. }
  2902. }
  2903. return false;
  2904. }
  2905. } // namespace
  2906. Context::Context(int argc, const char* const* argv)
  2907. : p(new detail::ContextState) {
  2908. parseArgs(argc, argv, true);
  2909. if(argc)
  2910. p->binary_name = argv[0];
  2911. }
  2912. Context::~Context() {
  2913. if(g_cs == p)
  2914. g_cs = nullptr;
  2915. delete p;
  2916. }
  2917. void Context::applyCommandLine(int argc, const char* const* argv) {
  2918. parseArgs(argc, argv);
  2919. if(argc)
  2920. p->binary_name = argv[0];
  2921. }
  2922. // parses args
  2923. void Context::parseArgs(int argc, const char* const* argv, bool withDefaults) {
  2924. using namespace detail;
  2925. // clang-format off
  2926. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "source-file=", p->filters[0]);
  2927. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "sf=", p->filters[0]);
  2928. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "source-file-exclude=",p->filters[1]);
  2929. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "sfe=", p->filters[1]);
  2930. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "test-suite=", p->filters[2]);
  2931. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "ts=", p->filters[2]);
  2932. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "test-suite-exclude=", p->filters[3]);
  2933. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "tse=", p->filters[3]);
  2934. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "test-case=", p->filters[4]);
  2935. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "tc=", p->filters[4]);
  2936. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "test-case-exclude=", p->filters[5]);
  2937. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "tce=", p->filters[5]);
  2938. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "subcase=", p->filters[6]);
  2939. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "sc=", p->filters[6]);
  2940. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "subcase-exclude=", p->filters[7]);
  2941. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "sce=", p->filters[7]);
  2942. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "reporters=", p->filters[8]);
  2943. parseCommaSepArgs(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "r=", p->filters[8]);
  2944. // clang-format on
  2945. int intRes = 0;
  2946. String strRes;
  2947. #define DOCTEST_PARSE_AS_BOOL_OR_FLAG(name, sname, var, default) \
  2948. if(parseIntOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX name "=", option_bool, intRes) || \
  2949. parseIntOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX sname "=", option_bool, intRes)) \
  2950. p->var = static_cast<bool>(intRes); \
  2951. else if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX name) || \
  2952. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX sname)) \
  2953. p->var = true; \
  2954. else if(withDefaults) \
  2955. p->var = default
  2956. #define DOCTEST_PARSE_INT_OPTION(name, sname, var, default) \
  2957. if(parseIntOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX name "=", option_int, intRes) || \
  2958. parseIntOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX sname "=", option_int, intRes)) \
  2959. p->var = intRes; \
  2960. else if(withDefaults) \
  2961. p->var = default
  2962. #define DOCTEST_PARSE_STR_OPTION(name, sname, var, default) \
  2963. if(parseOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX name "=", &strRes, default) || \
  2964. parseOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX sname "=", &strRes, default) || \
  2965. withDefaults) \
  2966. p->var = strRes
  2967. // clang-format off
  2968. DOCTEST_PARSE_STR_OPTION("out", "o", out, "");
  2969. DOCTEST_PARSE_STR_OPTION("order-by", "ob", order_by, "file");
  2970. DOCTEST_PARSE_INT_OPTION("rand-seed", "rs", rand_seed, 0);
  2971. DOCTEST_PARSE_INT_OPTION("first", "f", first, 0);
  2972. DOCTEST_PARSE_INT_OPTION("last", "l", last, UINT_MAX);
  2973. DOCTEST_PARSE_INT_OPTION("abort-after", "aa", abort_after, 0);
  2974. DOCTEST_PARSE_INT_OPTION("subcase-filter-levels", "scfl", subcase_filter_levels, INT_MAX);
  2975. DOCTEST_PARSE_AS_BOOL_OR_FLAG("success", "s", success, false);
  2976. DOCTEST_PARSE_AS_BOOL_OR_FLAG("case-sensitive", "cs", case_sensitive, false);
  2977. DOCTEST_PARSE_AS_BOOL_OR_FLAG("exit", "e", exit, false);
  2978. DOCTEST_PARSE_AS_BOOL_OR_FLAG("duration", "d", duration, false);
  2979. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-throw", "nt", no_throw, false);
  2980. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-exitcode", "ne", no_exitcode, false);
  2981. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-run", "nr", no_run, false);
  2982. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-version", "nv", no_version, false);
  2983. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-colors", "nc", no_colors, false);
  2984. DOCTEST_PARSE_AS_BOOL_OR_FLAG("force-colors", "fc", force_colors, false);
  2985. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-breaks", "nb", no_breaks, false);
  2986. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-skip", "ns", no_skip, false);
  2987. DOCTEST_PARSE_AS_BOOL_OR_FLAG("gnu-file-line", "gfl", gnu_file_line, !bool(DOCTEST_MSVC));
  2988. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-path-filenames", "npf", no_path_in_filenames, false);
  2989. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-line-numbers", "nln", no_line_numbers, false);
  2990. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-debug-output", "ndo", no_debug_output, false);
  2991. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-skipped-summary", "nss", no_skipped_summary, false);
  2992. DOCTEST_PARSE_AS_BOOL_OR_FLAG("no-time-in-output", "ntio", no_time_in_output, false);
  2993. // clang-format on
  2994. if(withDefaults) {
  2995. p->help = false;
  2996. p->version = false;
  2997. p->count = false;
  2998. p->list_test_cases = false;
  2999. p->list_test_suites = false;
  3000. p->list_reporters = false;
  3001. }
  3002. if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "help") ||
  3003. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "h") ||
  3004. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "?")) {
  3005. p->help = true;
  3006. p->exit = true;
  3007. }
  3008. if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "version") ||
  3009. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "v")) {
  3010. p->version = true;
  3011. p->exit = true;
  3012. }
  3013. if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "count") ||
  3014. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "c")) {
  3015. p->count = true;
  3016. p->exit = true;
  3017. }
  3018. if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "list-test-cases") ||
  3019. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "ltc")) {
  3020. p->list_test_cases = true;
  3021. p->exit = true;
  3022. }
  3023. if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "list-test-suites") ||
  3024. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "lts")) {
  3025. p->list_test_suites = true;
  3026. p->exit = true;
  3027. }
  3028. if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "list-reporters") ||
  3029. parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX "lr")) {
  3030. p->list_reporters = true;
  3031. p->exit = true;
  3032. }
  3033. }
  3034. // allows the user to add procedurally to the filters from the command line
  3035. void Context::addFilter(const char* filter, const char* value) { setOption(filter, value); }
  3036. // allows the user to clear all filters from the command line
  3037. void Context::clearFilters() {
  3038. for(auto& curr : p->filters)
  3039. curr.clear();
  3040. }
  3041. // allows the user to override procedurally the int/bool options from the command line
  3042. void Context::setOption(const char* option, int value) {
  3043. setOption(option, toString(value).c_str());
  3044. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  3045. }
  3046. // allows the user to override procedurally the string options from the command line
  3047. void Context::setOption(const char* option, const char* value) {
  3048. auto argv = String("-") + option + "=" + value;
  3049. auto lvalue = argv.c_str();
  3050. parseArgs(1, &lvalue);
  3051. }
  3052. // users should query this in their main() and exit the program if true
  3053. bool Context::shouldExit() { return p->exit; }
  3054. void Context::setAsDefaultForAssertsOutOfTestCases() { g_cs = p; }
  3055. void Context::setAssertHandler(detail::assert_handler ah) { p->ah = ah; }
  3056. // the main function that does all the filtering and test running
  3057. int Context::run() {
  3058. using namespace detail;
  3059. // save the old context state in case such was setup - for using asserts out of a testing context
  3060. auto old_cs = g_cs;
  3061. // this is the current contest
  3062. g_cs = p;
  3063. is_running_in_test = true;
  3064. g_no_colors = p->no_colors;
  3065. p->resetRunData();
  3066. // stdout by default
  3067. p->cout = &std::cout;
  3068. p->cerr = &std::cerr;
  3069. // or to a file if specified
  3070. std::fstream fstr;
  3071. if(p->out.size()) {
  3072. fstr.open(p->out.c_str(), std::fstream::out);
  3073. p->cout = &fstr;
  3074. }
  3075. FatalConditionHandler::allocateAltStackMem();
  3076. auto cleanup_and_return = [&]() {
  3077. FatalConditionHandler::freeAltStackMem();
  3078. if(fstr.is_open())
  3079. fstr.close();
  3080. // restore context
  3081. g_cs = old_cs;
  3082. is_running_in_test = false;
  3083. // we have to free the reporters which were allocated when the run started
  3084. for(auto& curr : p->reporters_currently_used)
  3085. delete curr;
  3086. p->reporters_currently_used.clear();
  3087. if(p->numTestCasesFailed && !p->no_exitcode)
  3088. return EXIT_FAILURE;
  3089. return EXIT_SUCCESS;
  3090. };
  3091. // setup default reporter if none is given through the command line
  3092. if(p->filters[8].empty())
  3093. p->filters[8].push_back("console");
  3094. // check to see if any of the registered reporters has been selected
  3095. for(auto& curr : getReporters()) {
  3096. if(matchesAny(curr.first.second.c_str(), p->filters[8], false, p->case_sensitive))
  3097. p->reporters_currently_used.push_back(curr.second(*g_cs));
  3098. }
  3099. // TODO: check if there is nothing in reporters_currently_used
  3100. // prepend all listeners
  3101. for(auto& curr : getListeners())
  3102. p->reporters_currently_used.insert(p->reporters_currently_used.begin(), curr.second(*g_cs));
  3103. #ifdef DOCTEST_PLATFORM_WINDOWS
  3104. if(isDebuggerActive() && p->no_debug_output == false)
  3105. p->reporters_currently_used.push_back(new DebugOutputWindowReporter(*g_cs));
  3106. #endif // DOCTEST_PLATFORM_WINDOWS
  3107. // handle version, help and no_run
  3108. if(p->no_run || p->version || p->help || p->list_reporters) {
  3109. DOCTEST_ITERATE_THROUGH_REPORTERS(report_query, QueryData());
  3110. return cleanup_and_return();
  3111. }
  3112. std::vector<const TestCase*> testArray;
  3113. for(auto& curr : getRegisteredTests())
  3114. testArray.push_back(&curr);
  3115. p->numTestCases = testArray.size();
  3116. // sort the collected records
  3117. if(!testArray.empty()) {
  3118. if(p->order_by.compare("file", true) == 0) {
  3119. std::sort(testArray.begin(), testArray.end(), fileOrderComparator);
  3120. } else if(p->order_by.compare("suite", true) == 0) {
  3121. std::sort(testArray.begin(), testArray.end(), suiteOrderComparator);
  3122. } else if(p->order_by.compare("name", true) == 0) {
  3123. std::sort(testArray.begin(), testArray.end(), nameOrderComparator);
  3124. } else if(p->order_by.compare("rand", true) == 0) {
  3125. std::srand(p->rand_seed);
  3126. // random_shuffle implementation
  3127. const auto first = &testArray[0];
  3128. for(size_t i = testArray.size() - 1; i > 0; --i) {
  3129. int idxToSwap = std::rand() % (i + 1); // NOLINT
  3130. const auto temp = first[i];
  3131. first[i] = first[idxToSwap];
  3132. first[idxToSwap] = temp;
  3133. }
  3134. } else if(p->order_by.compare("none", true) == 0) {
  3135. // means no sorting - beneficial for death tests which call into the executable
  3136. // with a specific test case in mind - we don't want to slow down the startup times
  3137. }
  3138. }
  3139. std::set<String> testSuitesPassingFilt;
  3140. bool query_mode = p->count || p->list_test_cases || p->list_test_suites;
  3141. std::vector<const TestCaseData*> queryResults;
  3142. if(!query_mode)
  3143. DOCTEST_ITERATE_THROUGH_REPORTERS(test_run_start, DOCTEST_EMPTY);
  3144. // invoke the registered functions if they match the filter criteria (or just count them)
  3145. for(auto& curr : testArray) {
  3146. const auto& tc = *curr;
  3147. bool skip_me = false;
  3148. if(tc.m_skip && !p->no_skip)
  3149. skip_me = true;
  3150. if(!matchesAny(tc.m_file.c_str(), p->filters[0], true, p->case_sensitive))
  3151. skip_me = true;
  3152. if(matchesAny(tc.m_file.c_str(), p->filters[1], false, p->case_sensitive))
  3153. skip_me = true;
  3154. if(!matchesAny(tc.m_test_suite, p->filters[2], true, p->case_sensitive))
  3155. skip_me = true;
  3156. if(matchesAny(tc.m_test_suite, p->filters[3], false, p->case_sensitive))
  3157. skip_me = true;
  3158. if(!matchesAny(tc.m_name, p->filters[4], true, p->case_sensitive))
  3159. skip_me = true;
  3160. if(matchesAny(tc.m_name, p->filters[5], false, p->case_sensitive))
  3161. skip_me = true;
  3162. if(!skip_me)
  3163. p->numTestCasesPassingFilters++;
  3164. // skip the test if it is not in the execution range
  3165. if((p->last < p->numTestCasesPassingFilters && p->first <= p->last) ||
  3166. (p->first > p->numTestCasesPassingFilters))
  3167. skip_me = true;
  3168. if(skip_me) {
  3169. if(!query_mode)
  3170. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_skipped, tc);
  3171. continue;
  3172. }
  3173. // do not execute the test if we are to only count the number of filter passing tests
  3174. if(p->count)
  3175. continue;
  3176. // print the name of the test and don't execute it
  3177. if(p->list_test_cases) {
  3178. queryResults.push_back(&tc);
  3179. continue;
  3180. }
  3181. // print the name of the test suite if not done already and don't execute it
  3182. if(p->list_test_suites) {
  3183. if((testSuitesPassingFilt.count(tc.m_test_suite) == 0) && tc.m_test_suite[0] != '\0') {
  3184. queryResults.push_back(&tc);
  3185. testSuitesPassingFilt.insert(tc.m_test_suite);
  3186. p->numTestSuitesPassingFilters++;
  3187. }
  3188. continue;
  3189. }
  3190. // execute the test if it passes all the filtering
  3191. {
  3192. p->currentTest = &tc;
  3193. p->failure_flags = TestCaseFailureReason::None;
  3194. p->seconds = 0;
  3195. // reset atomic counters
  3196. p->numAssertsFailedCurrentTest_atomic = 0;
  3197. p->numAssertsCurrentTest_atomic = 0;
  3198. p->subcasesPassed.clear();
  3199. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_start, tc);
  3200. p->timer.start();
  3201. bool run_test = true;
  3202. do {
  3203. // reset some of the fields for subcases (except for the set of fully passed ones)
  3204. p->should_reenter = false;
  3205. p->subcasesCurrentMaxLevel = 0;
  3206. p->subcasesStack.clear();
  3207. p->shouldLogCurrentException = true;
  3208. // reset stuff for logging with INFO()
  3209. p->stringifiedContexts.clear();
  3210. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  3211. try {
  3212. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  3213. // MSVC 2015 diagnoses fatalConditionHandler as unused (because reset() is a static method)
  3214. DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4101) // unreferenced local variable
  3215. FatalConditionHandler fatalConditionHandler; // Handle signals
  3216. // execute the test
  3217. tc.m_test();
  3218. fatalConditionHandler.reset();
  3219. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  3220. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  3221. } catch(const TestFailureException&) {
  3222. p->failure_flags |= TestCaseFailureReason::AssertFailure;
  3223. } catch(...) {
  3224. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_exception,
  3225. {translateActiveException(), false});
  3226. p->failure_flags |= TestCaseFailureReason::Exception;
  3227. }
  3228. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  3229. // exit this loop if enough assertions have failed - even if there are more subcases
  3230. if(p->abort_after > 0 &&
  3231. p->numAssertsFailed + p->numAssertsFailedCurrentTest_atomic >= p->abort_after) {
  3232. run_test = false;
  3233. p->failure_flags |= TestCaseFailureReason::TooManyFailedAsserts;
  3234. }
  3235. if(p->should_reenter && run_test)
  3236. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_reenter, tc);
  3237. if(!p->should_reenter)
  3238. run_test = false;
  3239. } while(run_test);
  3240. p->finalizeTestCaseData();
  3241. DOCTEST_ITERATE_THROUGH_REPORTERS(test_case_end, *g_cs);
  3242. p->currentTest = nullptr;
  3243. // stop executing tests if enough assertions have failed
  3244. if(p->abort_after > 0 && p->numAssertsFailed >= p->abort_after)
  3245. break;
  3246. }
  3247. }
  3248. if(!query_mode) {
  3249. DOCTEST_ITERATE_THROUGH_REPORTERS(test_run_end, *g_cs);
  3250. } else {
  3251. QueryData qdata;
  3252. qdata.run_stats = g_cs;
  3253. qdata.data = queryResults.data();
  3254. qdata.num_data = unsigned(queryResults.size());
  3255. DOCTEST_ITERATE_THROUGH_REPORTERS(report_query, qdata);
  3256. }
  3257. // see these issues on the reasoning for this:
  3258. // - https://github.com/onqtam/doctest/issues/143#issuecomment-414418903
  3259. // - https://github.com/onqtam/doctest/issues/126
  3260. auto DOCTEST_FIX_FOR_MACOS_LIBCPP_IOSFWD_STRING_LINK_ERRORS = []() DOCTEST_NOINLINE
  3261. { std::cout << std::string(); };
  3262. DOCTEST_FIX_FOR_MACOS_LIBCPP_IOSFWD_STRING_LINK_ERRORS();
  3263. return cleanup_and_return();
  3264. }
  3265. IReporter::~IReporter() = default;
  3266. int IReporter::get_num_active_contexts() { return detail::g_infoContexts.size(); }
  3267. const IContextScope* const* IReporter::get_active_contexts() {
  3268. return get_num_active_contexts() ? &detail::g_infoContexts[0] : nullptr;
  3269. }
  3270. int IReporter::get_num_stringified_contexts() { return detail::g_cs->stringifiedContexts.size(); }
  3271. const String* IReporter::get_stringified_contexts() {
  3272. return get_num_stringified_contexts() ? &detail::g_cs->stringifiedContexts[0] : nullptr;
  3273. }
  3274. namespace detail {
  3275. void registerReporterImpl(const char* name, int priority, reporterCreatorFunc c, bool isReporter) {
  3276. if(isReporter)
  3277. getReporters().insert(reporterMap::value_type(reporterMap::key_type(priority, name), c));
  3278. else
  3279. getListeners().insert(reporterMap::value_type(reporterMap::key_type(priority, name), c));
  3280. }
  3281. } // namespace detail
  3282. } // namespace doctest
  3283. #endif // DOCTEST_CONFIG_DISABLE
  3284. #ifdef DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
  3285. DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007) // 'function' : must be 'attribute' - see issue #182
  3286. int main(int argc, char** argv) { return doctest::Context(argc, argv).run(); }
  3287. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  3288. #endif // DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
  3289. DOCTEST_CLANG_SUPPRESS_WARNING_POP
  3290. DOCTEST_MSVC_SUPPRESS_WARNING_POP
  3291. DOCTEST_GCC_SUPPRESS_WARNING_POP
  3292. #endif // DOCTEST_LIBRARY_IMPLEMENTATION
  3293. #endif // DOCTEST_CONFIG_IMPLEMENT