tctypeparser.pas 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881
  1. unit tctypeparser;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, pastree, pscanner, pparser,
  6. tcbaseparser, testregistry;
  7. type
  8. { TBaseTestTypeParser }
  9. TBaseTestTypeParser= Class(TTestParser)
  10. private
  11. FAddComment: Boolean;
  12. FType : TPasType;
  13. FHint : string;
  14. FErrorSource : String;
  15. Protected
  16. Function ParseType(ASource : String; ATypeClass : TClass;Const AHint : String = '') : TPasType; virtual; overload;
  17. Procedure AssertParseTypeError(ASource : String);
  18. Procedure AssertComment;
  19. procedure SetUp; override;
  20. Procedure TearDown; override;
  21. Property TheType : TPasType Read FType Write FType;
  22. Property Hint : string Read FHint Write FHint;
  23. Property AddComment : Boolean Read FAddComment Write FAddComment;
  24. end;
  25. { TTestTypeParser }
  26. TTestTypeParser = Class(TBaseTestTypeParser)
  27. private
  28. Protected
  29. procedure StartTypeHelper(ForType: String; AParent: String);
  30. Procedure DoTestAliasType(Const AnAliasType : String; Const AHint : String);
  31. procedure DoTestStringType(const AnAliasType: String; const AHint: String);
  32. procedure DoTypeError(Const AMsg,ASource : string);
  33. Procedure DoParseError;
  34. Procedure DoParsePointer(Const ASource : String; Const AHint : String; ADestType : TClass = Nil);
  35. Procedure DoParseArray(Const ASource : String; Const AHint : String; ADestType : TClass = Nil);
  36. Procedure DoParseEnumerated(Const ASource : String; Const AHint : String; ACount : integer);
  37. Procedure DoTestFileType(Const AType : String; Const AHint : String; ADestType : TClass = Nil);
  38. Procedure DoTestRangeType(Const AStart,AStop,AHint : String);
  39. Procedure DoParseSimpleSet(Const ASource : String; Const AHint : String; IsPacked : Boolean = False);
  40. Procedure DoParseComplexSet(Const ASource : String; Const AHint : String);
  41. procedure DoParseRangeSet(const ASource: String; const AHint: String);
  42. Procedure DoTestComplexSet;
  43. Procedure DoTestClassOf(Const AHint : string);
  44. Published
  45. Procedure TestAliasType;
  46. procedure TestAbsoluteAliasType;
  47. Procedure TestCrossUnitAliasType;
  48. Procedure TestAliasTypeDeprecated;
  49. Procedure TestAliasTypePlatform;
  50. Procedure TestSimpleTypeByte;
  51. Procedure TestSimpleTypeByteComment;
  52. Procedure TestSimpleTypeByteDeprecated;
  53. Procedure TestSimpleTypeBytePlatform;
  54. Procedure TestSimpleTypeBoolean;
  55. Procedure TestSimpleTypeBooleanDeprecated;
  56. Procedure TestSimpleTypeBooleanPlatform;
  57. Procedure TestSimpleTypeChar;
  58. Procedure TestSimpleTypeCharDeprecated;
  59. Procedure TestSimpleTypeCharPlatform;
  60. Procedure TestSimpleTypeInteger;
  61. Procedure TestSimpleTypeIntegerDeprecated;
  62. Procedure TestSimpleTypeIntegerPlatform;
  63. Procedure TestSimpleTypeInt64;
  64. Procedure TestSimpleTypeInt64Deprecated;
  65. Procedure TestSimpleTypeInt64Platform;
  66. Procedure TestSimpleTypeLongInt;
  67. Procedure TestSimpleTypeLongIntDeprecated;
  68. Procedure TestSimpleTypeLongIntPlatform;
  69. Procedure TestSimpleTypeLongWord;
  70. Procedure TestSimpleTypeLongWordDeprecated;
  71. Procedure TestSimpleTypeLongWordPlatform;
  72. Procedure TestSimpleTypeDouble;
  73. Procedure TestSimpleTypeDoubleDeprecated;
  74. Procedure TestSimpleTypeDoublePlatform;
  75. Procedure TestSimpleTypeShortInt;
  76. Procedure TestSimpleTypeShortIntDeprecated;
  77. Procedure TestSimpleTypeShortIntPlatform;
  78. Procedure TestSimpleTypeSmallInt;
  79. Procedure TestSimpleTypeSmallIntDeprecated;
  80. Procedure TestSimpleTypeSmallIntPlatform;
  81. Procedure TestSimpleTypeString;
  82. Procedure TestSimpleTypeStringDeprecated;
  83. Procedure TestSimpleTypeStringPlatform;
  84. Procedure TestSimpleTypeStringSize;
  85. Procedure TestSimpleTypeStringSizeIncomplete;
  86. Procedure TestSimpleTypeStringSizeWrong;
  87. Procedure TestSimpleTypeStringSizeDeprecated;
  88. Procedure TestSimpleTypeStringSizePlatform;
  89. procedure TestSimpleTypeStringCodePage;
  90. procedure TestSimpleTypeStringCodePageIncomplete;
  91. procedure TestSimpleTypeStringCodePageWrong;
  92. procedure TestSimpleTypeStringCodePageDeprecated;
  93. procedure TestSimpleTypeStringCodePagePlatform;
  94. Procedure TestSimpleTypeWord;
  95. Procedure TestSimpleTypeWordDeprecated;
  96. Procedure TestSimpleTypeWordPlatform;
  97. Procedure TestSimpleTypeQWord;
  98. Procedure TestSimpleTypeQWordDeprecated;
  99. Procedure TestSimpleTypeQWordPlatform;
  100. Procedure TestSimpleTypeCardinal;
  101. Procedure TestSimpleTypeCardinalDeprecated;
  102. Procedure TestSimpleTypeCardinalPlatform;
  103. Procedure TestSimpleTypeWideChar;
  104. Procedure TestSimpleTypeWideCharDeprecated;
  105. Procedure TestSimpleTypeWideCharPlatform;
  106. Procedure TestPointerSimple;
  107. procedure TestPointerSimpleDeprecated;
  108. procedure TestPointerSimplePlatform;
  109. Procedure TestStaticArray;
  110. procedure TestStaticArrayAsRange;
  111. Procedure TestStaticArrayComment;
  112. procedure TestStaticArrayDeprecated;
  113. procedure TestStaticArrayPlatform;
  114. Procedure TestStaticArrayPacked;
  115. Procedure TestStaticArrayTypedIndex;
  116. Procedure TestStaticArrayOfMethod;
  117. procedure TestStaticArrayOfProcedure;
  118. Procedure TestDynamicArray;
  119. Procedure TestDynamicArrayComment;
  120. procedure TestDynamicArrayOfMethod;
  121. procedure TestDynamicArrayOfProcedure;
  122. Procedure TestGenericArray;
  123. Procedure TestSimpleEnumerated;
  124. Procedure TestSimpleEnumeratedComment;
  125. Procedure TestSimpleEnumeratedComment2;
  126. Procedure TestSimpleEnumeratedDeprecated;
  127. Procedure TestSimpleEnumeratedPlatform;
  128. Procedure TestAssignedEnumerated;
  129. Procedure TestAssignedEnumeratedDeprecated;
  130. Procedure TestAssignedEnumeratedPlatform;
  131. Procedure TestFileType;
  132. Procedure TestFileTypeDeprecated;
  133. Procedure TestFileTypePlatform;
  134. Procedure TestRangeType;
  135. Procedure TestCharRangeType;
  136. Procedure TestCharRangeType2;
  137. Procedure TestRangeTypeDeprecated;
  138. Procedure TestRangeTypePlatform;
  139. Procedure TestIdentifierRangeType;
  140. Procedure TestIdentifierRangeTypeDeprecated;
  141. Procedure TestIdentifierRangeTypePlatform;
  142. Procedure TestNegativeIdentifierRangeType;
  143. Procedure TestSimpleSet;
  144. Procedure TestPackedSet;
  145. Procedure TestSimpleSetDeprecated;
  146. Procedure TestSimpleSetPlatform;
  147. Procedure TestComplexSet;
  148. Procedure TestComplexSetDeprecated;
  149. Procedure TestComplexSetPlatform;
  150. procedure TestRangeLowHigh;
  151. Procedure TestRangeSet;
  152. Procedure TestSubRangeSet;
  153. Procedure TestRangeSetDeprecated;
  154. Procedure TestRangeSetPlatform;
  155. Procedure TestNegativeRangeType;
  156. Procedure TestClassOf;
  157. Procedure TestClassOfComment;
  158. Procedure TestClassOfDeprecated;
  159. Procedure TestClassOfPlatform;
  160. Procedure TestReferenceAlias;
  161. Procedure TestReferenceSet;
  162. Procedure TestReferenceClassOf;
  163. Procedure TestReferenceFile;
  164. Procedure TestReferenceArray;
  165. Procedure TestReferencePointer;
  166. Procedure TestInvalidColon;
  167. Procedure TestTypeHelper;
  168. Procedure TestTypeHelperWithParent;
  169. procedure TestPointerReference;
  170. Procedure TestPointerKeyWord;
  171. Procedure TestPointerFile;
  172. Procedure TestAbstractObject;
  173. end;
  174. { TTestRecordTypeParser }
  175. TTestRecordTypeParser = Class(TBaseTestTypeParser)
  176. private
  177. FDecl : TStrings;
  178. FAdvanced,
  179. FEnded,
  180. FStarted: boolean;
  181. FRecord: TPasRecordType;
  182. FMember1: TPasElement;
  183. function GetC(AIndex: Integer): TPasConst;
  184. Function GetField(AIndex : Integer; R : TPasRecordType) : TPasVariable;
  185. Function GetField(AIndex : Integer; R : TPasVariant) : TPasVariable;
  186. function GetF(AIndex: Integer): TPasVariable;
  187. function GetM(AIndex : Integer): TPasElement;
  188. Function GetVariant(AIndex : Integer; R : TPasRecordType) : TPasVariant;
  189. function GetV(AIndex: Integer): TPasVariant;
  190. Protected
  191. procedure SetUp; override;
  192. procedure TearDown; override;
  193. Procedure StartRecord(Advanced: boolean = false);
  194. Procedure EndRecord(AEnd : String = 'end');
  195. Procedure AddMember(S : String);
  196. Procedure ParseRecord;
  197. Procedure ParseRecordFail(Msg: string; MsgNumber: integer);
  198. Procedure DoParseRecord;
  199. Procedure TestFields(Const Fields : Array of string; AHint : String; HaveVariant : Boolean = False);
  200. procedure AssertVariantSelector(AName, AType: string);
  201. procedure AssertConst1(Hints: TPasMemberHints; Index: integer = 1);
  202. procedure AssertField1(Hints: TPasMemberHints);
  203. procedure AssertField2(Hints: TPasMemberHints);
  204. procedure AssertMethod2(Hints: TPasMemberHints; isClass : Boolean = False);
  205. procedure AssertConstructor2(Hints: TPasMemberHints; isClass : Boolean = False);
  206. procedure AssertOperatorMethod2(Hints: TPasMemberHints; isClass : Boolean = False);
  207. procedure AssertVariant1(Hints: TPasMemberHints);
  208. procedure AssertVariant1(Hints: TPasMemberHints; VariantLabels : Array of string);
  209. procedure AssertVariant2(Hints: TPasMemberHints);
  210. procedure AssertVariant2(Hints: TPasMemberHints; VariantLabels : Array of string);
  211. procedure AssertOneIntegerField(Hints: TPasMemberHints);
  212. procedure AssertTwoIntegerFields(Hints1, Hints2: TPasMemberHints);
  213. procedure AssertIntegerFieldAndMethod(Hints1, Hints2: TPasMemberHints);
  214. procedure AssertIntegerFieldAndConstructor(Hints1, Hints2: TPasMemberHints);
  215. procedure AssertRecordField(AIndex: Integer;Hints: TPasMemberHints);
  216. procedure AssertRecordVariant(AIndex: Integer;Hints: TPasMemberHints; VariantLabels : Array of string);
  217. Procedure AssertRecordVariantVariant(AIndex: Integer;Const AFieldName,ATypeName: string;Hints: TPasMemberHints; VariantLabels : Array of string);
  218. Procedure DoTestEmpty(Const AHint : String);
  219. procedure DoTestDeprecatedVariantNoStorage(Const AHint : string);
  220. procedure DoTestDeprecatedVariantStorage(Const AHint : string);
  221. procedure DoTestVariantNoStorage(Const AHint : string);
  222. procedure DoTestVariantStorage(Const AHint : string);
  223. procedure DoTestTwoVariantsNoStorage(Const AHint : string);
  224. procedure DoTestTwoVariantsStorage(Const AHint : string);
  225. procedure DoTestTwoVariantsFirstDeprecatedStorage(Const AHint : string);
  226. procedure DoTestTwoVariantsSecondDeprecatedStorage(Const AHint : string);
  227. Procedure DoTestVariantTwoLabels(Const AHint : string);
  228. Procedure DoTestTwoVariantsTwoLabels(Const AHint : string);
  229. procedure DoTestVariantNestedRecord(Const AHint : string);
  230. procedure DoTestVariantNestedVariant(Const AHint : string);
  231. procedure DoTestVariantNestedVariantFirstDeprecated(Const AHint : string);
  232. procedure DoTestVariantNestedVariantSecondDeprecated(const AHint: string);
  233. procedure DoTestVariantNestedVariantBothDeprecated(const AHint: string);
  234. Property TheRecord : TPasRecordType Read FRecord;
  235. Property Advanced: boolean read FAdvanced;
  236. Property Const1 : TPasConst Index 0 Read GetC;
  237. Property Field1 : TPasVariable Index 0 Read GetF;
  238. Property Field2 : TPasVariable Index 1 Read GetF;
  239. Property Variant1 : TPasVariant Index 0 Read GetV;
  240. Property Variant2 : TPasVariant Index 1 Read GetV;
  241. Property Members[AIndex : Integer] : TPasElement Read GetM;
  242. Property Member1 : TPasElement Read FMember1;
  243. Published
  244. Procedure TestEmpty;
  245. Procedure TestEmptyComment;
  246. Procedure TestEmptyDeprecated;
  247. Procedure TestEmptyPlatform;
  248. Procedure TestOneField;
  249. Procedure TestOneFieldComment;
  250. Procedure TestOneFieldDeprecated;
  251. Procedure TestOneFieldPlatform;
  252. Procedure TestOneFieldSemicolon;
  253. Procedure TestOneFieldSemicolonDeprecated;
  254. Procedure TestOneFieldSemicolonPlatform;
  255. Procedure TestOneDeprecatedField;
  256. Procedure TestOneDeprecatedFieldDeprecated;
  257. Procedure TestOneDeprecatedFieldPlatform;
  258. Procedure TestOnePlatformField;
  259. Procedure TestOnePlatformFieldDeprecated;
  260. Procedure TestOnePlatformFieldPlatform;
  261. Procedure TestOneGenericField;
  262. Procedure TestTwoFields;
  263. procedure TestTwoFieldProtected;
  264. procedure TestTwoFieldStrictPrivate;
  265. procedure TestTwoFieldPrivateNoDelphi;
  266. Procedure TestTwoFieldPrivate;
  267. Procedure TestTwoFieldDeprecated;
  268. Procedure TestTwoFieldPlatform;
  269. Procedure TestTwoFieldsFirstDeprecated;
  270. Procedure TestTwoFieldsFirstDeprecatedDeprecated;
  271. Procedure TestTwoFieldsFirstDeprecatedPlatform;
  272. Procedure TestTwoFieldsSecondDeprecated;
  273. Procedure TestTwoFieldsSecondDeprecatedDeprecated;
  274. Procedure TestTwoFieldsSecondDeprecatedPlatform;
  275. Procedure TestTwoFieldsBothDeprecated;
  276. Procedure TestTwoFieldsBothDeprecatedDeprecated;
  277. Procedure TestTwoFieldsBothDeprecatedPlatform;
  278. Procedure TestTwoFieldsCombined;
  279. Procedure TestTwoFieldsCombinedDeprecated;
  280. Procedure TestTwoFieldsCombinedPlatform;
  281. Procedure TestTwoDeprecatedFieldsCombined;
  282. Procedure TestTwoDeprecatedFieldsCombinedDeprecated;
  283. Procedure TestTwoDeprecatedFieldsCombinedPlatform;
  284. procedure TestFieldAndConstructor;
  285. Procedure TestFieldAndMethod;
  286. Procedure TestFieldAnd2Methods;
  287. Procedure TestFieldAndProperty;
  288. Procedure TestFieldAndClassMethod;
  289. Procedure TestFieldAndClassOperator;
  290. Procedure TestFieldAndClassVar;
  291. Procedure TestFieldAndVar;
  292. Procedure TestNested;
  293. Procedure TestNestedDeprecated;
  294. Procedure TestNestedPlatform;
  295. procedure TestNestedSemicolon;
  296. procedure TestNestedSemicolonDeprecated;
  297. procedure TestNestedSemicolonPlatform;
  298. procedure TestNestedFirst;
  299. procedure TestNestedFirstDeprecated;
  300. procedure TestNestedFirstPlatform;
  301. Procedure TestDeprecatedNested;
  302. Procedure TestDeprecatedNestedDeprecated;
  303. Procedure TestDeprecatedNestedPlatform;
  304. procedure TestDeprecatedNestedFirst;
  305. procedure TestDeprecatedNestedFirstDeprecated;
  306. procedure TestDeprecatedNestedFirstPlatform;
  307. Procedure TestVariantNoStorage;
  308. procedure TestVariantNoStorageDeprecated;
  309. procedure TestVariantNoStoragePlatform;
  310. Procedure TestVariantStorage;
  311. procedure TestVariantStorageDeprecated;
  312. procedure TestVariantStoragePlatform;
  313. Procedure TestDeprecatedVariantNoStorage;
  314. procedure TestDeprecatedVariantNoStorageDeprecated;
  315. procedure TestDeprecatedVariantNoStoragePlatform;
  316. Procedure TestDeprecatedVariantStorage;
  317. procedure TestDeprecatedVariantStorageDeprecated;
  318. procedure TestDeprecatedVariantStoragePlatform;
  319. Procedure TestTwoVariantsNoStorage;
  320. procedure TestTwoVariantsNoStorageDeprecated;
  321. procedure TestTwoVariantsNoStoragePlatform;
  322. Procedure TestTwoVariantsStorage;
  323. procedure TestTwoVariantsStorageDeprecated;
  324. procedure TestTwoVariantsStoragePlatform;
  325. Procedure TestTwoVariantsFirstDeprecatedStorage;
  326. procedure TestTwoVariantsFirstDeprecatedStorageDeprecated;
  327. procedure TestTwoVariantsFirstDeprecatedStoragePlatform;
  328. Procedure TestTwoVariantsSecondDeprecatedStorage;
  329. procedure TestTwoVariantsSecondDeprecatedStorageDeprecated;
  330. procedure TestTwoVariantsSecondDeprecatedStoragePlatform;
  331. Procedure TestVariantTwoLabels;
  332. Procedure TestVariantTwoLabelsDeprecated;
  333. Procedure TestVariantTwoLabelsPlatform;
  334. Procedure TestTwoVariantsTwoLabels;
  335. Procedure TestTwoVariantsTwoLabelsDeprecated;
  336. Procedure TestTwoVariantsTwoLabelsPlatform;
  337. Procedure TestVariantNestedRecord;
  338. Procedure TestVariantNestedRecordDeprecated;
  339. Procedure TestVariantNestedRecordPlatform;
  340. Procedure TestVariantNestedVariant;
  341. Procedure TestVariantNestedVariantDeprecated;
  342. Procedure TestVariantNestedVariantPlatForm;
  343. Procedure TestVariantNestedVariantFirstDeprecated;
  344. Procedure TestVariantNestedVariantFirstDeprecatedDeprecated;
  345. Procedure TestVariantNestedVariantFirstDeprecatedPlatform;
  346. Procedure TestVariantNestedVariantSecondDeprecated;
  347. Procedure TestVariantNestedVariantSecondDeprecatedDeprecated;
  348. Procedure TestVariantNestedVariantSecondDeprecatedPlatform;
  349. Procedure TestVariantNestedVariantBothDeprecated;
  350. Procedure TestVariantNestedVariantBothDeprecatedDeprecated;
  351. Procedure TestVariantNestedVariantBothDeprecatedPlatform;
  352. Procedure TestOperatorField;
  353. Procedure TestPropertyFail;
  354. Procedure TestAdvRec_TwoConst;
  355. Procedure TestAdvRec_Property;
  356. Procedure TestAdvRec_PropertyImplementsFail;
  357. Procedure TestAdvRec_PropertyNoTypeFail;
  358. Procedure TestAdvRec_ForwardFail;
  359. Procedure TestAdvRec_PublishedFail;
  360. Procedure TestAdvRec_ProcVirtualFail;
  361. Procedure TestAdvRec_ProcOverrideFail;
  362. Procedure TestAdvRec_ProcMessageFail;
  363. Procedure TestAdvRec_DestructorFail;
  364. Procedure TestAdvRec_CaseInVar;
  365. Procedure TestAdvRec_EmptySections;
  366. Procedure TestAdvRecordInFunction;
  367. Procedure TestAdvRecordInAnonFunction;
  368. Procedure TestAdvRecordClassOperator;
  369. Procedure TestAdvRecordInitOperator;
  370. Procedure TestAdvRecordGenericFunction;
  371. end;
  372. { TTestProcedureTypeParser }
  373. TCallingConventionTest = Procedure (CC : TCallingConvention;Const AHint : String) of object;
  374. TTestProcedureTypeParser = Class(TBaseTestTypeParser)
  375. Private
  376. FProc : TPasProcedureType;
  377. procedure CheckArrayOfConstArgument(Aindex: Integer; Ac: TArgumentAccess);
  378. Protected
  379. procedure DoTestFunction(CC: TCallingConvention; const AHint: String);
  380. procedure DoTestFunctionOfObject(CC: TCallingConvention; const AHint: String);
  381. procedure DoTestFunctionOneArg(CC: TCallingConvention; const AHint: String);
  382. procedure DoTestFunctionOneArgOfObject(CC: TCallingConvention; const AHint: String);
  383. procedure DoTestProcedureOfObject(CC: TCallingConvention; const AHint: String);
  384. procedure DoTestProcedureOfObjectOneArg(CC: TCallingConvention; const AHint: String);
  385. procedure DoTestProcedureIsNested(CC: TCallingConvention; const AHint: String);
  386. procedure DoTestProcedureIsNestedOneArg(CC: TCallingConvention; const AHint: String);
  387. procedure CheckOpenArrayArgument(Ac: TArgumentAccess);
  388. procedure DoTestProcedureArrayOfConst(CC: TCallingConvention; const AHint: String);
  389. procedure DoTestProcedureOpenArray(CC: TCallingConvention; const AHint: String);
  390. procedure DoTestProcedureConstOpenArray(CC: TCallingConvention; const AHint: String);
  391. procedure DoTestProcedureVarOpenArray(CC: TCallingConvention; const AHint: String);
  392. procedure DoTestProcedureOutOpenArray(CC: TCallingConvention; const AHint: String);
  393. procedure DoTestProcedureOneArgDefault(CC: TCallingConvention;const AHint: String);
  394. procedure DoTestProcedureOneArgDefaultExpr(CC: TCallingConvention;const AHint: String);
  395. procedure DoTestProcedureOneArgDefaultSet(CC: TCallingConvention;const AHint: String);
  396. procedure DoTestProcedureOneConstArgDefault(CC: TCallingConvention; const AHint: String);
  397. procedure DoTestProcedureOneVarArgDefault(CC: TCallingConvention; const AHint: String);
  398. procedure DoTestProcedureOneOutArgDefault(CC: TCallingConvention; const AHint: String);
  399. function CheckArgument(AIndex : Integer; Const AName,ATypeName : String; AAccess : TArgumentAccess) : TPasArgument;
  400. Function ParseType(ASource : String; CC : TCallingConvention; ATypeClass : TClass;Const AHint : String = '') : TPasProcedureType; virtual; overload;
  401. Procedure DoTestProcedureDecl(CC : TCallingConvention; Const AHint : String);
  402. Procedure DoTestProcedureOneArgDecl(CC : TCallingConvention; Const AHint : String);
  403. Procedure DoTestProcedureOneVarArgDecl(CC : TCallingConvention; Const AHint : String);
  404. Procedure DoTestProcedureOneConstArgDecl(CC : TCallingConvention; Const AHint : String);
  405. Procedure DoTestProcedureOneOutArgDecl(CC : TCallingConvention; Const AHint : String);
  406. Procedure DoTestProcedureTwoArgsDecl(CC : TCallingConvention; Const AHint : String);
  407. Procedure DoTestProcedureTwoVarArgsDecl(CC : TCallingConvention; Const AHint : String);
  408. Procedure DoTestProcedureTwoConstArgsDecl(CC : TCallingConvention; Const AHint : String);
  409. Procedure DoTestProcedureTwoOutArgsDecl(CC : TCallingConvention; Const AHint : String);
  410. Procedure DoTestProcedureTwoCombinedArgsDecl(CC : TCallingConvention; Const AHint : String);
  411. Procedure DoTestProcedureTwoCombinedVarArgsDecl(CC : TCallingConvention; Const AHint : String);
  412. Procedure DoTestProcedureTwoCombinedConstArgsDecl(CC : TCallingConvention; Const AHint : String);
  413. Procedure DoTestProcedureTwoCombinedOutArgsDecl(CC : TCallingConvention; Const AHint : String);
  414. Procedure DoTestProcedureDefaultConstArgsDecl(CC : TCallingConvention; Const AHint : String);
  415. procedure DoTestProcedureUntypedArgDecl(CC: TCallingConvention; const AHint: String);
  416. procedure DoTestProcedureUntypedConstArgDecl(CC: TCallingConvention; const AHint: String);
  417. procedure DoTestProcedureUntypedOutArgDecl(CC: TCallingConvention; const AHint: String);
  418. procedure DoTestProcedureUntypedDefArg;
  419. Procedure TestCallingConventions(Proc : TCallingConventionTest; Const AHint : String);
  420. Procedure TestCallingConventions(Proc : TCallingConventionTest);
  421. Function FuncProc : TPasFunctionType;
  422. Property Proc : TPasProcedureType Read FProc;
  423. Published
  424. Procedure TestProcedure;
  425. Procedure TestProcedureComment;
  426. Procedure TestProcedureOneArg;
  427. Procedure TestProcedureOneVarArg;
  428. Procedure TestProcedureOneConstArg;
  429. Procedure TestProcedureOneOutArg;
  430. Procedure TestProcedureTwoArgs;
  431. Procedure TestProcedureTwoVarArgs;
  432. Procedure TestProcedureTwoConstArgs;
  433. Procedure TestProcedureTwoOutArgs;
  434. Procedure TestProcedureTwoCombinedArgs;
  435. Procedure TestProcedureTwoCombinedVarArgs;
  436. Procedure TestProcedureTwoCombinedConstArgs;
  437. Procedure TestProcedureTwoCombinedOutArgs;
  438. Procedure TestProcedureDefaultConstArgs;
  439. Procedure TestProcedureUntypedArg;
  440. Procedure TestProcedureUntypedConstArg;
  441. Procedure TestProcedureUntypedOutArg;
  442. Procedure TestProcedureUntypedDefArg;
  443. Procedure TestProcedureOneArgDefault;
  444. Procedure TestProcedureOneArgDefaultExpr;
  445. Procedure TestProcedureOneArgDefaultSet;
  446. Procedure TestProcedureOneVarArgDefault;
  447. Procedure TestProcedureOneConstArgDefault;
  448. Procedure TestProcedureOneOutArgDefault;
  449. Procedure TestProcedureNoMultiArgDefaults;
  450. Procedure TestProcedureOpenArray;
  451. Procedure TestProcedureConstOpenArray;
  452. Procedure TestProcedureOutOpenArray;
  453. Procedure TestProcedureVarOpenArray;
  454. Procedure TestProcedureArrayOfConst;
  455. Procedure TestProcedureReference;
  456. Procedure TestProcedureOfObject;
  457. Procedure TestProcedureOfObjectOneArg;
  458. Procedure TestProcedureIsNested;
  459. Procedure TestProcedureIsNesteOneArg;
  460. Procedure TestFunction;
  461. Procedure TestFunctionOneArg;
  462. Procedure TestFunctionOfObject;
  463. Procedure TestFunctionOneArgOfObject;
  464. Procedure TestCBlock;
  465. Procedure TestMacPasoutArg;
  466. Procedure TestMacPasPropertyArg;
  467. Procedure TestMacPasPropertyVarArg;
  468. Procedure TestMacPasClassArg;
  469. end;
  470. implementation
  471. uses typinfo;
  472. { TTestProcedureTypeParser }
  473. procedure TTestProcedureTypeParser.DoTestProcedureUntypedArgDecl(
  474. CC: TCallingConvention; const AHint: String);
  475. Var
  476. A : TPasArgument;
  477. begin
  478. ParseType('procedure(var A)',CC,TPasProcedureType,AHint);
  479. AssertEquals('Argument count',1,Proc.Args.Count);
  480. A:=CheckArgument(0,'A','',argVar);
  481. AssertNull('No argument type', A.ArgType)
  482. end;
  483. procedure TTestProcedureTypeParser.DoTestProcedureUntypedConstArgDecl(
  484. CC: TCallingConvention; const AHint: String);
  485. Var
  486. A : TPasArgument;
  487. begin
  488. ParseType('procedure(const A)',CC,TPasProcedureType,AHint);
  489. AssertEquals('Argument count',1,Proc.Args.Count);
  490. A:=CheckArgument(0,'A','',argConst);
  491. AssertNull('No argument type', A.ArgType)
  492. end;
  493. procedure TTestProcedureTypeParser.DoTestProcedureUntypedOutArgDecl(
  494. CC: TCallingConvention; const AHint: String);
  495. Var
  496. A : TPasArgument;
  497. begin
  498. ParseType('procedure(out A)',CC,TPasProcedureType,AHint);
  499. AssertEquals('Argument count',1,Proc.Args.Count);
  500. A:=CheckArgument(0,'A','',argOut);
  501. AssertNull('No argument type', A.ArgType)
  502. end;
  503. procedure TTestProcedureTypeParser.DoTestProcedureUntypedDefArg;
  504. begin
  505. ParseType('procedure(A)',ccdefault,TPasProcedureType,'');
  506. end;
  507. procedure TTestProcedureTypeParser.DoTestProcedureOneVarArgDefault(
  508. CC: TCallingConvention; const AHint: String);
  509. Var
  510. A : TPasArgument;
  511. begin
  512. ParseType('procedure(var A : Integer = 1)',CC,TPasProcedureType,AHint);
  513. AssertEquals('Argument count',1,Proc.Args.Count);
  514. A:=CheckArgument(0,'A','Integer',argVar);
  515. AssertNotNull('have default argument type', A.Value);
  516. AssertEquals('argument expr type', TPrimitiveExpr, A.ValueExpr.ClassType);
  517. AssertEquals('argument expr type', '1', TPrimitiveExpr(A.ValueExpr).Value);
  518. end;
  519. procedure TTestProcedureTypeParser.DoTestProcedureOneOutArgDefault(
  520. CC: TCallingConvention; const AHint: String);
  521. Var
  522. A : TPasArgument;
  523. begin
  524. ParseType('procedure(out A : Integer = 1)',CC,TPasProcedureType,AHint);
  525. AssertEquals('Argument count',1,Proc.Args.Count);
  526. A:=CheckArgument(0,'A','Integer',argOut);
  527. AssertNotNull('have default argument type', A.Value);
  528. AssertEquals('argument expr type', TPrimitiveExpr, A.ValueExpr.ClassType);
  529. AssertEquals('argument expr type', '1', TPrimitiveExpr(A.ValueExpr).Value);
  530. end;
  531. procedure TTestProcedureTypeParser.DoTestProcedureOneConstArgDefault(
  532. CC: TCallingConvention; const AHint: String);
  533. Var
  534. A : TPasArgument;
  535. begin
  536. ParseType('procedure(const A : Integer = 1)',CC,TPasProcedureType,AHint);
  537. AssertEquals('Argument count',1,Proc.Args.Count);
  538. A:=CheckArgument(0,'A','Integer',argConst);
  539. AssertNotNull('have default argument type', A.Value);
  540. AssertEquals('argument expr type', TPrimitiveExpr, A.ValueExpr.ClassType);
  541. AssertEquals('argument expr type', '1', TPrimitiveExpr(A.ValueExpr).Value);
  542. end;
  543. procedure TTestProcedureTypeParser.DoTestProcedureArrayOfConst(
  544. CC: TCallingConvention; const AHint: String);
  545. begin
  546. ParseType('procedure(A : Array of const)',CC,TPasProcedureType,AHint);
  547. AssertEquals('Argument count',1,Proc.Args.Count);
  548. CheckArrayOfConstArgument(0,argDefault);
  549. end;
  550. procedure TTestProcedureTypeParser.DoTestProcedureOfObject(
  551. CC: TCallingConvention; const AHint: String);
  552. begin
  553. ParseType('procedure of Object',CC,TPasProcedureType,AHint);
  554. AssertEquals('Argument count',0,Proc.Args.Count);
  555. AssertEquals('Is OF Object',True,Proc.IsOfObject);
  556. end;
  557. procedure TTestProcedureTypeParser.DoTestProcedureOfObjectOneArg(
  558. CC: TCallingConvention; const AHint: String);
  559. begin
  560. ParseType('procedure (A : integer)of Object',CC,TPasProcedureType,AHint);
  561. AssertEquals('Argument count',1,Proc.Args.Count);
  562. AssertEquals('Is OF Object',True,Proc.IsOfObject);
  563. CheckArgument(0,'A','Integer',argDefault);
  564. end;
  565. procedure TTestProcedureTypeParser.DoTestProcedureIsNested(
  566. CC: TCallingConvention; const AHint: String);
  567. begin
  568. ParseType('procedure is nested',CC,TPasProcedureType,AHint);
  569. AssertEquals('Argument count',0,Proc.Args.Count);
  570. AssertEquals('Is nested',True,Proc.IsNested);
  571. end;
  572. procedure TTestProcedureTypeParser.DoTestProcedureIsNestedOneArg(
  573. CC: TCallingConvention; const AHint: String);
  574. begin
  575. ParseType('procedure (A : integer) is nested',CC,TPasProcedureType,AHint);
  576. AssertEquals('Argument count',1,Proc.Args.Count);
  577. AssertEquals('Is nested',True,Proc.IsNested);
  578. CheckArgument(0,'A','Integer',argDefault);
  579. end;
  580. procedure TTestProcedureTypeParser.CheckArrayOfConstArgument(Aindex : Integer; Ac : TArgumentAccess);
  581. Var
  582. A : TPasArgument;
  583. T : TPasArrayType;
  584. begin
  585. A:=CheckArgument(Aindex,'A','',ac);
  586. AssertEquals('ArrayType',TPasArrayType,A.ArgType.ClassType);
  587. T:=A.ArgType as TPasArrayType;
  588. AssertNull('Have Element type',T.ElType);
  589. end;
  590. procedure TTestProcedureTypeParser.DoTestFunction(CC: TCallingConvention;
  591. const AHint: String);
  592. begin
  593. ParseType('function : integer',CC,TPasFunctionType,AHint);
  594. AssertEquals('Argument count',0,Proc.Args.Count);
  595. AssertEquals('Is OF Object',False,Proc.IsOfObject);
  596. AssertNotNull('Have result',FuncProc.ResultEl);
  597. AssertEquals('Result type class',TPasResultElement,FuncProc.ResultEl.ClassType);
  598. AssertNotNull('Have result',FuncProc.ResultEl.ResultType);
  599. AssertEquals('Result type element class ',TPasUnresolvedTypeRef,FuncProc.ResultEl.ResultType.ClassType);
  600. AssertEquals('Result type element name','Integer',FuncProc.ResultEl.ResultType.Name);
  601. end;
  602. procedure TTestProcedureTypeParser.DoTestFunctionOfObject(CC: TCallingConvention;
  603. const AHint: String);
  604. begin
  605. ParseType('function : integer of object',CC,TPasFunctionType,AHint);
  606. AssertEquals('Argument count',0,Proc.Args.Count);
  607. AssertEquals('Is OF Object',True,Proc.IsOfObject);
  608. AssertNotNull('Have result',FuncProc.ResultEl);
  609. AssertEquals('Result type class',TPasResultElement,FuncProc.ResultEl.ClassType);
  610. AssertNotNull('Have result',FuncProc.ResultEl.ResultType);
  611. AssertEquals('Result type element class ',TPasUnresolvedTypeRef,FuncProc.ResultEl.ResultType.ClassType);
  612. AssertEquals('Result type element name','Integer',FuncProc.ResultEl.ResultType.Name);
  613. end;
  614. procedure TTestProcedureTypeParser.DoTestFunctionOneArg(CC: TCallingConvention;
  615. const AHint: String);
  616. begin
  617. ParseType('function (A : Integer) : Integer',CC,TPasFunctionType,AHint);
  618. AssertEquals('Argument count',1,Proc.Args.Count);
  619. CheckArgument(0,'A','Integer',argDefault);
  620. AssertNotNull('Have result',FuncProc.ResultEl);
  621. AssertEquals('Result type class',TPasResultElement,FuncProc.ResultEl.ClassType);
  622. AssertNotNull('Have result',FuncProc.ResultEl.ResultType);
  623. AssertEquals('Result type element class ',TPasUnresolvedTypeRef,FuncProc.ResultEl.ResultType.ClassType);
  624. AssertEquals('Result type element name','Integer',FuncProc.ResultEl.ResultType.Name);
  625. end;
  626. procedure TTestProcedureTypeParser.DoTestFunctionOneArgOfObject(
  627. CC: TCallingConvention; const AHint: String);
  628. begin
  629. ParseType('function (A : Integer) : Integer of object',CC,TPasFunctionType,AHint);
  630. AssertEquals('Argument count',1,Proc.Args.Count);
  631. AssertEquals('Is OF Object',True,Proc.IsOfObject);
  632. CheckArgument(0,'A','Integer',argDefault);
  633. AssertNotNull('Have result',FuncProc.ResultEl);
  634. AssertEquals('Result type class',TPasResultElement,FuncProc.ResultEl.ClassType);
  635. AssertNotNull('Have result',FuncProc.ResultEl.ResultType);
  636. AssertEquals('Result type element class ',TPasUnresolvedTypeRef,FuncProc.ResultEl.ResultType.ClassType);
  637. AssertEquals('Result type element name','Integer',FuncProc.ResultEl.ResultType.Name);
  638. end;
  639. procedure TTestProcedureTypeParser.CheckOpenArrayArgument(Ac : TArgumentAccess);
  640. Var
  641. A : TPasArgument;
  642. T : TPasArrayType;
  643. begin
  644. A:=CheckArgument(0,'A','',ac);
  645. AssertEquals('ArrayType',TPasArrayType,A.ArgType.ClassType);
  646. T:=A.ArgType as TPasArrayType;
  647. AssertNotNull('Have Element type',T.ElType);
  648. AssertEquals('Element type',TPasUnresolvedTypeRef,T.ElType.ClassType);
  649. AssertEquals('Element type name','Integer',TPasUnresolvedTypeRef(T.ElType).Name);
  650. AssertEquals('No boundaries','',T.IndexRange);
  651. end;
  652. procedure TTestProcedureTypeParser.DoTestProcedureOpenArray(
  653. CC: TCallingConvention; const AHint: String);
  654. begin
  655. ParseType('procedure(A : Array of integer)',CC,TPasProcedureType,AHint);
  656. AssertEquals('Argument count',1,Proc.Args.Count);
  657. CheckOpenArrayArgument(argDefault);
  658. end;
  659. procedure TTestProcedureTypeParser.DoTestProcedureConstOpenArray(
  660. CC: TCallingConvention; const AHint: String);
  661. begin
  662. ParseType('procedure(const A : Array of integer)',CC,TPasProcedureType,AHint);
  663. AssertEquals('Argument count',1,Proc.Args.Count);
  664. CheckOpenArrayArgument(argConst);
  665. end;
  666. procedure TTestProcedureTypeParser.DoTestProcedureVarOpenArray(
  667. CC: TCallingConvention; const AHint: String);
  668. begin
  669. ParseType('procedure(var A : Array of integer)',CC,TPasProcedureType,AHint);
  670. AssertEquals('Argument count',1,Proc.Args.Count);
  671. CheckOpenArrayArgument(argVar);
  672. end;
  673. procedure TTestProcedureTypeParser.DoTestProcedureOutOpenArray(
  674. CC: TCallingConvention; const AHint: String);
  675. begin
  676. ParseType('procedure(out A : Array of integer)',CC,TPasProcedureType,AHint);
  677. AssertEquals('Argument count',1,Proc.Args.Count);
  678. CheckOpenArrayArgument(argOut);
  679. end;
  680. procedure TTestProcedureTypeParser.DoTestProcedureOneArgDefault(
  681. CC: TCallingConvention; const AHint: String);
  682. Var
  683. A : TPasArgument;
  684. begin
  685. ParseType('procedure(A : Integer = 1)',CC,TPasProcedureType,AHint);
  686. AssertEquals('Argument count',1,Proc.Args.Count);
  687. A:=CheckArgument(0,'A','Integer',argDefault);
  688. AssertNotNull('have default argument type', A.ValueExpr);
  689. AssertEquals('argument expr type', TPrimitiveExpr, A.ValueExpr.ClassType);
  690. AssertEquals('argument expr value', '1', TPrimitiveExpr(A.ValueExpr).Value);
  691. end;
  692. procedure TTestProcedureTypeParser.DoTestProcedureOneArgDefaultExpr(
  693. CC: TCallingConvention; const AHint: String);
  694. Var
  695. A : TPasArgument;
  696. B : TBinaryExpr;
  697. begin
  698. ParseType('procedure(A : Integer = 1+2)',CC,TPasProcedureType,AHint);
  699. AssertEquals('Argument count',1,Proc.Args.Count);
  700. A:=CheckArgument(0,'A','Integer',argDefault);
  701. AssertNotNull('have default argument type', A.ValueExpr);
  702. AssertEquals('argument expr type', TBinaryExpr, A.ValueExpr.ClassType);
  703. B:=TBinaryExpr(A.ValueExpr);
  704. AssertNotNull('have left expr', B.Left);
  705. AssertEquals('argument left expr type', TPrimitiveExpr, B.Left.ClassType);
  706. AssertEquals('argument left expr value', '1', TPrimitiveExpr(B.Left).Value);
  707. AssertNotNull('have right expr', B.Right);
  708. AssertEquals('argument right expr type', TPrimitiveExpr, B.Right.ClassType);
  709. AssertEquals('argument right expr value', '2', TPrimitiveExpr(B.Right).Value);
  710. TAssert.AssertSame('B.left.parent=B',B,B.Left.Parent);
  711. TAssert.AssertSame('B.right.parent=B',B,B.Right.Parent);
  712. end;
  713. procedure TTestProcedureTypeParser.DoTestProcedureOneArgDefaultSet(
  714. CC: TCallingConvention; const AHint: String);
  715. Var
  716. A : TPasArgument;
  717. B : TParamsExpr;
  718. begin
  719. ParseType('procedure(A : TB = [])',CC,TPasProcedureType,AHint);
  720. AssertEquals('Argument count',1,Proc.Args.Count);
  721. A:=CheckArgument(0,'A','TB',argDefault);
  722. AssertNotNull('have default argument type', A.ValueExpr);
  723. AssertEquals('argument expr type', TParamsExpr, A.ValueExpr.ClassType);
  724. B:=TParamsExpr(A.ValueExpr);
  725. AssertEquals('No params',0,Length(B.Params));
  726. end;
  727. function TTestProcedureTypeParser.CheckArgument(AIndex: Integer; Const AName,
  728. ATypeName: String; AAccess: TArgumentAccess): TPasArgument;
  729. Var
  730. A : TPasArgument;
  731. C : String;
  732. begin
  733. C:='Argument '+IntToStr(AIndex)+' : ';
  734. AssertNotNull(C+'assigned',Proc.Args[AIndex]);
  735. AssertEquals(C+'class',TPasArgument,TObject(Proc.Args[AIndex]).ClassType);
  736. A:=TPasArgument(Proc.Args[AIndex]);
  737. AssertEquals(C+'Access',AAccess,A.Access);
  738. AssertEquals(C+'name',AName,A.Name);
  739. if (ATypeName<>'') then
  740. begin
  741. AssertNotNull(C+'type assigned',A.ArgType);
  742. if (ATypeName[1]='[') then
  743. AssertEquals(C+'type classname',LowerCase(Copy(ATypeName,2,Length(ATypeName)-2)),LowerCase(A.ArgType.ClassName))
  744. else
  745. AssertEquals(C+'type name',ATypeName,A.ArgType.Name);
  746. end;
  747. Result:=A;
  748. end;
  749. Function TTestProcedureTypeParser.ParseType(ASource: String;
  750. CC: TCallingConvention; ATypeClass: TClass; Const AHint: String
  751. ): TPasProcedureType;
  752. Var
  753. CCS : String;
  754. begin
  755. if CC=ccdefault then
  756. Result:=TPasProcedureType(ParseType(ASource,ATypeClass,AHint))
  757. else
  758. begin
  759. CCS:=cCallingConventions[CC];
  760. if (AHint<>'') then
  761. Result:=TPasProcedureType(ParseType(ASource+';' +CCS+';',ATypeClass,AHint))
  762. else
  763. Result:=TPasProcedureType(ParseType(ASource+';' +CCS,ATypeClass,AHint));
  764. end;
  765. FProc:=Result;
  766. AssertEquals('Correct calling convention for procedural type',cc,Result.CallingConvention);
  767. end;
  768. Procedure TTestProcedureTypeParser.DoTestProcedureDecl(CC: TCallingConvention;
  769. Const AHint: String);
  770. begin
  771. ParseType('procedure',CC,TPasProcedureType,AHint);
  772. AssertEquals('Argument count',0,Proc.Args.Count);
  773. if AddComment then
  774. AssertComment;
  775. end;
  776. Procedure TTestProcedureTypeParser.DoTestProcedureOneArgDecl(
  777. CC: TCallingConvention; Const AHint: String);
  778. begin
  779. ParseType('procedure(A : Integer)',CC,TPasProcedureType,AHint);
  780. AssertEquals('Argument count',1,Proc.Args.Count);
  781. CheckArgument(0,'A','Integer',argDefault);
  782. end;
  783. Procedure TTestProcedureTypeParser.DoTestProcedureOneVarArgDecl(
  784. CC: TCallingConvention; Const AHint: String);
  785. begin
  786. ParseType('procedure(var A : Integer)',CC,TPasProcedureType,AHint);
  787. AssertEquals('Argument count',1,Proc.Args.Count);
  788. CheckArgument(0,'A','Integer',argVar);
  789. end;
  790. Procedure TTestProcedureTypeParser.DoTestProcedureOneConstArgDecl(
  791. CC: TCallingConvention; Const AHint: String);
  792. begin
  793. ParseType('procedure(const A : Integer)',CC,TPasProcedureType,AHint);
  794. AssertEquals('Argument count',1,Proc.Args.Count);
  795. CheckArgument(0,'A','Integer',argConst);
  796. end;
  797. Procedure TTestProcedureTypeParser.DoTestProcedureOneOutArgDecl(
  798. CC: TCallingConvention; Const AHint: String);
  799. begin
  800. ParseType('procedure(out A : Integer)',CC,TPasProcedureType,AHint);
  801. AssertEquals('Argument count',1,Proc.Args.Count);
  802. CheckArgument(0,'A','Integer',argOut);
  803. end;
  804. Procedure TTestProcedureTypeParser.DoTestProcedureTwoArgsDecl(
  805. CC: TCallingConvention; Const AHint: String);
  806. begin
  807. ParseType('procedure(A : Integer;B : String)',CC,TPasProcedureType,AHint);
  808. AssertEquals('Argument count',2,Proc.Args.Count);
  809. CheckArgument(0,'A','Integer',argDefault);
  810. CheckArgument(1,'B','[TPasAliasType]',argDefault);
  811. end;
  812. Procedure TTestProcedureTypeParser.DoTestProcedureTwoVarArgsDecl(
  813. CC: TCallingConvention; Const AHint: String);
  814. begin
  815. ParseType('procedure(Var A : Integer;Var B : String)',CC,TPasProcedureType,AHint);
  816. AssertEquals('Argument count',2,Proc.Args.Count);
  817. CheckArgument(0,'A','Integer',argVar);
  818. CheckArgument(1,'B','[TPasAliasType]',argVar);
  819. end;
  820. Procedure TTestProcedureTypeParser.DoTestProcedureTwoConstArgsDecl(
  821. CC: TCallingConvention; Const AHint: String);
  822. begin
  823. ParseType('procedure(const A : Integer;Const B : String)',CC,TPasProcedureType,AHint);
  824. AssertEquals('Argument count',2,Proc.Args.Count);
  825. CheckArgument(0,'A','Integer',argConst);
  826. CheckArgument(1,'B','[TPasAliasType]',argConst);
  827. end;
  828. Procedure TTestProcedureTypeParser.DoTestProcedureTwoOutArgsDecl(
  829. CC: TCallingConvention; Const AHint: String);
  830. begin
  831. ParseType('procedure(out A : Integer;Out B : String)',CC,TPasProcedureType,AHint);
  832. AssertEquals('Argument count',2,Proc.Args.Count);
  833. CheckArgument(0,'A','Integer',argOut);
  834. CheckArgument(1,'B','[TPasAliasType]',argOut);
  835. end;
  836. Procedure TTestProcedureTypeParser.DoTestProcedureTwoCombinedArgsDecl(
  837. CC: TCallingConvention; Const AHint: String);
  838. begin
  839. ParseType('procedure(A,B : Integer)',CC,TPasProcedureType,AHint);
  840. AssertEquals('Argument count',2,Proc.Args.Count);
  841. CheckArgument(0,'A','Integer',argDefault);
  842. CheckArgument(1,'B','Integer',argDefault);
  843. end;
  844. Procedure TTestProcedureTypeParser.DoTestProcedureTwoCombinedVarArgsDecl(
  845. CC: TCallingConvention; Const AHint: String);
  846. begin
  847. ParseType('procedure(Var A,B : Integer)',CC,TPasProcedureType,AHint);
  848. AssertEquals('Argument count',2,Proc.Args.Count);
  849. CheckArgument(0,'A','Integer',argVar);
  850. CheckArgument(1,'B','Integer',argVar);
  851. end;
  852. Procedure TTestProcedureTypeParser.DoTestProcedureTwoCombinedConstArgsDecl(
  853. CC: TCallingConvention; Const AHint: String);
  854. begin
  855. ParseType('procedure(Const A,B : Integer)',CC,TPasProcedureType,AHint);
  856. AssertEquals('Argument count',2,Proc.Args.Count);
  857. CheckArgument(0,'A','Integer',argConst);
  858. CheckArgument(1,'B','Integer',argConst);
  859. end;
  860. Procedure TTestProcedureTypeParser.DoTestProcedureTwoCombinedOutArgsDecl(
  861. CC: TCallingConvention; Const AHint: String);
  862. begin
  863. ParseType('procedure(Out A,B : Integer)',CC,TPasProcedureType,AHint);
  864. AssertEquals('Argument count',2,Proc.Args.Count);
  865. CheckArgument(0,'A','Integer',argOut);
  866. CheckArgument(1,'B','Integer',argOut);
  867. end;
  868. Procedure TTestProcedureTypeParser.DoTestProcedureDefaultConstArgsDecl(
  869. CC: TCallingConvention; Const AHint: String);
  870. begin
  871. ParseType('procedure(A : Integer; Const B : Integer)',CC,TPasProcedureType,AHint);
  872. AssertEquals('Argument count',2,Proc.Args.Count);
  873. CheckArgument(0,'A','Integer',argDefault);
  874. CheckArgument(1,'B','Integer',argConst);
  875. end;
  876. Procedure TTestProcedureTypeParser.TestCallingConventions(
  877. Proc: TCallingConventionTest; Const AHint: String);
  878. Var
  879. CC : TCallingConvention;
  880. begin
  881. For cc:=ccDefault to High(TCallingConvention) do
  882. begin
  883. if CC<>ccDefault then
  884. Setup;
  885. try
  886. Proc(cc,AHint);
  887. finally
  888. tearDown;
  889. end;
  890. end;
  891. end;
  892. Procedure TTestProcedureTypeParser.TestCallingConventions(
  893. Proc: TCallingConventionTest);
  894. begin
  895. TestCallingConventions(Proc,'');
  896. Setup;
  897. TestCallingConventions(Proc,'deprecated');
  898. Setup;
  899. TestCallingConventions(Proc,'platform');
  900. end;
  901. Function TTestProcedureTypeParser.FuncProc: TPasFunctionType;
  902. begin
  903. Result:=Proc as TPasFunctionType;
  904. end;
  905. Procedure TTestProcedureTypeParser.TestProcedure;
  906. begin
  907. TestCallingConventions(@DoTestProcedureDecl);
  908. end;
  909. Procedure TTestProcedureTypeParser.TestProcedureComment;
  910. begin
  911. AddComment:=True;
  912. TestCallingConventions(@DoTestProcedureDecl);
  913. end;
  914. Procedure TTestProcedureTypeParser.TestProcedureOneArg;
  915. begin
  916. TestCallingConventions(@DoTestProcedureOneArgDecl);
  917. end;
  918. Procedure TTestProcedureTypeParser.TestProcedureOneVarArg;
  919. begin
  920. TestCallingConventions(@DoTestProcedureOneVarArgDecl);
  921. end;
  922. Procedure TTestProcedureTypeParser.TestProcedureOneConstArg;
  923. begin
  924. TestCallingConventions(@DoTestProcedureOneConstArgDecl);
  925. end;
  926. Procedure TTestProcedureTypeParser.TestProcedureOneOutArg;
  927. begin
  928. TestCallingConventions(@DoTestProcedureOneOutArgDecl);
  929. end;
  930. Procedure TTestProcedureTypeParser.TestProcedureTwoArgs;
  931. begin
  932. TestCallingConventions(@DoTestProcedureTwoArgsDecl);
  933. end;
  934. Procedure TTestProcedureTypeParser.TestProcedureTwoVarArgs;
  935. begin
  936. TestCallingConventions(@DoTestProcedureTwoVarArgsDecl);
  937. end;
  938. Procedure TTestProcedureTypeParser.TestProcedureTwoConstArgs;
  939. begin
  940. TestCallingConventions(@DoTestProcedureTwoConstArgsDecl);
  941. end;
  942. Procedure TTestProcedureTypeParser.TestProcedureTwoOutArgs;
  943. begin
  944. TestCallingConventions(@DoTestProcedureTwoOutArgsDecl);
  945. end;
  946. Procedure TTestProcedureTypeParser.TestProcedureTwoCombinedArgs;
  947. begin
  948. TestCallingConventions(@DoTestProcedureTwoCombinedArgsDecl);
  949. end;
  950. Procedure TTestProcedureTypeParser.TestProcedureTwoCombinedVarArgs;
  951. begin
  952. TestCallingConventions(@DoTestProcedureTwoCombinedVarArgsDecl);
  953. end;
  954. Procedure TTestProcedureTypeParser.TestProcedureTwoCombinedConstArgs;
  955. begin
  956. TestCallingConventions(@DoTestProcedureTwoCombinedConstArgsDecl);
  957. end;
  958. Procedure TTestProcedureTypeParser.TestProcedureTwoCombinedOutArgs;
  959. begin
  960. TestCallingConventions(@DoTestProcedureTwoCombinedOutArgsDecl);
  961. end;
  962. Procedure TTestProcedureTypeParser.TestProcedureDefaultConstArgs;
  963. begin
  964. TestCallingConventions(@DoTestProcedureDefaultConstArgsDecl);
  965. end;
  966. Procedure TTestProcedureTypeParser.TestProcedureUntypedArg;
  967. begin
  968. TestCallingConventions(@DoTestProcedureUntypedArgDecl);
  969. end;
  970. Procedure TTestProcedureTypeParser.TestProcedureUntypedConstArg;
  971. begin
  972. TestCallingConventions(@DoTestProcedureUntypedConstArgDecl);
  973. end;
  974. Procedure TTestProcedureTypeParser.TestProcedureUntypedOutArg;
  975. begin
  976. TestCallingConventions(@DoTestProcedureUntypedOutArgDecl);
  977. end;
  978. Procedure TTestProcedureTypeParser.TestProcedureUntypedDefArg;
  979. begin
  980. AssertException('No untyped arg by value',EParserError,@DoTestProcedureUntypedDefArg)
  981. end;
  982. Procedure TTestProcedureTypeParser.TestProcedureOneArgDefault;
  983. begin
  984. TestCallingConventions(@DoTestProcedureOneArgDefault);
  985. end;
  986. Procedure TTestProcedureTypeParser.TestProcedureOneArgDefaultExpr;
  987. begin
  988. TestCallingConventions(@DoTestProcedureOneArgDefaultExpr);
  989. end;
  990. Procedure TTestProcedureTypeParser.TestProcedureOneArgDefaultSet;
  991. begin
  992. TestCallingConventions(@DoTestProcedureOneArgDefaultSet);
  993. end;
  994. Procedure TTestProcedureTypeParser.TestProcedureOneVarArgDefault;
  995. begin
  996. TestCallingConventions(@DoTestProcedureOneVarArgDefault);
  997. end;
  998. Procedure TTestProcedureTypeParser.TestProcedureOneConstArgDefault;
  999. begin
  1000. TestCallingConventions(@DoTestProcedureOneConstArgDefault);
  1001. end;
  1002. Procedure TTestProcedureTypeParser.TestProcedureOneOutArgDefault;
  1003. begin
  1004. TestCallingConventions(@DoTestProcedureOneOutArgDefault);
  1005. end;
  1006. Procedure TTestProcedureTypeParser.TestProcedureNoMultiArgDefaults;
  1007. begin
  1008. AssertParseTypeError('procedure (A,B : Integer = 1)');
  1009. end;
  1010. Procedure TTestProcedureTypeParser.TestProcedureOpenArray;
  1011. begin
  1012. TestCallingConventions(@DoTestProcedureOpenArray);
  1013. end;
  1014. Procedure TTestProcedureTypeParser.TestProcedureConstOpenArray;
  1015. begin
  1016. TestCallingConventions(@DoTestProcedureConstOpenArray);
  1017. end;
  1018. Procedure TTestProcedureTypeParser.TestProcedureOutOpenArray;
  1019. begin
  1020. TestCallingConventions(@DoTestProcedureVarOpenArray);
  1021. end;
  1022. Procedure TTestProcedureTypeParser.TestProcedureVarOpenArray;
  1023. begin
  1024. TestCallingConventions(@DoTestProcedureOutOpenArray);
  1025. end;
  1026. Procedure TTestProcedureTypeParser.TestProcedureArrayOfConst;
  1027. begin
  1028. TestCallingConventions(@DoTestProcedureArrayOfConst);
  1029. end;
  1030. procedure TTestProcedureTypeParser.TestProcedureReference;
  1031. begin
  1032. ParseType('reference to procedure',ccDefault,TPasProcedureType);
  1033. AssertEquals('Argument count',0,Proc.Args.Count);
  1034. AssertEquals('Is Reference to',True,Proc.IsReferenceTo);
  1035. end;
  1036. Procedure TTestProcedureTypeParser.TestProcedureOfObject;
  1037. begin
  1038. TestCallingConventions(@DoTestProcedureOfObject);
  1039. end;
  1040. Procedure TTestProcedureTypeParser.TestProcedureOfObjectOneArg;
  1041. begin
  1042. TestCallingConventions(@DoTestProcedureOfObjectOneArg);
  1043. end;
  1044. Procedure TTestProcedureTypeParser.TestProcedureIsNested;
  1045. begin
  1046. TestCallingConventions(@DoTestProcedureIsNested);
  1047. end;
  1048. Procedure TTestProcedureTypeParser.TestProcedureIsNesteOneArg;
  1049. begin
  1050. TestCallingConventions(@DoTestProcedureIsNestedOneArg);
  1051. end;
  1052. Procedure TTestProcedureTypeParser.TestFunction;
  1053. begin
  1054. TestCallingConventions(@DoTestFunction);
  1055. end;
  1056. Procedure TTestProcedureTypeParser.TestFunctionOneArg;
  1057. begin
  1058. TestCallingConventions(@DoTestFunctionOneArg);
  1059. end;
  1060. Procedure TTestProcedureTypeParser.TestFunctionOfObject;
  1061. begin
  1062. TestCallingConventions(@DoTestFunctionOfObject);
  1063. end;
  1064. Procedure TTestProcedureTypeParser.TestFunctionOneArgOfObject;
  1065. begin
  1066. TestCallingConventions(@DoTestFunctionOneArgOfObject);
  1067. end;
  1068. procedure TTestProcedureTypeParser.TestCBlock;
  1069. begin
  1070. ParseType('reference to procedure (a: integer); cblock;',TPasProcedureType,'');
  1071. FProc:=Definition as TPasProcedureType;
  1072. AssertEquals('Argument count',1,Proc.Args.Count);
  1073. AssertEquals('Is cblock',True,ptmCblock in Proc.Modifiers);
  1074. end;
  1075. procedure TTestProcedureTypeParser.TestMacPasoutArg;
  1076. begin
  1077. Parser.CurrentModeswitches:=[msMac];
  1078. ParseType('procedure (out: integer); ',TPasProcedureType,'');
  1079. FProc:=Definition as TPasProcedureType;
  1080. AssertEquals('Argument count',1,Proc.Args.Count);
  1081. end;
  1082. procedure TTestProcedureTypeParser.TestMacPasPropertyArg;
  1083. begin
  1084. Parser.CurrentModeswitches:=[msMac];
  1085. ParseType('procedure (property : integer); ',TPasProcedureType,'');
  1086. FProc:=Definition as TPasProcedureType;
  1087. AssertEquals('Argument count',1,Proc.Args.Count);
  1088. end;
  1089. procedure TTestProcedureTypeParser.TestMacPasPropertyVarArg;
  1090. begin
  1091. Parser.CurrentModeswitches:=[msMac];
  1092. ParseType('procedure (var property : integer); ',TPasProcedureType,'');
  1093. FProc:=Definition as TPasProcedureType;
  1094. AssertEquals('Argument count',1,Proc.Args.Count);
  1095. end;
  1096. procedure TTestProcedureTypeParser.TestMacPasClassArg;
  1097. begin
  1098. Parser.CurrentModeswitches:=[msMac];
  1099. ParseType('procedure (class : integer); ',TPasProcedureType,'');
  1100. FProc:=Definition as TPasProcedureType;
  1101. AssertEquals('Argument count',1,Proc.Args.Count);
  1102. end;
  1103. { TTestRecordTypeParser }
  1104. function TTestRecordTypeParser.GetC(AIndex: Integer): TPasConst;
  1105. begin
  1106. Result:=TObject(TheRecord.Members[AIndex]) as TPasConst;
  1107. end;
  1108. function TTestRecordTypeParser.GetField(AIndex: Integer; R: TPasRecordType
  1109. ): TPasVariable;
  1110. begin
  1111. AssertNotNull(R);
  1112. AssertNotNull(R.Members);
  1113. AssertTrue('Have AIndex elements',R.Members.Count>AIndex);
  1114. AssertEquals('Correct class in member',TPasVariable,TObject(R.Members[AIndex]).ClassType);
  1115. Result:=TPasVariable(R.Members[AIndex]);
  1116. end;
  1117. function TTestRecordTypeParser.GetField(AIndex: Integer; R: TPasVariant
  1118. ): TPasVariable;
  1119. begin
  1120. AssertNotNull(R);
  1121. AssertNotNull('Have variant members', R.Members);
  1122. AssertNotNull('Have variant members member list',R.Members.Members);
  1123. AssertTrue('Have AIndex elements',R.Members.Members.Count>AIndex);
  1124. AssertEquals('Correct class in member',TPasVariable,TObject(R.Members.members[AIndex]).ClassType);
  1125. Result:=TPasVariable(R.Members.Members[AIndex]);
  1126. end;
  1127. function TTestRecordTypeParser.GetF(AIndex: Integer): TPasVariable;
  1128. begin
  1129. Result:=GetField(AIndex,TheRecord);
  1130. end;
  1131. function TTestRecordTypeParser.GetM(AIndex : Integer): TPasElement;
  1132. begin
  1133. AssertNotNull('Have Record',TheRecord);
  1134. if (AIndex>=TheRecord.Members.Count) then
  1135. Fail('No member '+IntToStr(AIndex));
  1136. AssertNotNull('Have member'+IntToStr(AIndex),TheRecord.Members[AIndex]);
  1137. If Not (TObject(TheRecord.Members[AIndex]) is TPasElement) then
  1138. Fail('Member '+IntTostr(AIndex)+' is not a TPasElement');
  1139. Result:=TPasElement(TheRecord.Members[AIndex])
  1140. end;
  1141. function TTestRecordTypeParser.GetVariant(AIndex: Integer; R: TPasRecordType
  1142. ): TPasVariant;
  1143. begin
  1144. AssertNotNull(R);
  1145. AssertNotNull(R.Variants);
  1146. AssertTrue('Have AIndex variant elements',R.Variants.Count>AIndex);
  1147. AssertEquals('Correct class in variant',TPasVariant,TObject(R.Variants[AIndex]).ClassType);
  1148. Result:=TPasVariant(R.Variants[AIndex]);
  1149. end;
  1150. function TTestRecordTypeParser.GetV(AIndex: Integer): TPasVariant;
  1151. begin
  1152. Result:=GetVariant(AIndex,TheRecord);
  1153. end;
  1154. procedure TTestRecordTypeParser.SetUp;
  1155. begin
  1156. inherited SetUp;
  1157. FDecl:=TStringList.Create;
  1158. FStarted:=false;
  1159. FEnded:=false;
  1160. end;
  1161. procedure TTestRecordTypeParser.TearDown;
  1162. begin
  1163. FreeAndNil(FDecl);
  1164. inherited TearDown;
  1165. end;
  1166. procedure TTestRecordTypeParser.StartRecord(Advanced: boolean);
  1167. var
  1168. S: String;
  1169. begin
  1170. if FStarted then
  1171. Fail('TTestRecordTypeParser.StartRecord already started');
  1172. FStarted:=True;
  1173. S:='TMyRecord = record';
  1174. if Advanced then
  1175. S:='{$modeswitch advancedrecords}'+sLineBreak+S;
  1176. FDecl.Add(S);
  1177. end;
  1178. procedure TTestRecordTypeParser.EndRecord(AEnd: String);
  1179. begin
  1180. if FEnded then exit;
  1181. if not FStarted then
  1182. StartRecord;
  1183. FEnded:=True;
  1184. if (AEnd<>'') then
  1185. FDecl.Add(' '+AEnd);
  1186. end;
  1187. procedure TTestRecordTypeParser.AddMember(S: String);
  1188. begin
  1189. if Not FStarted then
  1190. StartRecord;
  1191. FDecl.Add(' '+S);
  1192. end;
  1193. procedure TTestRecordTypeParser.ParseRecord;
  1194. begin
  1195. DoParseRecord;
  1196. end;
  1197. procedure TTestRecordTypeParser.ParseRecordFail(Msg: string; MsgNumber: integer
  1198. );
  1199. var
  1200. ok: Boolean;
  1201. begin
  1202. ok:=false;
  1203. try
  1204. ParseRecord;
  1205. except
  1206. on E: EParserError do
  1207. begin
  1208. AssertEquals('Expected {'+Msg+'} '+IntToStr(MsgNumber)+', but got msg {'+Parser.LastMsg+'} '+IntToStr(Parser.LastMsgNumber),MsgNumber,Parser.LastMsgNumber);
  1209. AssertEquals('Expected {'+Msg+'}, but got msg {'+Parser.LastMsg+'}',Msg,Parser.LastMsg);
  1210. ok:=true;
  1211. end;
  1212. end;
  1213. AssertEquals('Missing parser error {'+Msg+'} ('+IntToStr(MsgNumber)+')',true,ok);
  1214. end;
  1215. procedure TTestRecordTypeParser.DoParseRecord;
  1216. begin
  1217. EndRecord;
  1218. Add('Type');
  1219. if AddComment then
  1220. begin
  1221. Add('// A comment');
  1222. Engine.NeedComments:=True;
  1223. end;
  1224. Add(' '+TrimRight(FDecl.Text)+';');
  1225. ParseDeclarations;
  1226. AssertEquals('One record type definition',1,Declarations.Types.Count);
  1227. AssertEquals('First declaration is type definition.',TPasRecordType,TObject(Declarations.Types[0]).ClassType);
  1228. FRecord:=TObject(Declarations.Types[0]) as TPasRecordType;
  1229. TheType:=FRecord; // needed by AssertComment
  1230. Definition:=TheType; // needed by CheckHint
  1231. if TheRecord.Members.Count>0 then
  1232. FMember1:=TObject(TheRecord.Members[0]) as TPasElement;
  1233. end;
  1234. procedure TTestRecordTypeParser.TestFields(const Fields: array of string;
  1235. AHint: String; HaveVariant: Boolean);
  1236. Var
  1237. S : String;
  1238. I : integer;
  1239. begin
  1240. StartRecord;
  1241. For I:=Low(Fields) to High(Fields) do
  1242. AddMember(Fields[i]);
  1243. S:='end';
  1244. if AHint<>'' then
  1245. S:=S+' '+AHint;
  1246. EndRecord(S);
  1247. ParseRecord;
  1248. if HaveVariant then
  1249. begin
  1250. AssertNotNull('Have variants',TheRecord.Variants);
  1251. AssertNotNull('Have variant type',TheRecord.VariantEl);
  1252. end
  1253. else
  1254. begin
  1255. AssertNull('No variants',TheRecord.Variants);
  1256. AssertNull('No variant element',TheRecord.VariantEl);
  1257. end;
  1258. if AddComment then
  1259. AssertComment;
  1260. if (AHint<>'') then
  1261. CheckHint(TPasMemberHint(GetEnumValue(TypeInfo(TPasMemberHint),'h'+AHint)));
  1262. end;
  1263. procedure TTestRecordTypeParser.AssertVariantSelector(AName,AType : string);
  1264. var
  1265. V: TPasVariable;
  1266. begin
  1267. AssertNotNull('Have variant element',TheRecord.VariantEl);
  1268. if AName<>'' then
  1269. begin
  1270. AssertEquals('Have variant variable',TPasVariable,TheRecord.VariantEl.ClassType);
  1271. V:=TPasVariable(TheRecord.VariantEl);
  1272. AssertEquals('Have variant variable name',AName,V.Name);
  1273. AssertNotNull('Have variant var type',V.VarType);
  1274. AssertEquals('Have variant selector type',TPasUnresolvedTypeRef,V.VarType.ClassType);
  1275. AssertEquals('Have variant selector type name',lowercase(AType),lowercase(V.VarType.Name));
  1276. end else begin
  1277. AssertEquals('Have variant selector type',TPasUnresolvedTypeRef,TheRecord.VariantEl.ClassType);
  1278. AssertEquals('Have variant selector type name',lowercase(AType),lowercase(TheRecord.VariantEl.Name));
  1279. end;
  1280. end;
  1281. procedure TTestRecordTypeParser.AssertConst1(Hints: TPasMemberHints;
  1282. Index: integer);
  1283. begin
  1284. if Hints=[] then ;
  1285. AssertEquals('Member '+IntToStr(Index+1)+' type',TPasConst,TObject(TheRecord.Members[Index]).ClassType);
  1286. AssertEquals('Const '+IntToStr(Index+1)+' name','x',Const1.Name);
  1287. AssertNotNull('Have '+IntToStr(Index+1)+' const expr',Const1.Expr);
  1288. end;
  1289. procedure TTestRecordTypeParser.DoTestEmpty(const AHint: String);
  1290. begin
  1291. TestFields([],AHint);
  1292. AssertNotNull('Have members array',TheRecord.Members);
  1293. AssertEquals('Zero members in array',0,TheRecord.Members.Count);
  1294. end;
  1295. procedure TTestRecordTypeParser.AssertVariant1(Hints: TPasMemberHints);
  1296. begin
  1297. AssertVariant1(Hints,['0']);
  1298. end;
  1299. procedure TTestRecordTypeParser.AssertVariant1(Hints: TPasMemberHints;
  1300. VariantLabels: array of string);
  1301. Var
  1302. I : Integer;
  1303. begin
  1304. AssertNotNull('Have variant 1',Variant1);
  1305. AssertNotNull('Variant 1 has Values ',Variant1.Values);
  1306. if Length(VariantLabels)=0 then
  1307. begin
  1308. AssertEquals('Have 1 value',1,Variant1.Values.Count);
  1309. AssertNotNull('Assigned value',Variant1.Values[0]);
  1310. AssertEquals('Expression',TPrimitiveExpr,TObject(Variant1.Values[0]).CLassType);
  1311. AssertExpression('First value is 0',TPasExpr(Variant1.Values[0]),pekNumber,'0');
  1312. end
  1313. else
  1314. begin
  1315. AssertEquals('Have correct number of values',Length(VariantLabels),Variant1.Values.Count);
  1316. For I:=0 to Length(VariantLabels)-1 do
  1317. begin
  1318. AssertEquals(Format('Expression for variant %d',[I]),TPrimitiveExpr,TObject(Variant1.Values[0]).CLassType);
  1319. AssertExpression(Format('Value %d is %s',[i,VariantLabels[i]]),TPasExpr(Variant1.Values[I]),pekNumber,VariantLabels[i]);
  1320. end;
  1321. end;
  1322. AssertNotNull('Have members',Variant1.Members);
  1323. AssertNotNull('Have member members',Variant1.Members.Members);
  1324. AssertNotNull('member 0 not null',Variant1.Members.Members[0]);
  1325. AssertEquals('Member 0 has correct name',TPasVariable,TObject(Variant1.Members.Members[0]).ClassType);
  1326. AssertEquals('Member 0 has correct name','y',TPasVariable(Variant1.Members.Members[0]).Name);
  1327. AssertNotNull('member 0 has not null type',TPasVariable(Variant1.Members.Members[0]).VarType);
  1328. AssertEquals('member 0 has correct type',TPasUnresolvedTypeRef,TPasVariable(Variant1.Members.Members[0]).VarType.ClassType);
  1329. AssertEquals('member 0 has correct type name','Integer',TPasVariable(Variant1.Members.Members[0]).VarType.Name);
  1330. AssertTrue('Field 1 hints match',TPasVariable(Variant1.Members.Members[0]).Hints=Hints)
  1331. end;
  1332. procedure TTestRecordTypeParser.AssertVariant2(Hints: TPasMemberHints);
  1333. begin
  1334. AssertVariant2(Hints,['1']);
  1335. end;
  1336. procedure TTestRecordTypeParser.AssertVariant2(Hints: TPasMemberHints;
  1337. VariantLabels: array of string);
  1338. Var
  1339. I : Integer;
  1340. begin
  1341. AssertNotNull('Have variant 2',Variant2);
  1342. AssertNotNull('Variant 2 has Values ',Variant2.Values);
  1343. if Length(VariantLabels)=0 then
  1344. begin
  1345. AssertEquals('Variant 2 has 1 value',2,Variant2.Values.Count);
  1346. AssertEquals('Expression',TPrimitiveExpr,TObject(Variant2.Values[0]).CLassType);
  1347. AssertExpression('First value is 1',TPasExpr(Variant2.Values[0]),pekNumber,'1');
  1348. end
  1349. else
  1350. begin
  1351. AssertEquals('Variant 2 Has correct number of values',Length(VariantLabels),Variant2.Values.Count);
  1352. For I:=0 to Length(VariantLabels)-1 do
  1353. begin
  1354. AssertEquals(Format('Expression for variant %d',[I]),TPrimitiveExpr,TObject(Variant2.Values[I]).CLassType);
  1355. AssertExpression(Format('Value %d is %s',[i,VariantLabels[i]]),TPasExpr(Variant2.Values[I]),pekNumber,VariantLabels[i]);
  1356. // AssertEquals(Format('Variant 2, Value %d is %s',[i,VariantLabels[i]]),VariantLabels[i],Variant2.Values[I]);
  1357. end;
  1358. end;
  1359. AssertNotNull('Have members',Variant2.Members);
  1360. AssertNotNull('Have member members',Variant2.Members.Members);
  1361. AssertNotNull('member 1 not null',Variant2.Members.Members[0]);
  1362. AssertEquals('Member 1 has correct name',TPasVariable,TObject(Variant2.Members.Members[0]).ClassType);
  1363. AssertEquals('Member 1 has correct name','z',TPasVariable(Variant2.Members.Members[0]).Name);
  1364. AssertNotNull('member 1 has not null type',TPasVariable(Variant2.Members.Members[0]).VarType);
  1365. AssertEquals('member 1 has correct type',TPasUnresolvedTypeRef,TPasVariable(Variant2.Members.Members[0]).VarType.ClassType);
  1366. AssertEquals('member 1 has correct type name','Integer',TPasVariable(Variant2.Members.Members[0]).VarType.Name);
  1367. AssertTrue('Field 1 hints match',TPasVariable(Variant2.Members.Members[0]).Hints=Hints)
  1368. end;
  1369. procedure TTestRecordTypeParser.DoTestVariantNoStorage(const AHint: string);
  1370. begin
  1371. TestFields(['x : integer;','case integer of','0 : (y : integer;)'],AHint,True);
  1372. AssertField1([]);
  1373. AssertVariantSelector('','integer');
  1374. AssertVariant1([]);
  1375. end;
  1376. procedure TTestRecordTypeParser.DoTestDeprecatedVariantNoStorage(
  1377. const AHint: string);
  1378. begin
  1379. TestFields(['x : integer;','case integer of','0 : (y : integer deprecated;)'],AHint,True);
  1380. AssertField1([]);
  1381. AssertVariantSelector('','integer');
  1382. AssertVariant1([hDeprecated]);
  1383. end;
  1384. procedure TTestRecordTypeParser.DoTestDeprecatedVariantStorage(
  1385. const AHint: string);
  1386. begin
  1387. TestFields(['x : integer;','case s : integer of','0 : (y : integer deprecated;)'],AHint,True);
  1388. AssertField1([]);
  1389. AssertVariantSelector('s','integer');
  1390. AssertVariant1([hDeprecated]);
  1391. end;
  1392. procedure TTestRecordTypeParser.DoTestVariantStorage(const AHint: string);
  1393. begin
  1394. TestFields(['x : integer;','case s : integer of','0 : (y : integer;)'],AHint,True);
  1395. AssertField1([]);
  1396. AssertVariantSelector('s','integer');
  1397. AssertVariant1([]);
  1398. end;
  1399. procedure TTestRecordTypeParser.DoTestTwoVariantsNoStorage(const AHint: string);
  1400. begin
  1401. TestFields(['x : integer;','case integer of','0 : (y : integer;);','1 : (z : integer;)'],AHint,True);
  1402. AssertField1([]);
  1403. AssertVariantSelector('','integer');
  1404. AssertVariant1([]);
  1405. AssertVariant2([]);
  1406. end;
  1407. procedure TTestRecordTypeParser.DoTestTwoVariantsStorage(const AHint: string);
  1408. begin
  1409. TestFields(['x : integer;','case s : integer of','0 : (y : integer;);','1 : (z : integer;)'],AHint,True);
  1410. AssertField1([]);
  1411. AssertVariantSelector('s','integer');
  1412. AssertVariant1([]);
  1413. AssertVariant2([]);
  1414. end;
  1415. procedure TTestRecordTypeParser.DoTestTwoVariantsFirstDeprecatedStorage(
  1416. const AHint: string);
  1417. begin
  1418. TestFields(['x : integer;','case s : integer of','0 : (y : integer deprecated;);','1 : (z : integer;)'],AHint,True);
  1419. AssertField1([]);
  1420. AssertVariantSelector('s','integer');
  1421. AssertVariant1([hdeprecated]);
  1422. AssertVariant2([]);
  1423. end;
  1424. procedure TTestRecordTypeParser.DoTestTwoVariantsSecondDeprecatedStorage(
  1425. const AHint: string);
  1426. begin
  1427. TestFields(['x : integer;','case s : integer of','0 : (y : integer ;);','1 : (z : integer deprecated;)'],AHint,True);
  1428. AssertField1([]);
  1429. AssertVariantSelector('s','integer');
  1430. AssertVariant1([]);
  1431. AssertVariant2([hdeprecated]);
  1432. end;
  1433. procedure TTestRecordTypeParser.DoTestVariantTwoLabels(const AHint: string);
  1434. begin
  1435. TestFields(['x : integer;','case integer of','0,1 : (y : integer)'],AHint,True);
  1436. AssertField1([]);
  1437. AssertVariantSelector('','integer');
  1438. AssertVariant1([],['0','1']);
  1439. end;
  1440. procedure TTestRecordTypeParser.DoTestTwoVariantsTwoLabels(const AHint: string);
  1441. begin
  1442. TestFields(['x : integer;','case integer of','0,1 : (y : integer);','2,3 : (z : integer);'],AHint,True);
  1443. AssertField1([]);
  1444. AssertVariantSelector('','integer');
  1445. AssertVariant1([],['0','1']);
  1446. AssertVariant2([],['2','3']);
  1447. end;
  1448. procedure TTestRecordTypeParser.DoTestVariantNestedRecord(const AHint: string);
  1449. begin
  1450. TestFields(['x : integer;','case integer of','0 : ( y : record',' z : integer;','end)'],AHint,True);
  1451. AssertField1([]);
  1452. AssertVariantSelector('','integer');
  1453. AssertRecordVariant(0,[],['0']);
  1454. end;
  1455. procedure TTestRecordTypeParser.DoTestVariantNestedVariant(const AHint: string);
  1456. begin
  1457. TestFields(['x : integer;','case integer of','0 : ( y : record',' z : integer;',' case byte of ',' 1 : (i : integer);',' 2 : ( j : byte)', 'end)'],AHint,True);
  1458. AssertField1([]);
  1459. AssertVariantSelector('','integer');
  1460. AssertRecordVariant(0,[],['0']);
  1461. AssertRecordVariantVariant(0,'i','Integer',[],['1']);
  1462. AssertRecordVariantVariant(1,'j','Byte',[],['2'])
  1463. end;
  1464. procedure TTestRecordTypeParser.DoTestVariantNestedVariantFirstDeprecated(
  1465. const AHint: string);
  1466. begin
  1467. TestFields(['x : integer;','case integer of','0 : ( y : record',' z : integer;',' case byte of ',' 1 : (i : integer deprecated);',' 2 : ( j : byte)', 'end)'],AHint,True);
  1468. AssertField1([]);
  1469. AssertVariantSelector('','integer');
  1470. AssertRecordVariant(0,[],['0']);
  1471. AssertRecordVariantVariant(0,'i','Integer',[hDeprecated],['1']);
  1472. AssertRecordVariantVariant(1,'j','Byte',[],['2'])
  1473. end;
  1474. procedure TTestRecordTypeParser.DoTestVariantNestedVariantSecondDeprecated(
  1475. const AHint: string);
  1476. begin
  1477. TestFields(['x : integer;','case integer of','0 : ( y : record',' z : integer;',' case byte of ',' 1 : (i : integer );',' 2 : ( j : byte deprecated)', 'end)'],AHint,True);
  1478. AssertField1([]);
  1479. AssertVariantSelector('','integer');
  1480. AssertRecordVariant(0,[],['0']);
  1481. AssertRecordVariantVariant(0,'i','Integer',[],['1']);
  1482. AssertRecordVariantVariant(1,'j','Byte',[hDeprecated],['2'])
  1483. end;
  1484. procedure TTestRecordTypeParser.DoTestVariantNestedVariantBothDeprecated(const AHint: string);
  1485. begin
  1486. TestFields(['x : integer;','case integer of','0 : ( y : record',' z : integer;',' case byte of ',' 1 : (i : integer deprecated );',' 2 : ( j : byte deprecated)', 'end)'],AHint,True);
  1487. AssertField1([]);
  1488. AssertVariantSelector('','integer');
  1489. AssertRecordVariant(0,[],['0']);
  1490. AssertRecordVariantVariant(0,'i','Integer',[hdeprecated],['1']);
  1491. AssertRecordVariantVariant(1,'j','Byte',[hDeprecated],['2'])
  1492. end;
  1493. procedure TTestRecordTypeParser.TestEmpty;
  1494. begin
  1495. DoTestEmpty('')
  1496. end;
  1497. procedure TTestRecordTypeParser.TestEmptyComment;
  1498. begin
  1499. AddComment:=True;
  1500. TestEmpty;
  1501. end;
  1502. procedure TTestRecordTypeParser.TestEmptyDeprecated;
  1503. begin
  1504. DoTestEmpty('Deprecated')
  1505. end;
  1506. procedure TTestRecordTypeParser.TestEmptyPlatform;
  1507. begin
  1508. DoTestEmpty('Platform')
  1509. end;
  1510. procedure TTestRecordTypeParser.AssertField1(Hints : TPasMemberHints);
  1511. begin
  1512. AssertEquals('Member 1 field type',TPasVariable,TObject(TheRecord.Members[0]).ClassType);
  1513. AssertEquals('Field 1 name','x',Field1.Name);
  1514. AssertNotNull('Have 1 Field type',Field1.VarType);
  1515. AssertEquals('Field 1 type',TPasUnresolvedTypeRef,Field1.VarType.ClassType);
  1516. AssertEquals('Field 1 type name','Integer',Field1.VarType.Name);
  1517. AssertTrue('Field 1 hints match',Field1.Hints=Hints)
  1518. end;
  1519. procedure TTestRecordTypeParser.AssertField2(Hints : TPasMemberHints);
  1520. begin
  1521. AssertEquals('Member 2 field type',TPasVariable,TObject(TheRecord.Members[1]).ClassType);
  1522. AssertEquals('Field 2 name','y',Field2.Name);
  1523. AssertNotNull('Have 2 Field type',Field2.VarType);
  1524. AssertEquals('Field 2 type',TPasUnresolvedTypeRef,Field2.VarType.ClassType);
  1525. AssertEquals('Field 2 type name','Integer',Field2.VarType.Name);
  1526. AssertTrue('Field 2 hints match',Field2.Hints=Hints)
  1527. end;
  1528. procedure TTestRecordTypeParser.AssertMethod2(Hints: TPasMemberHints; isClass : Boolean = False);
  1529. Var
  1530. P : TPasProcedure;
  1531. begin
  1532. if IsClass then
  1533. AssertEquals('Member 2 type',TPasClassProcedure,TObject(TheRecord.Members[1]).ClassType)
  1534. else
  1535. AssertEquals('Member 2 type',TPasProcedure,TObject(TheRecord.Members[1]).ClassType);
  1536. P:=TPasProcedure(TheRecord.Members[1]);
  1537. AssertEquals('Method name','dosomething2',P.Name);
  1538. AssertTrue('Method hints match',P.Hints=Hints)
  1539. end;
  1540. procedure TTestRecordTypeParser.AssertConstructor2(Hints: TPasMemberHints;
  1541. isClass: Boolean);
  1542. Var
  1543. P : TPasProcedure;
  1544. begin
  1545. if IsClass then
  1546. AssertEquals('Member 2 type',TPasClassConstructor,TObject(TheRecord.Members[1]).ClassType)
  1547. else
  1548. AssertEquals('Member 2 type',TPasConstructor,TObject(TheRecord.Members[1]).ClassType);
  1549. P:=TPasProcedure(TheRecord.Members[1]);
  1550. AssertEquals('Constructor name','create',P.Name);
  1551. AssertTrue('Constructor hints match',P.Hints=Hints)
  1552. end;
  1553. procedure TTestRecordTypeParser.AssertOperatorMethod2(Hints: TPasMemberHints;
  1554. isClass: Boolean);
  1555. Var
  1556. P : TPasOperator;
  1557. begin
  1558. if IsClass then
  1559. AssertEquals('Member 2 type',TPasClassOperator,TObject(TheRecord.Members[1]).ClassType)
  1560. else
  1561. AssertEquals('Member 2 type',TPasOperator,TObject(TheRecord.Members[1]).ClassType);
  1562. P:=TPasOperator(TheRecord.Members[1]);
  1563. AssertEquals('Method name','assign(ta,Cardinal):Boolean',P.Name);
  1564. AssertTrue('Method hints match',P.Hints=Hints)
  1565. end;
  1566. procedure TTestRecordTypeParser.AssertOneIntegerField(Hints : TPasMemberHints);
  1567. begin
  1568. AssertEquals('One field',1,TheRecord.Members.Count);
  1569. AssertField1(Hints);
  1570. end;
  1571. procedure TTestRecordTypeParser.AssertTwoIntegerFields(Hints1,Hints2: TPasMemberHints);
  1572. begin
  1573. AssertEquals('Two field',2,TheRecord.Members.Count);
  1574. AssertField1(Hints1);
  1575. AssertField2(Hints2);
  1576. end;
  1577. procedure TTestRecordTypeParser.AssertIntegerFieldAndMethod(Hints1,
  1578. Hints2: TPasMemberHints);
  1579. begin
  1580. AssertEquals('Two members',2,TheRecord.Members.Count);
  1581. AssertField1(Hints1);
  1582. AssertMethod2(Hints2);
  1583. end;
  1584. procedure TTestRecordTypeParser.AssertIntegerFieldAndConstructor(Hints1,
  1585. Hints2: TPasMemberHints);
  1586. begin
  1587. AssertEquals('Two members',2,TheRecord.Members.Count);
  1588. AssertField1(Hints1);
  1589. AssertConstructor2(Hints2);
  1590. end;
  1591. procedure TTestRecordTypeParser.AssertRecordField(AIndex: Integer;
  1592. Hints: TPasMemberHints);
  1593. Var
  1594. F : TPasVariable;
  1595. R : TPasRecordtype;
  1596. begin
  1597. AssertEquals('Member 2 field type',TPasVariable,TObject(TheRecord.Members[AIndex]).ClassType);
  1598. F:=GetF(AIndex);
  1599. if AIndex=1 then
  1600. AssertEquals('Field 2 name','y',F.Name)
  1601. else
  1602. AssertEquals('Field 1 name','x',F.Name);
  1603. AssertNotNull('Have 2 Field type',F.VarType);
  1604. AssertEquals('Field 2 type',TPasRecordType,F.VarType.ClassType);
  1605. R:=F.VarType as TPasRecordType;
  1606. AssertNotNull('Record field has members',R.Members);
  1607. AssertEquals('Record field has 1 member',1,R.Members.Count);
  1608. AssertTrue('Record field hints match',F.Hints=Hints)
  1609. end;
  1610. procedure TTestRecordTypeParser.AssertRecordVariant(AIndex: Integer;
  1611. Hints: TPasMemberHints; VariantLabels: array of string);
  1612. Var
  1613. F : TPasVariant;
  1614. V : TPasVariable;
  1615. R : TPasRecordtype;
  1616. I : Integer;
  1617. MN : String;
  1618. begin
  1619. F:=GetV(AIndex);
  1620. MN:='Variant '+IntToStr(AIndex)+' ';
  1621. AssertNotNull('Have variant 1',F);
  1622. AssertEquals('Have correct number of values',Length(VariantLabels),F.Values.Count);
  1623. For I:=0 to Length(VariantLabels)-1 do
  1624. begin
  1625. AssertEquals(Format('Expression for variant %d',[I]),TPrimitiveExpr,TObject(Variant1.Values[i]).CLassType);
  1626. AssertExpression(Format('Value %d is %s',[i,VariantLabels[i]]),TPasExpr(Variant1.Values[I]),pekNumber,VariantLabels[i]);
  1627. end;
  1628. V:=GetField(0,F);
  1629. AssertEquals(MN+'has correct name','y',V.Name);
  1630. AssertNotNull(MN+'has not null type',V.VarType);
  1631. AssertEquals(MN+'has correct type',TPasRecordType,V.VarType.ClassType);
  1632. AssertTrue(MN+'hints match',V.Hints=Hints);
  1633. R:=TPasVariable(F.Members.Members[0]).VarType as TPasRecordType;
  1634. V:=GetField(0,R);
  1635. AssertEquals('Field 1 has correct name','z',V.Name);
  1636. AssertNotNull('Record field has members',R.Members);
  1637. AssertEquals('Record field has 1 member',1,R.Members.Count);
  1638. end;
  1639. procedure TTestRecordTypeParser.AssertRecordVariantVariant(AIndex: Integer;
  1640. const AFieldName, ATypeName: string; Hints: TPasMemberHints;
  1641. VariantLabels: array of string);
  1642. Var
  1643. F : TPasVariant;
  1644. V : TPasVariable;
  1645. R : TPasRecordtype;
  1646. I : Integer;
  1647. MN : String;
  1648. begin
  1649. F:=GetV(0);
  1650. MN:='Nested Variant '+IntToStr(AIndex)+' ';
  1651. AssertNotNull('Have variant 1',F);
  1652. AssertEquals('Have correct number of values',1,F.Values.Count);
  1653. AssertEquals('Expression',TPrimitiveExpr,TObject(F.Values[0]).CLassType);
  1654. AssertExpression('First value is 0',TPasExpr(F.Values[0]),pekNumber,'0');
  1655. // First variant, Y, record
  1656. V:=GetField(0,F);
  1657. AssertEquals(MN+'has correct name','y',V.Name);
  1658. AssertNotNull(MN+'has not null type',V.VarType);
  1659. AssertEquals(MN+'has correct type',TPasRecordType,V.VarType.ClassType);
  1660. R:=TPasVariable(F.Members.Members[0]).VarType as TPasRecordType;
  1661. AssertNotNull('Record field has members',R.Members);
  1662. AssertEquals('Record field has 2 members',1,R.Members.Count);
  1663. // First variant
  1664. F:=GetVariant(Aindex,R);
  1665. // First field of first variant, i
  1666. AssertEquals('Have correct number of values',Length(VariantLabels),F.Values.Count);
  1667. For I:=0 to Length(VariantLabels)-1 do
  1668. begin
  1669. AssertEquals(Format('Expression for variant %d',[I]),TPrimitiveExpr,TObject(F.Values[i]).CLassType);
  1670. AssertExpression(Format('Value %d is %s',[i,VariantLabels[i]]),TPasExpr(F.Values[I]),pekNumber,VariantLabels[i]);
  1671. end;
  1672. V:=GetField(0,F);
  1673. AssertEquals('Nested Variant 0 has correct name',AFieldName,V.Name);
  1674. AssertEquals('Nested variant 0 has correct type',TPasUnresolvedTypeRef,V.VarType.ClassType);
  1675. AssertEquals('Nested variant 0 has correct type name',ATypeName,V.VarType.Name);
  1676. AssertTrue(MN+'hints match',V.Hints=Hints);
  1677. end;
  1678. procedure TTestRecordTypeParser.TestOneField;
  1679. begin
  1680. TestFields(['x : integer'],'',False);
  1681. AssertOneIntegerField([]);
  1682. end;
  1683. procedure TTestRecordTypeParser.TestOneFieldComment;
  1684. begin
  1685. AddComment:=True;
  1686. TestFields(['{a} x : integer'],'',False);
  1687. AssertOneIntegerField([]);
  1688. AssertEquals('Member 1 comment','a'+sLineBreak,TPAsElement(TheRecord.Members[0]).DocComment);
  1689. end;
  1690. procedure TTestRecordTypeParser.TestOneFieldDeprecated;
  1691. begin
  1692. TestFields(['x : integer'],'deprecated',False);
  1693. AssertOneIntegerField([]);
  1694. end;
  1695. procedure TTestRecordTypeParser.TestOneFieldPlatform;
  1696. begin
  1697. TestFields(['x : integer'],'platform',False);
  1698. AssertOneIntegerField([]);
  1699. end;
  1700. procedure TTestRecordTypeParser.TestOneFieldSemicolon;
  1701. begin
  1702. TestFields(['x : integer;'],'',False);
  1703. AssertOneIntegerField([]);
  1704. end;
  1705. procedure TTestRecordTypeParser.TestOneFieldSemicolonDeprecated;
  1706. begin
  1707. TestFields(['x : integer;'],'deprecated',False);
  1708. AssertOneIntegerField([]);
  1709. end;
  1710. procedure TTestRecordTypeParser.TestOneFieldSemicolonPlatform;
  1711. begin
  1712. TestFields(['x : integer;'],'platform',False);
  1713. AssertOneIntegerField([]);
  1714. end;
  1715. procedure TTestRecordTypeParser.TestOneDeprecatedField;
  1716. begin
  1717. TestFields(['x : integer deprecated;'],'',False);
  1718. AssertOneIntegerField([hDeprecated]);
  1719. end;
  1720. procedure TTestRecordTypeParser.TestOneDeprecatedFieldDeprecated;
  1721. begin
  1722. TestFields(['x : integer deprecated;'],'deprecated',False);
  1723. AssertOneIntegerField([hDeprecated]);
  1724. end;
  1725. procedure TTestRecordTypeParser.TestOneDeprecatedFieldPlatform;
  1726. begin
  1727. TestFields(['x : integer deprecated;'],'platform',False);
  1728. AssertOneIntegerField([hDeprecated]);
  1729. end;
  1730. procedure TTestRecordTypeParser.TestOnePlatformField;
  1731. begin
  1732. TestFields(['x : integer platform;'],'',False);
  1733. AssertOneIntegerField([hplatform]);
  1734. end;
  1735. procedure TTestRecordTypeParser.TestOnePlatformFieldDeprecated;
  1736. begin
  1737. TestFields(['x : integer platform;'],'Deprecated',False);
  1738. AssertOneIntegerField([hplatform]);
  1739. end;
  1740. procedure TTestRecordTypeParser.TestOnePlatformFieldPlatform;
  1741. begin
  1742. TestFields(['x : integer platform;'],'Platform',False);
  1743. AssertOneIntegerField([hplatform]);
  1744. end;
  1745. procedure TTestRecordTypeParser.TestOneGenericField;
  1746. begin
  1747. TestFields(['Generic : Integer;'],'',False);
  1748. AssertEquals('Member 1 field type',TPasVariable,TObject(TheRecord.Members[0]).ClassType);
  1749. AssertEquals('Field 1 name','Generic',Field1.Name);
  1750. AssertNotNull('Have 1 Field type',Field1.VarType);
  1751. AssertEquals('Field 1 type',TPasUnresolvedTypeRef,Field1.VarType.ClassType);
  1752. AssertEquals('Field 1 type name','Integer',Field1.VarType.Name);
  1753. end;
  1754. procedure TTestRecordTypeParser.TestTwoFields;
  1755. begin
  1756. TestFields(['x : integer;','y : integer'],'',False);
  1757. AssertTwoIntegerFields([],[]);
  1758. end;
  1759. procedure TTestRecordTypeParser.TestTwoFieldPrivateNoDelphi;
  1760. Var
  1761. EC : TClass;
  1762. begin
  1763. EC:=nil;
  1764. try
  1765. TestFields(['private','x : integer'],'',False);
  1766. Fail('Need po_Delphi for visibility specifier');
  1767. except
  1768. on EA : EAssertionFailedError do
  1769. Raise;
  1770. on E : Exception do
  1771. EC:=E.ClassType;
  1772. end;
  1773. AssertEquals('Exception class',EParserError,EC);
  1774. end;
  1775. procedure TTestRecordTypeParser.TestTwoFieldProtected;
  1776. Var
  1777. B : Boolean;
  1778. EName: String;
  1779. begin
  1780. B:=false;
  1781. EName:='';
  1782. try
  1783. TestFields(['protected','x : integer'],'',False);
  1784. Fail('Protected not allowed as record visibility specifier')
  1785. except
  1786. on E : Exception do
  1787. begin
  1788. EName:=E.ClassName;
  1789. B:=E is EParserError;
  1790. end;
  1791. end;
  1792. If not B then
  1793. Fail('Wrong exception class "'+EName+'".');
  1794. end;
  1795. procedure TTestRecordTypeParser.TestTwoFieldPrivate;
  1796. begin
  1797. Scanner.Options:=[po_Delphi];
  1798. TestFields(['private','x,y : integer'],'',False);
  1799. AssertTwoIntegerFields([],[]);
  1800. end;
  1801. procedure TTestRecordTypeParser.TestTwoFieldStrictPrivate;
  1802. begin
  1803. Scanner.Options:=[po_Delphi];
  1804. TestFields(['strict private','x,y : integer'],'',False);
  1805. AssertTwoIntegerFields([],[]);
  1806. end;
  1807. procedure TTestRecordTypeParser.TestTwoFieldDeprecated;
  1808. begin
  1809. TestFields(['x : integer;','y : integer'],'deprecated',False);
  1810. AssertTwoIntegerFields([],[]);
  1811. end;
  1812. procedure TTestRecordTypeParser.TestTwoFieldPlatform;
  1813. begin
  1814. TestFields(['x : integer;','y : integer'],'platform',False);
  1815. AssertTwoIntegerFields([],[]);
  1816. end;
  1817. procedure TTestRecordTypeParser.TestTwoFieldsFirstDeprecated;
  1818. begin
  1819. TestFields(['x : integer deprecated;','y : integer'],'',False);
  1820. AssertTwoIntegerFields([hdeprecated],[]);
  1821. end;
  1822. procedure TTestRecordTypeParser.TestTwoFieldsFirstDeprecatedDeprecated;
  1823. begin
  1824. TestFields(['x : integer deprecated;','y : integer'],'deprecated',False);
  1825. AssertTwoIntegerFields([hdeprecated],[]);
  1826. end;
  1827. procedure TTestRecordTypeParser.TestTwoFieldsFirstDeprecatedPlatform;
  1828. begin
  1829. TestFields(['x : integer deprecated;','y : integer'],'platform',False);
  1830. AssertTwoIntegerFields([hdeprecated],[]);
  1831. end;
  1832. procedure TTestRecordTypeParser.TestTwoFieldsSecondDeprecated;
  1833. begin
  1834. TestFields(['x : integer;','y : integer deprecated;'],'',False);
  1835. AssertTwoIntegerFields([],[hdeprecated]);
  1836. end;
  1837. procedure TTestRecordTypeParser.TestTwoFieldsSecondDeprecatedDeprecated;
  1838. begin
  1839. TestFields(['x : integer;','y : integer deprecated;'],'deprecated',False);
  1840. AssertTwoIntegerFields([],[hdeprecated]);
  1841. end;
  1842. procedure TTestRecordTypeParser.TestTwoFieldsSecondDeprecatedPlatform;
  1843. begin
  1844. TestFields(['x : integer;','y : integer deprecated;'],'platform',False);
  1845. AssertTwoIntegerFields([],[hdeprecated]);
  1846. end;
  1847. procedure TTestRecordTypeParser.TestTwoFieldsBothDeprecated;
  1848. begin
  1849. TestFields(['x : integer deprecated;','y : integer deprecated;'],'',False);
  1850. AssertTwoIntegerFields([hdeprecated],[hdeprecated]);
  1851. end;
  1852. procedure TTestRecordTypeParser.TestTwoFieldsBothDeprecatedDeprecated;
  1853. begin
  1854. TestFields(['x : integer deprecated;','y : integer deprecated;'],'deprecated',False);
  1855. AssertTwoIntegerFields([hdeprecated],[hdeprecated]);
  1856. end;
  1857. procedure TTestRecordTypeParser.TestTwoFieldsBothDeprecatedPlatform;
  1858. begin
  1859. TestFields(['x : integer deprecated;','y : integer deprecated;'],'platform',False);
  1860. AssertTwoIntegerFields([hdeprecated],[hdeprecated]);
  1861. end;
  1862. procedure TTestRecordTypeParser.TestTwoFieldsCombined;
  1863. begin
  1864. TestFields(['x,y : integer;'],'',False);
  1865. AssertTwoIntegerFields([],[]);
  1866. end;
  1867. procedure TTestRecordTypeParser.TestTwoFieldsCombinedDeprecated;
  1868. begin
  1869. TestFields(['x,y : integer;'],'deprecated',False);
  1870. AssertTwoIntegerFields([],[]);
  1871. end;
  1872. procedure TTestRecordTypeParser.TestTwoFieldsCombinedPlatform;
  1873. begin
  1874. TestFields(['x,y : integer;'],'platform',False);
  1875. AssertTwoIntegerFields([],[]);
  1876. end;
  1877. procedure TTestRecordTypeParser.TestTwoDeprecatedFieldsCombined;
  1878. begin
  1879. TestFields(['x,y : integer deprecated;'],'',False);
  1880. AssertTwoIntegerFields([hdeprecated],[hdeprecated]);
  1881. end;
  1882. procedure TTestRecordTypeParser.TestTwoDeprecatedFieldsCombinedDeprecated;
  1883. begin
  1884. TestFields(['x,y : integer deprecated;'],'deprecated',False);
  1885. AssertTwoIntegerFields([hdeprecated],[hdeprecated]);
  1886. end;
  1887. procedure TTestRecordTypeParser.TestTwoDeprecatedFieldsCombinedPlatform;
  1888. begin
  1889. TestFields(['x,y : integer deprecated;'],'platform',False);
  1890. AssertTwoIntegerFields([hdeprecated],[hdeprecated]);
  1891. end;
  1892. procedure TTestRecordTypeParser.TestFieldAndConstructor;
  1893. begin
  1894. Parser.Options:=[po_delphi];
  1895. TestFields(['x : integer;','constructor create;'],'',False);
  1896. AssertIntegerFieldAndConstructor([],[]);
  1897. end;
  1898. procedure TTestRecordTypeParser.TestFieldAndMethod;
  1899. begin
  1900. Parser.Options:=[po_delphi];
  1901. TestFields(['x : integer;','procedure dosomething2;'],'',False);
  1902. AssertIntegerFieldAndMethod([],[]);
  1903. end;
  1904. procedure TTestRecordTypeParser.TestFieldAnd2Methods;
  1905. Var
  1906. P : TPasFunction;
  1907. begin
  1908. Parser.Options:=[po_delphi];
  1909. TestFields(['x : integer;','procedure dosomething2;','function dosomething3 : Integer;'],'',False);
  1910. AssertEquals('Member count',3,TheRecord.Members.Count);
  1911. AssertField1([]);
  1912. AssertMethod2([]);
  1913. AssertEquals('Member 3 type',TPasFunction,TObject(TheRecord.Members[2]).ClassType);
  1914. P:=TPasFunction(TheRecord.Members[2]);
  1915. AssertEquals('Method 2 name','dosomething3',P.Name);
  1916. AssertTrue('Method 2 hints match',[]=P.Hints);
  1917. // Standard type
  1918. AssertEquals('Method 2 result type','Integer', P.FuncType.ResultEl.ResultType.Name);
  1919. end;
  1920. procedure TTestRecordTypeParser.TestFieldAndProperty;
  1921. Var
  1922. P : TPasProperty;
  1923. begin
  1924. Parser.Options:=[po_delphi];
  1925. TestFields(['private','x : integer;','public','property MyX : Integer read X write X'],'',False);
  1926. AssertEquals('Member count',2,TheRecord.Members.Count);
  1927. AssertField1([]);
  1928. AssertEquals('Member 2 type',TPasProperty,TObject(TheRecord.Members[1]).ClassType);
  1929. P:=TPasProperty(TheRecord.Members[1]);
  1930. AssertEquals('Property name','MyX',P.Name);
  1931. AssertNotNull('Method 2 type',P.ResolvedType);
  1932. AssertEquals('Method 2 type','Integer',P.ResolvedType.Name);
  1933. AssertEquals('Method 2 read','X', P.ReadAccessorName);
  1934. AssertEquals('Method 2 Write','X', P.WriteAccessorName);
  1935. end;
  1936. procedure TTestRecordTypeParser.TestFieldAndClassMethod;
  1937. Var
  1938. P : TPasFunction;
  1939. begin
  1940. Parser.Options:=[po_delphi];
  1941. TestFields(['x : integer;','class procedure dosomething2;','function dosomething3 : Integer;'],'',False);
  1942. AssertEquals('Member count',3,TheRecord.Members.Count);
  1943. AssertField1([]);
  1944. AssertMethod2([],True);
  1945. AssertEquals('Class procedure',TPasClassProcedure,TObject(TheRecord.Members[1]).ClassType);
  1946. AssertEquals('Member 3 type',TPasFunction,TObject(TheRecord.Members[2]).ClassType);
  1947. P:=TPasFunction(TheRecord.Members[2]);
  1948. AssertEquals('Method 2 name','dosomething3',P.Name);
  1949. AssertTrue('Method 2 hints match',[]=P.Hints);
  1950. // Standard type
  1951. AssertEquals('Method 2 result type','Integer', P.FuncType.ResultEl.ResultType.Name);
  1952. end;
  1953. procedure TTestRecordTypeParser.TestFieldAndClassOperator;
  1954. Var
  1955. P : TPasFunction;
  1956. begin
  1957. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msAdvancedRecords];
  1958. TestFields(['x : integer;','class operator assign(a : ta; b : Cardinal) : boolean;','function dosomething3 : Integer;'],'',False);
  1959. AssertEquals('Member count',3,TheRecord.Members.Count);
  1960. AssertField1([]);
  1961. AssertOperatorMethod2([],True);
  1962. AssertEquals('Member 3 type',TPasFunction,TObject(TheRecord.Members[2]).ClassType);
  1963. P:=TPasFunction(TheRecord.Members[2]);
  1964. AssertEquals('Method 2 name','dosomething3',P.Name);
  1965. AssertTrue('Method 2 hints match',[]=P.Hints);
  1966. // Standard type
  1967. AssertEquals('Method 2 result type','Integer', P.FuncType.ResultEl.ResultType.Name);
  1968. end;
  1969. procedure TTestRecordTypeParser.TestFieldAndClassVar;
  1970. begin
  1971. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msAdvancedRecords];
  1972. TestFields(['x : integer;','class var y : integer;'],'',False);
  1973. AssertField1([]);
  1974. AssertTrue('Second field is class var',vmClass in Field2.VarModifiers);
  1975. end;
  1976. procedure TTestRecordTypeParser.TestFieldAndVar;
  1977. begin
  1978. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msAdvancedRecords];
  1979. TestFields(['x : integer;','var y : integer;'],'',False);
  1980. AssertField1([]);
  1981. AssertTrue('Second field is regular var',not (vmClass in Field2.VarModifiers));
  1982. end;
  1983. procedure TTestRecordTypeParser.TestNested;
  1984. begin
  1985. TestFields(['x : integer;','y : record',' z : integer;','end'],'',False);
  1986. AssertField1([]);
  1987. AssertRecordField(1,[])
  1988. end;
  1989. procedure TTestRecordTypeParser.TestNestedSemicolon;
  1990. begin
  1991. TestFields(['x : integer;','y : record',' z : integer;','end;'],'',False);
  1992. AssertField1([]);
  1993. AssertRecordField(1,[])
  1994. end;
  1995. procedure TTestRecordTypeParser.TestNestedSemicolonDeprecated;
  1996. begin
  1997. TestFields(['x : integer;','y : record',' z : integer;','end;'],'deprecated',False);
  1998. AssertField1([]);
  1999. AssertRecordField(1,[])
  2000. end;
  2001. procedure TTestRecordTypeParser.TestNestedSemicolonPlatform;
  2002. begin
  2003. TestFields(['x : integer;','y : record',' z : integer;','end;'],'platform',False);
  2004. AssertField1([]);
  2005. AssertRecordField(1,[])
  2006. end;
  2007. procedure TTestRecordTypeParser.TestNestedDeprecated;
  2008. begin
  2009. TestFields(['x : integer;','y : record',' z : integer;','end'],'deprecated',False);
  2010. AssertField1([]);
  2011. AssertRecordField(1,[])
  2012. end;
  2013. procedure TTestRecordTypeParser.TestNestedPlatform;
  2014. begin
  2015. TestFields(['x : integer;','y : record',' z : integer;','end'],'platform',False);
  2016. AssertField1([]);
  2017. AssertRecordField(1,[])
  2018. end;
  2019. procedure TTestRecordTypeParser.TestNestedFirst;
  2020. begin
  2021. TestFields(['x : record',' z : integer;','end;','y : integer;'],'',False);
  2022. AssertField2([]);
  2023. AssertRecordField(0,[])
  2024. end;
  2025. procedure TTestRecordTypeParser.TestNestedFirstDeprecated;
  2026. begin
  2027. TestFields(['x : record',' z : integer;','end;','y : integer;'],'deprecated',False);
  2028. AssertField2([]);
  2029. AssertRecordField(0,[])
  2030. end;
  2031. procedure TTestRecordTypeParser.TestNestedFirstPlatform;
  2032. begin
  2033. TestFields(['x : record',' z : integer;','end;','y : integer;'],'platform',False);
  2034. AssertField2([]);
  2035. AssertRecordField(0,[])
  2036. end;
  2037. procedure TTestRecordTypeParser.TestDeprecatedNested;
  2038. begin
  2039. TestFields(['x : integer;','y : record',' z : integer;','end deprecated;'],'',False);
  2040. AssertField1([]);
  2041. AssertRecordField(1,[hdeprecated])
  2042. end;
  2043. procedure TTestRecordTypeParser.TestDeprecatedNestedDeprecated;
  2044. begin
  2045. TestFields(['x : integer;','y : record',' z : integer;','end deprecated;'],'deprecated',False);
  2046. AssertField1([]);
  2047. AssertRecordField(1,[hdeprecated])
  2048. end;
  2049. procedure TTestRecordTypeParser.TestDeprecatedNestedPlatform;
  2050. begin
  2051. TestFields(['x : integer;','y : record',' z : integer;','end deprecated;'],'platform',False);
  2052. AssertField1([]);
  2053. AssertRecordField(1,[hdeprecated])
  2054. end;
  2055. procedure TTestRecordTypeParser.TestDeprecatedNestedFirst;
  2056. begin
  2057. TestFields(['x : record',' z : integer;','end deprecated;','y : integer;'],'',False);
  2058. AssertField2([]);
  2059. AssertRecordField(0,[hdeprecated])
  2060. end;
  2061. procedure TTestRecordTypeParser.TestDeprecatedNestedFirstDeprecated;
  2062. begin
  2063. TestFields(['x : record',' z : integer;','end deprecated;','y : integer;'],'deprecated',False);
  2064. AssertField2([]);
  2065. AssertRecordField(0,[hdeprecated])
  2066. end;
  2067. procedure TTestRecordTypeParser.TestDeprecatedNestedFirstPlatform;
  2068. begin
  2069. TestFields(['x : record',' z : integer;','end deprecated;','y : integer;'],'platform',False);
  2070. AssertField2([]);
  2071. AssertRecordField(0,[hdeprecated])
  2072. end;
  2073. procedure TTestRecordTypeParser.TestVariantNoStorage;
  2074. begin
  2075. DoTestVariantNoStorage('');
  2076. end;
  2077. procedure TTestRecordTypeParser.TestVariantNoStorageDeprecated;
  2078. begin
  2079. DoTestVariantNoStorage('deprecated');
  2080. end;
  2081. procedure TTestRecordTypeParser.TestVariantNoStoragePlatform;
  2082. begin
  2083. DoTestVariantNoStorage('platform');
  2084. end;
  2085. procedure TTestRecordTypeParser.TestVariantStorage;
  2086. begin
  2087. DoTestVariantStorage('');
  2088. end;
  2089. procedure TTestRecordTypeParser.TestVariantStorageDeprecated;
  2090. begin
  2091. DoTestVariantStorage('deprecated');
  2092. end;
  2093. procedure TTestRecordTypeParser.TestVariantStoragePlatform;
  2094. begin
  2095. DoTestVariantStorage('platform');
  2096. end;
  2097. procedure TTestRecordTypeParser.TestDeprecatedVariantNoStorage;
  2098. begin
  2099. DoTestDeprecatedVariantNoStorage('');
  2100. end;
  2101. procedure TTestRecordTypeParser.TestDeprecatedVariantNoStorageDeprecated;
  2102. begin
  2103. DoTestDeprecatedVariantNoStorage('Deprecated');
  2104. end;
  2105. procedure TTestRecordTypeParser.TestDeprecatedVariantNoStoragePlatform;
  2106. begin
  2107. DoTestDeprecatedVariantNoStorage('Platform');
  2108. end;
  2109. procedure TTestRecordTypeParser.TestDeprecatedVariantStorage;
  2110. begin
  2111. DoTestDeprecatedVariantStorage('');
  2112. end;
  2113. procedure TTestRecordTypeParser.TestDeprecatedVariantStorageDeprecated;
  2114. begin
  2115. DoTestDeprecatedVariantStorage('Deprecated');
  2116. end;
  2117. procedure TTestRecordTypeParser.TestDeprecatedVariantStoragePlatform;
  2118. begin
  2119. DoTestDeprecatedVariantStorage('Platform');
  2120. end;
  2121. procedure TTestRecordTypeParser.TestTwoVariantsNoStorage;
  2122. begin
  2123. DoTestTwoVariantsNoStorage('');
  2124. end;
  2125. procedure TTestRecordTypeParser.TestTwoVariantsNoStorageDeprecated;
  2126. begin
  2127. DoTestTwoVariantsNoStorage('deprecated');
  2128. end;
  2129. procedure TTestRecordTypeParser.TestTwoVariantsNoStoragePlatform;
  2130. begin
  2131. DoTestTwoVariantsNoStorage('platform');
  2132. end;
  2133. procedure TTestRecordTypeParser.TestTwoVariantsStorage;
  2134. begin
  2135. DoTestTwoVariantsStorage('');
  2136. end;
  2137. procedure TTestRecordTypeParser.TestTwoVariantsStorageDeprecated;
  2138. begin
  2139. DoTestTwoVariantsStorage('deprecated');
  2140. end;
  2141. procedure TTestRecordTypeParser.TestTwoVariantsStoragePlatform;
  2142. begin
  2143. DoTestTwoVariantsStorage('platform');
  2144. end;
  2145. procedure TTestRecordTypeParser.TestTwoVariantsFirstDeprecatedStorage;
  2146. begin
  2147. DoTestTwoVariantsFirstDeprecatedStorage('');
  2148. end;
  2149. procedure TTestRecordTypeParser.TestTwoVariantsFirstDeprecatedStorageDeprecated;
  2150. begin
  2151. DoTestTwoVariantsFirstDeprecatedStorage('deprecated');
  2152. end;
  2153. procedure TTestRecordTypeParser.TestTwoVariantsFirstDeprecatedStoragePlatform;
  2154. begin
  2155. DoTestTwoVariantsFirstDeprecatedStorage('platform');
  2156. end;
  2157. procedure TTestRecordTypeParser.TestTwoVariantsSecondDeprecatedStorage;
  2158. begin
  2159. DoTestTwoVariantsSecondDeprecatedStorage('');
  2160. end;
  2161. procedure TTestRecordTypeParser.TestTwoVariantsSecondDeprecatedStorageDeprecated;
  2162. begin
  2163. DoTestTwoVariantsSecondDeprecatedStorage('deprecated');
  2164. end;
  2165. procedure TTestRecordTypeParser.TestTwoVariantsSecondDeprecatedStoragePlatform;
  2166. begin
  2167. DoTestTwoVariantsSecondDeprecatedStorage('platform');
  2168. end;
  2169. procedure TTestRecordTypeParser.TestVariantTwoLabels;
  2170. begin
  2171. DoTestVariantTwoLabels('');
  2172. end;
  2173. procedure TTestRecordTypeParser.TestVariantTwoLabelsDeprecated;
  2174. begin
  2175. DoTestVariantTwoLabels('Deprecated');
  2176. end;
  2177. procedure TTestRecordTypeParser.TestVariantTwoLabelsPlatform;
  2178. begin
  2179. DoTestVariantTwoLabels('Platform');
  2180. end;
  2181. procedure TTestRecordTypeParser.TestTwoVariantsTwoLabels;
  2182. begin
  2183. DoTestTwoVariantsTwoLabels('');
  2184. end;
  2185. procedure TTestRecordTypeParser.TestTwoVariantsTwoLabelsDeprecated;
  2186. begin
  2187. DoTestTwoVariantsTwoLabels('Deprecated');
  2188. end;
  2189. procedure TTestRecordTypeParser.TestTwoVariantsTwoLabelsPlatform;
  2190. begin
  2191. DoTestTwoVariantsTwoLabels('Platform');
  2192. end;
  2193. procedure TTestRecordTypeParser.TestVariantNestedRecord;
  2194. begin
  2195. DoTestVariantNestedRecord('');
  2196. end;
  2197. procedure TTestRecordTypeParser.TestVariantNestedRecordDeprecated;
  2198. begin
  2199. DoTestVariantNestedRecord('Deprecated');
  2200. end;
  2201. procedure TTestRecordTypeParser.TestVariantNestedRecordPlatform;
  2202. begin
  2203. DoTestVariantNestedRecord('Platform');
  2204. end;
  2205. procedure TTestRecordTypeParser.TestVariantNestedVariant;
  2206. begin
  2207. DoTestVariantNestedVariant('');
  2208. end;
  2209. procedure TTestRecordTypeParser.TestVariantNestedVariantDeprecated;
  2210. begin
  2211. DoTestVariantNestedVariant('deprecated');
  2212. end;
  2213. procedure TTestRecordTypeParser.TestVariantNestedVariantPlatForm;
  2214. begin
  2215. DoTestVariantNestedVariant('Platform');
  2216. end;
  2217. procedure TTestRecordTypeParser.TestVariantNestedVariantFirstDeprecated;
  2218. begin
  2219. DoTestVariantNestedVariantFirstDeprecated('');
  2220. end;
  2221. procedure TTestRecordTypeParser.TestVariantNestedVariantFirstDeprecatedDeprecated;
  2222. begin
  2223. DoTestVariantNestedVariantFirstDeprecated('deprecated');
  2224. end;
  2225. procedure TTestRecordTypeParser.TestVariantNestedVariantFirstDeprecatedPlatform;
  2226. begin
  2227. DoTestVariantNestedVariantFirstDeprecated('platform');
  2228. end;
  2229. procedure TTestRecordTypeParser.TestVariantNestedVariantSecondDeprecated;
  2230. begin
  2231. DoTestVariantNestedVariantSecondDeprecated('');
  2232. end;
  2233. procedure TTestRecordTypeParser.TestVariantNestedVariantSecondDeprecatedDeprecated;
  2234. begin
  2235. DoTestVariantNestedVariantSecondDeprecated('deprecated');
  2236. end;
  2237. procedure TTestRecordTypeParser.TestVariantNestedVariantSecondDeprecatedPlatform;
  2238. begin
  2239. DoTestVariantNestedVariantSecondDeprecated('platform');
  2240. end;
  2241. procedure TTestRecordTypeParser.TestVariantNestedVariantBothDeprecated;
  2242. begin
  2243. DoTestVariantNestedVariantBothDeprecated('');
  2244. end;
  2245. procedure TTestRecordTypeParser.TestVariantNestedVariantBothDeprecatedDeprecated;
  2246. begin
  2247. DoTestVariantNestedVariantBothDeprecated('deprecated');
  2248. end;
  2249. procedure TTestRecordTypeParser.TestVariantNestedVariantBothDeprecatedPlatform;
  2250. begin
  2251. DoTestVariantNestedVariantBothDeprecated('platform');
  2252. end;
  2253. procedure TTestRecordTypeParser.TestOperatorField;
  2254. begin
  2255. TestFields(['operator : integer;'],'',False);
  2256. AssertEquals('Field 1 name','operator',Field1.Name);
  2257. end;
  2258. procedure TTestRecordTypeParser.TestPropertyFail;
  2259. begin
  2260. AddMember('Property Something');
  2261. ParseRecordFail(SErrRecordPropertiesNotAllowed,nErrRecordPropertiesNotAllowed);
  2262. end;
  2263. procedure TTestRecordTypeParser.TestAdvRec_TwoConst;
  2264. var
  2265. aConst: TPasConst;
  2266. begin
  2267. Scanner.Options:=[po_Delphi];
  2268. TestFields(['public','Const x =123;','y : integer = 456'],'',False);
  2269. AssertEquals('Two Const',2,TheRecord.Members.Count);
  2270. AssertConst1([]);
  2271. AssertEquals('Correct visibility',visPublic,TPasConst(TheRecord.Members[0]).Visibility);
  2272. AssertEquals('Member 2 type',TPasConst,TObject(TheRecord.Members[1]).ClassType);
  2273. aConst:=TPasConst(TheRecord.Members[1]);
  2274. AssertEquals('Const 2 name','y',aConst.Name);
  2275. AssertNotNull('Have 2 const expr',aConst.Expr);
  2276. end;
  2277. procedure TTestRecordTypeParser.TestAdvRec_Property;
  2278. begin
  2279. StartRecord(true);
  2280. AddMember('Property Something: word');
  2281. ParseRecord;
  2282. end;
  2283. procedure TTestRecordTypeParser.TestAdvRec_PropertyImplementsFail;
  2284. begin
  2285. StartRecord(true);
  2286. AddMember('Property Something: word implements ISome;');
  2287. ParseRecordFail('Expected ";"',nParserExpectTokenError);
  2288. end;
  2289. procedure TTestRecordTypeParser.TestAdvRec_PropertyNoTypeFail;
  2290. begin
  2291. StartRecord(true);
  2292. AddMember('Property Something;');
  2293. ParseRecordFail('Expected ":"',nParserExpectTokenError);
  2294. end;
  2295. procedure TTestRecordTypeParser.TestAdvRec_ForwardFail;
  2296. begin
  2297. StartRecord(true);
  2298. FDecl.Add(';TMyRecord = record');
  2299. ParseRecordFail('Syntax error in type',nParserTypeSyntaxError);
  2300. end;
  2301. procedure TTestRecordTypeParser.TestAdvRec_PublishedFail;
  2302. begin
  2303. StartRecord(true);
  2304. AddMember('published');
  2305. AddMember('A: word;');
  2306. ParseRecordFail(SParserInvalidRecordVisibility,nParserInvalidRecordVisibility);
  2307. end;
  2308. procedure TTestRecordTypeParser.TestAdvRec_ProcVirtualFail;
  2309. begin
  2310. StartRecord(true);
  2311. AddMember('procedure DoIt; virtual;');
  2312. ParseRecordFail(SParserExpectedCommaColon,nParserExpectedCommaColon);
  2313. end;
  2314. procedure TTestRecordTypeParser.TestAdvRec_ProcOverrideFail;
  2315. begin
  2316. StartRecord(true);
  2317. AddMember('procedure DoIt; override;');
  2318. ParseRecordFail(SParserExpectedCommaColon,nParserExpectedCommaColon);
  2319. end;
  2320. procedure TTestRecordTypeParser.TestAdvRec_ProcMessageFail;
  2321. begin
  2322. StartRecord(true);
  2323. AddMember('procedure DoIt; message 2;');
  2324. ParseRecordFail(SParserExpectedCommaColon,nParserExpectedCommaColon);
  2325. end;
  2326. procedure TTestRecordTypeParser.TestAdvRec_DestructorFail;
  2327. begin
  2328. StartRecord(true);
  2329. AddMember('destructor Free;');
  2330. ParseRecordFail(SParserNoConstructorAllowed,nParserNoConstructorAllowed);
  2331. end;
  2332. procedure TTestRecordTypeParser.TestAdvRec_CaseInVar;
  2333. // Found in System.UITypes.pas
  2334. begin
  2335. StartRecord(true);
  2336. AddMember('var');
  2337. AddMember('Case Integer of');
  2338. AddMember(' 1 : (x: integer);');
  2339. AddMember(' 2 : (y,z: integer)');
  2340. ParseRecord;
  2341. end;
  2342. procedure TTestRecordTypeParser.TestAdvRec_EmptySections;
  2343. begin
  2344. StartRecord(true);
  2345. AddMember('const');
  2346. AddMember('type');
  2347. AddMember('var');
  2348. AddMember(' x: integer;');
  2349. ParseRecord;
  2350. end;
  2351. procedure TTestRecordTypeParser.TestAdvRecordInFunction;
  2352. // Src from bug report 36179
  2353. Const
  2354. Src =
  2355. '{$mode objfpc}'+sLineBreak+
  2356. '{$modeswitch advancedrecords}'+sLineBreak+
  2357. 'program afile;'+sLineBreak+
  2358. ' procedure DoThis;'+sLineBreak+
  2359. ' type'+sLineBreak+
  2360. ' TMyRecord = record'+sLineBreak+
  2361. ' private'+sLineBreak+
  2362. ' x, y, z: integer;'+sLineBreak+
  2363. ' end;'+sLineBreak+
  2364. ' begin'+sLineBreak+
  2365. ' end;'+sLineBreak+
  2366. 'begin'+sLineBreak+
  2367. 'end.';
  2368. begin
  2369. Source.Text:=Src;
  2370. ParseModule; // We're just interested in that it parses.
  2371. end;
  2372. procedure TTestRecordTypeParser.TestAdvRecordInAnonFunction;
  2373. // Src from bug report 36179, modified to put record in anonymous function
  2374. // Delphi 10.3.2 allows this
  2375. Const
  2376. Src =
  2377. '{$mode objfpc}'+sLineBreak+
  2378. '{$modeswitch advancedrecords}'+sLineBreak+
  2379. 'program afile;'+sLineBreak+
  2380. 'var a : Procedure;'+sLineBreak+
  2381. 'begin'+sLineBreak+
  2382. ' a := '+sLineBreak+
  2383. ' procedure '+sLineBreak+
  2384. ' type'+sLineBreak+
  2385. ' TMyRecord = record'+sLineBreak+
  2386. ' private'+sLineBreak+
  2387. ' x, y, z: integer;'+sLineBreak+
  2388. ' end;'+sLineBreak+
  2389. ' begin'+sLineBreak+
  2390. ' end;'+sLineBreak+
  2391. 'end.';
  2392. begin
  2393. Source.Text:=Src;
  2394. ParseModule; // We're just interested in that it parses.
  2395. end;
  2396. procedure TTestRecordTypeParser.TestAdvRecordClassOperator;
  2397. // Source from bug id 36180
  2398. Const
  2399. SRC =
  2400. '{$mode objfpc}'+sLineBreak+
  2401. '{$modeswitch advancedrecords}'+sLineBreak+
  2402. 'program afile;'+sLineBreak+
  2403. 'type'+sLineBreak+
  2404. ' TMyRecord = record'+sLineBreak+
  2405. ' class operator = (a, b: TMyRecord): boolean;'+sLineBreak+
  2406. ' end;'+sLineBreak+
  2407. 'class operator TMyRecord.= (a, b: TMyRecord): boolean;'+sLineBreak+
  2408. 'begin'+sLineBreak+
  2409. ' result := (@a = @b);'+sLineBreak+
  2410. 'end;'+sLineBreak+
  2411. 'begin'+sLineBreak+
  2412. 'end.';
  2413. begin
  2414. Source.Text:=Src;
  2415. ParseModule; // We're just interested in that it parses.
  2416. end;
  2417. procedure TTestRecordTypeParser.TestAdvRecordInitOperator;
  2418. // Source from bug id 36180
  2419. Const
  2420. SRC =
  2421. '{$mode objfpc}'+sLineBreak+
  2422. '{$modeswitch advancedrecords}'+sLineBreak+
  2423. 'program afile;'+sLineBreak+
  2424. 'type'+sLineBreak+
  2425. ' TMyRecord = record'+sLineBreak+
  2426. ' class operator initialize (var self: TMyRecord);'+sLineBreak+
  2427. ' end;'+sLineBreak+
  2428. 'class operator TMyRecord.initialize (a, b: TMyRecord);'+sLineBreak+
  2429. 'begin'+sLineBreak+
  2430. ' result := (@a = @b);'+sLineBreak+
  2431. 'end;'+sLineBreak+
  2432. 'begin'+sLineBreak+
  2433. 'end.';
  2434. begin
  2435. Source.Text:=Src;
  2436. ParseModule; // We're just interested in that it parses.
  2437. end;
  2438. procedure TTestRecordTypeParser.TestAdvRecordGenericFunction;
  2439. Const
  2440. SRC =
  2441. '{$mode objfpc}'+sLineBreak+
  2442. '{$modeswitch advancedrecords}'+sLineBreak+
  2443. 'program afile;'+sLineBreak+
  2444. 'type'+sLineBreak+
  2445. ' TMyRecord = record'+sLineBreak+
  2446. ' generic class procedure doit<T> (a: T);'+sLineBreak+
  2447. ' end;'+sLineBreak+
  2448. 'generic class procedure TMyRecord.DoIt<T>(a: T);'+sLineBreak+
  2449. 'begin'+sLineBreak+
  2450. 'end;'+sLineBreak+
  2451. 'begin'+sLineBreak+
  2452. 'end.';
  2453. begin
  2454. Source.Text:=Src;
  2455. ParseModule; // We're just interested in that it parses.
  2456. end;
  2457. { TBaseTestTypeParser }
  2458. Function TBaseTestTypeParser.ParseType(ASource: String; ATypeClass: TClass;
  2459. Const AHint: String): TPasType;
  2460. Var
  2461. D : String;
  2462. begin
  2463. Hint:=AHint;
  2464. Add('Type');
  2465. If AddComment then
  2466. begin
  2467. Add('// A comment');
  2468. Parser.Engine.NeedComments:=True;
  2469. end;
  2470. D:='A = '+ASource;
  2471. If Hint<>'' then
  2472. D:=D+' '+Hint;
  2473. Add(' '+D+';');
  2474. // Writeln(source.text);
  2475. ParseDeclarations;
  2476. if ATypeClass.InHeritsFrom(TPasClassType) then
  2477. AssertEquals('One type definition',1,Declarations.Classes.Count)
  2478. else
  2479. AssertEquals('One type definition',1,Declarations.Types.Count);
  2480. If ATypeClass<>Nil then
  2481. begin
  2482. if ATypeClass.InheritsFrom(TPasClassType) then
  2483. Result:=TPasType(Declarations.Classes[0])
  2484. else
  2485. Result:=TPasType(Declarations.Types[0]);
  2486. AssertEquals('First declaration is type definition.',ATypeClass,Result.ClassType);
  2487. end;
  2488. AssertEquals('First declaration has correct name.','A',Result.Name);
  2489. FType:=Result;
  2490. Definition:=Result;
  2491. if (Hint<>'') then
  2492. CheckHint(TPasMemberHint(GetEnumValue(TypeInfo(TPasMemberHint),'h'+Hint)));
  2493. end;
  2494. Procedure TBaseTestTypeParser.AssertParseTypeError(ASource: String);
  2495. begin
  2496. try
  2497. ParseType(ASource,Nil,'');
  2498. Fail('Expected parser error');
  2499. except
  2500. // all OK.
  2501. end;
  2502. end;
  2503. Procedure TBaseTestTypeParser.AssertComment;
  2504. begin
  2505. AssertNotNull('Have type',TheType);
  2506. AssertEquals('Type comment',' A comment'+sLineBreak,TheTYpe.DocComment);
  2507. end;
  2508. procedure TBaseTestTypeParser.SetUp;
  2509. begin
  2510. Inherited;
  2511. FErrorSource:='';
  2512. FHint:='';
  2513. FType:=Nil;
  2514. Parser.CurrentModeswitches:=[msObjfpc];
  2515. end;
  2516. Procedure TBaseTestTypeParser.TearDown;
  2517. begin
  2518. inherited TearDown;
  2519. FType:=Nil;
  2520. end;
  2521. { TTestTypeParser }
  2522. procedure TTestTypeParser.DoTestAliasType(const AnAliasType: String;
  2523. const AHint: String);
  2524. begin
  2525. ParseType(AnAliasType,TPasAliasType,AHint);
  2526. AssertEquals('Unresolved type',TPasUnresolvedTypeRef,TPasAliasType(TheType).DestType.ClassType);
  2527. end;
  2528. procedure TTestTypeParser.DoTestStringType(const AnAliasType: String;
  2529. const AHint: String);
  2530. begin
  2531. ParseType(AnAliasType,TPasAliasType,AHint);
  2532. AssertEquals('String type',TPasStringType,TPasAliasType(TheType).DestType.ClassType);
  2533. end;
  2534. procedure TTestTypeParser.DoTypeError(const AMsg, ASource: string);
  2535. begin
  2536. FErrorSource:=ASource;
  2537. AssertException(AMsg,EParserError,@DoParseError);
  2538. end;
  2539. procedure TTestTypeParser.DoParseError;
  2540. begin
  2541. ParseType(FErrorSource,Nil);
  2542. end;
  2543. procedure TTestTypeParser.DoParsePointer(const ASource: String;
  2544. const AHint: String; ADestType: TClass);
  2545. begin
  2546. ParseType('^'+ASource,TPasPointerType,AHint);
  2547. if ADestType = Nil then
  2548. ADestType:=TPasUnresolvedTypeRef;
  2549. AssertEquals('Destination type '+ADestType.ClassName,ADestType,TPasPointerType(TheType).DestType.ClassType);
  2550. end;
  2551. procedure TTestTypeParser.DoParseArray(const ASource: String;
  2552. const AHint: String; ADestType: TClass);
  2553. begin
  2554. ParseType(ASource,TPasArrayType,AHint);
  2555. if ADestType = Nil then
  2556. ADestType:=TPasUnresolvedTypeRef;
  2557. AssertEquals('Destination type '+ADestType.ClassName,ADestType,TPasArrayType(TheType).ElType.ClassType);
  2558. end;
  2559. procedure TTestTypeParser.DoParseEnumerated(const ASource: String;
  2560. const AHint: String; ACount: integer);
  2561. Var
  2562. I : Integer;
  2563. begin
  2564. ParseType(ASource,TPasEnumType,AHint);
  2565. AssertNotNull('Have values',TPasEnumType(TheType).Values);
  2566. AssertEquals('Value count',ACount,TPasEnumType(TheType).Values.Count);
  2567. For I:=0 to TPasEnumType(TheType).Values.Count-1 do
  2568. AssertEquals('Enum value typed element '+IntToStr(I),TPasEnumValue,TObject(TPasEnumType(TheType).Values[i]).ClassType);
  2569. end;
  2570. procedure TTestTypeParser.DoTestFileType(const AType: String;
  2571. const AHint: String; ADestType: TClass);
  2572. begin
  2573. ParseType('File of '+AType,TPasFileType,AHint);
  2574. AssertNotNull('Have element type',TPasFileType(TheType).ElType);
  2575. if ADestType = Nil then
  2576. ADestType:=TPasUnresolvedTypeRef;
  2577. AssertEquals('Element type '+ADestType.ClassName,ADestType,TPasFileType(TheType).ElType.ClassType);
  2578. end;
  2579. procedure TTestTypeParser.DoTestRangeType(const AStart, AStop, AHint: String);
  2580. begin
  2581. ParseType(AStart+'..'+AStop,TPasRangeType,AHint);
  2582. AssertEquals('Range start',AStart,Stringreplace(TPasRangeType(TheType).RangeStart,' ','',[rfReplaceAll]));
  2583. AssertEquals('Range start',AStop,Stringreplace(TPasRangeType(TheType).RangeEnd,' ','',[rfReplaceAll]));
  2584. end;
  2585. procedure TTestTypeParser.DoParseSimpleSet(const ASource: String; const AHint: String; IsPacked: Boolean);
  2586. begin
  2587. if IsPacked then
  2588. ParseType('Packed Set of '+ASource,TPasSetType,AHint)
  2589. else
  2590. ParseType('Set of '+ASource,TPasSetType,AHint);
  2591. AssertNotNull('Have enumtype',TPasSetType(TheType).EnumType);
  2592. AssertEquals('Element type ',TPasUnresolvedTypeRef,TPasSetType(TheType).EnumType.ClassType);
  2593. AssertEquals('IsPacked is correct',isPacked,TPasSetType(TheType).IsPacked);
  2594. end;
  2595. procedure TTestTypeParser.DoParseComplexSet(const ASource: String;
  2596. const AHint: String);
  2597. begin
  2598. ParseType('Set of '+ASource,TPasSetType,AHint);
  2599. AssertNotNull('Have enumtype',TPasSetType(TheType).EnumType);
  2600. AssertEquals('Element type ',TPasEnumType,TPasSetType(TheType).EnumType.ClassType);
  2601. end;
  2602. procedure TTestTypeParser.DoParseRangeSet(const ASource: String;
  2603. const AHint: String);
  2604. begin
  2605. ParseType('Set of '+ASource,TPasSetType,AHint);
  2606. AssertNotNull('Have enumtype',TPasSetType(TheType).EnumType);
  2607. AssertEquals('Element type ',TPasRangeType,TPasSetType(TheType).EnumType.ClassType);
  2608. end;
  2609. procedure TTestTypeParser.DoTestComplexSet;
  2610. Var
  2611. I : integer;
  2612. begin
  2613. AssertNotNull('Have values',TPasEnumType(TPasSetType(TheType).EnumType).Values);
  2614. AssertEquals('Have 3 values',3, TPasEnumType(TPasSetType(TheType).EnumType).Values.Count);
  2615. For I:=0 to TPasEnumType(TPasSetType(TheType).EnumType).Values.Count-1 do
  2616. AssertEquals('Enum value typed element '+IntToStr(I),TPasEnumValue,TObject(TPasEnumType(TPasSetType(TheType).EnumType).Values[i]).ClassType);
  2617. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[0]).Name);
  2618. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[1]).Name);
  2619. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[2]).Name);
  2620. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[0]).AssignedValue);
  2621. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[1]).AssignedValue);
  2622. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[2]).AssignedValue);
  2623. end;
  2624. procedure TTestTypeParser.DoTestClassOf(const AHint: string);
  2625. begin
  2626. ParseType('Class of TSomeClass',TPasClassOfType,AHint);
  2627. AssertNotNull('Have class type',TPasClassOfType(TheType).DestType);
  2628. AssertEquals('Element type ',TPasUnresolvedTypeRef,TPasClassOfType(TheType).DestType.ClassType);
  2629. end;
  2630. procedure TTestTypeParser.TestAliasType;
  2631. begin
  2632. DoTestAliasType('othertype','');
  2633. AssertEquals('Unresolved type name ','othertype',TPasUnresolvedTypeRef(TPasAliasType(TheType).DestType).name);
  2634. end;
  2635. procedure TTestTypeParser.TestAbsoluteAliasType;
  2636. begin
  2637. Add('Type');
  2638. Add(' Absolute = Integer;');
  2639. ParseDeclarations;
  2640. AssertEquals('First declaration is type definition.',TPasAliasType,TPasElement(Declarations.Types[0]).ClassType);
  2641. AssertEquals('First declaration has correct name.','Absolute',TPasElement(Declarations.Types[0]).Name);
  2642. end;
  2643. procedure TTestTypeParser.TestCrossUnitAliasType;
  2644. begin
  2645. DoTestAliasType('otherunit.othertype','');
  2646. end;
  2647. procedure TTestTypeParser.TestAliasTypeDeprecated;
  2648. begin
  2649. DoTestALiasType('othertype','deprecated');
  2650. end;
  2651. procedure TTestTypeParser.TestAliasTypePlatform;
  2652. begin
  2653. DoTestALiasType('othertype','platform');
  2654. end;
  2655. procedure TTestTypeParser.TestSimpleTypeByte;
  2656. begin
  2657. DoTestAliasType('BYTE','');
  2658. end;
  2659. procedure TTestTypeParser.TestSimpleTypeByteComment;
  2660. begin
  2661. AddComment:=True;
  2662. DoTestAliasType('BYTE','');
  2663. AssertComment;
  2664. end;
  2665. procedure TTestTypeParser.TestSimpleTypeByteDeprecated;
  2666. begin
  2667. DoTestAliasType('BYTE','deprecated');
  2668. end;
  2669. procedure TTestTypeParser.TestSimpleTypeBytePlatform;
  2670. begin
  2671. DoTestAliasType('BYTE','platform');
  2672. end;
  2673. procedure TTestTypeParser.TestSimpleTypeBoolean;
  2674. begin
  2675. DoTestAliasType('BOOLEAN','');
  2676. end;
  2677. procedure TTestTypeParser.TestSimpleTypeBooleanDeprecated;
  2678. begin
  2679. DoTestAliasType('BOOLEAN','deprecated');
  2680. end;
  2681. procedure TTestTypeParser.TestSimpleTypeBooleanPlatform;
  2682. begin
  2683. DoTestAliasType('BOOLEAN','platform');
  2684. end;
  2685. procedure TTestTypeParser.TestSimpleTypeChar;
  2686. begin
  2687. DoTestAliasType('AnsiChar','');
  2688. end;
  2689. procedure TTestTypeParser.TestSimpleTypeCharDeprecated;
  2690. begin
  2691. DoTestAliasType('AnsiChar','deprecated');
  2692. end;
  2693. procedure TTestTypeParser.TestSimpleTypeCharPlatform;
  2694. begin
  2695. DoTestAliasType('AnsiChar','platform');
  2696. end;
  2697. procedure TTestTypeParser.TestSimpleTypeInteger;
  2698. begin
  2699. DoTestAliasType('INTEGER','');
  2700. end;
  2701. procedure TTestTypeParser.TestSimpleTypeIntegerDeprecated;
  2702. begin
  2703. DoTestAliasType('INTEGER','deprecated');
  2704. end;
  2705. procedure TTestTypeParser.TestSimpleTypeIntegerPlatform;
  2706. begin
  2707. DoTestAliasType('INTEGER','platform');
  2708. end;
  2709. procedure TTestTypeParser.TestSimpleTypeInt64;
  2710. begin
  2711. DoTestAliasType('INT64','');
  2712. end;
  2713. procedure TTestTypeParser.TestSimpleTypeInt64Deprecated;
  2714. begin
  2715. DoTestAliasType('INT64','deprecated');
  2716. end;
  2717. procedure TTestTypeParser.TestSimpleTypeInt64Platform;
  2718. begin
  2719. DoTestAliasType('INT64','platform');
  2720. end;
  2721. procedure TTestTypeParser.TestSimpleTypeLongInt;
  2722. begin
  2723. DoTestAliasType('LONGINT','');
  2724. end;
  2725. procedure TTestTypeParser.TestSimpleTypeLongIntDeprecated;
  2726. begin
  2727. DoTestAliasType('LONGINT','deprecated');
  2728. end;
  2729. procedure TTestTypeParser.TestSimpleTypeLongIntPlatform;
  2730. begin
  2731. DoTestAliasType('LONGINT','platform');
  2732. end;
  2733. procedure TTestTypeParser.TestSimpleTypeLongWord;
  2734. begin
  2735. DoTestAliasType('LONGWORD','');
  2736. end;
  2737. procedure TTestTypeParser.TestSimpleTypeLongWordDeprecated;
  2738. begin
  2739. DoTestAliasType('LONGWORD','deprecated');
  2740. end;
  2741. procedure TTestTypeParser.TestSimpleTypeLongWordPlatform;
  2742. begin
  2743. DoTestAliasType('LONGWORD','platform');
  2744. end;
  2745. procedure TTestTypeParser.TestSimpleTypeDouble;
  2746. begin
  2747. DoTestAliasType('Double','');
  2748. end;
  2749. procedure TTestTypeParser.TestSimpleTypeDoubleDeprecated;
  2750. begin
  2751. DoTestAliasType('Double','deprecated');
  2752. end;
  2753. procedure TTestTypeParser.TestSimpleTypeDoublePlatform;
  2754. begin
  2755. DoTestAliasType('Double','platform');
  2756. end;
  2757. procedure TTestTypeParser.TestSimpleTypeShortInt;
  2758. begin
  2759. DoTestAliasType('SHORTINT','');
  2760. end;
  2761. procedure TTestTypeParser.TestSimpleTypeShortIntDeprecated;
  2762. begin
  2763. DoTestAliasType('SHORTINT','deprecated');
  2764. end;
  2765. procedure TTestTypeParser.TestSimpleTypeShortIntPlatform;
  2766. begin
  2767. DoTestAliasType('SHORTINT','platform');
  2768. end;
  2769. procedure TTestTypeParser.TestSimpleTypeSmallInt;
  2770. begin
  2771. DoTestAliasType('SMALLINT','');
  2772. end;
  2773. procedure TTestTypeParser.TestSimpleTypeSmallIntDeprecated;
  2774. begin
  2775. DoTestAliasType('SMALLINT','deprecated');
  2776. end;
  2777. procedure TTestTypeParser.TestSimpleTypeSmallIntPlatform;
  2778. begin
  2779. DoTestAliasType('SMALLINT','platform');
  2780. end;
  2781. procedure TTestTypeParser.TestSimpleTypeString;
  2782. begin
  2783. DoTestAliasType('STRING','');
  2784. end;
  2785. procedure TTestTypeParser.TestSimpleTypeStringDeprecated;
  2786. begin
  2787. DoTestAliasType('STRING','deprecated');
  2788. end;
  2789. procedure TTestTypeParser.TestSimpleTypeStringPlatform;
  2790. begin
  2791. DoTestAliasType('STRING','platform');
  2792. end;
  2793. procedure TTestTypeParser.TestSimpleTypeStringSize;
  2794. begin
  2795. DoTestStringType('String[10]','');
  2796. AssertEquals('Correct length', '10', TPasStringType(TPasAliasType(TheType).DestType).LengthExpr);
  2797. end;
  2798. procedure TTestTypeParser.TestSimpleTypeStringCodePage;
  2799. begin
  2800. DoTestStringType('String(10)','');
  2801. AssertEquals('Correct length', '10', TPasStringType(TPasAliasType(TheType).DestType).CodePageExpr);
  2802. AssertNotNull('Have codepage expression', TPasAliasType(TheType).CodePageExpr);
  2803. end;
  2804. procedure TTestTypeParser.TestSimpleTypeStringCodePageIncomplete;
  2805. begin
  2806. DoTypeError('Incomplete string: missing )','string(10');
  2807. end;
  2808. procedure TTestTypeParser.TestSimpleTypeStringCodePageWrong;
  2809. begin
  2810. DoTypeError('Incomplete string, ] instead of (','string(10]');
  2811. end;
  2812. procedure TTestTypeParser.TestSimpleTypeStringCodePageDeprecated;
  2813. begin
  2814. DoTestStringType('String(10)','deprecated');
  2815. end;
  2816. procedure TTestTypeParser.TestSimpleTypeStringCodePagePlatform;
  2817. begin
  2818. DoTestStringType('String(10)','Platform');
  2819. end;
  2820. procedure TTestTypeParser.TestSimpleTypeStringSizeIncomplete;
  2821. begin
  2822. DoTypeError('Incomplete string: missing ]','string[10');
  2823. end;
  2824. procedure TTestTypeParser.TestSimpleTypeStringSizeWrong;
  2825. begin
  2826. DoTypeError('Incomplete string, ) instead of ]','string[10)');
  2827. end;
  2828. procedure TTestTypeParser.TestSimpleTypeStringSizeDeprecated;
  2829. begin
  2830. DoTestStringType('String[10]','deprecated');
  2831. end;
  2832. procedure TTestTypeParser.TestSimpleTypeStringSizePlatform;
  2833. begin
  2834. DoTestStringType('String[10]','Platform');
  2835. end;
  2836. procedure TTestTypeParser.TestSimpleTypeWord;
  2837. BEGIN
  2838. DoTestAliasType('WORD','');
  2839. end;
  2840. procedure TTestTypeParser.TestSimpleTypeWordDeprecated;
  2841. begin
  2842. DoTestAliasType('WORD','deprecated');
  2843. end;
  2844. procedure TTestTypeParser.TestSimpleTypeWordPlatform;
  2845. begin
  2846. DoTestAliasType('WORD','platform');
  2847. end;
  2848. procedure TTestTypeParser.TestSimpleTypeQWord;
  2849. BEGIN
  2850. DoTestAliasType('QWORD','');
  2851. end;
  2852. procedure TTestTypeParser.TestSimpleTypeQWordDeprecated;
  2853. begin
  2854. DoTestAliasType('QWORD','deprecated');
  2855. end;
  2856. procedure TTestTypeParser.TestSimpleTypeQWordPlatform;
  2857. begin
  2858. DoTestAliasType('QWORD','platform');
  2859. end;
  2860. procedure TTestTypeParser.TestSimpleTypeCardinal;
  2861. begin
  2862. DoTestAliasType('CARDINAL','');
  2863. end;
  2864. procedure TTestTypeParser.TestSimpleTypeCardinalDeprecated;
  2865. begin
  2866. DoTestAliasType('CARDINAL','deprecated');
  2867. end;
  2868. procedure TTestTypeParser.TestSimpleTypeCardinalPlatform;
  2869. begin
  2870. DoTestAliasType('CARDINAL','platform');
  2871. end;
  2872. procedure TTestTypeParser.TestSimpleTypeWideChar;
  2873. begin
  2874. DoTestAliasType('WIDECHAR','');
  2875. end;
  2876. procedure TTestTypeParser.TestSimpleTypeWideCharDeprecated;
  2877. begin
  2878. DoTestAliasType('WIDECHAR','deprecated');
  2879. end;
  2880. procedure TTestTypeParser.TestSimpleTypeWideCharPlatform;
  2881. begin
  2882. DoTestAliasType('WIDECHAR','platform');
  2883. end;
  2884. procedure TTestTypeParser.TestPointerSimple;
  2885. begin
  2886. DoParsePointer('integer','');
  2887. end;
  2888. procedure TTestTypeParser.TestPointerSimpleDeprecated;
  2889. begin
  2890. DoParsePointer('integer','deprecated');
  2891. end;
  2892. procedure TTestTypeParser.TestPointerSimplePlatform;
  2893. begin
  2894. DoParsePointer('integer','platform');
  2895. end;
  2896. procedure TTestTypeParser.TestStaticArray;
  2897. begin
  2898. DoParseArray('array [0..2] of integer','',Nil);
  2899. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2900. end;
  2901. procedure TTestTypeParser.TestStaticArrayComment;
  2902. begin
  2903. AddComment:=True;
  2904. TestStaticArray;
  2905. AssertComment;
  2906. end;
  2907. procedure TTestTypeParser.TestStaticArrayDeprecated;
  2908. begin
  2909. DoParseArray('array [0..2] of integer','deprecated',Nil);
  2910. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2911. end;
  2912. procedure TTestTypeParser.TestStaticArrayPlatform;
  2913. begin
  2914. DoParseArray('array [0..2] of integer','platform',Nil);
  2915. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2916. end;
  2917. procedure TTestTypeParser.TestStaticArrayPacked;
  2918. begin
  2919. DoParseArray('packed array [0..2] of integer','',Nil);
  2920. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2921. AssertEquals('Packed',True,TPasArrayType(TheType).IsPacked);
  2922. end;
  2923. procedure TTestTypeParser.TestStaticArrayAsRange;
  2924. begin
  2925. Parser.Options:=Parser.Options+[po_arrayrangeexpr];
  2926. DoParseArray('packed array [0..2] of integer','',Nil);
  2927. AssertEquals('Array has ranges',1,Length(TPasArrayType(TheType).Ranges));
  2928. end;
  2929. procedure TTestTypeParser.TestStaticArrayTypedIndex;
  2930. begin
  2931. DoParseArray('array [Boolean] of integer','',Nil);
  2932. AssertEquals('Array type','Boolean',TPasArrayType(TheType).IndexRange);
  2933. end;
  2934. procedure TTestTypeParser.TestStaticArrayOfMethod;
  2935. begin
  2936. DoParseArray('array[0..127] of procedure of object','',TPasProcedureType);
  2937. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2938. end;
  2939. procedure TTestTypeParser.TestStaticArrayOfProcedure;
  2940. begin
  2941. DoParseArray('array[0..127] of procedure','',TPasProcedureType);
  2942. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2943. end;
  2944. procedure TTestTypeParser.TestDynamicArrayOfMethod;
  2945. begin
  2946. DoParseArray('array of procedure of object','',TPasProcedureType);
  2947. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2948. end;
  2949. procedure TTestTypeParser.TestDynamicArrayOfProcedure;
  2950. begin
  2951. DoParseArray('array of procedure ','',TPasProcedureType);
  2952. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2953. end;
  2954. procedure TTestTypeParser.TestDynamicArray;
  2955. begin
  2956. DoParseArray('array of integer','',Nil);
  2957. AssertEquals('Array type','',TPasArrayType(TheType).IndexRange);
  2958. end;
  2959. procedure TTestTypeParser.TestDynamicArrayComment;
  2960. begin
  2961. AddComment:=True;
  2962. DoParseArray('array of integer','',Nil);
  2963. AssertEquals('Array type','',TPasArrayType(TheType).IndexRange);
  2964. AssertComment;
  2965. end;
  2966. procedure TTestTypeParser.TestGenericArray;
  2967. begin
  2968. Add('Type');
  2969. Add('generic TArray<T> = array of T;');
  2970. // Writeln(source.text);
  2971. ParseDeclarations;
  2972. AssertEquals('One type definition',1,Declarations.Types.Count);
  2973. AssertEquals('First declaration is type definition.',TPasArrayType,TObject(Declarations.Types[0]).ClassType);
  2974. FType:=TPasType(Declarations.Types[0]);
  2975. AssertEquals('First declaration has correct name.','TArray',FType.Name);
  2976. AssertEquals('Array type','',TPasArrayType(TheType).IndexRange);
  2977. AssertEquals('Generic Array type',True,TPasArrayType(TheType).IsGenericArray);
  2978. end;
  2979. procedure TTestTypeParser.TestSimpleEnumerated;
  2980. begin
  2981. DoParseEnumerated('(one,two,three)','',3);
  2982. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2983. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2984. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2985. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2986. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2987. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2988. end;
  2989. procedure TTestTypeParser.TestSimpleEnumeratedComment;
  2990. begin
  2991. AddComment:=True;
  2992. TestSimpleEnumerated;
  2993. AssertComment;
  2994. AssertEquals('No comment on enum 0','',TPasEnumValue(TPasEnumType(TheType).Values[0]).DocComment);
  2995. AssertEquals('No comment on enum 1','',TPasEnumValue(TPasEnumType(TheType).Values[1]).DocComment);
  2996. AssertEquals('No comment on enum 2','',TPasEnumValue(TPasEnumType(TheType).Values[2]).DocComment);
  2997. end;
  2998. procedure TTestTypeParser.TestSimpleEnumeratedComment2;
  2999. begin
  3000. AddComment:=True;
  3001. DoParseEnumerated('( {a} one, {b} two, {c} three)','',3);
  3002. AssertEquals('comment on enum 0','a'+sLineBreak,TPasEnumValue(TPasEnumType(TheType).Values[0]).DocComment);
  3003. AssertEquals('comment on enum 1','b'+sLineBreak,TPasEnumValue(TPasEnumType(TheType).Values[1]).DocComment);
  3004. AssertEquals('comment on enum 2','c'+sLineBreak,TPasEnumValue(TPasEnumType(TheType).Values[2]).DocComment);
  3005. end;
  3006. procedure TTestTypeParser.TestSimpleEnumeratedDeprecated;
  3007. begin
  3008. DoParseEnumerated('(one,two,three)','deprecated',3);
  3009. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  3010. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  3011. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  3012. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  3013. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  3014. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  3015. end;
  3016. procedure TTestTypeParser.TestSimpleEnumeratedPlatform;
  3017. begin
  3018. DoParseEnumerated('(one,two,three)','platform',3);
  3019. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  3020. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  3021. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  3022. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  3023. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  3024. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  3025. end;
  3026. procedure TTestTypeParser.TestAssignedEnumerated;
  3027. begin
  3028. DoParseEnumerated('(one,two:=2,three)','',3);
  3029. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  3030. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  3031. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  3032. AssertEquals('Assigned value enumerated','2',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  3033. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  3034. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  3035. end;
  3036. procedure TTestTypeParser.TestAssignedEnumeratedDeprecated;
  3037. begin
  3038. DoParseEnumerated('(one,two:=2,three)','',3);
  3039. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  3040. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  3041. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  3042. AssertEquals('Assigned value enumerated','2',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  3043. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  3044. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  3045. end;
  3046. procedure TTestTypeParser.TestAssignedEnumeratedPlatform;
  3047. begin
  3048. DoParseEnumerated('(one,two:=2,three)','',3);
  3049. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  3050. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  3051. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  3052. AssertEquals('Assigned value enumerated','2',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  3053. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  3054. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  3055. end;
  3056. procedure TTestTypeParser.TestFileType;
  3057. begin
  3058. DoTestFileType('integer','');
  3059. end;
  3060. procedure TTestTypeParser.TestFileTypeDeprecated;
  3061. begin
  3062. DoTestFileType('integer','deprecated');
  3063. end;
  3064. procedure TTestTypeParser.TestFileTypePlatform;
  3065. begin
  3066. DoTestFileType('integer','platform');
  3067. end;
  3068. procedure TTestTypeParser.TestRangeType;
  3069. begin
  3070. DoTestRangeType('1','4','');
  3071. end;
  3072. procedure TTestTypeParser.TestCharRangeType;
  3073. begin
  3074. DoTestRangeType('#1','#4','');
  3075. end;
  3076. procedure TTestTypeParser.TestCharRangeType2;
  3077. begin
  3078. DoTestRangeType('''A''','''B''','');
  3079. end;
  3080. procedure TTestTypeParser.TestRangeTypeDeprecated;
  3081. begin
  3082. DoTestRangeType('1','4','deprecated');
  3083. end;
  3084. procedure TTestTypeParser.TestRangeTypePlatform;
  3085. begin
  3086. DoTestRangeType('1','4','platform');
  3087. end;
  3088. procedure TTestTypeParser.TestIdentifierRangeType;
  3089. begin
  3090. DoTestRangeType('tkFirst','tkLast','');
  3091. end;
  3092. procedure TTestTypeParser.TestIdentifierRangeTypeDeprecated;
  3093. begin
  3094. DoTestRangeType('tkFirst','tkLast','deprecated');
  3095. end;
  3096. procedure TTestTypeParser.TestIdentifierRangeTypePlatform;
  3097. begin
  3098. DoTestRangeType('tkFirst','tkLast','platform');
  3099. end;
  3100. procedure TTestTypeParser.TestNegativeIdentifierRangeType;
  3101. begin
  3102. DoTestRangeType('-tkLast','tkLast','');
  3103. end;
  3104. procedure TTestTypeParser.TestSimpleSet;
  3105. begin
  3106. DoParseSimpleSet('Byte','');
  3107. end;
  3108. procedure TTestTypeParser.TestSimpleSetDeprecated;
  3109. begin
  3110. DoParseSimpleSet('Byte','deprecated');
  3111. end;
  3112. procedure TTestTypeParser.TestSimpleSetPlatform;
  3113. begin
  3114. DoParseSimpleSet('Byte','platform');
  3115. end;
  3116. procedure TTestTypeParser.TestComplexSet;
  3117. begin
  3118. DoParseComplexSet('(one, two, three)','');
  3119. DoTestComplexSet;
  3120. end;
  3121. procedure TTestTypeParser.TestComplexSetDeprecated;
  3122. begin
  3123. DoParseComplexSet('(one, two, three)','deprecated');
  3124. DoTestComplexSet;
  3125. end;
  3126. procedure TTestTypeParser.TestComplexSetPlatform;
  3127. begin
  3128. DoParseComplexSet('(one, two, three)','platform');
  3129. DoTestComplexSet;
  3130. end;
  3131. procedure TTestTypeParser.TestPackedSet;
  3132. begin
  3133. DoParseSimpleSet('Byte','',True);
  3134. end;
  3135. procedure TTestTypeParser.TestRangeLowHigh;
  3136. begin
  3137. DoParseRangeSet('low(TRange)..high(TRange)','');
  3138. end;
  3139. procedure TTestTypeParser.TestRangeSet;
  3140. begin
  3141. // TRange = (rLow, rMiddle, rHigh);
  3142. DoParseRangeSet('rMiddle..high(TRange)','');
  3143. end;
  3144. procedure TTestTypeParser.TestSubRangeSet;
  3145. begin
  3146. DoParseRangeSet('0..SizeOf(Integer)*8-1','');
  3147. end;
  3148. procedure TTestTypeParser.TestRangeSetDeprecated;
  3149. begin
  3150. DoParseRangeSet('0..SizeOf(Integer)*8-1','deprecated');
  3151. end;
  3152. procedure TTestTypeParser.TestRangeSetPlatform;
  3153. begin
  3154. DoParseRangeSet('0..SizeOf(Integer)*8-1','platform');
  3155. end;
  3156. procedure TTestTypeParser.TestNegativeRangeType;
  3157. begin
  3158. DoTestRangeType('2-1','3','');
  3159. end;
  3160. procedure TTestTypeParser.TestClassOf;
  3161. begin
  3162. DoTestClassOf('');
  3163. end;
  3164. procedure TTestTypeParser.TestClassOfComment;
  3165. begin
  3166. AddComment:=True;
  3167. DoTestClassOf('');
  3168. AssertComment;
  3169. end;
  3170. procedure TTestTypeParser.TestClassOfDeprecated;
  3171. begin
  3172. DoTestClassOf('deprecated');
  3173. end;
  3174. procedure TTestTypeParser.TestClassOfPlatform;
  3175. begin
  3176. DoTestClassOf('Platform');
  3177. end;
  3178. procedure TTestTypeParser.TestReferenceAlias;
  3179. begin
  3180. Add('Type');
  3181. Add(' Type1 = Integer;');
  3182. Add(' Type2 = Type1;');
  3183. Add('end.');
  3184. ParseDeclarations;
  3185. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3186. AssertEquals('First declaration is type definition.',TPasAliasType,TObject(Declarations.Types[0]).ClassType);
  3187. AssertEquals('Second declaration is type definition.',TPasAliasType,TObject(Declarations.Types[1]).ClassType);
  3188. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3189. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3190. AssertSame('Second declaration references first.',Declarations.Types[0],TPasAliasType(Declarations.Types[1]).DestType);
  3191. end;
  3192. procedure TTestTypeParser.TestReferenceSet;
  3193. begin
  3194. Add('Type');
  3195. Add(' Type1 = (a,b,c);');
  3196. Add(' Type2 = set of Type1;');
  3197. Add('end.');
  3198. ParseDeclarations;
  3199. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3200. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3201. AssertEquals('Second declaration is type definition.',TPasSetType,TObject(Declarations.Types[1]).ClassType);
  3202. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3203. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3204. AssertSame('Second declaration references first.',Declarations.Types[0],TPasSetType(Declarations.Types[1]).EnumType);
  3205. end;
  3206. procedure TTestTypeParser.TestReferenceClassOf;
  3207. begin
  3208. Add('Type');
  3209. Add(' Type1 = Class(TObject);');
  3210. Add(' Type2 = Class of Type1;');
  3211. Add('end.');
  3212. ParseDeclarations;
  3213. AssertEquals('1 type definitions',1,Declarations.Types.Count);
  3214. AssertEquals('1 class definitions',1,Declarations.Classes.Count);
  3215. AssertEquals('First declaration is class definition.',TPasClassType,TObject(Declarations.Classes[0]).ClassType);
  3216. AssertEquals('Second declaration is type definition.',TPasClassOfType,TObject(Declarations.Types[0]).ClassType);
  3217. AssertEquals('First declaration has correct name.','Type2',TPasType(Declarations.Types[0]).Name);
  3218. AssertEquals('Second declaration has correct name.','Type1',TPasType(Declarations.Classes[0]).Name);
  3219. AssertSame('Second declaration references first.',Declarations.Classes[0],TPasClassOfType(Declarations.Types[0]).DestType);
  3220. end;
  3221. procedure TTestTypeParser.TestReferenceFile;
  3222. begin
  3223. Add('Type');
  3224. Add(' Type1 = (a,b,c);');
  3225. Add(' Type2 = File of Type1;');
  3226. Add('end.');
  3227. ParseDeclarations;
  3228. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3229. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3230. AssertEquals('Second declaration is type definition.',TPasFileType,TObject(Declarations.Types[1]).ClassType);
  3231. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3232. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3233. AssertSame('Second declaration references first.',Declarations.Types[0],TPasFileType(Declarations.Types[1]).elType);
  3234. end;
  3235. procedure TTestTypeParser.TestReferenceArray;
  3236. begin
  3237. Add('Type');
  3238. Add(' Type1 = (a,b,c);');
  3239. Add(' Type2 = Array of Type1;');
  3240. Add('end.');
  3241. ParseDeclarations;
  3242. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3243. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3244. AssertEquals('Second declaration is type definition.',TPasArrayType,TObject(Declarations.Types[1]).ClassType);
  3245. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3246. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3247. AssertSame('Second declaration references first.',Declarations.Types[0],TPasArrayType(Declarations.Types[1]).elType);
  3248. end;
  3249. procedure TTestTypeParser.TestReferencePointer;
  3250. begin
  3251. Add('Type');
  3252. Add(' Type1 = (a,b,c);');
  3253. Add(' Type2 = ^Type1;');
  3254. Add('end.');
  3255. ParseDeclarations;
  3256. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3257. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3258. AssertEquals('Second declaration is type definition.',TPasPointerType,TObject(Declarations.Types[1]).ClassType);
  3259. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3260. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3261. AssertSame('Second declaration references first.',Declarations.Types[0],TPasPointerType(Declarations.Types[1]).DestType);
  3262. end;
  3263. procedure TTestTypeParser.TestInvalidColon;
  3264. var
  3265. ok: Boolean;
  3266. begin
  3267. ok:=false;
  3268. try
  3269. ParseType(':1..2',TPasSetType);
  3270. except
  3271. on E: EParserError do
  3272. ok:=true;
  3273. end;
  3274. AssertEquals('wrong colon in type raised an error',true,ok);
  3275. end;
  3276. procedure TTestTypeParser.StartTypeHelper(ForType: String; AParent: String);
  3277. Var
  3278. S : String;
  3279. begin
  3280. S:='TMyClass = Type Helper';
  3281. if (AParent<>'') then
  3282. begin
  3283. S:=S+'('+AParent;
  3284. S:=S+')';
  3285. end;
  3286. S:=S+' for '+ForType;
  3287. Add(S);
  3288. end;
  3289. procedure TTestTypeParser.TestTypeHelper;
  3290. begin
  3291. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msTypeHelpers];
  3292. ParseType('Type Helper for AnsiString end',TPasClassType,'');
  3293. end;
  3294. procedure TTestTypeParser.TestTypeHelperWithParent;
  3295. begin
  3296. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msTypeHelpers];
  3297. ParseType('Type Helper(TOtherHelper) for AnsiString end',TPasClassType,'');
  3298. end;
  3299. procedure TTestTypeParser.TestPointerReference;
  3300. begin
  3301. Add('Type');
  3302. Add(' pReference = ^Reference;');
  3303. Add(' Reference = object');
  3304. Add(' end;');
  3305. ParseDeclarations;
  3306. AssertEquals('type definition count',1,Declarations.Types.Count);
  3307. AssertEquals('object definition count',1,Declarations.Classes.Count);
  3308. end;
  3309. procedure TTestTypeParser.TestPointerKeyWord;
  3310. begin
  3311. Add('type');
  3312. Add(' &file = object');
  3313. Add(' end;');
  3314. ParseDeclarations;
  3315. AssertEquals('object definition count',1,Declarations.Classes.Count);
  3316. end;
  3317. procedure TTestTypeParser.TestPointerFile;
  3318. begin
  3319. Add('type');
  3320. Add(' pfile = ^file;');
  3321. ParseDeclarations;
  3322. AssertEquals('object definition count',1,Declarations.Types.Count);
  3323. end;
  3324. procedure TTestTypeParser.TestAbstractObject;
  3325. begin
  3326. Add('Type');
  3327. Add(' Reference = object abstract');
  3328. Add(' x : integer;');
  3329. Add(' end;');
  3330. ParseDeclarations;
  3331. AssertEquals('object definition count',1,Declarations.Classes.Count);
  3332. AssertEquals('object is abstract',True,TPasClassType(Declarations.Classes[0]).IsAbstract);
  3333. AssertEquals('object is object',True,TPasClassType(Declarations.Classes[0]).ObjKind=okObject);
  3334. end;
  3335. initialization
  3336. RegisterTests([TTestTypeParser,TTestRecordTypeParser,TTestProcedureTypeParser]);
  3337. end.