catch.hpp 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238
  1. // This file has been merged from multiple headers. Please don't edit it directly
  2. #ifndef TWOBLUECUBES_CATCH_HPP_INCLUDED
  3. #define TWOBLUECUBES_CATCH_HPP_INCLUDED
  4. /*
  5. * catch.hpp
  6. * Catch
  7. *
  8. * Created by Phil on 22/10/2010.
  9. * Copyright 2010 Two Blue Cubes Ltd
  10. *
  11. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. *
  14. */
  15. /* TBD:
  16. Next:
  17. Later:
  18. Finish command line parser (list as xml)
  19. Tags?
  20. Finish macros, listed here, later (just CHECK_NOFAIL now)
  21. */
  22. #define TWOBLUECUBES_CATCH_HPP_INCLUDED
  23. // #included from: internal/catch_hub.h
  24. /*
  25. * catch_hub.h
  26. * Catch
  27. *
  28. * Created by Phil on 31/12/2010.
  29. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  30. *
  31. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  32. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  33. *
  34. */
  35. #define TWOBLUECUBES_CATCH_HUB_H_INCLUDED
  36. // #included from: catch_interfaces_reporter.h
  37. /*
  38. * catch_interfaces_reporter.h
  39. * Test
  40. *
  41. * Created by Phil on 31/12/2010.
  42. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  43. *
  44. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  45. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  46. *
  47. */
  48. #define TWOBLUECUBES_CATCH_IREPORTERREGISTRY_INCLUDED
  49. // #included from: catch_common.h
  50. /*
  51. * catch_common.h
  52. * Catch
  53. *
  54. * Created by Phil on 29/10/2010.
  55. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  56. *
  57. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  58. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  59. *
  60. */
  61. #define TWOBLUECUBES_CATCH_COMMON_H_INCLUDED
  62. #define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line
  63. #define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
  64. #define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ )
  65. #define INTERNAL_CATCH_STRINGIFY2( expr ) #expr
  66. #define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
  67. #ifdef __GNUC__
  68. #define ATTRIBUTE_NORETURN __attribute__ ((noreturn))
  69. #else
  70. #define ATTRIBUTE_NORETURN
  71. #endif
  72. #include <sstream>
  73. #include <stdexcept>
  74. #include <algorithm>
  75. namespace Catch
  76. {
  77. class NonCopyable
  78. {
  79. NonCopyable( const NonCopyable& );
  80. void operator = ( const NonCopyable& );
  81. protected:
  82. NonCopyable(){}
  83. virtual ~NonCopyable() {}
  84. };
  85. typedef char NoType;
  86. typedef int YesType;
  87. // create a T for use in sizeof expressions
  88. template<typename T> T Synth();
  89. template<typename ContainerT>
  90. inline void deleteAll( ContainerT& container )
  91. {
  92. typename ContainerT::const_iterator it = container.begin();
  93. typename ContainerT::const_iterator itEnd = container.end();
  94. for(; it != itEnd; ++it )
  95. {
  96. delete *it;
  97. }
  98. }
  99. template<typename AssociativeContainerT>
  100. inline void deleteAllValues( AssociativeContainerT& container )
  101. {
  102. typename AssociativeContainerT::const_iterator it = container.begin();
  103. typename AssociativeContainerT::const_iterator itEnd = container.end();
  104. for(; it != itEnd; ++it )
  105. {
  106. delete it->second;
  107. }
  108. }
  109. template<typename ContainerT, typename Function>
  110. inline void forEach( ContainerT& container, Function function )
  111. {
  112. std::for_each( container.begin(), container.end(), function );
  113. }
  114. template<typename ContainerT, typename Function>
  115. inline void forEach( const ContainerT& container, Function function )
  116. {
  117. std::for_each( container.begin(), container.end(), function );
  118. }
  119. ATTRIBUTE_NORETURN
  120. inline void throwLogicError( const std::string& message, const std::string& file, long line )
  121. {
  122. std::ostringstream oss;
  123. oss << "Internal Catch error: '" << message << "' at: " << file << "(" << line << ")";
  124. throw std::logic_error( oss.str() );
  125. }
  126. }
  127. #define CATCH_INTERNAL_ERROR( msg ) throwLogicError( msg, __FILE__, __LINE__ );
  128. #include <string>
  129. #include <ostream>
  130. #include <map>
  131. namespace Catch
  132. {
  133. ///////////////////////////////////////////////////////////////////////////
  134. struct IReporterConfig
  135. {
  136. virtual ~IReporterConfig
  137. ()
  138. {}
  139. virtual std::ostream& stream
  140. () const = 0;
  141. virtual bool includeSuccessfulResults
  142. () const = 0;
  143. virtual std::string getName
  144. () const = 0;
  145. };
  146. class TestCaseInfo;
  147. class ResultInfo;
  148. ///////////////////////////////////////////////////////////////////////////
  149. struct IReporter : NonCopyable
  150. {
  151. virtual ~IReporter
  152. (){}
  153. virtual void StartTesting
  154. () = 0;
  155. virtual void EndTesting
  156. ( std::size_t succeeded,
  157. std::size_t failed
  158. ) = 0;
  159. virtual void StartGroup
  160. ( const std::string& groupName
  161. ) = 0;
  162. virtual void EndGroup
  163. ( const std::string& groupName,
  164. std::size_t succeeded,
  165. std::size_t failed
  166. ) = 0;
  167. virtual void StartSection
  168. ( const std::string& sectionName,
  169. const std::string description
  170. ) = 0;
  171. virtual void EndSection
  172. ( const std::string& sectionName,
  173. std::size_t succeeded,
  174. std::size_t failed
  175. ) = 0;
  176. virtual void StartTestCase
  177. ( const TestCaseInfo& testInfo
  178. ) = 0;
  179. virtual void EndTestCase
  180. ( const TestCaseInfo& testInfo,
  181. std::size_t succeeded,
  182. std::size_t failed,
  183. const std::string& stdOut,
  184. const std::string& stdErr
  185. ) = 0;
  186. virtual void Result
  187. ( const ResultInfo& result
  188. ) = 0;
  189. };
  190. ///////////////////////////////////////////////////////////////////////////
  191. struct IReporterFactory
  192. {
  193. virtual ~IReporterFactory
  194. (){}
  195. virtual IReporter* create
  196. ( const IReporterConfig& config
  197. ) const = 0;
  198. virtual std::string getDescription
  199. () const = 0;
  200. };
  201. ///////////////////////////////////////////////////////////////////////////
  202. struct IReporterRegistry
  203. {
  204. typedef std::map<std::string, IReporterFactory*> FactoryMap;
  205. virtual ~IReporterRegistry
  206. (){}
  207. virtual IReporter* create
  208. ( const std::string& name,
  209. const IReporterConfig& config
  210. ) const = 0;
  211. virtual void registerReporter
  212. ( const std::string& name,
  213. IReporterFactory* factory
  214. ) = 0;
  215. virtual const FactoryMap& getFactories
  216. () const = 0;
  217. };
  218. ///////////////////////////////////////////////////////////////////////////
  219. inline std::string trim( const std::string& str )
  220. {
  221. std::string::size_type start = str.find_first_not_of( "\n\r\t " );
  222. std::string::size_type end = str.find_last_not_of( "\n\r\t " );
  223. return start < end ? str.substr( start, 1+end-start ) : "";
  224. }
  225. }
  226. #include <memory>
  227. #include <vector>
  228. #include <stdlib.h>
  229. namespace Catch
  230. {
  231. class TestCaseInfo;
  232. struct IResultCapture;
  233. struct ITestCaseRegistry;
  234. struct IRunner;
  235. struct IExceptionTranslatorRegistry;
  236. class GeneratorsForTest;
  237. class StreamBufBase : public std::streambuf
  238. {
  239. };
  240. class Hub
  241. {
  242. Hub();
  243. static Hub& me();
  244. Hub( const Hub& );
  245. void operator=( const Hub& );
  246. public:
  247. static void setRunner
  248. ( IRunner* runner
  249. );
  250. static void setResultCapture
  251. ( IResultCapture* resultCapture
  252. );
  253. static IResultCapture& getResultCapture
  254. ();
  255. static IReporterRegistry& getReporterRegistry
  256. ();
  257. static ITestCaseRegistry& getTestCaseRegistry
  258. ();
  259. static IExceptionTranslatorRegistry& getExceptionTranslatorRegistry
  260. ();
  261. static std::streambuf* createStreamBuf
  262. ( const std::string& streamName
  263. );
  264. static IRunner& getRunner
  265. ();
  266. static size_t getGeneratorIndex
  267. ( const std::string& fileInfo,
  268. size_t totalSize
  269. );
  270. static bool advanceGeneratorsForCurrentTest
  271. ();
  272. private:
  273. GeneratorsForTest* findGeneratorsForCurrentTest
  274. ();
  275. GeneratorsForTest& getGeneratorsForCurrentTest
  276. ();
  277. std::auto_ptr<IReporterRegistry> m_reporterRegistry;
  278. std::auto_ptr<ITestCaseRegistry> m_testCaseRegistry;
  279. std::auto_ptr<IExceptionTranslatorRegistry> m_exceptionTranslatorRegistry;
  280. IRunner* m_runner;
  281. IResultCapture* m_resultCapture;
  282. std::map<std::string, GeneratorsForTest*> m_generatorsByTestName;
  283. };
  284. }
  285. // #included from: internal/catch_test_registry.hpp
  286. /*
  287. * catch_test_registry.hpp
  288. * Catch
  289. *
  290. * Created by Phil on 18/10/2010.
  291. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  292. *
  293. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  294. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  295. *
  296. */
  297. #define TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED
  298. // #included from: catch_interfaces_testcase.h
  299. /*
  300. * catch_interfaces_testcase.h
  301. * Catch
  302. *
  303. * Created by Phil on 07/01/2011.
  304. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  305. *
  306. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  307. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  308. *
  309. */
  310. #define TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED
  311. #include <vector>
  312. namespace Catch
  313. {
  314. struct ITestCase
  315. {
  316. virtual ~ITestCase
  317. ()
  318. {}
  319. virtual void invoke
  320. () const = 0;
  321. virtual ITestCase* clone
  322. () const = 0;
  323. virtual bool operator ==
  324. ( const ITestCase& other
  325. ) const = 0;
  326. virtual bool operator <
  327. ( const ITestCase& other
  328. ) const = 0;
  329. };
  330. class TestCaseInfo;
  331. struct ITestCaseRegistry
  332. {
  333. virtual ~ITestCaseRegistry
  334. ()
  335. {}
  336. virtual void registerTest
  337. ( const TestCaseInfo& testInfo
  338. ) = 0;
  339. virtual const std::vector<TestCaseInfo>& getAllTests
  340. () const = 0;
  341. virtual std::vector<TestCaseInfo> getMatchingTestCases
  342. ( const std::string& rawTestSpec
  343. ) = 0;
  344. };
  345. }
  346. namespace Catch
  347. {
  348. template<typename C>
  349. struct MethodTestCase : ITestCase
  350. {
  351. ///////////////////////////////////////////////////////////////////////////
  352. MethodTestCase
  353. (
  354. void (C::*method)()
  355. )
  356. : m_method( method )
  357. {}
  358. ///////////////////////////////////////////////////////////////////////////
  359. virtual void invoke
  360. ()
  361. const
  362. {
  363. C obj;
  364. (obj.*m_method)();
  365. }
  366. ///////////////////////////////////////////////////////////////////////////
  367. virtual ITestCase* clone
  368. ()
  369. const
  370. {
  371. return new MethodTestCase<C>( m_method );
  372. }
  373. ///////////////////////////////////////////////////////////////////////////
  374. virtual bool operator ==
  375. (
  376. const ITestCase& other
  377. )
  378. const
  379. {
  380. const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
  381. return mtOther && m_method == mtOther->m_method;
  382. }
  383. ///////////////////////////////////////////////////////////////////////////
  384. virtual bool operator <
  385. (
  386. const ITestCase& other
  387. )
  388. const
  389. {
  390. const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
  391. return mtOther && &m_method < &mtOther->m_method;
  392. }
  393. private:
  394. void (C::*m_method)();
  395. };
  396. typedef void(*TestFunction)();
  397. struct AutoReg
  398. {
  399. AutoReg
  400. ( TestFunction function,
  401. const char* name,
  402. const char* description,
  403. const char* filename,
  404. std::size_t line
  405. );
  406. ///////////////////////////////////////////////////////////////////////////
  407. template<typename C>
  408. AutoReg
  409. (
  410. void (C::*method)(),
  411. const char* name,
  412. const char* description,
  413. const char* filename,
  414. std::size_t line
  415. )
  416. {
  417. registerTestCase( new MethodTestCase<C>( method ), name, description, filename, line );
  418. }
  419. ///////////////////////////////////////////////////////////////////////////
  420. void registerTestCase
  421. (
  422. ITestCase* testCase,
  423. const char* name,
  424. const char* description,
  425. const char* filename,
  426. std::size_t line
  427. );
  428. ~AutoReg
  429. ();
  430. private:
  431. AutoReg
  432. ( const AutoReg& );
  433. void operator=
  434. ( const AutoReg& );
  435. };
  436. } // end namespace Catch
  437. ///////////////////////////////////////////////////////////////////////////////
  438. #define INTERNAL_CATCH_TESTCASE( Name, Desc ) \
  439. static void INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction )(); \
  440. namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction ), Name, Desc, __FILE__, __LINE__ ); }\
  441. static void INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction )()
  442. ///////////////////////////////////////////////////////////////////////////////
  443. #define INTERNAL_CATCH_TESTCASE_NORETURN( Name, Desc ) \
  444. static void INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction )() ATTRIBUTE_NORETURN; \
  445. namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction ), Name, Desc, __FILE__, __LINE__ ); }\
  446. static void INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction )()
  447. ///////////////////////////////////////////////////////////////////////////////
  448. #define CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
  449. namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, Name, Desc, __FILE__, __LINE__ ); }
  450. ///////////////////////////////////////////////////////////////////////////////
  451. #define TEST_CASE_METHOD( ClassName, TestName, Desc )\
  452. namespace{ \
  453. struct INTERNAL_CATCH_UNIQUE_NAME( Catch_FixtureWrapper ) : ClassName{ \
  454. void test(); \
  455. }; \
  456. Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &INTERNAL_CATCH_UNIQUE_NAME( Catch_FixtureWrapper )::test, TestName, Desc, __FILE__, __LINE__ ); \
  457. } \
  458. void INTERNAL_CATCH_UNIQUE_NAME( Catch_FixtureWrapper )::test()
  459. // #included from: internal/catch_capture.hpp
  460. /*
  461. * catch_capture.hpp
  462. * Catch
  463. *
  464. * Created by Phil on 18/10/2010.
  465. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  466. *
  467. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  468. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  469. *
  470. */
  471. #define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
  472. // #included from: catch_resultinfo.hpp
  473. /*
  474. * catch_resultinfo.hpp
  475. * Catch
  476. *
  477. * Created by Phil on 28/10/2010.
  478. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  479. *
  480. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  481. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  482. *
  483. */
  484. #define TWOBLUECUBES_CATCH_RESULT_INFO_HPP_INCLUDED
  485. #include <string>
  486. // #included from: catch_result_type.h
  487. /*
  488. * catch_result_type.h
  489. * Catch
  490. *
  491. * Created by Phil on 07/01/2011.
  492. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  493. *
  494. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  495. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  496. *
  497. */
  498. #define TWOBLUECUBES_CATCH_RESULT_TYPE_H_INCLUDED
  499. namespace Catch
  500. {
  501. struct ResultWas{ enum OfType
  502. {
  503. Unknown = -1,
  504. Ok = 0,
  505. Info = 1,
  506. Warning = 2,
  507. FailureBit = 0x10,
  508. ExpressionFailed = FailureBit | 1,
  509. ExplicitFailure = FailureBit | 2,
  510. Exception = 0x100 | FailureBit,
  511. ThrewException = Exception | 1,
  512. DidntThrowException = Exception | 2
  513. }; };
  514. struct ResultAction
  515. {
  516. enum Value
  517. {
  518. None,
  519. Failed = 1, // Failure - but no debug break if Debug bit not set
  520. DebugFailed = 3 // Indicates that the debugger should break, if possible
  521. };
  522. };
  523. }
  524. namespace Catch
  525. {
  526. class ResultInfo
  527. {
  528. public:
  529. ///////////////////////////////////////////////////////////////////////////
  530. ResultInfo
  531. ()
  532. : m_macroName(),
  533. m_filename(),
  534. m_line( 0 ),
  535. m_expr(),
  536. m_lhs(),
  537. m_rhs(),
  538. m_op(),
  539. m_message(),
  540. m_result( ResultWas::Unknown ),
  541. m_isNot( false )
  542. {}
  543. ///////////////////////////////////////////////////////////////////////////
  544. ResultInfo
  545. (
  546. const char* expr,
  547. ResultWas::OfType result,
  548. bool isNot,
  549. const char* filename,
  550. std::size_t line,
  551. const char* macroName,
  552. const char* message
  553. )
  554. : m_macroName( macroName ),
  555. m_filename( filename ),
  556. m_line( line ),
  557. m_expr( expr ),
  558. m_lhs(),
  559. m_rhs(),
  560. m_op( isNotExpression( expr ) ? "!" : "" ),
  561. m_message( message ),
  562. m_result( result ),
  563. m_isNot( isNot )
  564. {
  565. if( isNot )
  566. m_expr = "!" + m_expr;
  567. }
  568. ///////////////////////////////////////////////////////////////////////////
  569. virtual ~ResultInfo
  570. ()
  571. {
  572. }
  573. ///////////////////////////////////////////////////////////////////////////
  574. bool ok
  575. ()
  576. const
  577. {
  578. return ( m_result & ResultWas::FailureBit ) != ResultWas::FailureBit;
  579. }
  580. ///////////////////////////////////////////////////////////////////////////
  581. ResultWas::OfType getResultType
  582. ()
  583. const
  584. {
  585. return m_result;
  586. }
  587. ///////////////////////////////////////////////////////////////////////////
  588. bool hasExpression
  589. ()
  590. const
  591. {
  592. return !m_expr.empty();
  593. }
  594. ///////////////////////////////////////////////////////////////////////////
  595. bool hasMessage
  596. ()
  597. const
  598. {
  599. return !m_message.empty();
  600. }
  601. ///////////////////////////////////////////////////////////////////////////
  602. std::string getExpression
  603. ()
  604. const
  605. {
  606. return m_expr;
  607. }
  608. ///////////////////////////////////////////////////////////////////////////
  609. std::string getExpandedExpression
  610. ()
  611. const
  612. {
  613. return hasExpression() ? getExpandedExpressionInternal() : "";
  614. }
  615. ///////////////////////////////////////////////////////////////////////////
  616. std::string getMessage
  617. ()
  618. const
  619. {
  620. return m_message;
  621. }
  622. ///////////////////////////////////////////////////////////////////////////
  623. std::string getFilename
  624. ()
  625. const
  626. {
  627. return m_filename;
  628. }
  629. ///////////////////////////////////////////////////////////////////////////
  630. std::size_t getLine
  631. ()
  632. const
  633. {
  634. return m_line;
  635. }
  636. ///////////////////////////////////////////////////////////////////////////
  637. std::string getTestMacroName
  638. ()
  639. const
  640. {
  641. return m_macroName;
  642. }
  643. protected:
  644. ///////////////////////////////////////////////////////////////////////////
  645. std::string getExpandedExpressionInternal
  646. ()
  647. const
  648. {
  649. if( m_op == "" || m_isNot )
  650. return m_lhs.empty() ? m_expr : m_op + m_lhs;
  651. else if( m_op != "!" )
  652. return m_lhs + " " + m_op + " " + m_rhs;
  653. else
  654. return "{can't expand - use " + m_macroName + "_NOT( " + m_expr.substr(1) + " ) instead of " + m_macroName + "( " + m_expr + " ) for better diagnostics}";
  655. }
  656. ///////////////////////////////////////////////////////////////////////////
  657. bool isNotExpression
  658. (
  659. const char* expr
  660. )
  661. {
  662. return expr && expr[0] == '!';
  663. }
  664. protected:
  665. std::string m_macroName;
  666. std::string m_filename;
  667. std::size_t m_line;
  668. std::string m_expr, m_lhs, m_rhs, m_op;
  669. std::string m_message;
  670. ResultWas::OfType m_result;
  671. bool m_isNot;
  672. };
  673. } // end namespace Catch
  674. // #included from: catch_interfaces_capture.h
  675. /*
  676. * catch_interfaces_capture.h
  677. * Catch
  678. *
  679. * Created by Phil on 07/01/2011.
  680. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  681. *
  682. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  683. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  684. *
  685. */
  686. #define TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED
  687. #include <string>
  688. namespace Catch
  689. {
  690. class TestCaseInfo;
  691. class ScopedInfo;
  692. class MutableResultInfo;
  693. struct IResultCapture
  694. {
  695. virtual ~IResultCapture
  696. ()
  697. {}
  698. virtual void testEnded
  699. ( const ResultInfo& result
  700. ) = 0;
  701. virtual bool sectionStarted
  702. ( const std::string& name,
  703. const std::string& description,
  704. const std::string& filename,
  705. std::size_t line,
  706. std::size_t& successes,
  707. std::size_t& failures
  708. ) = 0;
  709. virtual void sectionEnded
  710. ( const std::string& name,
  711. std::size_t successes,
  712. std::size_t failures
  713. ) = 0;
  714. virtual void pushScopedInfo
  715. ( ScopedInfo* scopedInfo
  716. ) = 0;
  717. virtual void popScopedInfo
  718. ( ScopedInfo* scopedInfo
  719. ) = 0;
  720. virtual bool shouldDebugBreak
  721. () const = 0;
  722. virtual ResultAction::Value acceptResult
  723. ( bool result
  724. ) = 0;
  725. virtual ResultAction::Value acceptResult
  726. ( ResultWas::OfType result
  727. ) = 0;
  728. virtual ResultAction::Value acceptExpression
  729. ( const MutableResultInfo& resultInfo
  730. ) = 0;
  731. virtual void acceptMessage
  732. ( const std::string& msg
  733. ) = 0;
  734. virtual std::string getCurrentTestName
  735. () const = 0;
  736. };
  737. }
  738. // #included from: catch_debugger.hpp
  739. /*
  740. * catch_debugger.hpp
  741. * Catch
  742. *
  743. * Created by Phil on 27/12/2010.
  744. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  745. *
  746. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  747. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  748. *
  749. * Provides a BreakIntoDebugger() macro for Windows and Mac (so far)
  750. */
  751. #define TWOBLUECUBES_CATCH_DEBUGGER_HPP_INCLUDED
  752. #include <iostream>
  753. #if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
  754. #define CATCH_PLATFORM_MAC
  755. #elif defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER)
  756. #define CATCH_PLATFORM_WINDOWS
  757. #endif
  758. #ifdef CATCH_PLATFORM_MAC
  759. #include <assert.h>
  760. #include <stdbool.h>
  761. #include <sys/types.h>
  762. #include <unistd.h>
  763. #include <sys/sysctl.h>
  764. namespace Catch
  765. {
  766. // The following function is taken directly from the following technical note:
  767. // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html
  768. inline bool isDebuggerActive()
  769. // Returns true if the current process is being debugged (either
  770. // running under the debugger or has a debugger attached post facto).
  771. {
  772. int junk;
  773. int mib[4];
  774. struct kinfo_proc info;
  775. size_t size;
  776. // Initialize the flags so that, if sysctl fails for some bizarre
  777. // reason, we get a predictable result.
  778. info.kp_proc.p_flag = 0;
  779. // Initialize mib, which tells sysctl the info we want, in this case
  780. // we're looking for information about a specific process ID.
  781. mib[0] = CTL_KERN;
  782. mib[1] = KERN_PROC;
  783. mib[2] = KERN_PROC_PID;
  784. mib[3] = getpid();
  785. // Call sysctl.
  786. size = sizeof(info);
  787. junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
  788. assert(junk == 0);
  789. // We're being debugged if the P_TRACED flag is set.
  790. return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
  791. }
  792. }
  793. // The following code snippet taken from:
  794. // http://cocoawithlove.com/2008/03/break-into-debugger.html
  795. #ifdef DEBUG
  796. #if defined(__ppc64__) || defined(__ppc__)
  797. #define BreakIntoDebugger() \
  798. if( Catch::isDebuggerActive() ) \
  799. { \
  800. __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" \
  801. : : : "memory","r0","r3","r4" ); \
  802. }
  803. #else
  804. #define BreakIntoDebugger() if( Catch::isDebuggerActive() ) {__asm__("int $3\n" : : );}
  805. #endif
  806. #else
  807. inline void BreakIntoDebugger(){}
  808. #endif
  809. #elif defined(_MSC_VER)
  810. extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
  811. #define BreakIntoDebugger() if (IsDebuggerPresent() ) { __debugbreak(); }
  812. inline bool isDebuggerActive()
  813. {
  814. return IsDebuggerPresent() != 0;
  815. }
  816. #else
  817. inline void BreakIntoDebugger(){}
  818. inline bool isDebuggerActive() { return false; }
  819. #endif
  820. #ifdef CATCH_PLATFORM_WINDOWS
  821. extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA( const char* );
  822. inline void writeToDebugConsole( const std::string& text )
  823. {
  824. ::OutputDebugStringA( text.c_str() );
  825. }
  826. #else
  827. inline void writeToDebugConsole( const std::string& text )
  828. {
  829. // !TBD: Need a version for Mac/ XCode and other IDEs
  830. std::cout << text;
  831. }
  832. #endif // CATCH_PLATFORM_WINDOWS
  833. // #included from: catch_evaluate.hpp
  834. /*
  835. * catch_evaluate.hpp
  836. * Catch
  837. *
  838. * Created by Phil on 04/03/2011.
  839. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  840. *
  841. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  842. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  843. *
  844. */
  845. #define TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED
  846. namespace Catch
  847. {
  848. namespace Internal
  849. {
  850. enum Operator
  851. {
  852. IsEqualTo,
  853. IsNotEqualTo,
  854. IsLessThan,
  855. IsGreaterThan,
  856. IsLessThanOrEqualTo,
  857. IsGreaterThanOrEqualTo
  858. };
  859. template<Operator Op>
  860. struct OperatorTraits{ static const char* getName(){ return "*error - unknown operator*"; } };
  861. template<>
  862. struct OperatorTraits<IsEqualTo>{ static const char* getName(){ return "=="; } };
  863. template<>
  864. struct OperatorTraits<IsNotEqualTo>{ static const char* getName(){ return "!="; } };
  865. template<>
  866. struct OperatorTraits<IsLessThan>{ static const char* getName(){ return "<"; } };
  867. template<>
  868. struct OperatorTraits<IsGreaterThan>{ static const char* getName(){ return ">"; } };
  869. template<>
  870. struct OperatorTraits<IsLessThanOrEqualTo>{ static const char* getName(){ return "<="; } };
  871. template<>
  872. struct OperatorTraits<IsGreaterThanOrEqualTo>{ static const char* getName(){ return ">="; } };
  873. // So the compare overloads can be operator agnostic we convey the operator as a template
  874. // enum, which is used to specialise an Evaluator for doing the comparison.
  875. template<typename T1, typename T2, Operator Op>
  876. class Evaluator{};
  877. template<typename T1, typename T2>
  878. struct Evaluator<T1, T2, IsEqualTo>
  879. {
  880. static bool evaluate( const T1& lhs, const T2& rhs)
  881. {
  882. return const_cast<T1&>( lhs ) == const_cast<T2&>( rhs );
  883. }
  884. };
  885. template<typename T1, typename T2>
  886. struct Evaluator<T1, T2, IsNotEqualTo>
  887. {
  888. static bool evaluate( const T1& lhs, const T2& rhs )
  889. {
  890. return const_cast<T1&>( lhs ) != const_cast<T2&>( rhs );
  891. }
  892. };
  893. template<typename T1, typename T2>
  894. struct Evaluator<T1, T2, IsLessThan>
  895. {
  896. static bool evaluate( const T1& lhs, const T2& rhs )
  897. {
  898. return const_cast<T1&>( lhs ) < const_cast<T2&>( rhs );
  899. }
  900. };
  901. template<typename T1, typename T2>
  902. struct Evaluator<T1, T2, IsGreaterThan>
  903. {
  904. static bool evaluate( const T1& lhs, const T2& rhs )
  905. {
  906. return const_cast<T1&>( lhs ) > const_cast<T2&>( rhs );
  907. }
  908. };
  909. template<typename T1, typename T2>
  910. struct Evaluator<T1, T2, IsGreaterThanOrEqualTo>
  911. {
  912. static bool evaluate( const T1& lhs, const T2& rhs )
  913. {
  914. return const_cast<T1&>( lhs ) >= const_cast<T2&>( rhs );
  915. }
  916. };
  917. template<typename T1, typename T2>
  918. struct Evaluator<T1, T2, IsLessThanOrEqualTo>
  919. {
  920. static bool evaluate( const T1& lhs, const T2& rhs )
  921. {
  922. return const_cast<T1&>( lhs ) <= const_cast<T2&>( rhs );
  923. }
  924. };
  925. template<Operator Op, typename T1, typename T2>
  926. bool applyEvaluator( const T1& lhs, const T2& rhs )
  927. {
  928. return Evaluator<T1, T2, Op>::evaluate( lhs, rhs );
  929. }
  930. // "base" overload
  931. template<Operator Op, typename T1, typename T2>
  932. bool compare( const T1& lhs, const T2& rhs )
  933. {
  934. return Evaluator<T1, T2, Op>::evaluate( lhs, rhs );
  935. }
  936. // unsigned X to int
  937. template<Operator Op> bool compare( unsigned int lhs, int rhs )
  938. {
  939. return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
  940. }
  941. template<Operator Op> bool compare( unsigned long lhs, int rhs )
  942. {
  943. return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
  944. }
  945. template<Operator Op> bool compare( unsigned char lhs, int rhs )
  946. {
  947. return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) );
  948. }
  949. // unsigned X to long
  950. template<Operator Op> bool compare( unsigned int lhs, long rhs )
  951. {
  952. return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
  953. }
  954. template<Operator Op> bool compare( unsigned long lhs, long rhs )
  955. {
  956. return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
  957. }
  958. template<Operator Op> bool compare( unsigned char lhs, long rhs )
  959. {
  960. return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) );
  961. }
  962. // int to unsigned X
  963. template<Operator Op> bool compare( int lhs, unsigned int rhs )
  964. {
  965. return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
  966. }
  967. template<Operator Op> bool compare( int lhs, unsigned long rhs )
  968. {
  969. return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
  970. }
  971. template<Operator Op> bool compare( int lhs, unsigned char rhs )
  972. {
  973. return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs );
  974. }
  975. // long to unsigned X
  976. template<Operator Op> bool compare( long lhs, unsigned int rhs )
  977. {
  978. return applyEvaluator<Op>( static_cast<unsigned long>( lhs ) );
  979. }
  980. template<Operator Op> bool compare( long lhs, unsigned long rhs )
  981. {
  982. return applyEvaluator<Op>( static_cast<unsigned long>( lhs ) );
  983. }
  984. template<Operator Op> bool compare( long lhs, unsigned char rhs )
  985. {
  986. return applyEvaluator<Op>( static_cast<unsigned long>( lhs ) );
  987. }
  988. template<Operator Op, typename T>
  989. bool compare( long lhs, const T* rhs )
  990. {
  991. return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( NULL ), rhs );
  992. }
  993. template<Operator Op, typename T>
  994. bool compare( long lhs, T* rhs )
  995. {
  996. return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs );
  997. }
  998. } // end of namespace Internal
  999. } // end of namespace Catch
  1000. #include <sstream>
  1001. namespace Catch
  1002. {
  1003. namespace Detail
  1004. {
  1005. struct NonStreamable
  1006. {
  1007. template<typename T>
  1008. NonStreamable( const T& )
  1009. {
  1010. }
  1011. };
  1012. // If the type does not have its own << overload for ostream then
  1013. // this one will be used instead
  1014. inline std::ostream& operator << ( std::ostream& ss, NonStreamable )
  1015. {
  1016. ss << "{?}";
  1017. return ss;
  1018. }
  1019. template<typename T>
  1020. inline std::string makeString
  1021. (
  1022. const T& value
  1023. )
  1024. {
  1025. std::ostringstream oss;
  1026. oss << value;
  1027. return oss.str();
  1028. }
  1029. }// end namespace Detail
  1030. ///////////////////////////////////////////////////////////////////////////////
  1031. template<typename T>
  1032. std::string toString
  1033. (
  1034. const T& value
  1035. )
  1036. {
  1037. return Detail::makeString( value );
  1038. }
  1039. // Shortcut overloads
  1040. ///////////////////////////////////////////////////////////////////////////////
  1041. inline std::string toString
  1042. (
  1043. const std::string& value
  1044. )
  1045. {
  1046. return "\"" + value + "\"";
  1047. }
  1048. ///////////////////////////////////////////////////////////////////////////////
  1049. inline std::string toString
  1050. (
  1051. const std::wstring& value
  1052. )
  1053. {
  1054. std::ostringstream oss;
  1055. oss << "\"";
  1056. for(size_t i = 0; i < value.size(); ++i )
  1057. oss << static_cast<char>( value[i] <= 0xff ? value[i] : '?');
  1058. oss << "\"";
  1059. return oss.str();
  1060. }
  1061. ///////////////////////////////////////////////////////////////////////////////
  1062. inline std::string toString
  1063. (
  1064. const char* const value
  1065. )
  1066. {
  1067. return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" );
  1068. }
  1069. ///////////////////////////////////////////////////////////////////////////////
  1070. inline std::string toString
  1071. (
  1072. char* const value
  1073. )
  1074. {
  1075. return Catch::toString( static_cast<const char* const>( value ) );
  1076. }
  1077. ///////////////////////////////////////////////////////////////////////////////
  1078. inline std::string toString
  1079. (
  1080. int value
  1081. )
  1082. {
  1083. std::ostringstream oss;
  1084. oss << value;
  1085. return oss.str();
  1086. }
  1087. ///////////////////////////////////////////////////////////////////////////////
  1088. inline std::string toString
  1089. (
  1090. unsigned int value
  1091. )
  1092. {
  1093. std::ostringstream oss;
  1094. if( value > 8192 )
  1095. oss << "0x" << std::hex << value;
  1096. else
  1097. oss << value;
  1098. return oss.str();
  1099. }
  1100. ///////////////////////////////////////////////////////////////////////////////
  1101. inline std::string toString
  1102. (
  1103. unsigned long value
  1104. )
  1105. {
  1106. std::ostringstream oss;
  1107. if( value > 8192 )
  1108. oss << "0x" << std::hex << value;
  1109. else
  1110. oss << value;
  1111. return oss.str();
  1112. }
  1113. ///////////////////////////////////////////////////////////////////////////////
  1114. inline std::string toString
  1115. (
  1116. const double value
  1117. )
  1118. {
  1119. std::ostringstream oss;
  1120. oss << value;
  1121. return oss.str();
  1122. }
  1123. ///////////////////////////////////////////////////////////////////////////////
  1124. inline std::string toString
  1125. (
  1126. bool value
  1127. )
  1128. {
  1129. return value ? "true" : "false";
  1130. }
  1131. ///////////////////////////////////////////////////////////////////////////////
  1132. inline std::string toString
  1133. (
  1134. void* p
  1135. )
  1136. {
  1137. if( !p )
  1138. return INTERNAL_CATCH_STRINGIFY( NULL );
  1139. std::ostringstream oss;
  1140. oss << p;
  1141. return oss.str();
  1142. }
  1143. ///////////////////////////////////////////////////////////////////////////////
  1144. template<typename T>
  1145. inline std::string toString
  1146. (
  1147. T* p
  1148. )
  1149. {
  1150. return Catch::toString( static_cast<void*>( p ) );
  1151. }
  1152. ///////////////////////////////////////////////////////////////////////////////
  1153. template<typename T>
  1154. inline std::string toString
  1155. (
  1156. const T* p
  1157. )
  1158. {
  1159. return Catch::toString( static_cast<void*>( const_cast<T*>( p ) ) );
  1160. }
  1161. struct TestFailureException
  1162. {
  1163. };
  1164. struct DummyExceptionType_DontUse
  1165. {
  1166. };
  1167. struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
  1168. class MutableResultInfo : public ResultInfo
  1169. {
  1170. public:
  1171. ///////////////////////////////////////////////////////////////////////////
  1172. MutableResultInfo
  1173. ()
  1174. {}
  1175. ///////////////////////////////////////////////////////////////////////////
  1176. MutableResultInfo
  1177. (
  1178. const char* expr,
  1179. bool isNot,
  1180. const char* filename,
  1181. std::size_t line,
  1182. const char* macroName,
  1183. const char* message = ""
  1184. )
  1185. : ResultInfo( expr, ResultWas::Unknown, isNot, filename, line, macroName, message )
  1186. {
  1187. }
  1188. ///////////////////////////////////////////////////////////////////////////
  1189. void setResultType
  1190. (
  1191. ResultWas::OfType result
  1192. )
  1193. {
  1194. // Flip bool results if isNot is set
  1195. if( m_isNot && result == ResultWas::Ok )
  1196. m_result = ResultWas::ExpressionFailed;
  1197. else if( m_isNot && result == ResultWas::ExpressionFailed )
  1198. m_result = ResultWas::Ok;
  1199. else
  1200. m_result = result;
  1201. }
  1202. ///////////////////////////////////////////////////////////////////////////
  1203. void setMessage
  1204. (
  1205. const std::string& message
  1206. )
  1207. {
  1208. m_message = message;
  1209. }
  1210. ///////////////////////////////////////////////////////////////////////////
  1211. void setFileAndLine
  1212. (
  1213. const std::string& filename,
  1214. std::size_t line
  1215. )
  1216. {
  1217. m_filename = filename;
  1218. m_line = line;
  1219. }
  1220. ///////////////////////////////////////////////////////////////////////////
  1221. template<typename RhsT>
  1222. STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator ||
  1223. (
  1224. const RhsT&
  1225. );
  1226. ///////////////////////////////////////////////////////////////////////////
  1227. template<typename RhsT>
  1228. STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator &&
  1229. (
  1230. const RhsT&
  1231. );
  1232. private:
  1233. friend class ResultBuilder;
  1234. template<typename T> friend class Expression;
  1235. template<typename T> friend class PtrExpression;
  1236. ///////////////////////////////////////////////////////////////////////////
  1237. MutableResultInfo& captureBoolExpression
  1238. (
  1239. bool result
  1240. )
  1241. {
  1242. m_lhs = Catch::toString( result );
  1243. m_op = m_isNot ? "!" : "";
  1244. setResultType( result ? ResultWas::Ok : ResultWas::ExpressionFailed );
  1245. return *this;
  1246. }
  1247. ///////////////////////////////////////////////////////////////////////////
  1248. template<Internal::Operator Op, typename T1, typename T2>
  1249. MutableResultInfo& captureExpression
  1250. (
  1251. const T1& lhs,
  1252. const T2& rhs
  1253. )
  1254. {
  1255. setResultType( Internal::compare<Op>( lhs, rhs ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
  1256. m_lhs = Catch::toString( lhs );
  1257. m_rhs = Catch::toString( rhs );
  1258. m_op = Internal::OperatorTraits<Op>::getName();
  1259. return *this;
  1260. }
  1261. ///////////////////////////////////////////////////////////////////////////
  1262. template<Internal::Operator Op, typename T>
  1263. MutableResultInfo& captureExpression
  1264. (
  1265. const T* lhs,
  1266. int rhs
  1267. )
  1268. {
  1269. return captureExpression<Op>( lhs, reinterpret_cast<const T*>( rhs ) );
  1270. }
  1271. };
  1272. template<typename T>
  1273. class Expression
  1274. {
  1275. void operator = ( const Expression& );
  1276. public:
  1277. ///////////////////////////////////////////////////////////////////////////
  1278. Expression
  1279. (
  1280. MutableResultInfo& result,
  1281. T lhs
  1282. )
  1283. : m_result( result ),
  1284. m_lhs( lhs )
  1285. {
  1286. }
  1287. ///////////////////////////////////////////////////////////////////////////
  1288. template<typename RhsT>
  1289. MutableResultInfo& operator ==
  1290. (
  1291. const RhsT& rhs
  1292. )
  1293. {
  1294. return m_result.captureExpression<Internal::IsEqualTo>( m_lhs, rhs );
  1295. }
  1296. ///////////////////////////////////////////////////////////////////////////
  1297. template<typename RhsT>
  1298. MutableResultInfo& operator !=
  1299. (
  1300. const RhsT& rhs
  1301. )
  1302. {
  1303. return m_result.captureExpression<Internal::IsNotEqualTo>( m_lhs, rhs );
  1304. }
  1305. ///////////////////////////////////////////////////////////////////////////
  1306. template<typename RhsT>
  1307. MutableResultInfo& operator <
  1308. (
  1309. const RhsT& rhs
  1310. )
  1311. {
  1312. return m_result.captureExpression<Internal::IsLessThan>( m_lhs, rhs );
  1313. }
  1314. ///////////////////////////////////////////////////////////////////////////
  1315. template<typename RhsT>
  1316. MutableResultInfo& operator >
  1317. (
  1318. const RhsT& rhs
  1319. )
  1320. {
  1321. return m_result.captureExpression<Internal::IsGreaterThan>( m_lhs, rhs );
  1322. }
  1323. ///////////////////////////////////////////////////////////////////////////
  1324. template<typename RhsT>
  1325. MutableResultInfo& operator <=
  1326. (
  1327. const RhsT& rhs
  1328. )
  1329. {
  1330. return m_result.captureExpression<Internal::IsLessThanOrEqualTo>( m_lhs, rhs );
  1331. }
  1332. ///////////////////////////////////////////////////////////////////////////
  1333. template<typename RhsT>
  1334. MutableResultInfo& operator >=
  1335. (
  1336. const RhsT& rhs
  1337. )
  1338. {
  1339. return m_result.captureExpression<Internal::IsGreaterThanOrEqualTo>( m_lhs, rhs );
  1340. }
  1341. ///////////////////////////////////////////////////////////////////////////
  1342. MutableResultInfo& operator ==
  1343. (
  1344. bool rhs
  1345. )
  1346. {
  1347. return m_result.captureExpression<Internal::IsEqualTo>( m_lhs, rhs );
  1348. }
  1349. ///////////////////////////////////////////////////////////////////////////
  1350. MutableResultInfo& operator !=
  1351. (
  1352. bool rhs
  1353. )
  1354. {
  1355. return m_result.captureExpression<Internal::IsNotEqualTo>( m_lhs, rhs );
  1356. }
  1357. ///////////////////////////////////////////////////////////////////////////
  1358. operator MutableResultInfo&
  1359. ()
  1360. {
  1361. return m_result.captureBoolExpression( m_lhs );
  1362. }
  1363. ///////////////////////////////////////////////////////////////////////////
  1364. template<typename RhsT>
  1365. STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator +
  1366. (
  1367. const RhsT&
  1368. );
  1369. ///////////////////////////////////////////////////////////////////////////
  1370. template<typename RhsT>
  1371. STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator -
  1372. (
  1373. const RhsT&
  1374. );
  1375. private:
  1376. MutableResultInfo& m_result;
  1377. T m_lhs;
  1378. };
  1379. template<typename LhsT>
  1380. class PtrExpression
  1381. {
  1382. public:
  1383. ///////////////////////////////////////////////////////////////////////////
  1384. PtrExpression
  1385. (
  1386. MutableResultInfo& result,
  1387. const LhsT* lhs
  1388. )
  1389. : m_result( &result ),
  1390. m_lhs( lhs )
  1391. {}
  1392. ///////////////////////////////////////////////////////////////////////////
  1393. template<typename RhsT>
  1394. MutableResultInfo& operator ==
  1395. (
  1396. const RhsT* rhs
  1397. )
  1398. {
  1399. return m_result->captureExpression<Internal::IsEqualTo>( m_lhs, rhs );
  1400. }
  1401. ///////////////////////////////////////////////////////////////////////////
  1402. // This catches NULL
  1403. MutableResultInfo& operator ==
  1404. (
  1405. LhsT* rhs
  1406. )
  1407. {
  1408. return m_result->captureExpression<Internal::IsEqualTo>( m_lhs, rhs );
  1409. }
  1410. ///////////////////////////////////////////////////////////////////////////
  1411. template<typename RhsT>
  1412. MutableResultInfo& operator !=
  1413. (
  1414. const RhsT* rhs
  1415. )
  1416. {
  1417. return m_result->captureExpression<Internal::IsNotEqualTo>( m_lhs, rhs );
  1418. }
  1419. ///////////////////////////////////////////////////////////////////////////
  1420. // This catches NULL
  1421. MutableResultInfo& operator !=
  1422. (
  1423. LhsT* rhs
  1424. )
  1425. {
  1426. return m_result->captureExpression<Internal::IsNotEqualTo>( m_lhs, rhs );
  1427. }
  1428. ///////////////////////////////////////////////////////////////////////////
  1429. operator MutableResultInfo&
  1430. ()
  1431. {
  1432. return m_result->captureBoolExpression( m_lhs );
  1433. }
  1434. private:
  1435. MutableResultInfo* m_result;
  1436. const LhsT* m_lhs;
  1437. };
  1438. class ResultBuilder
  1439. {
  1440. public:
  1441. ///////////////////////////////////////////////////////////////////////////
  1442. ResultBuilder
  1443. (
  1444. const char* filename,
  1445. std::size_t line,
  1446. const char* macroName,
  1447. const char* expr = "",
  1448. bool isNot = false
  1449. )
  1450. : m_result( expr, isNot, filename, line, macroName ),
  1451. m_messageStream()
  1452. {}
  1453. ///////////////////////////////////////////////////////////////////////////
  1454. template<typename T>
  1455. Expression<const T&> operator->*
  1456. (
  1457. const T & operand
  1458. )
  1459. {
  1460. Expression<const T&> expr( m_result, operand );
  1461. return expr;
  1462. }
  1463. ///////////////////////////////////////////////////////////////////////////
  1464. Expression<const char*> operator->*
  1465. (
  1466. const char* const& operand
  1467. )
  1468. {
  1469. Expression<const char*> expr( m_result, operand );
  1470. return expr;
  1471. }
  1472. ///////////////////////////////////////////////////////////////////////////
  1473. template<typename T>
  1474. PtrExpression<T> operator->*
  1475. (
  1476. const T* operand
  1477. )
  1478. {
  1479. PtrExpression<T> expr( m_result, operand );
  1480. return expr;
  1481. }
  1482. ///////////////////////////////////////////////////////////////////////////
  1483. template<typename T>
  1484. PtrExpression<T> operator->*
  1485. (
  1486. T* operand
  1487. )
  1488. {
  1489. PtrExpression<T> expr( m_result, operand );
  1490. return expr;
  1491. }
  1492. ///////////////////////////////////////////////////////////////////////////
  1493. Expression<bool> operator->*
  1494. (
  1495. bool value
  1496. )
  1497. {
  1498. Expression<bool> expr( m_result, value );
  1499. return expr;
  1500. }
  1501. ///////////////////////////////////////////////////////////////////////////
  1502. template<typename T>
  1503. ResultBuilder& operator <<
  1504. (
  1505. const T & value
  1506. )
  1507. {
  1508. m_messageStream << Catch::toString( value );
  1509. return *this;
  1510. }
  1511. ///////////////////////////////////////////////////////////////////////////
  1512. ResultBuilder& setResultType
  1513. (
  1514. ResultWas::OfType resultType
  1515. )
  1516. {
  1517. m_result.setResultType( resultType );
  1518. return *this;
  1519. }
  1520. ///////////////////////////////////////////////////////////////////////////
  1521. operator MutableResultInfo&
  1522. ()
  1523. {
  1524. m_result.setMessage( m_messageStream.str() );
  1525. return m_result;
  1526. }
  1527. private:
  1528. MutableResultInfo m_result;
  1529. std::ostringstream m_messageStream;
  1530. };
  1531. class ScopedInfo
  1532. {
  1533. public:
  1534. ///////////////////////////////////////////////////////////////////////////
  1535. ScopedInfo
  1536. () : m_oss()
  1537. {
  1538. Hub::getResultCapture().pushScopedInfo( this );
  1539. }
  1540. ///////////////////////////////////////////////////////////////////////////
  1541. ~ScopedInfo
  1542. ()
  1543. {
  1544. Hub::getResultCapture().popScopedInfo( this );
  1545. }
  1546. ///////////////////////////////////////////////////////////////////////////
  1547. ScopedInfo& operator <<
  1548. (
  1549. const char* str
  1550. )
  1551. {
  1552. m_oss << str;
  1553. return *this;
  1554. }
  1555. ///////////////////////////////////////////////////////////////////////////
  1556. std::string getInfo
  1557. ()
  1558. const
  1559. {
  1560. return m_oss.str();
  1561. }
  1562. private:
  1563. std::ostringstream m_oss;
  1564. };
  1565. ///////////////////////////////////////////////////////////////////////////////
  1566. // This is just here to avoid compiler warnings with macro constants
  1567. inline bool isTrue
  1568. (
  1569. bool value
  1570. )
  1571. {
  1572. return value;
  1573. }
  1574. } // end namespace Catch
  1575. ///////////////////////////////////////////////////////////////////////////////
  1576. #define INTERNAL_CATCH_ACCEPT_EXPR( expr, stopOnFailure ) \
  1577. if( Catch::ResultAction::Value internal_catch_action = Catch::Hub::getResultCapture().acceptExpression( expr ) ) \
  1578. { \
  1579. if( internal_catch_action == Catch::ResultAction::DebugFailed ) BreakIntoDebugger(); \
  1580. if( Catch::isTrue( stopOnFailure ) ) throw Catch::TestFailureException(); \
  1581. }
  1582. ///////////////////////////////////////////////////////////////////////////////
  1583. #define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
  1584. INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
  1585. if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); }
  1586. ///////////////////////////////////////////////////////////////////////////////
  1587. #define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \
  1588. try \
  1589. { \
  1590. expr; \
  1591. INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \
  1592. } \
  1593. catch( std::exception& internal_catch_exception ) \
  1594. { \
  1595. INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << internal_catch_exception.what() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
  1596. } \
  1597. catch( ... ) \
  1598. { \
  1599. INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
  1600. }
  1601. ///////////////////////////////////////////////////////////////////////////////
  1602. #define INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \
  1603. try \
  1604. { \
  1605. expr; \
  1606. INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::DidntThrowException ), stopOnFailure ); \
  1607. } \
  1608. catch( Catch::TestFailureException& ) \
  1609. { \
  1610. throw; \
  1611. } \
  1612. catch( exceptionType ) \
  1613. { \
  1614. INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \
  1615. }
  1616. ///////////////////////////////////////////////////////////////////////////////
  1617. #define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, stopOnFailure, macroName ) \
  1618. INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \
  1619. catch( ... ) \
  1620. { \
  1621. INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
  1622. }
  1623. ///////////////////////////////////////////////////////////////////////////////
  1624. #define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \
  1625. Catch::Hub::getResultCapture().acceptExpression( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName ) << reason ).setResultType( resultType ) );
  1626. ///////////////////////////////////////////////////////////////////////////////
  1627. #define INTERNAL_CATCH_SCOPED_INFO( log ) \
  1628. Catch::ScopedInfo INTERNAL_CATCH_UNIQUE_NAME( info ); \
  1629. INTERNAL_CATCH_UNIQUE_NAME( info ) << log
  1630. // #included from: internal/catch_section.hpp
  1631. /*
  1632. * catch_section.hpp
  1633. * Catch
  1634. *
  1635. * Created by Phil on 03/11/2010.
  1636. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  1637. *
  1638. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  1639. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  1640. *
  1641. */
  1642. #define TWOBLUECUBES_CATCH_SECTION_HPP_INCLUDED
  1643. #include <string>
  1644. namespace Catch
  1645. {
  1646. class Section
  1647. {
  1648. public:
  1649. ///////////////////////////////////////////////////////////////////////
  1650. Section
  1651. (
  1652. const std::string& name,
  1653. const std::string& description,
  1654. const std::string& filename,
  1655. std::size_t line
  1656. )
  1657. : m_name( name ),
  1658. m_successes(0),
  1659. m_failures(0),
  1660. m_sectionIncluded( Hub::getResultCapture().sectionStarted( name, description, filename, line, m_successes, m_failures ) )
  1661. {
  1662. }
  1663. ///////////////////////////////////////////////////////////////////////
  1664. ~Section
  1665. ()
  1666. {
  1667. if( m_sectionIncluded )
  1668. Hub::getResultCapture().sectionEnded( m_name, m_successes, m_failures );
  1669. }
  1670. ///////////////////////////////////////////////////////////////////////
  1671. // This indicates whether the section should be executed or not
  1672. operator bool
  1673. ()
  1674. {
  1675. return m_sectionIncluded;
  1676. }
  1677. private:
  1678. std::string m_name;
  1679. std::size_t m_successes;
  1680. std::size_t m_failures;
  1681. bool m_sectionIncluded;
  1682. };
  1683. } // end namespace Catch
  1684. #define INTERNAL_CATCH_SECTION( name, desc ) \
  1685. if( Catch::Section INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::Section( name, desc, __FILE__, __LINE__ ) )
  1686. // #included from: internal/catch_generators.hpp
  1687. /*
  1688. * catch_generators.hpp
  1689. * Catch
  1690. *
  1691. * Created by Phil on 27/01/2011.
  1692. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  1693. *
  1694. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  1695. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  1696. *
  1697. */
  1698. #define TWOBLUECUBES_CATCH_GENERATORS_HPP_INCLUDED
  1699. #include <iterator>
  1700. #include <vector>
  1701. #include <string>
  1702. #include <stdlib.h>
  1703. namespace Catch
  1704. {
  1705. template<typename T>
  1706. struct IGenerator
  1707. {
  1708. virtual ~IGenerator
  1709. ()
  1710. {}
  1711. virtual T getValue
  1712. ( std::size_t index
  1713. ) const = 0;
  1714. virtual std::size_t size
  1715. () const = 0;
  1716. };
  1717. template<typename T>
  1718. class BetweenGenerator : public IGenerator<T>
  1719. {
  1720. public:
  1721. ///////////////////////////////////////////////////////////////////////////
  1722. BetweenGenerator
  1723. (
  1724. T from,
  1725. T to
  1726. )
  1727. : m_from( from ),
  1728. m_to( to )
  1729. {
  1730. }
  1731. ///////////////////////////////////////////////////////////////////////////
  1732. virtual T getValue
  1733. (
  1734. std::size_t index
  1735. )
  1736. const
  1737. {
  1738. return m_from+static_cast<T>( index );
  1739. }
  1740. ///////////////////////////////////////////////////////////////////////////
  1741. virtual std::size_t size
  1742. ()
  1743. const
  1744. {
  1745. return 1+m_to-m_from;
  1746. }
  1747. private:
  1748. T m_from;
  1749. T m_to;
  1750. };
  1751. template<typename T>
  1752. class ValuesGenerator : public IGenerator<T>
  1753. {
  1754. public:
  1755. ///////////////////////////////////////////////////////////////////////////
  1756. ValuesGenerator
  1757. ()
  1758. {
  1759. }
  1760. ///////////////////////////////////////////////////////////////////////////
  1761. void add
  1762. (
  1763. T value
  1764. )
  1765. {
  1766. m_values.push_back( value );
  1767. }
  1768. ///////////////////////////////////////////////////////////////////////////
  1769. virtual T getValue
  1770. (
  1771. std::size_t index
  1772. )
  1773. const
  1774. {
  1775. return m_values[index];
  1776. }
  1777. ///////////////////////////////////////////////////////////////////////////
  1778. virtual std::size_t size
  1779. ()
  1780. const
  1781. {
  1782. return m_values.size();
  1783. }
  1784. private:
  1785. std::vector<T> m_values;
  1786. };
  1787. template<typename T>
  1788. class CompositeGenerator
  1789. {
  1790. public:
  1791. ///////////////////////////////////////////////////////////////////////////
  1792. CompositeGenerator()
  1793. : m_totalSize( 0 )
  1794. {
  1795. }
  1796. ///////////////////////////////////////////////////////////////////////////
  1797. // *** Move semantics, similar to auto_ptr ***
  1798. CompositeGenerator( CompositeGenerator& other )
  1799. : m_fileInfo( other.m_fileInfo ),
  1800. m_totalSize( 0 )
  1801. {
  1802. move( other );
  1803. }
  1804. ///////////////////////////////////////////////////////////////////////////
  1805. CompositeGenerator& setFileInfo
  1806. (
  1807. const char* fileInfo
  1808. )
  1809. {
  1810. m_fileInfo = fileInfo;
  1811. return *this;
  1812. }
  1813. ///////////////////////////////////////////////////////////////////////////
  1814. ~CompositeGenerator
  1815. ()
  1816. {
  1817. deleteAll( m_composed );
  1818. }
  1819. ///////////////////////////////////////////////////////////////////////////
  1820. operator T
  1821. ()
  1822. const
  1823. {
  1824. size_t overallIndex = Hub::getGeneratorIndex( m_fileInfo, m_totalSize );
  1825. typename std::vector<const IGenerator<T>*>::const_iterator it = m_composed.begin();
  1826. typename std::vector<const IGenerator<T>*>::const_iterator itEnd = m_composed.end();
  1827. for( size_t index = 0; it != itEnd; ++it )
  1828. {
  1829. const IGenerator<T>* generator = *it;
  1830. if( overallIndex >= index && overallIndex < index + generator->size() )
  1831. {
  1832. return generator->getValue( overallIndex-index );
  1833. }
  1834. index += generator->size();
  1835. }
  1836. CATCH_INTERNAL_ERROR( "Indexed past end of generated range" );
  1837. return T(); // Suppress spurious "not all control paths return a value" warning in Visual Studio - if you know how to fix this please do so
  1838. }
  1839. ///////////////////////////////////////////////////////////////////////////
  1840. void add
  1841. (
  1842. const IGenerator<T>* generator
  1843. )
  1844. {
  1845. m_totalSize += generator->size();
  1846. m_composed.push_back( generator );
  1847. }
  1848. ///////////////////////////////////////////////////////////////////////////
  1849. CompositeGenerator& then
  1850. (
  1851. CompositeGenerator& other
  1852. )
  1853. {
  1854. move( other );
  1855. return *this;
  1856. }
  1857. ///////////////////////////////////////////////////////////////////////////
  1858. CompositeGenerator& then
  1859. (
  1860. T value
  1861. )
  1862. {
  1863. ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>();
  1864. valuesGen->add( value );
  1865. add( valuesGen );
  1866. return *this;
  1867. }
  1868. private:
  1869. ///////////////////////////////////////////////////////////////////////////
  1870. void move
  1871. (
  1872. CompositeGenerator& other
  1873. )
  1874. {
  1875. std::copy( other.m_composed.begin(), other.m_composed.end(), std::back_inserter( m_composed ) );
  1876. m_totalSize += other.m_totalSize;
  1877. other.m_composed.clear();
  1878. }
  1879. std::vector<const IGenerator<T>*> m_composed;
  1880. std::string m_fileInfo;
  1881. size_t m_totalSize;
  1882. };
  1883. namespace Generators
  1884. {
  1885. ///////////////////////////////////////////////////////////////////////////
  1886. template<typename T>
  1887. CompositeGenerator<T> between
  1888. (
  1889. T from,
  1890. T to
  1891. )
  1892. {
  1893. CompositeGenerator<T> generators;
  1894. generators.add( new BetweenGenerator<T>( from, to ) );
  1895. return generators;
  1896. }
  1897. ///////////////////////////////////////////////////////////////////////////
  1898. template<typename T>
  1899. CompositeGenerator<T> values
  1900. (
  1901. T val1,
  1902. T val2
  1903. )
  1904. {
  1905. CompositeGenerator<T> generators;
  1906. ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>();
  1907. valuesGen->add( val1 );
  1908. valuesGen->add( val2 );
  1909. generators.add( valuesGen );
  1910. return generators;
  1911. }
  1912. ///////////////////////////////////////////////////////////////////////////
  1913. template<typename T>
  1914. CompositeGenerator<T> values
  1915. (
  1916. T val1,
  1917. T val2,
  1918. T val3
  1919. )
  1920. {
  1921. CompositeGenerator<T> generators;
  1922. ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>();
  1923. valuesGen->add( val1 );
  1924. valuesGen->add( val2 );
  1925. valuesGen->add( val3 );
  1926. generators.add( valuesGen );
  1927. return generators;
  1928. }
  1929. ///////////////////////////////////////////////////////////////////////////
  1930. template<typename T>
  1931. CompositeGenerator<T> values
  1932. (
  1933. T val1,
  1934. T val2,
  1935. T val3,
  1936. T val4
  1937. )
  1938. {
  1939. CompositeGenerator<T> generators;
  1940. ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>();
  1941. valuesGen->add( val1 );
  1942. valuesGen->add( val2 );
  1943. valuesGen->add( val3 );
  1944. valuesGen->add( val4 );
  1945. generators.add( valuesGen );
  1946. return generators;
  1947. }
  1948. } // end namespace Generators
  1949. using namespace Generators;
  1950. } // end namespace Catch
  1951. #define INTERNAL_CATCH_LINESTR2( line ) #line
  1952. #define INTERNAL_CATCH_LINESTR( line ) INTERNAL_CATCH_LINESTR2( line )
  1953. #define INTERNAL_CATCH_GENERATE( expr ) expr.setFileInfo( __FILE__ "(" INTERNAL_CATCH_LINESTR( __LINE__ ) ")" )
  1954. // #included from: internal/catch_interfaces_exception.h
  1955. /*
  1956. * catch_exception_interfaces.h
  1957. * Catch
  1958. *
  1959. * Created by Phil on 20/04/2011.
  1960. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  1961. *
  1962. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  1963. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  1964. *
  1965. */
  1966. #define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
  1967. #include <string>
  1968. namespace Catch
  1969. {
  1970. typedef std::string(*exceptionTranslateFunction)();
  1971. struct IExceptionTranslator
  1972. {
  1973. virtual ~IExceptionTranslator(){}
  1974. virtual std::string translate() const = 0;
  1975. };
  1976. struct IExceptionTranslatorRegistry
  1977. {
  1978. virtual ~IExceptionTranslatorRegistry
  1979. ()
  1980. {}
  1981. virtual void registerTranslator
  1982. ( IExceptionTranslator* translator
  1983. ) = 0;
  1984. virtual std::string translateActiveException
  1985. () const = 0;
  1986. };
  1987. class ExceptionTranslatorRegistrar
  1988. {
  1989. template<typename T>
  1990. class ExceptionTranslator : public IExceptionTranslator
  1991. {
  1992. public:
  1993. ExceptionTranslator
  1994. (
  1995. std::string(*translateFunction)( T& )
  1996. )
  1997. : m_translateFunction( translateFunction )
  1998. {}
  1999. virtual std::string translate
  2000. ()
  2001. const
  2002. {
  2003. try
  2004. {
  2005. throw;
  2006. }
  2007. catch( T& ex )
  2008. {
  2009. return m_translateFunction( ex );
  2010. }
  2011. }
  2012. protected:
  2013. std::string(*m_translateFunction)( T& );
  2014. };
  2015. public:
  2016. template<typename T>
  2017. ExceptionTranslatorRegistrar
  2018. (
  2019. std::string(*translateFunction)( T& )
  2020. )
  2021. {
  2022. Catch::Hub::getExceptionTranslatorRegistry().registerTranslator
  2023. ( new ExceptionTranslator<T>( translateFunction ) );
  2024. }
  2025. };
  2026. }
  2027. ///////////////////////////////////////////////////////////////////////////////
  2028. #define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) \
  2029. static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature ); \
  2030. namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ) ); }\
  2031. static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature )
  2032. // #included from: internal/catch_approx.hpp
  2033. /*
  2034. * catch_approx.hpp
  2035. * Catch
  2036. *
  2037. * Created by Phil on 28/04/2011.
  2038. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2039. *
  2040. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2041. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2042. *
  2043. */
  2044. #define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
  2045. #include <cmath>
  2046. #include <limits>
  2047. namespace Catch
  2048. {
  2049. namespace Detail
  2050. {
  2051. class Approx
  2052. {
  2053. public:
  2054. ///////////////////////////////////////////////////////////////////////////
  2055. explicit Approx
  2056. (
  2057. double value
  2058. )
  2059. : m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
  2060. m_scale( 1.0 ),
  2061. m_value( value )
  2062. {
  2063. }
  2064. ///////////////////////////////////////////////////////////////////////////
  2065. Approx
  2066. (
  2067. const Approx& other
  2068. )
  2069. : m_epsilon( other.m_epsilon ),
  2070. m_scale( other.m_scale ),
  2071. m_value( other.m_value )
  2072. {
  2073. }
  2074. ///////////////////////////////////////////////////////////////////////////
  2075. static Approx custom
  2076. ()
  2077. {
  2078. return Approx( 0 );
  2079. }
  2080. ///////////////////////////////////////////////////////////////////////////
  2081. Approx operator()
  2082. (
  2083. double value
  2084. )
  2085. {
  2086. Approx approx( value );
  2087. approx.epsilon( m_epsilon );
  2088. approx.scale( m_scale );
  2089. return approx;
  2090. }
  2091. ///////////////////////////////////////////////////////////////////////////
  2092. friend bool operator ==
  2093. (
  2094. double lhs,
  2095. const Approx& rhs
  2096. )
  2097. {
  2098. // Thanks to Richard Harris for his help refining this formula
  2099. return fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) );
  2100. }
  2101. ///////////////////////////////////////////////////////////////////////////
  2102. friend bool operator ==
  2103. (
  2104. const Approx& lhs,
  2105. double rhs
  2106. )
  2107. {
  2108. return operator==( rhs, lhs );
  2109. }
  2110. ///////////////////////////////////////////////////////////////////////////
  2111. friend bool operator !=
  2112. (
  2113. double lhs,
  2114. const Approx& rhs
  2115. )
  2116. {
  2117. return !operator==( lhs, rhs );
  2118. }
  2119. ///////////////////////////////////////////////////////////////////////////
  2120. friend bool operator !=
  2121. (
  2122. const Approx& lhs,
  2123. double rhs
  2124. )
  2125. {
  2126. return !operator==( rhs, lhs );
  2127. }
  2128. ///////////////////////////////////////////////////////////////////////////
  2129. Approx& epsilon
  2130. (
  2131. double newEpsilon
  2132. )
  2133. {
  2134. m_epsilon = newEpsilon;
  2135. return *this;
  2136. }
  2137. ///////////////////////////////////////////////////////////////////////////
  2138. Approx& scale
  2139. (
  2140. double newScale
  2141. )
  2142. {
  2143. m_scale = newScale;
  2144. return *this;
  2145. }
  2146. ///////////////////////////////////////////////////////////////////////////
  2147. std::string toString() const
  2148. {
  2149. std::ostringstream oss;
  2150. oss << "Approx( " << m_value << ")";
  2151. return oss.str();
  2152. }
  2153. private:
  2154. double m_epsilon;
  2155. double m_scale;
  2156. double m_value;
  2157. };
  2158. }
  2159. ///////////////////////////////////////////////////////////////////////////////
  2160. template<>
  2161. inline std::string toString<Detail::Approx>
  2162. (
  2163. const Detail::Approx& value
  2164. )
  2165. {
  2166. return value.toString();
  2167. }
  2168. } // end namespace Catch
  2169. // #included from: internal/catch_test_case_info.hpp
  2170. /*
  2171. * catch_test_case_info.hpp
  2172. * Catch
  2173. *
  2174. * Created by Phil on 29/10/2010.
  2175. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2176. *
  2177. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2178. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2179. *
  2180. */
  2181. #define TWOBLUECUBES_CATCH_TESTCASEINFO_HPP_INCLUDED
  2182. #include <map>
  2183. #include <string>
  2184. namespace Catch
  2185. {
  2186. class TestCaseInfo
  2187. {
  2188. public:
  2189. ///////////////////////////////////////////////////////////////////////
  2190. TestCaseInfo
  2191. (
  2192. ITestCase* testCase,
  2193. const char* name,
  2194. const char* description,
  2195. const char* filename,
  2196. std::size_t line
  2197. )
  2198. : m_test( testCase ),
  2199. m_name( name ),
  2200. m_description( description ),
  2201. m_filename( filename ),
  2202. m_line( line )
  2203. {
  2204. }
  2205. ///////////////////////////////////////////////////////////////////////
  2206. TestCaseInfo
  2207. ()
  2208. : m_test( NULL ),
  2209. m_name(),
  2210. m_description(),
  2211. m_filename(),
  2212. m_line( 0 )
  2213. {
  2214. }
  2215. ///////////////////////////////////////////////////////////////////////
  2216. TestCaseInfo
  2217. (
  2218. const TestCaseInfo& other
  2219. )
  2220. : m_test( other.m_test->clone() ),
  2221. m_name( other.m_name ),
  2222. m_description( other.m_description ),
  2223. m_filename( other.m_filename ),
  2224. m_line( other.m_line )
  2225. {
  2226. }
  2227. ///////////////////////////////////////////////////////////////////////
  2228. TestCaseInfo
  2229. (
  2230. const TestCaseInfo& other,
  2231. const std::string& name
  2232. )
  2233. : m_test( other.m_test->clone() ),
  2234. m_name( name ),
  2235. m_description( other.m_description ),
  2236. m_filename( other.m_filename ),
  2237. m_line( other.m_line )
  2238. {
  2239. }
  2240. ///////////////////////////////////////////////////////////////////////
  2241. TestCaseInfo& operator =
  2242. (
  2243. const TestCaseInfo& other
  2244. )
  2245. {
  2246. TestCaseInfo temp( other );
  2247. swap( temp );
  2248. return *this;
  2249. }
  2250. ///////////////////////////////////////////////////////////////////////
  2251. ~TestCaseInfo
  2252. ()
  2253. {
  2254. delete m_test;
  2255. }
  2256. ///////////////////////////////////////////////////////////////////////
  2257. void invoke
  2258. ()
  2259. const
  2260. {
  2261. m_test->invoke();
  2262. }
  2263. ///////////////////////////////////////////////////////////////////////
  2264. const std::string& getName
  2265. ()
  2266. const
  2267. {
  2268. return m_name;
  2269. }
  2270. ///////////////////////////////////////////////////////////////////////
  2271. const std::string& getDescription
  2272. ()
  2273. const
  2274. {
  2275. return m_description;
  2276. }
  2277. ///////////////////////////////////////////////////////////////////////
  2278. const std::string& getFilename
  2279. ()
  2280. const
  2281. {
  2282. return m_filename;
  2283. }
  2284. ///////////////////////////////////////////////////////////////////////
  2285. std::size_t getLine
  2286. ()
  2287. const
  2288. {
  2289. return m_line;
  2290. }
  2291. ///////////////////////////////////////////////////////////////////////
  2292. bool isHidden
  2293. ()
  2294. const
  2295. {
  2296. return m_name.size() >= 2 && m_name[0] == '.' && m_name[1] == '/';
  2297. }
  2298. ///////////////////////////////////////////////////////////////////////
  2299. void swap
  2300. (
  2301. TestCaseInfo& other
  2302. )
  2303. {
  2304. std::swap( m_test, other.m_test );
  2305. m_name.swap( other.m_name );
  2306. m_description.swap( other.m_description );
  2307. }
  2308. ///////////////////////////////////////////////////////////////////////
  2309. bool operator ==
  2310. (
  2311. const TestCaseInfo& other
  2312. )
  2313. const
  2314. {
  2315. return *m_test == *other.m_test && m_name == other.m_name && m_description == other.m_description;
  2316. }
  2317. ///////////////////////////////////////////////////////////////////////
  2318. bool operator <
  2319. (
  2320. const TestCaseInfo& other
  2321. )
  2322. const
  2323. {
  2324. if( m_name < other.m_name )
  2325. return true;
  2326. if( m_name > other.m_name )
  2327. return false;
  2328. return *m_test < *other.m_test;
  2329. }
  2330. private:
  2331. ITestCase* m_test;
  2332. std::string m_name;
  2333. std::string m_description;
  2334. std::string m_filename;
  2335. std::size_t m_line;
  2336. };
  2337. ///////////////////////////////////////////////////////////////////////////
  2338. ///////////////////////////////////////////////////////////////////////////
  2339. class TestSpec
  2340. {
  2341. public:
  2342. ///////////////////////////////////////////////////////////////////////
  2343. TestSpec
  2344. (
  2345. const std::string& rawSpec
  2346. )
  2347. : m_rawSpec( rawSpec ),
  2348. m_isWildcarded( false )
  2349. {
  2350. if( m_rawSpec[m_rawSpec.size()-1] == '*' )
  2351. {
  2352. m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 );
  2353. m_isWildcarded = true;
  2354. }
  2355. }
  2356. ///////////////////////////////////////////////////////////////////////
  2357. bool matches
  2358. (
  2359. const std::string& testName
  2360. )
  2361. const
  2362. {
  2363. if( !m_isWildcarded )
  2364. return m_rawSpec == testName;
  2365. else
  2366. return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec;
  2367. }
  2368. private:
  2369. std::string m_rawSpec;
  2370. bool m_isWildcarded;
  2371. };
  2372. }
  2373. #ifdef __OBJC__
  2374. // #included from: internal/catch_objc.hpp
  2375. /*
  2376. * catch_objc.hpp
  2377. * Catch
  2378. *
  2379. * Created by Phil on 14/11/2010.
  2380. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2381. *
  2382. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2383. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2384. *
  2385. */
  2386. #define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED
  2387. #import <objc/runtime.h>
  2388. #include <string>
  2389. // NB. Any general catch headers included here must be included
  2390. // in catch.hpp first to make sure they are included by the single
  2391. // header for non obj-usage
  2392. ///////////////////////////////////////////////////////////////////////////////
  2393. // This protocol is really only here for (self) documenting purposes, since
  2394. // all its methods are optional.
  2395. @protocol OcFixture
  2396. @optional
  2397. -(void) setUp;
  2398. -(void) tearDown;
  2399. @end
  2400. namespace Catch
  2401. {
  2402. class OcMethod : public ITestCase
  2403. {
  2404. public:
  2405. ///////////////////////////////////////////////////////////////////////
  2406. OcMethod
  2407. (
  2408. Class cls,
  2409. SEL sel
  2410. )
  2411. : m_cls( cls ),
  2412. m_sel( sel )
  2413. {
  2414. }
  2415. ///////////////////////////////////////////////////////////////////////
  2416. virtual void invoke
  2417. ()
  2418. const
  2419. {
  2420. id obj = class_createInstance( m_cls, 0 );
  2421. obj = [obj init];
  2422. if( [obj respondsToSelector: @selector(setUp) ] )
  2423. [obj performSelector: @selector(setUp)];
  2424. if( [obj respondsToSelector: m_sel] )
  2425. [obj performSelector: m_sel];
  2426. if( [obj respondsToSelector: @selector(tearDown) ] )
  2427. [obj performSelector: @selector(tearDown)];
  2428. [obj release];
  2429. }
  2430. ///////////////////////////////////////////////////////////////////////
  2431. virtual ITestCase* clone
  2432. ()
  2433. const
  2434. {
  2435. return new OcMethod( m_cls, m_sel );
  2436. }
  2437. ///////////////////////////////////////////////////////////////////////
  2438. virtual bool operator ==
  2439. (
  2440. const ITestCase& other
  2441. )
  2442. const
  2443. {
  2444. const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other );
  2445. return ocmOther && ocmOther->m_sel == m_sel;
  2446. }
  2447. ///////////////////////////////////////////////////////////////////////
  2448. virtual bool operator <
  2449. (
  2450. const ITestCase& other
  2451. )
  2452. const
  2453. {
  2454. const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other );
  2455. return ocmOther && ocmOther->m_sel < m_sel;
  2456. }
  2457. private:
  2458. Class m_cls;
  2459. SEL m_sel;
  2460. };
  2461. namespace Detail
  2462. {
  2463. ///////////////////////////////////////////////////////////////////////
  2464. inline bool startsWith
  2465. (
  2466. const std::string& str,
  2467. const std::string& sub
  2468. )
  2469. {
  2470. return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub;
  2471. }
  2472. ///////////////////////////////////////////////////////////////////////
  2473. inline const char* getAnnotation
  2474. (
  2475. Class cls,
  2476. const std::string& annotationName,
  2477. const std::string& testCaseName
  2478. )
  2479. {
  2480. NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()];
  2481. SEL sel = NSSelectorFromString( selStr );
  2482. [selStr release];
  2483. if( [cls respondsToSelector: sel] )
  2484. return (const char*)[cls performSelector: sel];
  2485. return "";
  2486. }
  2487. }
  2488. ///////////////////////////////////////////////////////////////////////////
  2489. inline size_t registerTestMethods
  2490. ()
  2491. {
  2492. size_t noTestMethods = 0;
  2493. int noClasses = objc_getClassList( NULL, 0 );
  2494. std::vector<Class> classes( noClasses );
  2495. objc_getClassList( &classes[0], noClasses );
  2496. for( int c = 0; c < noClasses; c++ )
  2497. {
  2498. Class cls = classes[c];
  2499. {
  2500. u_int count;
  2501. Method* methods = class_copyMethodList( cls, &count );
  2502. for( int m = 0; m < count ; m++ )
  2503. {
  2504. SEL selector = method_getName(methods[m]);
  2505. std::string methodName = sel_getName(selector);
  2506. if( Detail::startsWith( methodName, "Catch_TestCase_" ) )
  2507. {
  2508. std::string testCaseName = methodName.substr( 15 );
  2509. const char* name = Detail::getAnnotation( cls, "Name", testCaseName );
  2510. const char* desc = Detail::getAnnotation( cls, "Description", testCaseName );
  2511. Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new OcMethod( cls, selector ), name, desc, "", 0 ) );
  2512. noTestMethods++;
  2513. }
  2514. }
  2515. free(methods);
  2516. }
  2517. }
  2518. return noTestMethods;
  2519. }
  2520. }
  2521. ///////////////////////////////////////////////////////////////////////////////
  2522. #define OC_TEST_CASE( name, desc )\
  2523. +(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \
  2524. {\
  2525. return name; \
  2526. }\
  2527. +(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \
  2528. { \
  2529. return desc; \
  2530. } \
  2531. -(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test )
  2532. #endif
  2533. #if defined( CATCH_CONFIG_MAIN ) || defined( CATCH_CONFIG_RUNNER )
  2534. // #included from: catch_runner.hpp
  2535. /*
  2536. * catch_runner.hpp
  2537. * Catch
  2538. *
  2539. * Created by Phil on 31/10/2010.
  2540. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2541. *
  2542. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2543. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2544. *
  2545. */
  2546. #define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
  2547. // #included from: internal/catch_hub_impl.hpp
  2548. /*
  2549. * catch_hub_impl.hpp
  2550. * Catch
  2551. *
  2552. * Created by Phil on 31/12/2010.
  2553. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2554. *
  2555. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2556. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2557. *
  2558. */
  2559. // #included from: catch_reporter_registry.hpp
  2560. /*
  2561. * catch_reporter_registry.hpp
  2562. * Catch
  2563. *
  2564. * Created by Phil on 29/10/2010.
  2565. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2566. *
  2567. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2568. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2569. *
  2570. */
  2571. #define TWOBLUECUBES_CATCH_REPORTER_REGISTRY_HPP_INCLUDED
  2572. #include <map>
  2573. namespace Catch
  2574. {
  2575. class ReporterRegistry : public IReporterRegistry
  2576. {
  2577. public:
  2578. ///////////////////////////////////////////////////////////////////////
  2579. ~ReporterRegistry
  2580. ()
  2581. {
  2582. deleteAllValues( m_factories );
  2583. }
  2584. ///////////////////////////////////////////////////////////////////////
  2585. virtual IReporter* create
  2586. (
  2587. const std::string& name,
  2588. const IReporterConfig& config
  2589. )
  2590. const
  2591. {
  2592. FactoryMap::const_iterator it = m_factories.find( name );
  2593. if( it == m_factories.end() )
  2594. return NULL;
  2595. return it->second->create( config );
  2596. }
  2597. ///////////////////////////////////////////////////////////////////////
  2598. void registerReporter
  2599. (
  2600. const std::string& name,
  2601. IReporterFactory* factory
  2602. )
  2603. {
  2604. m_factories.insert( std::make_pair( name, factory ) );
  2605. }
  2606. ///////////////////////////////////////////////////////////////////////
  2607. const FactoryMap& getFactories
  2608. ()
  2609. const
  2610. {
  2611. return m_factories;
  2612. }
  2613. private:
  2614. FactoryMap m_factories;
  2615. };
  2616. }
  2617. // #included from: catch_test_case_registry_impl.hpp
  2618. /*
  2619. * catch_test_case_registry_impl.hpp
  2620. * Catch
  2621. *
  2622. * Created by Phil on 7/1/2011
  2623. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2624. *
  2625. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2626. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2627. *
  2628. */
  2629. #include <vector>
  2630. #include <set>
  2631. #include <sstream>
  2632. #include <iostream> // !TBD DBG
  2633. namespace Catch
  2634. {
  2635. class TestRegistry : public ITestCaseRegistry
  2636. {
  2637. public:
  2638. ///////////////////////////////////////////////////////////////////////////
  2639. TestRegistry
  2640. ()
  2641. : m_unnamedCount( 0 )
  2642. {
  2643. }
  2644. ///////////////////////////////////////////////////////////////////////////
  2645. virtual void registerTest
  2646. (
  2647. const TestCaseInfo& testInfo
  2648. )
  2649. {
  2650. if( testInfo.getName() == "" )
  2651. {
  2652. std::ostringstream oss;
  2653. oss << testInfo.getName() << "unnamed/" << ++m_unnamedCount;
  2654. return registerTest( TestCaseInfo( testInfo, oss.str() ) );
  2655. }
  2656. if( m_functions.find( testInfo ) == m_functions.end() )
  2657. {
  2658. m_functions.insert( testInfo );
  2659. m_functionsInOrder.push_back( testInfo );
  2660. }
  2661. }
  2662. ///////////////////////////////////////////////////////////////////////////
  2663. virtual const std::vector<TestCaseInfo>& getAllTests
  2664. ()
  2665. const
  2666. {
  2667. return m_functionsInOrder;
  2668. }
  2669. ///////////////////////////////////////////////////////////////////////////
  2670. virtual std::vector<TestCaseInfo> getMatchingTestCases
  2671. (
  2672. const std::string& rawTestSpec
  2673. )
  2674. {
  2675. TestSpec testSpec( rawTestSpec );
  2676. std::vector<TestCaseInfo> testList;
  2677. std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin();
  2678. std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end();
  2679. for(; it != itEnd; ++it )
  2680. {
  2681. if( testSpec.matches( it->getName() ) )
  2682. {
  2683. testList.push_back( *it );
  2684. }
  2685. }
  2686. return testList;
  2687. }
  2688. private:
  2689. std::set<TestCaseInfo> m_functions;
  2690. std::vector<TestCaseInfo> m_functionsInOrder;
  2691. size_t m_unnamedCount;
  2692. };
  2693. ///////////////////////////////////////////////////////////////////////////
  2694. ///////////////////////////////////////////////////////////////////////////
  2695. struct FreeFunctionTestCase : ITestCase
  2696. {
  2697. ///////////////////////////////////////////////////////////////////////////
  2698. FreeFunctionTestCase
  2699. (
  2700. TestFunction fun
  2701. )
  2702. : m_fun( fun )
  2703. {}
  2704. ///////////////////////////////////////////////////////////////////////////
  2705. virtual void invoke
  2706. ()
  2707. const
  2708. {
  2709. m_fun();
  2710. }
  2711. ///////////////////////////////////////////////////////////////////////////
  2712. virtual ITestCase* clone
  2713. ()
  2714. const
  2715. {
  2716. return new FreeFunctionTestCase( m_fun );
  2717. }
  2718. ///////////////////////////////////////////////////////////////////////////
  2719. virtual bool operator ==
  2720. (
  2721. const ITestCase& other
  2722. )
  2723. const
  2724. {
  2725. const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
  2726. return ffOther && m_fun == ffOther->m_fun;
  2727. }
  2728. ///////////////////////////////////////////////////////////////////////////
  2729. virtual bool operator <
  2730. (
  2731. const ITestCase& other
  2732. )
  2733. const
  2734. {
  2735. const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
  2736. return ffOther && m_fun < ffOther->m_fun;
  2737. }
  2738. private:
  2739. TestFunction m_fun;
  2740. };
  2741. ///////////////////////////////////////////////////////////////////////////
  2742. ///////////////////////////////////////////////////////////////////////////
  2743. ///////////////////////////////////////////////////////////////////////////
  2744. AutoReg::AutoReg
  2745. (
  2746. TestFunction function,
  2747. const char* name,
  2748. const char* description,
  2749. const char* filename,
  2750. std::size_t line
  2751. )
  2752. {
  2753. registerTestCase( new FreeFunctionTestCase( function ), name, description, filename, line );
  2754. }
  2755. ///////////////////////////////////////////////////////////////////////////
  2756. AutoReg::~AutoReg
  2757. ()
  2758. {
  2759. }
  2760. ///////////////////////////////////////////////////////////////////////////
  2761. void AutoReg::registerTestCase
  2762. (
  2763. ITestCase* testCase,
  2764. const char* name,
  2765. const char* description,
  2766. const char* filename,
  2767. std::size_t line
  2768. )
  2769. {
  2770. Hub::getTestCaseRegistry().registerTest( TestCaseInfo( testCase, name, description, filename, line ) );
  2771. }
  2772. } // end namespace Catch
  2773. // #included from: catch_exception_translator_registry.hpp
  2774. /*
  2775. * catch_exception_translator_registry.hpp
  2776. * Catch
  2777. *
  2778. * Created by Phil on 20/04/2011.
  2779. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  2780. *
  2781. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2782. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2783. *
  2784. */
  2785. #define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_HPP_INCLUDED
  2786. namespace Catch
  2787. {
  2788. class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry
  2789. {
  2790. ///////////////////////////////////////////////////////////////////////
  2791. virtual void registerTranslator
  2792. (
  2793. IExceptionTranslator* translator
  2794. )
  2795. {
  2796. m_translators.push_back( translator );
  2797. }
  2798. ///////////////////////////////////////////////////////////////////////
  2799. virtual std::string translateActiveException
  2800. ()
  2801. const
  2802. {
  2803. return tryTranslators( m_translators.begin() );
  2804. }
  2805. ///////////////////////////////////////////////////////////////////////
  2806. std::string tryTranslators
  2807. (
  2808. std::vector<IExceptionTranslator*>::const_iterator it
  2809. )
  2810. const
  2811. {
  2812. if( it == m_translators.end() )
  2813. return "Unknown exception";
  2814. try
  2815. {
  2816. return (*it)->translate();
  2817. }
  2818. catch(...)
  2819. {
  2820. return tryTranslators( it+1 );
  2821. }
  2822. }
  2823. private:
  2824. std::vector<IExceptionTranslator*> m_translators;
  2825. };
  2826. }
  2827. // #included from: catch_runner_impl.hpp
  2828. /*
  2829. * catch_runner.hpp
  2830. * Catch
  2831. *
  2832. * Created by Phil on 22/10/2010.
  2833. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2834. *
  2835. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2836. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2837. *
  2838. */
  2839. #define TWOBLUECUBES_INTERNAL_CATCH_RUNNER_HPP_INCLUDED
  2840. // #included from: catch_interfaces_runner.h
  2841. /*
  2842. * catch_interfaces_runner.h
  2843. * Catch
  2844. *
  2845. * Created by Phil on 07/01/2011.
  2846. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  2847. *
  2848. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2849. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2850. *
  2851. */
  2852. #define TWOBLUECUBES_INTERNAL_CATCH_INTERFACES_RUNNER_H_INCLUDED
  2853. #include <string>
  2854. namespace Catch
  2855. {
  2856. class TestCaseInfo;
  2857. struct IRunner
  2858. {
  2859. virtual ~IRunner
  2860. ()
  2861. {}
  2862. virtual void runAll
  2863. ( bool runHiddenTests = false
  2864. ) = 0;
  2865. virtual std::size_t runMatching
  2866. ( const std::string& rawTestSpec
  2867. ) = 0;
  2868. virtual std::size_t getSuccessCount
  2869. () const = 0;
  2870. virtual std:: size_t getFailureCount
  2871. () const = 0;
  2872. };
  2873. }
  2874. // #included from: catch_config.hpp
  2875. /*
  2876. * catch_config.hpp
  2877. * Catch
  2878. *
  2879. * Created by Phil on 08/11/2010.
  2880. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  2881. *
  2882. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  2883. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  2884. *
  2885. */
  2886. #define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
  2887. #include <memory>
  2888. #include <vector>
  2889. #include <string>
  2890. #include <iostream>
  2891. namespace Catch
  2892. {
  2893. class Config : public IReporterConfig
  2894. {
  2895. private:
  2896. Config( const Config& other );
  2897. Config& operator = ( const Config& other );
  2898. public:
  2899. struct Include { enum What
  2900. {
  2901. FailedOnly,
  2902. SuccessfulResults
  2903. }; };
  2904. struct List{ enum What
  2905. {
  2906. None = 0,
  2907. Reports = 1,
  2908. Tests = 2,
  2909. All = 3,
  2910. WhatMask = 0xf,
  2911. AsText = 0x10,
  2912. AsXml = 0x11,
  2913. AsMask = 0xf0
  2914. }; };
  2915. ///////////////////////////////////////////////////////////////////////////
  2916. Config()
  2917. : m_reporter( NULL ),
  2918. m_listSpec( List::None ),
  2919. m_shouldDebugBreak( false ),
  2920. m_showHelp( false ),
  2921. m_streambuf( NULL ),
  2922. m_os( std::cout.rdbuf() ),
  2923. m_includeWhat( Include::FailedOnly )
  2924. {}
  2925. ///////////////////////////////////////////////////////////////////////////
  2926. ~Config()
  2927. {
  2928. m_os.rdbuf( std::cout.rdbuf() );
  2929. delete m_streambuf;
  2930. }
  2931. ///////////////////////////////////////////////////////////////////////////
  2932. void setReporter( const std::string& reporterName )
  2933. {
  2934. if( m_reporter.get() )
  2935. return setError( "Only one reporter may be specified" );
  2936. setReporter( Hub::getReporterRegistry().create( reporterName, *this ) );
  2937. }
  2938. ///////////////////////////////////////////////////////////////////////////
  2939. void addTestSpec( const std::string& testSpec )
  2940. {
  2941. m_testSpecs.push_back( testSpec );
  2942. }
  2943. ///////////////////////////////////////////////////////////////////////////
  2944. bool testsSpecified() const
  2945. {
  2946. return !m_testSpecs.empty();
  2947. }
  2948. ///////////////////////////////////////////////////////////////////////////
  2949. const std::vector<std::string>& getTestSpecs() const
  2950. {
  2951. return m_testSpecs;
  2952. }
  2953. ///////////////////////////////////////////////////////////////////////////
  2954. List::What getListSpec( void ) const
  2955. {
  2956. return m_listSpec;
  2957. }
  2958. ///////////////////////////////////////////////////////////////////////////
  2959. void setListSpec( List::What listSpec )
  2960. {
  2961. m_listSpec = listSpec;
  2962. }
  2963. ///////////////////////////////////////////////////////////////////////////
  2964. void setFilename( const std::string& filename )
  2965. {
  2966. m_filename = filename;
  2967. }
  2968. ///////////////////////////////////////////////////////////////////////////
  2969. const std::string& getFilename() const
  2970. {
  2971. return m_filename;
  2972. }
  2973. ///////////////////////////////////////////////////////////////////////////
  2974. const std::string& getMessage() const
  2975. {
  2976. return m_message;
  2977. }
  2978. ///////////////////////////////////////////////////////////////////////////
  2979. void setError( const std::string& errorMessage )
  2980. {
  2981. m_message = errorMessage + "\n\n" + "Usage: ...";
  2982. }
  2983. ///////////////////////////////////////////////////////////////////////////
  2984. void setReporter( IReporter* reporter )
  2985. {
  2986. m_reporter = std::auto_ptr<IReporter>( reporter );
  2987. }
  2988. ///////////////////////////////////////////////////////////////////////////
  2989. IReporter* getReporter() const
  2990. {
  2991. if( !m_reporter.get() )
  2992. const_cast<Config*>( this )->setReporter( Hub::getReporterRegistry().create( "basic", *this ) );
  2993. return m_reporter.get();
  2994. }
  2995. ///////////////////////////////////////////////////////////////////////////
  2996. List::What listWhat() const
  2997. {
  2998. return static_cast<List::What>( m_listSpec & List::WhatMask );
  2999. }
  3000. ///////////////////////////////////////////////////////////////////////////
  3001. List::What listAs() const
  3002. {
  3003. return static_cast<List::What>( m_listSpec & List::AsMask );
  3004. }
  3005. ///////////////////////////////////////////////////////////////////////////
  3006. void setIncludeWhat( Include::What includeWhat )
  3007. {
  3008. m_includeWhat = includeWhat;
  3009. }
  3010. ///////////////////////////////////////////////////////////////////////////
  3011. void setShouldDebugBreak( bool shouldDebugBreakFlag )
  3012. {
  3013. m_shouldDebugBreak = shouldDebugBreakFlag;
  3014. }
  3015. ///////////////////////////////////////////////////////////////////////////
  3016. void setName( const std::string& name )
  3017. {
  3018. m_name = name;
  3019. }
  3020. ///////////////////////////////////////////////////////////////////////////
  3021. std::string getName() const
  3022. {
  3023. return m_name;
  3024. }
  3025. ///////////////////////////////////////////////////////////////////////////
  3026. bool shouldDebugBreak() const
  3027. {
  3028. return m_shouldDebugBreak;
  3029. }
  3030. ///////////////////////////////////////////////////////////////////////////
  3031. void setShowHelp( bool showHelpFlag )
  3032. {
  3033. m_showHelp = showHelpFlag;
  3034. }
  3035. ///////////////////////////////////////////////////////////////////////////
  3036. bool showHelp() const
  3037. {
  3038. return m_showHelp;
  3039. }
  3040. ///////////////////////////////////////////////////////////////////////////
  3041. virtual std::ostream& stream() const
  3042. {
  3043. return m_os;
  3044. }
  3045. ///////////////////////////////////////////////////////////////////////////
  3046. void setStreamBuf( std::streambuf* buf )
  3047. {
  3048. m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
  3049. }
  3050. ///////////////////////////////////////////////////////////////////////////
  3051. void useStream( const std::string& streamName )
  3052. {
  3053. std::streambuf* newBuf = Hub::createStreamBuf( streamName );
  3054. setStreamBuf( newBuf );
  3055. delete m_streambuf;
  3056. m_streambuf = newBuf;
  3057. }
  3058. ///////////////////////////////////////////////////////////////////////////
  3059. virtual bool includeSuccessfulResults() const
  3060. {
  3061. return m_includeWhat == Include::SuccessfulResults;
  3062. }
  3063. private:
  3064. std::auto_ptr<IReporter> m_reporter;
  3065. std::string m_filename;
  3066. std::string m_message;
  3067. List::What m_listSpec;
  3068. std::vector<std::string> m_testSpecs;
  3069. bool m_shouldDebugBreak;
  3070. bool m_showHelp;
  3071. std::streambuf* m_streambuf;
  3072. mutable std::ostream m_os;
  3073. Include::What m_includeWhat;
  3074. std::string m_name;
  3075. };
  3076. } // end namespace Catch
  3077. #include <set>
  3078. #include <string>
  3079. namespace Catch
  3080. {
  3081. class StreamRedirect
  3082. {
  3083. public:
  3084. ///////////////////////////////////////////////////////////////////////
  3085. StreamRedirect
  3086. (
  3087. std::ostream& stream,
  3088. std::string& targetString
  3089. )
  3090. : m_stream( stream ),
  3091. m_prevBuf( stream.rdbuf() ),
  3092. m_targetString( targetString )
  3093. {
  3094. stream.rdbuf( m_oss.rdbuf() );
  3095. }
  3096. ///////////////////////////////////////////////////////////////////////
  3097. ~StreamRedirect
  3098. ()
  3099. {
  3100. m_targetString += m_oss.str();
  3101. m_stream.rdbuf( m_prevBuf );
  3102. }
  3103. private:
  3104. std::ostream& m_stream;
  3105. std::streambuf* m_prevBuf;
  3106. std::ostringstream m_oss;
  3107. std::string& m_targetString;
  3108. };
  3109. ///////////////////////////////////////////////////////////////////////////
  3110. ///////////////////////////////////////////////////////////////////////////
  3111. class SectionInfo
  3112. {
  3113. public:
  3114. enum Status
  3115. {
  3116. Root,
  3117. Unknown,
  3118. NonLeaf,
  3119. TestedLeaf
  3120. };
  3121. ///////////////////////////////////////////////////////////////////////
  3122. SectionInfo
  3123. (
  3124. SectionInfo* parent
  3125. )
  3126. : m_status( Unknown ),
  3127. m_parent( parent )
  3128. {
  3129. }
  3130. ///////////////////////////////////////////////////////////////////////
  3131. SectionInfo
  3132. ()
  3133. : m_status( Root ),
  3134. m_parent( NULL )
  3135. {
  3136. }
  3137. ///////////////////////////////////////////////////////////////////////
  3138. ~SectionInfo
  3139. ()
  3140. {
  3141. deleteAllValues( m_subSections );
  3142. }
  3143. ///////////////////////////////////////////////////////////////////////
  3144. bool shouldRun
  3145. ()
  3146. const
  3147. {
  3148. return m_status != TestedLeaf;
  3149. }
  3150. ///////////////////////////////////////////////////////////////////////
  3151. bool ran
  3152. ()
  3153. {
  3154. if( m_status != NonLeaf )
  3155. {
  3156. m_status = TestedLeaf;
  3157. return true;
  3158. }
  3159. return false;
  3160. }
  3161. ///////////////////////////////////////////////////////////////////////
  3162. SectionInfo* findSubSection
  3163. (
  3164. const std::string& name
  3165. )
  3166. {
  3167. std::map<std::string, SectionInfo*>::const_iterator it = m_subSections.find( name );
  3168. return it != m_subSections.end()
  3169. ? it->second
  3170. : NULL;
  3171. }
  3172. ///////////////////////////////////////////////////////////////////////
  3173. SectionInfo* addSubSection
  3174. (
  3175. const std::string& name
  3176. )
  3177. {
  3178. SectionInfo* subSection = new SectionInfo( this );
  3179. m_subSections.insert( std::make_pair( name, subSection ) );
  3180. m_status = NonLeaf;
  3181. return subSection;
  3182. }
  3183. ///////////////////////////////////////////////////////////////////////
  3184. SectionInfo* getParent
  3185. ()
  3186. {
  3187. return m_parent;
  3188. }
  3189. ///////////////////////////////////////////////////////////////////////
  3190. bool hasUntestedSections
  3191. ()
  3192. const
  3193. {
  3194. if( m_status == Unknown )
  3195. return true;
  3196. std::map<std::string, SectionInfo*>::const_iterator it = m_subSections.begin();
  3197. std::map<std::string, SectionInfo*>::const_iterator itEnd = m_subSections.end();
  3198. for(; it != itEnd; ++it )
  3199. {
  3200. if( it->second->hasUntestedSections() )
  3201. return true;
  3202. }
  3203. return false;
  3204. }
  3205. private:
  3206. Status m_status;
  3207. std::map<std::string, SectionInfo*> m_subSections;
  3208. SectionInfo* m_parent;
  3209. };
  3210. ///////////////////////////////////////////////////////////////////////////
  3211. ///////////////////////////////////////////////////////////////////////////
  3212. class RunningTest
  3213. {
  3214. enum RunStatus
  3215. {
  3216. NothingRun,
  3217. EncounteredASection,
  3218. RanAtLeastOneSection,
  3219. RanToCompletionWithSections,
  3220. RanToCompletionWithNoSections
  3221. };
  3222. public:
  3223. ///////////////////////////////////////////////////////////////////////
  3224. explicit RunningTest
  3225. (
  3226. const TestCaseInfo* info = NULL
  3227. )
  3228. : m_info( info ),
  3229. m_runStatus( RanAtLeastOneSection ),
  3230. m_currentSection( &m_rootSection ),
  3231. m_changed( false )
  3232. {
  3233. }
  3234. ///////////////////////////////////////////////////////////////////////
  3235. bool wasSectionSeen
  3236. ()
  3237. const
  3238. {
  3239. return m_runStatus == RanAtLeastOneSection ||
  3240. m_runStatus == RanToCompletionWithSections;
  3241. }
  3242. ///////////////////////////////////////////////////////////////////////
  3243. void reset
  3244. ()
  3245. {
  3246. m_runStatus = NothingRun;
  3247. m_changed = false;
  3248. }
  3249. ///////////////////////////////////////////////////////////////////////
  3250. void ranToCompletion
  3251. ()
  3252. {
  3253. m_runStatus = m_runStatus == RanAtLeastOneSection ||
  3254. m_runStatus == EncounteredASection
  3255. ? RanToCompletionWithSections
  3256. : RanToCompletionWithNoSections;
  3257. }
  3258. ///////////////////////////////////////////////////////////////////////
  3259. bool addSection
  3260. (
  3261. const std::string& name
  3262. )
  3263. {
  3264. if( m_runStatus == NothingRun )
  3265. m_runStatus = EncounteredASection;
  3266. SectionInfo* thisSection = m_currentSection->findSubSection( name );
  3267. if( !thisSection )
  3268. {
  3269. thisSection = m_currentSection->addSubSection( name );
  3270. m_changed = true;
  3271. }
  3272. if( !wasSectionSeen() && thisSection->shouldRun() )
  3273. {
  3274. m_currentSection = thisSection;
  3275. return true;
  3276. }
  3277. return false;
  3278. }
  3279. ///////////////////////////////////////////////////////////////////////
  3280. void endSection
  3281. (
  3282. const std::string&
  3283. )
  3284. {
  3285. if( m_currentSection->ran() )
  3286. {
  3287. m_runStatus = RanAtLeastOneSection;
  3288. m_changed = true;
  3289. }
  3290. m_currentSection = m_currentSection->getParent();
  3291. }
  3292. ///////////////////////////////////////////////////////////////////////
  3293. const TestCaseInfo& getTestCaseInfo
  3294. ()
  3295. const
  3296. {
  3297. return *m_info;
  3298. }
  3299. ///////////////////////////////////////////////////////////////////////
  3300. bool hasUntestedSections
  3301. ()
  3302. const
  3303. {
  3304. return m_runStatus == RanAtLeastOneSection ||
  3305. ( m_rootSection.hasUntestedSections() && m_changed );
  3306. }
  3307. private:
  3308. const TestCaseInfo* m_info;
  3309. RunStatus m_runStatus;
  3310. SectionInfo m_rootSection;
  3311. SectionInfo* m_currentSection;
  3312. bool m_changed;
  3313. };
  3314. ///////////////////////////////////////////////////////////////////////////
  3315. ///////////////////////////////////////////////////////////////////////////
  3316. class Runner : public IResultCapture, public IRunner
  3317. {
  3318. Runner( const Runner& );
  3319. void operator =( const Runner& );
  3320. public:
  3321. ///////////////////////////////////////////////////////////////////////////
  3322. explicit Runner
  3323. (
  3324. const Config& config
  3325. )
  3326. : m_runningTest( NULL ),
  3327. m_config( config ),
  3328. m_successes( 0 ),
  3329. m_failures( 0 ),
  3330. m_reporter( m_config.getReporter() ),
  3331. m_prevRunner( &Hub::getRunner() ),
  3332. m_prevResultCapture( &Hub::getResultCapture() )
  3333. {
  3334. Hub::setRunner( this );
  3335. Hub::setResultCapture( this );
  3336. m_reporter->StartTesting();
  3337. }
  3338. ///////////////////////////////////////////////////////////////////////////
  3339. ~Runner
  3340. ()
  3341. {
  3342. m_reporter->EndTesting( m_successes, m_failures );
  3343. Hub::setRunner( m_prevRunner );
  3344. Hub::setResultCapture( m_prevResultCapture );
  3345. }
  3346. ///////////////////////////////////////////////////////////////////////////
  3347. virtual void runAll
  3348. (
  3349. bool runHiddenTests = false
  3350. )
  3351. {
  3352. std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests();
  3353. for( std::size_t i=0; i < allTests.size(); ++i )
  3354. {
  3355. if( runHiddenTests || !allTests[i].isHidden() )
  3356. runTest( allTests[i] );
  3357. }
  3358. }
  3359. ///////////////////////////////////////////////////////////////////////////
  3360. virtual std::size_t runMatching
  3361. (
  3362. const std::string& rawTestSpec
  3363. )
  3364. {
  3365. TestSpec testSpec( rawTestSpec );
  3366. std::vector<TestCaseInfo> allTests = Hub::getTestCaseRegistry().getAllTests();
  3367. std::size_t testsRun = 0;
  3368. for( std::size_t i=0; i < allTests.size(); ++i )
  3369. {
  3370. if( testSpec.matches( allTests[i].getName() ) )
  3371. {
  3372. runTest( allTests[i] );
  3373. testsRun++;
  3374. }
  3375. }
  3376. return testsRun;
  3377. }
  3378. ///////////////////////////////////////////////////////////////////////////
  3379. void runTest
  3380. (
  3381. const TestCaseInfo& testInfo
  3382. )
  3383. {
  3384. std::size_t prevSuccessCount = m_successes;
  3385. std::size_t prevFailureCount = m_failures;
  3386. std::string redirectedCout;
  3387. std::string redirectedCerr;
  3388. m_reporter->StartTestCase( testInfo );
  3389. m_runningTest = new RunningTest( &testInfo );
  3390. do
  3391. {
  3392. do
  3393. {
  3394. m_currentResult.setFileAndLine( m_runningTest->getTestCaseInfo().getFilename(),
  3395. m_runningTest->getTestCaseInfo().getLine() );
  3396. runCurrentTest( redirectedCout, redirectedCerr );
  3397. }
  3398. while( m_runningTest->hasUntestedSections() );
  3399. }
  3400. while( Hub::advanceGeneratorsForCurrentTest() );
  3401. delete m_runningTest;
  3402. m_runningTest = NULL;
  3403. m_reporter->EndTestCase( testInfo, m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr );
  3404. }
  3405. ///////////////////////////////////////////////////////////////////////////
  3406. virtual std::size_t getSuccessCount
  3407. ()
  3408. const
  3409. {
  3410. return m_successes;
  3411. }
  3412. ///////////////////////////////////////////////////////////////////////////
  3413. virtual std:: size_t getFailureCount
  3414. ()
  3415. const
  3416. {
  3417. return m_failures;
  3418. }
  3419. private: // IResultCapture
  3420. ///////////////////////////////////////////////////////////////////////////
  3421. virtual ResultAction::Value acceptResult
  3422. (
  3423. bool result
  3424. )
  3425. {
  3426. return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed );
  3427. }
  3428. ///////////////////////////////////////////////////////////////////////////
  3429. virtual ResultAction::Value acceptResult
  3430. (
  3431. ResultWas::OfType result
  3432. )
  3433. {
  3434. m_currentResult.setResultType( result );
  3435. return actOnCurrentResult();
  3436. }
  3437. ///////////////////////////////////////////////////////////////////////////
  3438. virtual ResultAction::Value acceptExpression
  3439. (
  3440. const MutableResultInfo& resultInfo
  3441. )
  3442. {
  3443. m_currentResult = resultInfo;
  3444. return actOnCurrentResult();
  3445. }
  3446. ///////////////////////////////////////////////////////////////////////////
  3447. virtual void acceptMessage
  3448. (
  3449. const std::string& msg
  3450. )
  3451. {
  3452. m_currentResult.setMessage( msg );
  3453. }
  3454. ///////////////////////////////////////////////////////////////////////////
  3455. virtual void testEnded
  3456. (
  3457. const ResultInfo& result
  3458. )
  3459. {
  3460. if( result.getResultType() == ResultWas::Ok )
  3461. {
  3462. m_successes++;
  3463. }
  3464. else if( !result.ok() )
  3465. {
  3466. m_failures++;
  3467. std::vector<ResultInfo>::const_iterator it = m_info.begin();
  3468. std::vector<ResultInfo>::const_iterator itEnd = m_info.end();
  3469. for(; it != itEnd; ++it )
  3470. m_reporter->Result( *it );
  3471. m_info.clear();
  3472. }
  3473. if( result.getResultType() == ResultWas::Info )
  3474. m_info.push_back( result );
  3475. else
  3476. m_reporter->Result( result );
  3477. }
  3478. ///////////////////////////////////////////////////////////////////////////
  3479. virtual bool sectionStarted
  3480. (
  3481. const std::string& name,
  3482. const std::string& description,
  3483. const std::string& filename,
  3484. std::size_t line,
  3485. std::size_t& successes,
  3486. std::size_t& failures
  3487. )
  3488. {
  3489. std::ostringstream oss;
  3490. oss << name << "@" << filename << ":" << line;
  3491. if( !m_runningTest->addSection( oss.str() ) )
  3492. return false;
  3493. m_currentResult.setFileAndLine( filename, line );
  3494. m_reporter->StartSection( name, description );
  3495. successes = m_successes;
  3496. failures = m_failures;
  3497. return true;
  3498. }
  3499. ///////////////////////////////////////////////////////////////////////////
  3500. virtual void sectionEnded
  3501. (
  3502. const std::string& name,
  3503. std::size_t prevSuccesses,
  3504. std::size_t prevFailures
  3505. )
  3506. {
  3507. m_runningTest->endSection( name );
  3508. m_reporter->EndSection( name, m_successes - prevSuccesses, m_failures - prevFailures );
  3509. }
  3510. ///////////////////////////////////////////////////////////////////////////
  3511. virtual void pushScopedInfo
  3512. (
  3513. ScopedInfo* scopedInfo
  3514. )
  3515. {
  3516. m_scopedInfos.push_back( scopedInfo );
  3517. }
  3518. ///////////////////////////////////////////////////////////////////////////
  3519. virtual void popScopedInfo
  3520. (
  3521. ScopedInfo* scopedInfo
  3522. )
  3523. {
  3524. if( m_scopedInfos.back() == scopedInfo )
  3525. m_scopedInfos.pop_back();
  3526. }
  3527. ///////////////////////////////////////////////////////////////////////////
  3528. virtual bool shouldDebugBreak
  3529. ()
  3530. const
  3531. {
  3532. return m_config.shouldDebugBreak();
  3533. }
  3534. ///////////////////////////////////////////////////////////////////////////
  3535. virtual std::string getCurrentTestName
  3536. ()
  3537. const
  3538. {
  3539. return m_runningTest
  3540. ? m_runningTest->getTestCaseInfo().getName()
  3541. : "";
  3542. }
  3543. private:
  3544. ///////////////////////////////////////////////////////////////////////////
  3545. ResultAction::Value actOnCurrentResult
  3546. ()
  3547. {
  3548. testEnded( m_currentResult );
  3549. bool ok = m_currentResult.ok();
  3550. m_currentResult = MutableResultInfo();
  3551. if( ok )
  3552. return ResultAction::None;
  3553. else if( shouldDebugBreak() )
  3554. return ResultAction::DebugFailed;
  3555. else
  3556. return ResultAction::Failed;
  3557. }
  3558. ///////////////////////////////////////////////////////////////////////////
  3559. void runCurrentTest
  3560. (
  3561. std::string& redirectedCout,
  3562. std::string& redirectedCerr
  3563. )
  3564. {
  3565. try
  3566. {
  3567. m_runningTest->reset();
  3568. StreamRedirect coutRedir( std::cout, redirectedCout );
  3569. StreamRedirect cerrRedir( std::cerr, redirectedCerr );
  3570. m_runningTest->getTestCaseInfo().invoke();
  3571. m_runningTest->ranToCompletion();
  3572. }
  3573. catch( TestFailureException& )
  3574. {
  3575. // This just means the test was aborted due to failure
  3576. }
  3577. catch( std::exception& ex )
  3578. {
  3579. acceptMessage( ex.what() );
  3580. acceptResult( ResultWas::ThrewException );
  3581. }
  3582. catch( std::string& msg )
  3583. {
  3584. acceptMessage( msg );
  3585. acceptResult( ResultWas::ThrewException );
  3586. }
  3587. catch( const char* msg )
  3588. {
  3589. acceptMessage( msg );
  3590. acceptResult( ResultWas::ThrewException );
  3591. }
  3592. catch(...)
  3593. {
  3594. acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
  3595. acceptResult( ResultWas::ThrewException );
  3596. }
  3597. m_info.clear();
  3598. }
  3599. private:
  3600. RunningTest* m_runningTest;
  3601. MutableResultInfo m_currentResult;
  3602. const Config& m_config;
  3603. std::size_t m_successes;
  3604. std::size_t m_failures;
  3605. IReporter* m_reporter;
  3606. std::vector<ScopedInfo*> m_scopedInfos;
  3607. std::vector<ResultInfo> m_info;
  3608. IRunner* m_prevRunner;
  3609. IResultCapture* m_prevResultCapture;
  3610. };
  3611. }
  3612. // #included from: catch_generators_impl.hpp
  3613. /*
  3614. * catch_generators_impl.hpp
  3615. * Catch
  3616. *
  3617. * Created by Phil on 28/01/2011.
  3618. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  3619. *
  3620. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  3621. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  3622. *
  3623. */
  3624. #define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED
  3625. #include <vector>
  3626. #include <string>
  3627. #include <map>
  3628. namespace Catch
  3629. {
  3630. struct GeneratorInfo
  3631. {
  3632. ///////////////////////////////////////////////////////////////////////
  3633. GeneratorInfo
  3634. (
  3635. std::size_t size
  3636. )
  3637. : m_size( size ),
  3638. m_currentIndex( 0 )
  3639. {
  3640. }
  3641. ///////////////////////////////////////////////////////////////////////
  3642. bool moveNext
  3643. ()
  3644. {
  3645. if( ++m_currentIndex == m_size )
  3646. {
  3647. m_currentIndex = 0;
  3648. return false;
  3649. }
  3650. return true;
  3651. }
  3652. ///////////////////////////////////////////////////////////////////////
  3653. std::size_t getCurrentIndex
  3654. ()
  3655. const
  3656. {
  3657. return m_currentIndex;
  3658. }
  3659. std::size_t m_size;
  3660. std::size_t m_currentIndex;
  3661. };
  3662. ///////////////////////////////////////////////////////////////////////////
  3663. ///////////////////////////////////////////////////////////////////////////
  3664. class GeneratorsForTest
  3665. {
  3666. public:
  3667. ///////////////////////////////////////////////////////////////////////
  3668. ~GeneratorsForTest
  3669. ()
  3670. {
  3671. deleteAll( m_generatorsInOrder );
  3672. }
  3673. ///////////////////////////////////////////////////////////////////////
  3674. GeneratorInfo& getGeneratorInfo
  3675. (
  3676. const std::string& fileInfo,
  3677. std::size_t size
  3678. )
  3679. {
  3680. std::map<std::string, GeneratorInfo*>::const_iterator it = m_generatorsByName.find( fileInfo );
  3681. if( it == m_generatorsByName.end() )
  3682. {
  3683. GeneratorInfo* info = new GeneratorInfo( size );
  3684. m_generatorsByName.insert( std::make_pair( fileInfo, info ) );
  3685. m_generatorsInOrder.push_back( info );
  3686. return *info;
  3687. }
  3688. return *it->second;
  3689. }
  3690. ///////////////////////////////////////////////////////////////////////
  3691. bool moveNext
  3692. ()
  3693. {
  3694. std::vector<GeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin();
  3695. std::vector<GeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end();
  3696. for(; it != itEnd; ++it )
  3697. {
  3698. if( (*it)->moveNext() )
  3699. return true;
  3700. }
  3701. return false;
  3702. }
  3703. private:
  3704. std::map<std::string, GeneratorInfo*> m_generatorsByName;
  3705. std::vector<GeneratorInfo*> m_generatorsInOrder;
  3706. };
  3707. } // end namespace Catch
  3708. #define INTERNAL_CATCH_LINESTR2( line ) #line
  3709. #define INTERNAL_CATCH_LINESTR( line ) INTERNAL_CATCH_LINESTR2( line )
  3710. #define INTERNAL_CATCH_GENERATE( expr ) expr.setFileInfo( __FILE__ "(" INTERNAL_CATCH_LINESTR( __LINE__ ) ")" )
  3711. // #included from: catch_stream.hpp
  3712. /*
  3713. * catch_stream.hpp
  3714. * Catch
  3715. *
  3716. * Created by Phil on 17/01/2011.
  3717. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  3718. *
  3719. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  3720. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  3721. *
  3722. */
  3723. #define TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED
  3724. #include <stdexcept>
  3725. #include <cstdio>
  3726. namespace Catch
  3727. {
  3728. template<typename WriterF, size_t bufferSize=256>
  3729. class StreamBufImpl : public StreamBufBase
  3730. {
  3731. char data[bufferSize];
  3732. WriterF m_writer;
  3733. public:
  3734. ///////////////////////////////////////////////////////////////////////
  3735. StreamBufImpl
  3736. ()
  3737. {
  3738. setp( data, data + sizeof(data) );
  3739. }
  3740. ///////////////////////////////////////////////////////////////////////
  3741. ~StreamBufImpl
  3742. ()
  3743. {
  3744. sync();
  3745. }
  3746. private:
  3747. ///////////////////////////////////////////////////////////////////////
  3748. int overflow
  3749. (
  3750. int c
  3751. )
  3752. {
  3753. sync();
  3754. if( c != EOF )
  3755. {
  3756. if( pbase() == epptr() )
  3757. m_writer( std::string( 1, static_cast<char>( c ) ) );
  3758. else
  3759. sputc( static_cast<char>( c ) );
  3760. }
  3761. return 0;
  3762. }
  3763. ///////////////////////////////////////////////////////////////////////
  3764. int sync
  3765. ()
  3766. {
  3767. if( pbase() != pptr() )
  3768. {
  3769. m_writer( std::string( pbase(), pptr() - pbase() ) );
  3770. setp( pbase(), epptr() );
  3771. }
  3772. return 0;
  3773. }
  3774. };
  3775. ///////////////////////////////////////////////////////////////////////////
  3776. ///////////////////////////////////////////////////////////////////////////
  3777. struct OutputDebugWriter
  3778. {
  3779. ///////////////////////////////////////////////////////////////////////
  3780. void operator()
  3781. (
  3782. const std::string &str
  3783. )
  3784. {
  3785. writeToDebugConsole( str );
  3786. }
  3787. };
  3788. }
  3789. namespace Catch
  3790. {
  3791. ///////////////////////////////////////////////////////////////////////////
  3792. Hub::Hub
  3793. ()
  3794. : m_reporterRegistry( new ReporterRegistry ),
  3795. m_testCaseRegistry( new TestRegistry ),
  3796. m_exceptionTranslatorRegistry( new ExceptionTranslatorRegistry )
  3797. {
  3798. }
  3799. ///////////////////////////////////////////////////////////////////////////
  3800. Hub& Hub::me
  3801. ()
  3802. {
  3803. static Hub hub;
  3804. return hub;
  3805. }
  3806. ///////////////////////////////////////////////////////////////////////////
  3807. void Hub::setRunner( IRunner* runner )
  3808. {
  3809. me().m_runner = runner;
  3810. }
  3811. ///////////////////////////////////////////////////////////////////////////
  3812. void Hub::setResultCapture( IResultCapture* resultCapture )
  3813. {
  3814. me().m_resultCapture = resultCapture;
  3815. }
  3816. ///////////////////////////////////////////////////////////////////////////
  3817. IResultCapture& Hub::getResultCapture
  3818. ()
  3819. {
  3820. return *me().m_resultCapture;
  3821. }
  3822. ///////////////////////////////////////////////////////////////////////////
  3823. IRunner& Hub::getRunner
  3824. ()
  3825. {
  3826. return *me().m_runner;
  3827. }
  3828. ///////////////////////////////////////////////////////////////////////////
  3829. IReporterRegistry& Hub::getReporterRegistry
  3830. ()
  3831. {
  3832. return *me().m_reporterRegistry.get();
  3833. }
  3834. ///////////////////////////////////////////////////////////////////////////
  3835. ITestCaseRegistry& Hub::getTestCaseRegistry
  3836. ()
  3837. {
  3838. return *me().m_testCaseRegistry.get();
  3839. }
  3840. ///////////////////////////////////////////////////////////////////////////
  3841. IExceptionTranslatorRegistry& Hub::getExceptionTranslatorRegistry
  3842. ()
  3843. {
  3844. return *me().m_exceptionTranslatorRegistry.get();
  3845. }
  3846. ///////////////////////////////////////////////////////////////////////////
  3847. std::streambuf* Hub::createStreamBuf
  3848. (
  3849. const std::string& streamName
  3850. )
  3851. {
  3852. if( streamName == "stdout" ) return std::cout.rdbuf();
  3853. if( streamName == "stderr" ) return std::cerr.rdbuf();
  3854. if( streamName == "debug" ) return new StreamBufImpl<OutputDebugWriter>;
  3855. throw std::domain_error( "Unknown stream: " + streamName );
  3856. }
  3857. ///////////////////////////////////////////////////////////////////////////
  3858. GeneratorsForTest* Hub::findGeneratorsForCurrentTest
  3859. ()
  3860. {
  3861. std::string testName = getResultCapture().getCurrentTestName();
  3862. std::map<std::string, GeneratorsForTest*>::const_iterator it =
  3863. m_generatorsByTestName.find( testName );
  3864. return it != m_generatorsByTestName.end()
  3865. ? it->second
  3866. : NULL;
  3867. }
  3868. ///////////////////////////////////////////////////////////////////////////
  3869. GeneratorsForTest& Hub::getGeneratorsForCurrentTest
  3870. ()
  3871. {
  3872. GeneratorsForTest* generators = findGeneratorsForCurrentTest();
  3873. if( !generators )
  3874. {
  3875. std::string testName = getResultCapture().getCurrentTestName();
  3876. generators = new GeneratorsForTest();
  3877. m_generatorsByTestName.insert( std::make_pair( testName, generators ) );
  3878. }
  3879. return *generators;
  3880. }
  3881. ///////////////////////////////////////////////////////////////////////////
  3882. size_t Hub::getGeneratorIndex
  3883. (
  3884. const std::string& fileInfo,
  3885. size_t totalSize
  3886. )
  3887. {
  3888. return me().getGeneratorsForCurrentTest()
  3889. .getGeneratorInfo( fileInfo, totalSize )
  3890. .getCurrentIndex();
  3891. }
  3892. ///////////////////////////////////////////////////////////////////////////
  3893. bool Hub::advanceGeneratorsForCurrentTest
  3894. ()
  3895. {
  3896. GeneratorsForTest* generators = me().findGeneratorsForCurrentTest();
  3897. return generators && generators->moveNext();
  3898. }
  3899. }
  3900. // #included from: internal/catch_commandline.hpp
  3901. /*
  3902. * catch_commandline.hpp
  3903. * Catch
  3904. *
  3905. * Created by Phil on 02/11/2010.
  3906. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  3907. *
  3908. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  3909. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  3910. *
  3911. */
  3912. #define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
  3913. namespace Catch
  3914. {
  3915. // !TBD: This could be refactored to be more "declarative"
  3916. // have a table up front that relates the mode, option strings, # arguments, names of arguments
  3917. // - may not be worth it at this scale
  3918. // -l, --list tests [xml] lists available tests (optionally in xml)
  3919. // -l, --list reporters [xml] lists available reports (optionally in xml)
  3920. // -l, --list all [xml] lists available tests and reports (optionally in xml)
  3921. // -t, --test "testspec" ["testspec", ...]
  3922. // -r, --reporter <type>
  3923. // -o, --out filename to write to
  3924. // -s, --success report successful cases too
  3925. // -b, --break breaks into debugger on test failure
  3926. // -n, --name specifies an optional name for the test run
  3927. class ArgParser : NonCopyable
  3928. {
  3929. enum Mode
  3930. {
  3931. modeNone,
  3932. modeList,
  3933. modeTest,
  3934. modeReport,
  3935. modeOutput,
  3936. modeSuccess,
  3937. modeBreak,
  3938. modeName,
  3939. modeHelp,
  3940. modeError
  3941. };
  3942. public:
  3943. ///////////////////////////////////////////////////////////////////////
  3944. ArgParser
  3945. (
  3946. int argc,
  3947. char * const argv[],
  3948. Config& config
  3949. )
  3950. : m_mode( modeNone ),
  3951. m_config( config )
  3952. {
  3953. for( int i=1; i < argc; ++i )
  3954. {
  3955. if( argv[i][0] == '-' )
  3956. {
  3957. std::string cmd = ( argv[i] );
  3958. if( cmd == "-l" || cmd == "--list" )
  3959. changeMode( cmd, modeList );
  3960. else if( cmd == "-t" || cmd == "--test" )
  3961. changeMode( cmd, modeTest );
  3962. else if( cmd == "-r" || cmd == "--reporter" )
  3963. changeMode( cmd, modeReport );
  3964. else if( cmd == "-o" || cmd == "--out" )
  3965. changeMode( cmd, modeOutput );
  3966. else if( cmd == "-s" || cmd == "--success" )
  3967. changeMode( cmd, modeSuccess );
  3968. else if( cmd == "-b" || cmd == "--break" )
  3969. changeMode( cmd, modeBreak );
  3970. else if( cmd == "-n" || cmd == "--name" )
  3971. changeMode( cmd, modeName );
  3972. else if( cmd == "-h" || cmd == "-?" || cmd == "--help" )
  3973. changeMode( cmd, modeHelp );
  3974. }
  3975. else
  3976. {
  3977. m_args.push_back( argv[i] );
  3978. }
  3979. if( m_mode == modeError )
  3980. return;
  3981. }
  3982. changeMode( "", modeNone );
  3983. }
  3984. private:
  3985. ///////////////////////////////////////////////////////////////////////
  3986. std::string argsAsString
  3987. ()
  3988. {
  3989. std::ostringstream oss;
  3990. std::vector<std::string>::const_iterator it = m_args.begin();
  3991. std::vector<std::string>::const_iterator itEnd = m_args.end();
  3992. for( bool first = true; it != itEnd; ++it, first = false )
  3993. {
  3994. if( !first )
  3995. oss << " ";
  3996. oss << *it;
  3997. }
  3998. return oss.str();
  3999. }
  4000. ///////////////////////////////////////////////////////////////////////
  4001. void changeMode
  4002. (
  4003. const std::string& cmd,
  4004. Mode mode
  4005. )
  4006. {
  4007. m_command = cmd;
  4008. switch( m_mode )
  4009. {
  4010. case modeNone:
  4011. if( m_args.size() > 0 )
  4012. return setErrorMode( "Unexpected arguments before " + m_command + ": " + argsAsString() );
  4013. break;
  4014. case modeList:
  4015. if( m_args.size() > 2 )
  4016. {
  4017. return setErrorMode( m_command + " expected upto 2 arguments but recieved: " + argsAsString() );
  4018. }
  4019. else
  4020. {
  4021. Config::List::What listSpec = Config::List::All;
  4022. if( m_args.size() >= 1 )
  4023. {
  4024. if( m_args[0] == "tests" )
  4025. listSpec = Config::List::Tests;
  4026. else if( m_args[0] == "reporters" )
  4027. listSpec = Config::List::Reports;
  4028. else
  4029. return setErrorMode( m_command + " expected [tests] or [reporters] but recieved: [" + m_args[0] + "]" );
  4030. }
  4031. if( m_args.size() >= 2 )
  4032. {
  4033. if( m_args[1] == "xml" )
  4034. listSpec = static_cast<Config::List::What>( listSpec | Config::List::AsXml );
  4035. else if( m_args[1] == "text" )
  4036. listSpec = static_cast<Config::List::What>( listSpec | Config::List::AsText );
  4037. else
  4038. return setErrorMode( m_command + " expected [xml] or [text] but recieved: [" + m_args[1] + "]" );
  4039. }
  4040. m_config.setListSpec( static_cast<Config::List::What>( m_config.getListSpec() | listSpec ) );
  4041. }
  4042. break;
  4043. case modeTest:
  4044. if( m_args.size() == 0 )
  4045. return setErrorMode( m_command + " expected at least 1 argument but recieved none" );
  4046. {
  4047. std::vector<std::string>::const_iterator it = m_args.begin();
  4048. std::vector<std::string>::const_iterator itEnd = m_args.end();
  4049. for(; it != itEnd; ++it )
  4050. m_config.addTestSpec( *it );
  4051. }
  4052. break;
  4053. case modeReport:
  4054. if( m_args.size() != 1 )
  4055. return setErrorMode( m_command + " expected one argument, recieved: " + argsAsString() );
  4056. m_config.setReporter( m_args[0] );
  4057. break;
  4058. case modeOutput:
  4059. if( m_args.size() == 0 )
  4060. return setErrorMode( m_command + " expected filename" );
  4061. if( m_args[0][0] == '%' )
  4062. m_config.useStream( m_args[0].substr( 1 ) );
  4063. else
  4064. m_config.setFilename( m_args[0] );
  4065. break;
  4066. case modeSuccess:
  4067. if( m_args.size() != 0 )
  4068. return setErrorMode( m_command + " does not accept arguments" );
  4069. m_config.setIncludeWhat( Config::Include::SuccessfulResults );
  4070. break;
  4071. case modeBreak:
  4072. if( m_args.size() != 0 )
  4073. return setErrorMode( m_command + " does not accept arguments" );
  4074. m_config.setShouldDebugBreak( true );
  4075. break;
  4076. case modeName:
  4077. if( m_args.size() != 1 )
  4078. return setErrorMode( m_command + " requires exactly one argument (a name)" );
  4079. m_config.setName( m_args[0] );
  4080. break;
  4081. case modeHelp:
  4082. if( m_args.size() != 0 )
  4083. return setErrorMode( m_command + " does not accept arguments" );
  4084. m_config.setShowHelp( true );
  4085. break;
  4086. case modeError:
  4087. default:
  4088. break;
  4089. }
  4090. m_args.clear();
  4091. m_mode = mode;
  4092. }
  4093. ///////////////////////////////////////////////////////////////////////
  4094. void setErrorMode
  4095. (
  4096. const std::string& errorMessage
  4097. )
  4098. {
  4099. m_mode = modeError;
  4100. m_command = "";
  4101. m_config.setError( errorMessage );
  4102. }
  4103. private:
  4104. Mode m_mode;
  4105. std::string m_command;
  4106. std::vector<std::string> m_args;
  4107. Config& m_config;
  4108. };
  4109. } // end namespace Catch
  4110. // #included from: internal/catch_list.hpp
  4111. /*
  4112. * catch_list.hpp
  4113. * Catch
  4114. *
  4115. * Created by Phil on 5/11/2010.
  4116. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4117. *
  4118. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  4119. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4120. *
  4121. */
  4122. #define TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED
  4123. #include <limits>
  4124. namespace Catch
  4125. {
  4126. ///////////////////////////////////////////////////////////////////////////
  4127. inline int List
  4128. (
  4129. const Config& config
  4130. )
  4131. {
  4132. if( config.listWhat() & Config::List::Reports )
  4133. {
  4134. std::cout << "Available reports:\n";
  4135. IReporterRegistry::FactoryMap::const_iterator it = Hub::getReporterRegistry().getFactories().begin();
  4136. IReporterRegistry::FactoryMap::const_iterator itEnd = Hub::getReporterRegistry().getFactories().end();
  4137. for(; it != itEnd; ++it )
  4138. {
  4139. // !TBD: consider listAs()
  4140. std::cout << "\t" << it->first << "\n\t\t'" << it->second->getDescription() << "'\n";
  4141. }
  4142. std::cout << std::endl;
  4143. }
  4144. if( config.listWhat() & Config::List::Tests )
  4145. {
  4146. std::cout << "Available tests:\n";
  4147. std::vector<TestCaseInfo>::const_iterator it = Hub::getTestCaseRegistry().getAllTests().begin();
  4148. std::vector<TestCaseInfo>::const_iterator itEnd = Hub::getTestCaseRegistry().getAllTests().end();
  4149. for(; it != itEnd; ++it )
  4150. {
  4151. // !TBD: consider listAs()
  4152. std::cout << "\t" << it->getName() << "\n\t\t '" << it->getDescription() << "'\n";
  4153. }
  4154. std::cout << std::endl;
  4155. }
  4156. if( ( config.listWhat() & Config::List::All ) == 0 )
  4157. {
  4158. std::cerr << "Unknown list type" << std::endl;
  4159. return (std::numeric_limits<int>::max)();
  4160. }
  4161. if( config.getReporter() )
  4162. {
  4163. std::cerr << "Reporters ignored when listing" << std::endl;
  4164. }
  4165. if( !config.testsSpecified() )
  4166. {
  4167. std::cerr << "Test specs ignored when listing" << std::endl;
  4168. }
  4169. return 0;
  4170. }
  4171. } // end namespace Catch
  4172. // #included from: reporters/catch_reporter_basic.hpp
  4173. /*
  4174. * catch_reporter_basic.hpp
  4175. * Catch
  4176. *
  4177. * Created by Phil on 28/10/2010.
  4178. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4179. *
  4180. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  4181. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4182. *
  4183. */
  4184. #define TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED
  4185. // #included from: ../internal/catch_reporter_registrars.hpp
  4186. /*
  4187. * catch_reporter_registrars.hpp
  4188. * Test
  4189. *
  4190. * Created by Phil on 31/12/2010.
  4191. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4192. *
  4193. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  4194. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4195. *
  4196. */
  4197. #define TWOBLUECUBES_CATCH_REPORTER_REGISTRARS_HPP_INCLUDED
  4198. namespace Catch
  4199. {
  4200. template<typename T>
  4201. class ReporterRegistrar
  4202. {
  4203. class ReporterFactory : public IReporterFactory
  4204. {
  4205. ///////////////////////////////////////////////////////////////////
  4206. virtual IReporter* create
  4207. (
  4208. const IReporterConfig& config
  4209. )
  4210. const
  4211. {
  4212. return new T( config );
  4213. }
  4214. ///////////////////////////////////////////////////////////////////
  4215. virtual std::string getDescription
  4216. ()
  4217. const
  4218. {
  4219. return T::getDescription();
  4220. }
  4221. };
  4222. public:
  4223. ///////////////////////////////////////////////////////////////////////
  4224. ReporterRegistrar
  4225. (
  4226. const std::string& name
  4227. )
  4228. {
  4229. Hub::getReporterRegistry().registerReporter( name, new ReporterFactory() );
  4230. }
  4231. };
  4232. }
  4233. ///////////////////////////////////////////////////////////////////////////////
  4234. #define INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) \
  4235. Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name );
  4236. namespace Catch
  4237. {
  4238. class BasicReporter : public IReporter
  4239. {
  4240. struct SpanInfo
  4241. {
  4242. SpanInfo()
  4243. : emitted( false )
  4244. {}
  4245. SpanInfo( const std::string& spanName )
  4246. : name( spanName ),
  4247. emitted( false )
  4248. {}
  4249. SpanInfo( const SpanInfo& other )
  4250. : name( other.name ),
  4251. emitted( other.emitted )
  4252. {}
  4253. std::string name;
  4254. bool emitted;
  4255. };
  4256. public:
  4257. ///////////////////////////////////////////////////////////////////////////
  4258. BasicReporter
  4259. (
  4260. const IReporterConfig& config
  4261. )
  4262. : m_config( config ),
  4263. m_firstSectionInTestCase( true )
  4264. {
  4265. }
  4266. ///////////////////////////////////////////////////////////////////////////
  4267. static std::string getDescription
  4268. ()
  4269. {
  4270. return "Reports test results as lines of text";
  4271. }
  4272. private:
  4273. ///////////////////////////////////////////////////////////////////////////
  4274. void ReportCounts
  4275. (
  4276. std::size_t succeeded,
  4277. std::size_t failed
  4278. )
  4279. {
  4280. if( failed + succeeded == 0 )
  4281. m_config.stream() << "No tests ran";
  4282. else if( failed == 0 )
  4283. m_config.stream() << "All " << succeeded << " test(s) succeeded";
  4284. else if( succeeded == 0 )
  4285. m_config.stream() << "All " << failed << " test(s) failed";
  4286. else
  4287. m_config.stream() << succeeded << " test(s) passed but " << failed << " test(s) failed";
  4288. }
  4289. private: // IReporter
  4290. ///////////////////////////////////////////////////////////////////////////
  4291. virtual void StartTesting
  4292. ()
  4293. {
  4294. m_testingSpan = SpanInfo();
  4295. }
  4296. ///////////////////////////////////////////////////////////////////////////
  4297. virtual void EndTesting
  4298. (
  4299. std::size_t succeeded,
  4300. std::size_t failed
  4301. )
  4302. {
  4303. // Output the overall test results even if "Started Testing" was not emitted
  4304. m_config.stream() << "[Testing completed. ";
  4305. ReportCounts( succeeded, failed );
  4306. m_config.stream() << "]\n" << std::endl;
  4307. }
  4308. ///////////////////////////////////////////////////////////////////////////
  4309. virtual void StartGroup
  4310. (
  4311. const std::string& groupName
  4312. )
  4313. {
  4314. m_groupSpan = groupName;
  4315. }
  4316. ///////////////////////////////////////////////////////////////////////////
  4317. virtual void EndGroup
  4318. (
  4319. const std::string& groupName,
  4320. std::size_t succeeded,
  4321. std::size_t failed
  4322. )
  4323. {
  4324. if( m_groupSpan.emitted && !groupName.empty() )
  4325. {
  4326. m_config.stream() << "[End of group: '" << groupName << "'. ";
  4327. ReportCounts( succeeded, failed );
  4328. m_config.stream() << "]\n" << std::endl;
  4329. m_groupSpan = SpanInfo();
  4330. }
  4331. }
  4332. ///////////////////////////////////////////////////////////////////////////
  4333. virtual void StartTestCase
  4334. (
  4335. const TestCaseInfo& testInfo
  4336. )
  4337. {
  4338. m_testSpan = testInfo.getName();
  4339. }
  4340. ///////////////////////////////////////////////////////////////////////////
  4341. virtual void StartSection
  4342. (
  4343. const std::string& sectionName,
  4344. const std::string /*description*/
  4345. )
  4346. {
  4347. m_sectionSpans.push_back( SpanInfo( sectionName ) );
  4348. }
  4349. ///////////////////////////////////////////////////////////////////////////
  4350. virtual void EndSection
  4351. (
  4352. const std::string& sectionName,
  4353. std::size_t succeeded,
  4354. std::size_t failed
  4355. )
  4356. {
  4357. SpanInfo& sectionSpan = m_sectionSpans.back();
  4358. if( sectionSpan.emitted && !sectionSpan.name.empty() )
  4359. {
  4360. m_config.stream() << "[End of section: '" << sectionName << "'. ";
  4361. ReportCounts( succeeded, failed );
  4362. m_config.stream() << "]\n" << std::endl;
  4363. }
  4364. m_sectionSpans.pop_back();
  4365. }
  4366. ///////////////////////////////////////////////////////////////////////////
  4367. virtual void Result
  4368. (
  4369. const ResultInfo& resultInfo
  4370. )
  4371. {
  4372. if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok )
  4373. return;
  4374. StartSpansLazily();
  4375. if( !resultInfo.getFilename().empty() )
  4376. #ifndef __GNUG__
  4377. m_config.stream() << resultInfo.getFilename() << "(" << resultInfo.getLine() << "): ";
  4378. #else
  4379. m_config.stream() << resultInfo.getFilename() << ":" << resultInfo.getLine() << ": ";
  4380. #endif
  4381. if( resultInfo.hasExpression() )
  4382. {
  4383. m_config.stream() << resultInfo.getExpression();
  4384. if( resultInfo.ok() )
  4385. m_config.stream() << " succeeded";
  4386. else
  4387. m_config.stream() << " failed";
  4388. }
  4389. switch( resultInfo.getResultType() )
  4390. {
  4391. case ResultWas::ThrewException:
  4392. if( resultInfo.hasExpression() )
  4393. m_config.stream() << " with unexpected";
  4394. else
  4395. m_config.stream() << "Unexpected";
  4396. m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'";
  4397. break;
  4398. case ResultWas::DidntThrowException:
  4399. if( resultInfo.hasExpression() )
  4400. m_config.stream() << " because no exception was thrown where one was expected";
  4401. else
  4402. m_config.stream() << "No exception thrown where one was expected";
  4403. break;
  4404. case ResultWas::Info:
  4405. streamVariableLengthText( "info", resultInfo.getMessage() );
  4406. break;
  4407. case ResultWas::Warning:
  4408. m_config.stream() << "warning:\n'" << resultInfo.getMessage() << "'";
  4409. break;
  4410. case ResultWas::ExplicitFailure:
  4411. m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'";
  4412. break;
  4413. case ResultWas::Unknown: // These cases are here to prevent compiler warnings
  4414. case ResultWas::Ok:
  4415. case ResultWas::FailureBit:
  4416. case ResultWas::ExpressionFailed:
  4417. case ResultWas::Exception:
  4418. default:
  4419. if( !resultInfo.hasExpression() )
  4420. {
  4421. if( resultInfo.ok() )
  4422. m_config.stream() << " succeeded";
  4423. else
  4424. m_config.stream() << " failed";
  4425. }
  4426. break;
  4427. }
  4428. if( resultInfo.hasExpression() )
  4429. {
  4430. m_config.stream() << " for: " << resultInfo.getExpandedExpression();
  4431. }
  4432. m_config.stream() << std::endl;
  4433. }
  4434. ///////////////////////////////////////////////////////////////////////////
  4435. virtual void EndTestCase
  4436. (
  4437. const TestCaseInfo& testInfo,
  4438. std::size_t succeeded,
  4439. std::size_t failed,
  4440. const std::string& stdOut,
  4441. const std::string& stdErr
  4442. )
  4443. {
  4444. if( !stdOut.empty() )
  4445. {
  4446. StartSpansLazily();
  4447. streamVariableLengthText( "stdout", stdOut );
  4448. }
  4449. if( !stdErr.empty() )
  4450. {
  4451. StartSpansLazily();
  4452. streamVariableLengthText( "stderr", stdErr );
  4453. }
  4454. if( m_testSpan.emitted )
  4455. {
  4456. m_config.stream() << "[Finished: " << testInfo.getName() << " ";
  4457. ReportCounts( succeeded, failed );
  4458. m_config.stream() << "]" << std::endl;
  4459. }
  4460. }
  4461. private: // helpers
  4462. ///////////////////////////////////////////////////////////////////////////
  4463. void StartSpansLazily()
  4464. {
  4465. if( !m_testingSpan.emitted )
  4466. {
  4467. if( m_config.getName().empty() )
  4468. m_config.stream() << "[Started testing]" << std::endl;
  4469. else
  4470. m_config.stream() << "[Started testing: " << m_config.getName() << "]" << std::endl;
  4471. m_testingSpan.emitted = true;
  4472. }
  4473. if( !m_groupSpan.emitted && !m_groupSpan.name.empty() )
  4474. {
  4475. m_config.stream() << "[Started group: '" << m_groupSpan.name << "']" << std::endl;
  4476. m_groupSpan.emitted = true;
  4477. }
  4478. if( !m_testSpan.emitted )
  4479. {
  4480. m_config.stream() << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl;
  4481. m_testSpan.emitted = true;
  4482. }
  4483. if( !m_sectionSpans.empty() )
  4484. {
  4485. SpanInfo& sectionSpan = m_sectionSpans.back();
  4486. if( !sectionSpan.emitted && !sectionSpan.name.empty() )
  4487. {
  4488. if( m_firstSectionInTestCase )
  4489. {
  4490. m_config.stream() << "\n";
  4491. m_firstSectionInTestCase = false;
  4492. }
  4493. std::vector<SpanInfo>::iterator it = m_sectionSpans.begin();
  4494. std::vector<SpanInfo>::iterator itEnd = m_sectionSpans.end();
  4495. for(; it != itEnd; ++it )
  4496. {
  4497. SpanInfo& prevSpan = *it;
  4498. if( !prevSpan.emitted && !prevSpan.name.empty() )
  4499. {
  4500. m_config.stream() << "[Started section: '" << prevSpan.name << "']" << std::endl;
  4501. prevSpan.emitted = true;
  4502. }
  4503. }
  4504. }
  4505. }
  4506. }
  4507. ///////////////////////////////////////////////////////////////////////////
  4508. void streamVariableLengthText
  4509. (
  4510. const std::string& prefix,
  4511. const std::string& text
  4512. )
  4513. {
  4514. std::string trimmed = trim( text );
  4515. if( trimmed.find_first_of( "\r\n" ) == std::string::npos )
  4516. {
  4517. m_config.stream() << "[" << prefix << ": " << trimmed << "]\n";
  4518. }
  4519. else
  4520. {
  4521. m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
  4522. << "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
  4523. }
  4524. }
  4525. private:
  4526. const IReporterConfig& m_config;
  4527. bool m_firstSectionInTestCase;
  4528. SpanInfo m_testingSpan;
  4529. SpanInfo m_groupSpan;
  4530. SpanInfo m_testSpan;
  4531. std::vector<SpanInfo> m_sectionSpans;
  4532. };
  4533. INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
  4534. } // end namespace Catch
  4535. // #included from: reporters/catch_reporter_xml.hpp
  4536. /*
  4537. * catch_reporter_xml.hpp
  4538. * Catch
  4539. *
  4540. * Created by Phil on 28/10/2010.
  4541. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4542. *
  4543. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  4544. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4545. *
  4546. */
  4547. #define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
  4548. // #included from: ../internal/catch_xmlwriter.hpp
  4549. /*
  4550. * catch_xmlwriter.hpp
  4551. * Catch
  4552. *
  4553. * Created by Phil on 09/12/2010.
  4554. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4555. *
  4556. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  4557. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4558. */
  4559. #define TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED
  4560. #include <sstream>
  4561. #include <string>
  4562. #include <vector>
  4563. namespace Catch
  4564. {
  4565. class XmlWriter
  4566. {
  4567. public:
  4568. class ScopedElement
  4569. {
  4570. public:
  4571. ///////////////////////////////////////////////////////////////////
  4572. ScopedElement
  4573. (
  4574. XmlWriter* writer
  4575. )
  4576. : m_writer( writer )
  4577. {
  4578. }
  4579. ///////////////////////////////////////////////////////////////////
  4580. ScopedElement
  4581. (
  4582. const ScopedElement& other
  4583. )
  4584. : m_writer( other.m_writer )
  4585. {
  4586. other.m_writer = NULL;
  4587. }
  4588. ///////////////////////////////////////////////////////////////////
  4589. ~ScopedElement
  4590. ()
  4591. {
  4592. if( m_writer )
  4593. m_writer->endElement();
  4594. }
  4595. ///////////////////////////////////////////////////////////////////
  4596. ScopedElement& writeText
  4597. (
  4598. const std::string& text
  4599. )
  4600. {
  4601. m_writer->writeText( text );
  4602. return *this;
  4603. }
  4604. ///////////////////////////////////////////////////////////////////
  4605. template<typename T>
  4606. ScopedElement& writeAttribute
  4607. (
  4608. const std::string& name,
  4609. const T& attribute
  4610. )
  4611. {
  4612. m_writer->writeAttribute( name, attribute );
  4613. return *this;
  4614. }
  4615. private:
  4616. mutable XmlWriter* m_writer;
  4617. };
  4618. ///////////////////////////////////////////////////////////////////////
  4619. XmlWriter
  4620. ()
  4621. : m_tagIsOpen( false ),
  4622. m_needsNewline( false ),
  4623. m_os( &std::cout )
  4624. {
  4625. }
  4626. ///////////////////////////////////////////////////////////////////////
  4627. XmlWriter
  4628. (
  4629. std::ostream& os
  4630. )
  4631. : m_tagIsOpen( false ),
  4632. m_needsNewline( false ),
  4633. m_os( &os )
  4634. {
  4635. }
  4636. ///////////////////////////////////////////////////////////////////////
  4637. ~XmlWriter
  4638. ()
  4639. {
  4640. while( !m_tags.empty() )
  4641. {
  4642. endElement();
  4643. }
  4644. }
  4645. ///////////////////////////////////////////////////////////////////////
  4646. XmlWriter& operator =
  4647. (
  4648. const XmlWriter& other
  4649. )
  4650. {
  4651. XmlWriter temp( other );
  4652. swap( temp );
  4653. return *this;
  4654. }
  4655. ///////////////////////////////////////////////////////////////////////
  4656. void swap
  4657. (
  4658. XmlWriter& other
  4659. )
  4660. {
  4661. std::swap( m_tagIsOpen, other.m_tagIsOpen );
  4662. std::swap( m_needsNewline, other.m_needsNewline );
  4663. std::swap( m_tags, other.m_tags );
  4664. std::swap( m_indent, other.m_indent );
  4665. std::swap( m_os, other.m_os );
  4666. }
  4667. ///////////////////////////////////////////////////////////////////////
  4668. XmlWriter& startElement
  4669. (
  4670. const std::string& name
  4671. )
  4672. {
  4673. ensureTagClosed();
  4674. newlineIfNecessary();
  4675. stream() << m_indent << "<" << name;
  4676. m_tags.push_back( name );
  4677. m_indent += " ";
  4678. m_tagIsOpen = true;
  4679. return *this;
  4680. }
  4681. ///////////////////////////////////////////////////////////////////////
  4682. ScopedElement scopedElement
  4683. (
  4684. const std::string& name
  4685. )
  4686. {
  4687. ScopedElement scoped( this );
  4688. startElement( name );
  4689. return scoped;
  4690. }
  4691. ///////////////////////////////////////////////////////////////////////
  4692. XmlWriter& endElement
  4693. ()
  4694. {
  4695. newlineIfNecessary();
  4696. m_indent = m_indent.substr( 0, m_indent.size()-2 );
  4697. if( m_tagIsOpen )
  4698. {
  4699. stream() << "/>\n";
  4700. m_tagIsOpen = false;
  4701. }
  4702. else
  4703. {
  4704. stream() << m_indent << "</" << m_tags.back() << ">\n";
  4705. }
  4706. m_tags.pop_back();
  4707. return *this;
  4708. }
  4709. ///////////////////////////////////////////////////////////////////////
  4710. XmlWriter& writeAttribute
  4711. (
  4712. const std::string& name,
  4713. const std::string& attribute
  4714. )
  4715. {
  4716. if( !name.empty() && !attribute.empty() )
  4717. {
  4718. stream() << " " << name << "=\"";
  4719. writeEncodedText( attribute );
  4720. stream() << "\"";
  4721. }
  4722. return *this;
  4723. }
  4724. ///////////////////////////////////////////////////////////////////////
  4725. XmlWriter& writeAttribute
  4726. (
  4727. const std::string& name,
  4728. bool attribute
  4729. )
  4730. {
  4731. stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\"";
  4732. return *this;
  4733. }
  4734. ///////////////////////////////////////////////////////////////////////
  4735. template<typename T>
  4736. XmlWriter& writeAttribute
  4737. (
  4738. const std::string& name,
  4739. const T& attribute
  4740. )
  4741. {
  4742. if( !name.empty() )
  4743. {
  4744. stream() << " " << name << "=\"" << attribute << "\"";
  4745. }
  4746. return *this;
  4747. }
  4748. ///////////////////////////////////////////////////////////////////////
  4749. XmlWriter& writeText
  4750. (
  4751. const std::string& text
  4752. )
  4753. {
  4754. if( !text.empty() )
  4755. {
  4756. bool tagWasOpen = m_tagIsOpen;
  4757. ensureTagClosed();
  4758. if( tagWasOpen )
  4759. stream() << m_indent;
  4760. writeEncodedText( text );
  4761. m_needsNewline = true;
  4762. }
  4763. return *this;
  4764. }
  4765. ///////////////////////////////////////////////////////////////////////
  4766. XmlWriter& writeComment
  4767. (
  4768. const std::string& text
  4769. )
  4770. {
  4771. ensureTagClosed();
  4772. stream() << m_indent << "<!--" << text << "-->";
  4773. m_needsNewline = true;
  4774. return *this;
  4775. }
  4776. ///////////////////////////////////////////////////////////////////////
  4777. XmlWriter& writeBlankLine
  4778. ()
  4779. {
  4780. ensureTagClosed();
  4781. stream() << "\n";
  4782. return *this;
  4783. }
  4784. private:
  4785. ///////////////////////////////////////////////////////////////////////
  4786. std::ostream& stream
  4787. ()
  4788. {
  4789. return *m_os;
  4790. }
  4791. ///////////////////////////////////////////////////////////////////////
  4792. void ensureTagClosed
  4793. ()
  4794. {
  4795. if( m_tagIsOpen )
  4796. {
  4797. stream() << ">\n";
  4798. m_tagIsOpen = false;
  4799. }
  4800. }
  4801. ///////////////////////////////////////////////////////////////////////
  4802. void newlineIfNecessary
  4803. ()
  4804. {
  4805. if( m_needsNewline )
  4806. {
  4807. stream() << "\n";
  4808. m_needsNewline = false;
  4809. }
  4810. }
  4811. ///////////////////////////////////////////////////////////////////////
  4812. void writeEncodedText
  4813. (
  4814. const std::string& text
  4815. )
  4816. {
  4817. // !TBD finish this
  4818. if( !findReplaceableString( text, "<", "&lt;" ) &&
  4819. !findReplaceableString( text, "&", "&amp;" ) &&
  4820. !findReplaceableString( text, "\"", "&quot;" ) )
  4821. {
  4822. stream() << text;
  4823. }
  4824. }
  4825. ///////////////////////////////////////////////////////////////////////
  4826. bool findReplaceableString
  4827. (
  4828. const std::string& text,
  4829. const std::string& replaceWhat,
  4830. const std::string& replaceWith
  4831. )
  4832. {
  4833. std::string::size_type pos = text.find_first_of( replaceWhat );
  4834. if( pos != std::string::npos )
  4835. {
  4836. stream() << text.substr( 0, pos ) << replaceWith;
  4837. writeEncodedText( text.substr( pos+1 ) );
  4838. return true;
  4839. }
  4840. return false;
  4841. }
  4842. bool m_tagIsOpen;
  4843. bool m_needsNewline;
  4844. std::vector<std::string> m_tags;
  4845. std::string m_indent;
  4846. std::ostream* m_os;
  4847. };
  4848. }
  4849. namespace Catch
  4850. {
  4851. class XmlReporter : public Catch::IReporter
  4852. {
  4853. public:
  4854. ///////////////////////////////////////////////////////////////////////////
  4855. XmlReporter
  4856. (
  4857. const IReporterConfig& config
  4858. )
  4859. : m_config( config )
  4860. {
  4861. }
  4862. ///////////////////////////////////////////////////////////////////////////
  4863. static std::string getDescription
  4864. ()
  4865. {
  4866. return "Reports test results as an XML document";
  4867. }
  4868. private: // IReporter
  4869. ///////////////////////////////////////////////////////////////////////////
  4870. virtual void StartTesting
  4871. ()
  4872. {
  4873. m_xml = XmlWriter( m_config.stream() );
  4874. m_xml.startElement( "Catch" );
  4875. if( !m_config.getName().empty() )
  4876. m_xml.writeAttribute( "name", m_config.getName() );
  4877. }
  4878. ///////////////////////////////////////////////////////////////////////////
  4879. virtual void EndTesting
  4880. (
  4881. std::size_t succeeded,
  4882. std::size_t failed
  4883. )
  4884. {
  4885. m_xml.scopedElement( "OverallResults" )
  4886. .writeAttribute( "successes", succeeded )
  4887. .writeAttribute( "failures", failed );
  4888. m_xml.endElement();
  4889. }
  4890. ///////////////////////////////////////////////////////////////////////////
  4891. virtual void StartGroup
  4892. (
  4893. const std::string& groupName
  4894. )
  4895. {
  4896. m_xml.startElement( "Group" )
  4897. .writeAttribute( "name", groupName );
  4898. }
  4899. ///////////////////////////////////////////////////////////////////////////
  4900. virtual void EndGroup
  4901. (
  4902. const std::string& /*groupName*/,
  4903. std::size_t succeeded,
  4904. std::size_t failed
  4905. )
  4906. {
  4907. m_xml.scopedElement( "OverallResults" )
  4908. .writeAttribute( "successes", succeeded )
  4909. .writeAttribute( "failures", failed );
  4910. m_xml.endElement();
  4911. }
  4912. ///////////////////////////////////////////////////////////////////////////
  4913. virtual void StartSection( const std::string& sectionName, const std::string description )
  4914. {
  4915. m_xml.startElement( "Section" )
  4916. .writeAttribute( "name", sectionName )
  4917. .writeAttribute( "description", description );
  4918. }
  4919. ///////////////////////////////////////////////////////////////////////////
  4920. virtual void EndSection( const std::string& /*sectionName*/, std::size_t succeeded, std::size_t failed )
  4921. {
  4922. m_xml.scopedElement( "OverallResults" )
  4923. .writeAttribute( "successes", succeeded )
  4924. .writeAttribute( "failures", failed );
  4925. m_xml.endElement();
  4926. }
  4927. ///////////////////////////////////////////////////////////////////////////
  4928. virtual void StartTestCase( const Catch::TestCaseInfo& testInfo )
  4929. {
  4930. m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.getName() );
  4931. m_currentTestSuccess = true;
  4932. }
  4933. ///////////////////////////////////////////////////////////////////////////
  4934. virtual void Result( const Catch::ResultInfo& resultInfo )
  4935. {
  4936. if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok )
  4937. return;
  4938. if( resultInfo.hasExpression() )
  4939. {
  4940. m_xml.startElement( "Expression" )
  4941. .writeAttribute( "success", resultInfo.ok() )
  4942. .writeAttribute( "filename", resultInfo.getFilename() )
  4943. .writeAttribute( "line", resultInfo.getLine() );
  4944. m_xml.scopedElement( "Original" )
  4945. .writeText( resultInfo.getExpression() );
  4946. m_xml.scopedElement( "Expanded" )
  4947. .writeText( resultInfo.getExpandedExpression() );
  4948. m_currentTestSuccess &= resultInfo.ok();
  4949. }
  4950. switch( resultInfo.getResultType() )
  4951. {
  4952. case ResultWas::ThrewException:
  4953. m_xml.scopedElement( "Exception" )
  4954. .writeAttribute( "filename", resultInfo.getFilename() )
  4955. .writeAttribute( "line", resultInfo.getLine() )
  4956. .writeText( resultInfo.getMessage() );
  4957. m_currentTestSuccess = false;
  4958. break;
  4959. case ResultWas::Info:
  4960. m_xml.scopedElement( "Info" )
  4961. .writeText( resultInfo.getMessage() );
  4962. break;
  4963. case ResultWas::Warning:
  4964. m_xml.scopedElement( "Warning" )
  4965. .writeText( resultInfo.getMessage() );
  4966. break;
  4967. case ResultWas::ExplicitFailure:
  4968. m_xml.scopedElement( "Failure" )
  4969. .writeText( resultInfo.getMessage() );
  4970. m_currentTestSuccess = false;
  4971. break;
  4972. case ResultWas::Unknown:
  4973. case ResultWas::Ok:
  4974. case ResultWas::FailureBit:
  4975. case ResultWas::ExpressionFailed:
  4976. case ResultWas::Exception:
  4977. case ResultWas::DidntThrowException:
  4978. default:
  4979. break;
  4980. }
  4981. if( resultInfo.hasExpression() )
  4982. m_xml.endElement();
  4983. }
  4984. ///////////////////////////////////////////////////////////////////////////
  4985. virtual void EndTestCase( const Catch::TestCaseInfo&, std::size_t /* succeeded */, std::size_t /* failed */, const std::string& /*stdOut*/, const std::string& /*stdErr*/ )
  4986. {
  4987. m_xml.scopedElement( "OverallResult" ).writeAttribute( "success", m_currentTestSuccess );
  4988. m_xml.endElement();
  4989. }
  4990. private:
  4991. const IReporterConfig& m_config;
  4992. bool m_currentTestSuccess;
  4993. XmlWriter m_xml;
  4994. };
  4995. INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
  4996. } // end namespace Catch
  4997. // #included from: reporters/catch_reporter_junit.hpp
  4998. /*
  4999. * catch_reporter_junit.hpp
  5000. * Catch
  5001. *
  5002. * Created by Phil on 26/11/2010.
  5003. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  5004. *
  5005. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5006. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5007. *
  5008. */
  5009. #define TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
  5010. namespace Catch
  5011. {
  5012. class JunitReporter : public Catch::IReporter
  5013. {
  5014. struct TestStats
  5015. {
  5016. std::string m_element;
  5017. std::string m_resultType;
  5018. std::string m_message;
  5019. std::string m_content;
  5020. };
  5021. struct TestCaseStats
  5022. {
  5023. TestCaseStats( const std::string& name = std::string() )
  5024. : m_name( name )
  5025. {
  5026. }
  5027. double m_timeInSeconds;
  5028. std::string m_status;
  5029. std::string m_className;
  5030. std::string m_name;
  5031. std::vector<TestStats> m_testStats;
  5032. };
  5033. struct Stats
  5034. {
  5035. Stats( const std::string& name = std::string() )
  5036. : m_testsCount( 0 ),
  5037. m_failuresCount( 0 ),
  5038. m_disabledCount( 0 ),
  5039. m_errorsCount( 0 ),
  5040. m_timeInSeconds( 0 ),
  5041. m_name( name )
  5042. {
  5043. }
  5044. std::size_t m_testsCount;
  5045. std::size_t m_failuresCount;
  5046. std::size_t m_disabledCount;
  5047. std::size_t m_errorsCount;
  5048. double m_timeInSeconds;
  5049. std::string m_name;
  5050. std::vector<TestCaseStats> m_testCaseStats;
  5051. };
  5052. public:
  5053. ///////////////////////////////////////////////////////////////////////////
  5054. JunitReporter( const IReporterConfig& config )
  5055. : m_config( config ),
  5056. m_testSuiteStats( "AllTests" ),
  5057. m_currentStats( &m_testSuiteStats )
  5058. {
  5059. }
  5060. ///////////////////////////////////////////////////////////////////////////
  5061. static std::string getDescription()
  5062. {
  5063. return "Reports test results in an XML format that looks like Ant's junitreport target";
  5064. }
  5065. private: // IReporter
  5066. ///////////////////////////////////////////////////////////////////////////
  5067. virtual void StartTesting()
  5068. {
  5069. }
  5070. ///////////////////////////////////////////////////////////////////////////
  5071. virtual void StartGroup( const std::string& groupName )
  5072. {
  5073. m_statsForSuites.push_back( Stats( groupName ) );
  5074. m_currentStats = &m_statsForSuites.back();
  5075. }
  5076. ///////////////////////////////////////////////////////////////////////////
  5077. virtual void EndGroup( const std::string&, std::size_t succeeded, std::size_t failed )
  5078. {
  5079. m_currentStats->m_testsCount = failed+succeeded;
  5080. m_currentStats = &m_testSuiteStats;
  5081. }
  5082. virtual void StartSection( const std::string& /*sectionName*/, const std::string /*description*/ )
  5083. {
  5084. }
  5085. virtual void EndSection( const std::string& /*sectionName*/, std::size_t /*succeeded*/, std::size_t /*failed*/ )
  5086. {
  5087. }
  5088. ///////////////////////////////////////////////////////////////////////////
  5089. virtual void StartTestCase( const Catch::TestCaseInfo& testInfo )
  5090. {
  5091. m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getName() ) );
  5092. }
  5093. ///////////////////////////////////////////////////////////////////////////
  5094. virtual void Result( const Catch::ResultInfo& resultInfo )
  5095. {
  5096. if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults() )
  5097. {
  5098. TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
  5099. TestStats stats;
  5100. std::ostringstream oss;
  5101. if( !resultInfo.getMessage().empty() )
  5102. {
  5103. oss << resultInfo.getMessage() << " at ";
  5104. }
  5105. oss << resultInfo.getFilename() << ":" << resultInfo.getLine();
  5106. stats.m_content = oss.str();
  5107. stats.m_message = resultInfo.getExpandedExpression();
  5108. stats.m_resultType = resultInfo.getTestMacroName();
  5109. switch( resultInfo.getResultType() )
  5110. {
  5111. case ResultWas::ThrewException:
  5112. stats.m_element = "error";
  5113. m_currentStats->m_errorsCount++;
  5114. break;
  5115. case ResultWas::Info:
  5116. stats.m_element = "info"; // !TBD ?
  5117. break;
  5118. case ResultWas::Warning:
  5119. stats.m_element = "warning"; // !TBD ?
  5120. break;
  5121. case ResultWas::ExplicitFailure:
  5122. stats.m_element = "failure";
  5123. m_currentStats->m_failuresCount++;
  5124. break;
  5125. case ResultWas::ExpressionFailed:
  5126. stats.m_element = "failure";
  5127. m_currentStats->m_failuresCount++;
  5128. break;
  5129. case ResultWas::Ok:
  5130. stats.m_element = "success";
  5131. break;
  5132. case ResultWas::Unknown:
  5133. case ResultWas::FailureBit:
  5134. case ResultWas::Exception:
  5135. case ResultWas::DidntThrowException:
  5136. default:
  5137. stats.m_element = "unknown";
  5138. break;
  5139. }
  5140. testCaseStats.m_testStats.push_back( stats );
  5141. }
  5142. }
  5143. ///////////////////////////////////////////////////////////////////////////
  5144. virtual void EndTestCase( const Catch::TestCaseInfo&, std::size_t /* succeeded */, std::size_t /* failed */, const std::string& stdOut, const std::string& stdErr )
  5145. {
  5146. if( !stdOut.empty() )
  5147. m_stdOut << stdOut << "\n";
  5148. if( !stdErr.empty() )
  5149. m_stdErr << stdErr << "\n";
  5150. }
  5151. ///////////////////////////////////////////////////////////////////////////
  5152. virtual void EndTesting( std::size_t /* succeeded */, std::size_t /* failed */ )
  5153. {
  5154. std::ostream& str = m_config.stream();
  5155. {
  5156. XmlWriter xml( str );
  5157. if( m_statsForSuites.size() > 0 )
  5158. xml.startElement( "testsuites" );
  5159. std::vector<Stats>::const_iterator it = m_statsForSuites.begin();
  5160. std::vector<Stats>::const_iterator itEnd = m_statsForSuites.end();
  5161. for(; it != itEnd; ++it )
  5162. {
  5163. XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
  5164. xml.writeAttribute( "name", it->m_name );
  5165. xml.writeAttribute( "errors", it->m_errorsCount );
  5166. xml.writeAttribute( "failures", it->m_failuresCount );
  5167. xml.writeAttribute( "tests", it->m_testsCount );
  5168. xml.writeAttribute( "hostname", "tbd" );
  5169. xml.writeAttribute( "time", "tbd" );
  5170. xml.writeAttribute( "timestamp", "tbd" );
  5171. OutputTestCases( xml, *it );
  5172. }
  5173. xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ) );
  5174. xml.scopedElement( "system-err" ).writeText( trim( m_stdOut.str() ) );
  5175. }
  5176. }
  5177. ///////////////////////////////////////////////////////////////////////////
  5178. void OutputTestCases( XmlWriter& xml, const Stats& stats )
  5179. {
  5180. std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin();
  5181. std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end();
  5182. for(; it != itEnd; ++it )
  5183. {
  5184. xml.writeBlankLine();
  5185. xml.writeComment( "Test case" );
  5186. XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
  5187. xml.writeAttribute( "classname", it->m_className );
  5188. xml.writeAttribute( "name", it->m_name );
  5189. xml.writeAttribute( "time", "tbd" );
  5190. OutputTestResult( xml, *it );
  5191. }
  5192. }
  5193. ///////////////////////////////////////////////////////////////////////////
  5194. void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats )
  5195. {
  5196. std::vector<TestStats>::const_iterator it = stats.m_testStats.begin();
  5197. std::vector<TestStats>::const_iterator itEnd = stats.m_testStats.end();
  5198. for(; it != itEnd; ++it )
  5199. {
  5200. if( it->m_element != "success" )
  5201. {
  5202. XmlWriter::ScopedElement e = xml.scopedElement( it->m_element );
  5203. xml.writeAttribute( "message", it->m_message );
  5204. xml.writeAttribute( "type", it->m_resultType );
  5205. if( !it->m_content.empty() )
  5206. xml.writeText( it->m_content );
  5207. }
  5208. }
  5209. }
  5210. private:
  5211. const IReporterConfig& m_config;
  5212. bool m_currentTestSuccess;
  5213. Stats m_testSuiteStats;
  5214. Stats* m_currentStats;
  5215. std::vector<Stats> m_statsForSuites;
  5216. std::ostringstream m_stdOut;
  5217. std::ostringstream m_stdErr;
  5218. };
  5219. INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
  5220. } // end namespace Catch
  5221. #include <fstream>
  5222. #include <stdlib.h>
  5223. #include <limits>
  5224. namespace Catch
  5225. {
  5226. //////////////////////////////////////////////////////////////////////////
  5227. inline int Main
  5228. (
  5229. Config& config
  5230. )
  5231. {
  5232. // Handle list request
  5233. if( config.listWhat() != Config::List::None )
  5234. return List( config );
  5235. // Open output file, if specified
  5236. std::ofstream ofs;
  5237. if( !config.getFilename().empty() )
  5238. {
  5239. ofs.open( config.getFilename().c_str() );
  5240. if( ofs.fail() )
  5241. {
  5242. std::cerr << "Unable to open file: '" << config.getFilename() << "'" << std::endl;
  5243. return (std::numeric_limits<int>::max)();
  5244. }
  5245. config.setStreamBuf( ofs.rdbuf() );
  5246. }
  5247. Runner runner( config );
  5248. // Run test specs specified on the command line - or default to all
  5249. if( !config.testsSpecified() )
  5250. {
  5251. config.getReporter()->StartGroup( "" );
  5252. runner.runAll();
  5253. config.getReporter()->EndGroup( "", runner.getSuccessCount(), runner.getFailureCount() );
  5254. }
  5255. else
  5256. {
  5257. // !TBD We should get all the testcases upfront, report any missing,
  5258. // then just run them
  5259. std::vector<std::string>::const_iterator it = config.getTestSpecs().begin();
  5260. std::vector<std::string>::const_iterator itEnd = config.getTestSpecs().end();
  5261. for(; it != itEnd; ++it )
  5262. {
  5263. size_t prevSuccess = runner.getSuccessCount();
  5264. size_t prevFail = runner.getFailureCount();
  5265. config.getReporter()->StartGroup( *it );
  5266. if( runner.runMatching( *it ) == 0 )
  5267. {
  5268. // Use reporter?
  5269. // std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
  5270. }
  5271. config.getReporter()->EndGroup( *it, runner.getSuccessCount()-prevSuccess, runner.getFailureCount()-prevFail );
  5272. }
  5273. }
  5274. return static_cast<int>( runner.getFailureCount() );
  5275. }
  5276. //////////////////////////////////////////////////////////////////////////
  5277. inline void showHelp
  5278. (
  5279. std::string exeName
  5280. )
  5281. {
  5282. std::string::size_type pos = exeName.find_last_of( "/\\" );
  5283. if( pos != std::string::npos )
  5284. {
  5285. exeName = exeName.substr( pos+1 );
  5286. }
  5287. std::cout << exeName << " is a CATCH host application. Options are as follows:\n\n"
  5288. << "\t-l, --list <tests | reporters> [xml]\n"
  5289. << "\t-t, --test <testspec> [<testspec>...]\n"
  5290. << "\t-r, --reporter <reporter name>\n"
  5291. << "\t-o, --out <file name>|<%stream name>\n"
  5292. << "\t-s, --success\n"
  5293. << "\t-b, --break\n"
  5294. << "\t-n, --name <name>\n\n"
  5295. << "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl;
  5296. }
  5297. //////////////////////////////////////////////////////////////////////////
  5298. inline int Main
  5299. (
  5300. int argc,
  5301. char* const argv[],
  5302. Config& config
  5303. )
  5304. {
  5305. ArgParser( argc, argv, config );
  5306. if( !config.getMessage().empty() )
  5307. {
  5308. std::cerr << config.getMessage() << std::endl;
  5309. return (std::numeric_limits<int>::max)();
  5310. }
  5311. // Handle help
  5312. if( config.showHelp() )
  5313. {
  5314. showHelp( argv[0] );
  5315. return 0;
  5316. }
  5317. return Main( config );
  5318. }
  5319. //////////////////////////////////////////////////////////////////////////
  5320. inline int Main
  5321. (
  5322. int argc,
  5323. char* const argv[]
  5324. )
  5325. {
  5326. Config config;
  5327. // if( isDebuggerActive() )
  5328. // config.useStream( "debug" );
  5329. return Main( argc, argv, config );
  5330. }
  5331. } // end namespace Catch
  5332. #endif
  5333. #ifdef CATCH_CONFIG_MAIN
  5334. // #included from: internal/catch_default_main.hpp
  5335. /*
  5336. * catch_default_main.hpp
  5337. * Catch
  5338. *
  5339. * Created by Phil on 20/05/2011.
  5340. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  5341. *
  5342. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5343. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5344. *
  5345. */
  5346. #define TWOBLUECUBES_CATCH_DEFAULT_MAIN_HPP_INCLUDED
  5347. int main (int argc, char * const argv[])
  5348. {
  5349. #ifdef __OBJC__
  5350. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  5351. Catch::registerTestMethods();
  5352. int result = Catch::Main( argc, (char* const*)argv );
  5353. [pool drain];
  5354. return result;
  5355. #else
  5356. return Catch::Main( argc, argv );
  5357. #endif
  5358. }
  5359. #endif
  5360. //////
  5361. #define REQUIRE( expr ) INTERNAL_CATCH_TEST( expr, false, true, "REQUIRE" )
  5362. #define REQUIRE_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, true, "REQUIRE_FALSE" )
  5363. #define REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., true, "REQUIRE_THROWS" )
  5364. #define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, true, "REQUIRE_THROWS_AS" )
  5365. #define REQUIRE_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, true, "REQUIRE_NOTHROW" )
  5366. #define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" )
  5367. #define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, false, "CHECK_FALSE" )
  5368. #define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., false, "CHECK_THROWS" )
  5369. #define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" )
  5370. #define CHECK_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, false, "CHECK_NOTHROW" )
  5371. #define INFO( msg ) INTERNAL_CATCH_MSG( msg, Catch::ResultWas::Info, false, "INFO" )
  5372. #define WARN( msg ) INTERNAL_CATCH_MSG( msg, Catch::ResultWas::Warning, false, "WARN" )
  5373. #define FAIL( msg ) INTERNAL_CATCH_MSG( msg, Catch::ResultWas::ExplicitFailure, true, "FAIL" )
  5374. #define SCOPED_INFO( msg ) INTERNAL_CATCH_SCOPED_INFO( msg )
  5375. #define CAPTURE( msg ) INTERNAL_CATCH_MSG( #msg " := " << msg, Catch::ResultWas::Info, false, "CAPTURE" )
  5376. #define SECTION( name, description ) INTERNAL_CATCH_SECTION( name, description )
  5377. #define TEST_CASE( name, description ) INTERNAL_CATCH_TESTCASE( name, description )
  5378. #define TEST_CASE_NORETURN( name, description ) INTERNAL_CATCH_TESTCASE_NORETURN( name, description )
  5379. #define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE( "", "Anonymous test case" )
  5380. #define METHOD_AS_TEST_CASE( method, name, description ) CATCH_METHOD_AS_TEST_CASE( method, name, description )
  5381. #define REGISTER_REPORTER( name, reporterType ) INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType )
  5382. #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature )
  5383. #define GENERATE( expr) INTERNAL_CATCH_GENERATE( expr )
  5384. ///////////////
  5385. // Still to be implemented
  5386. #define CHECK_NOFAIL( expr ) // !TBD - reports violation, but doesn't fail Test
  5387. using Catch::Detail::Approx;
  5388. #endif // TWOBLUECUBES_CATCH_HPP_INCLUDED