tctypeparser.pas 121 KB

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