tctypeparser.pas 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681
  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
  2302. // Delphi 10.3.2 allows this
  2303. Const
  2304. Src =
  2305. '{$mode objfpc}'+sLineBreak+
  2306. '{$modeswitch advancedrecords}'+sLineBreak+
  2307. 'program afile;'+sLineBreak+
  2308. 'var a : Procedure;'+sLineBreak+
  2309. 'begin'+sLineBreak+
  2310. ' a := '+sLineBreak+
  2311. ' procedure '+sLineBreak+
  2312. ' type'+sLineBreak+
  2313. ' TMyRecord = record'+sLineBreak+
  2314. ' private'+sLineBreak+
  2315. ' x, y, z: integer;'+sLineBreak+
  2316. ' end;'+sLineBreak+
  2317. ' begin'+sLineBreak+
  2318. ' end;'+sLineBreak+
  2319. 'end.';
  2320. begin
  2321. Source.Text:=Src;
  2322. ParseModule; // We're just interested in that it parses.
  2323. end;
  2324. procedure TTestRecordTypeParser.TestAdvRecordClassOperator;
  2325. // Source from bug id 36180
  2326. Const
  2327. SRC =
  2328. '{$mode objfpc}'+sLineBreak+
  2329. '{$modeswitch advancedrecords}'+sLineBreak+
  2330. 'program afile;'+sLineBreak+
  2331. 'type'+sLineBreak+
  2332. ' TMyRecord = record'+sLineBreak+
  2333. ' class operator = (a, b: TMyRecord): boolean;'+sLineBreak+
  2334. ' end;'+sLineBreak+
  2335. 'class operator TMyRecord.= (a, b: TMyRecord): boolean;'+sLineBreak+
  2336. 'begin'+sLineBreak+
  2337. ' result := (@a = @b);'+sLineBreak+
  2338. 'end;'+sLineBreak+
  2339. 'begin'+sLineBreak+
  2340. 'end.';
  2341. begin
  2342. Source.Text:=Src;
  2343. ParseModule; // We're just interested in that it parses.
  2344. end;
  2345. { TBaseTestTypeParser }
  2346. Function TBaseTestTypeParser.ParseType(ASource: String; ATypeClass: TClass;
  2347. Const AHint: String): TPasType;
  2348. Var
  2349. D : String;
  2350. begin
  2351. Hint:=AHint;
  2352. Add('Type');
  2353. If AddComment then
  2354. begin
  2355. Add('// A comment');
  2356. Parser.Engine.NeedComments:=True;
  2357. end;
  2358. D:='A = '+ASource;
  2359. If Hint<>'' then
  2360. D:=D+' '+Hint;
  2361. Add(' '+D+';');
  2362. // Writeln(source.text);
  2363. ParseDeclarations;
  2364. if ATypeClass.InHeritsFrom(TPasClassType) then
  2365. AssertEquals('One type definition',1,Declarations.Classes.Count)
  2366. else
  2367. AssertEquals('One type definition',1,Declarations.Types.Count);
  2368. If ATypeClass<>Nil then
  2369. begin
  2370. if ATypeClass.InheritsFrom(TPasClassType) then
  2371. Result:=TPasType(Declarations.Classes[0])
  2372. else
  2373. Result:=TPasType(Declarations.Types[0]);
  2374. AssertEquals('First declaration is type definition.',ATypeClass,Result.ClassType);
  2375. end;
  2376. AssertEquals('First declaration has correct name.','A',Result.Name);
  2377. FType:=Result;
  2378. Definition:=Result;
  2379. if (Hint<>'') then
  2380. CheckHint(TPasMemberHint(GetEnumValue(TypeInfo(TPasMemberHint),'h'+Hint)));
  2381. end;
  2382. Procedure TBaseTestTypeParser.AssertParseTypeError(ASource: String);
  2383. begin
  2384. try
  2385. ParseType(ASource,Nil,'');
  2386. Fail('Expected parser error');
  2387. except
  2388. // all OK.
  2389. end;
  2390. end;
  2391. Procedure TBaseTestTypeParser.AssertComment;
  2392. begin
  2393. AssertNotNull('Have type',TheType);
  2394. AssertEquals('Type comment',' A comment'+sLineBreak,TheTYpe.DocComment);
  2395. end;
  2396. procedure TBaseTestTypeParser.SetUp;
  2397. begin
  2398. Inherited;
  2399. FErrorSource:='';
  2400. FHint:='';
  2401. FType:=Nil;
  2402. end;
  2403. Procedure TBaseTestTypeParser.TearDown;
  2404. begin
  2405. inherited TearDown;
  2406. FType:=Nil;
  2407. end;
  2408. { TTestTypeParser }
  2409. procedure TTestTypeParser.DoTestAliasType(const AnAliasType: String;
  2410. const AHint: String);
  2411. begin
  2412. ParseType(AnAliasType,TPasAliasType,AHint);
  2413. AssertEquals('Unresolved type',TPasUnresolvedTypeRef,TPasAliasType(TheType).DestType.ClassType);
  2414. end;
  2415. procedure TTestTypeParser.DoTestStringType(const AnAliasType: String;
  2416. const AHint: String);
  2417. begin
  2418. ParseType(AnAliasType,TPasAliasType,AHint);
  2419. AssertEquals('String type',TPasStringType,TPasAliasType(TheType).DestType.ClassType);
  2420. end;
  2421. procedure TTestTypeParser.DoTypeError(const AMsg, ASource: string);
  2422. begin
  2423. FErrorSource:=ASource;
  2424. AssertException(AMsg,EParserError,@DoParseError);
  2425. end;
  2426. procedure TTestTypeParser.DoParseError;
  2427. begin
  2428. ParseType(FErrorSource,Nil);
  2429. end;
  2430. procedure TTestTypeParser.DoParsePointer(const ASource: String;
  2431. const AHint: String; ADestType: TClass);
  2432. begin
  2433. ParseType('^'+ASource,TPasPointerType,AHint);
  2434. if ADestType = Nil then
  2435. ADestType:=TPasUnresolvedTypeRef;
  2436. AssertEquals('Destination type '+ADestType.ClassName,ADestType,TPasPointerType(TheType).DestType.ClassType);
  2437. end;
  2438. procedure TTestTypeParser.DoParseArray(const ASource: String;
  2439. const AHint: String; ADestType: TClass);
  2440. begin
  2441. ParseType(ASource,TPasArrayType,AHint);
  2442. if ADestType = Nil then
  2443. ADestType:=TPasUnresolvedTypeRef;
  2444. AssertEquals('Destination type '+ADestType.ClassName,ADestType,TPasArrayType(TheType).ElType.ClassType);
  2445. end;
  2446. procedure TTestTypeParser.DoParseEnumerated(const ASource: String;
  2447. const AHint: String; ACount: integer);
  2448. Var
  2449. I : Integer;
  2450. begin
  2451. ParseType(ASource,TPasEnumType,AHint);
  2452. AssertNotNull('Have values',TPasEnumType(TheType).Values);
  2453. AssertEquals('Value count',ACount,TPasEnumType(TheType).Values.Count);
  2454. For I:=0 to TPasEnumType(TheType).Values.Count-1 do
  2455. AssertEquals('Enum value typed element '+IntToStr(I),TPasEnumValue,TObject(TPasEnumType(TheType).Values[i]).ClassType);
  2456. end;
  2457. procedure TTestTypeParser.DoTestFileType(const AType: String;
  2458. const AHint: String; ADestType: TClass);
  2459. begin
  2460. ParseType('File of '+AType,TPasFileType,AHint);
  2461. AssertNotNull('Have element type',TPasFileType(TheType).ElType);
  2462. if ADestType = Nil then
  2463. ADestType:=TPasUnresolvedTypeRef;
  2464. AssertEquals('Element type '+ADestType.ClassName,ADestType,TPasFileType(TheType).ElType.ClassType);
  2465. end;
  2466. procedure TTestTypeParser.DoTestRangeType(const AStart, AStop, AHint: String);
  2467. begin
  2468. ParseType(AStart+'..'+AStop,TPasRangeType,AHint);
  2469. AssertEquals('Range start',AStart,Stringreplace(TPasRangeType(TheType).RangeStart,' ','',[rfReplaceAll]));
  2470. AssertEquals('Range start',AStop,Stringreplace(TPasRangeType(TheType).RangeEnd,' ','',[rfReplaceAll]));
  2471. end;
  2472. procedure TTestTypeParser.DoParseSimpleSet(const ASource: String; const AHint: String; IsPacked: Boolean);
  2473. begin
  2474. if IsPacked then
  2475. ParseType('Packed Set of '+ASource,TPasSetType,AHint)
  2476. else
  2477. ParseType('Set of '+ASource,TPasSetType,AHint);
  2478. AssertNotNull('Have enumtype',TPasSetType(TheType).EnumType);
  2479. AssertEquals('Element type ',TPasUnresolvedTypeRef,TPasSetType(TheType).EnumType.ClassType);
  2480. AssertEquals('IsPacked is correct',isPacked,TPasSetType(TheType).IsPacked);
  2481. end;
  2482. procedure TTestTypeParser.DoParseComplexSet(const ASource: String;
  2483. const AHint: String);
  2484. begin
  2485. ParseType('Set of '+ASource,TPasSetType,AHint);
  2486. AssertNotNull('Have enumtype',TPasSetType(TheType).EnumType);
  2487. AssertEquals('Element type ',TPasEnumType,TPasSetType(TheType).EnumType.ClassType);
  2488. end;
  2489. procedure TTestTypeParser.DoParseRangeSet(const ASource: String;
  2490. const AHint: String);
  2491. begin
  2492. ParseType('Set of '+ASource,TPasSetType,AHint);
  2493. AssertNotNull('Have enumtype',TPasSetType(TheType).EnumType);
  2494. AssertEquals('Element type ',TPasRangeType,TPasSetType(TheType).EnumType.ClassType);
  2495. end;
  2496. procedure TTestTypeParser.DoTestComplexSet;
  2497. Var
  2498. I : integer;
  2499. begin
  2500. AssertNotNull('Have values',TPasEnumType(TPasSetType(TheType).EnumType).Values);
  2501. AssertEquals('Have 3 values',3, TPasEnumType(TPasSetType(TheType).EnumType).Values.Count);
  2502. For I:=0 to TPasEnumType(TPasSetType(TheType).EnumType).Values.Count-1 do
  2503. AssertEquals('Enum value typed element '+IntToStr(I),TPasEnumValue,TObject(TPasEnumType(TPasSetType(TheType).EnumType).Values[i]).ClassType);
  2504. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[0]).Name);
  2505. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[1]).Name);
  2506. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[2]).Name);
  2507. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[0]).AssignedValue);
  2508. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[1]).AssignedValue);
  2509. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TPasSetType(TheType).EnumType).Values[2]).AssignedValue);
  2510. end;
  2511. procedure TTestTypeParser.DoTestClassOf(const AHint: string);
  2512. begin
  2513. ParseType('Class of TSomeClass',TPasClassOfType,AHint);
  2514. AssertNotNull('Have class type',TPasClassOfType(TheType).DestType);
  2515. AssertEquals('Element type ',TPasUnresolvedTypeRef,TPasClassOfType(TheType).DestType.ClassType);
  2516. end;
  2517. procedure TTestTypeParser.TestAliasType;
  2518. begin
  2519. DoTestAliasType('othertype','');
  2520. AssertEquals('Unresolved type name ','othertype',TPasUnresolvedTypeRef(TPasAliasType(TheType).DestType).name);
  2521. end;
  2522. procedure TTestTypeParser.TestCrossUnitAliasType;
  2523. begin
  2524. DoTestAliasType('otherunit.othertype','');
  2525. end;
  2526. procedure TTestTypeParser.TestAliasTypeDeprecated;
  2527. begin
  2528. DoTestALiasType('othertype','deprecated');
  2529. end;
  2530. procedure TTestTypeParser.TestAliasTypePlatform;
  2531. begin
  2532. DoTestALiasType('othertype','platform');
  2533. end;
  2534. procedure TTestTypeParser.TestSimpleTypeByte;
  2535. begin
  2536. DoTestAliasType('BYTE','');
  2537. end;
  2538. procedure TTestTypeParser.TestSimpleTypeByteComment;
  2539. begin
  2540. AddComment:=True;
  2541. DoTestAliasType('BYTE','');
  2542. AssertComment;
  2543. end;
  2544. procedure TTestTypeParser.TestSimpleTypeByteDeprecated;
  2545. begin
  2546. DoTestAliasType('BYTE','deprecated');
  2547. end;
  2548. procedure TTestTypeParser.TestSimpleTypeBytePlatform;
  2549. begin
  2550. DoTestAliasType('BYTE','platform');
  2551. end;
  2552. procedure TTestTypeParser.TestSimpleTypeBoolean;
  2553. begin
  2554. DoTestAliasType('BOOLEAN','');
  2555. end;
  2556. procedure TTestTypeParser.TestSimpleTypeBooleanDeprecated;
  2557. begin
  2558. DoTestAliasType('BOOLEAN','deprecated');
  2559. end;
  2560. procedure TTestTypeParser.TestSimpleTypeBooleanPlatform;
  2561. begin
  2562. DoTestAliasType('BOOLEAN','platform');
  2563. end;
  2564. procedure TTestTypeParser.TestSimpleTypeChar;
  2565. begin
  2566. DoTestAliasType('CHAR','');
  2567. end;
  2568. procedure TTestTypeParser.TestSimpleTypeCharDeprecated;
  2569. begin
  2570. DoTestAliasType('CHAR','deprecated');
  2571. end;
  2572. procedure TTestTypeParser.TestSimpleTypeCharPlatform;
  2573. begin
  2574. DoTestAliasType('CHAR','platform');
  2575. end;
  2576. procedure TTestTypeParser.TestSimpleTypeInteger;
  2577. begin
  2578. DoTestAliasType('INTEGER','');
  2579. end;
  2580. procedure TTestTypeParser.TestSimpleTypeIntegerDeprecated;
  2581. begin
  2582. DoTestAliasType('INTEGER','deprecated');
  2583. end;
  2584. procedure TTestTypeParser.TestSimpleTypeIntegerPlatform;
  2585. begin
  2586. DoTestAliasType('INTEGER','platform');
  2587. end;
  2588. procedure TTestTypeParser.TestSimpleTypeInt64;
  2589. begin
  2590. DoTestAliasType('INT64','');
  2591. end;
  2592. procedure TTestTypeParser.TestSimpleTypeInt64Deprecated;
  2593. begin
  2594. DoTestAliasType('INT64','deprecated');
  2595. end;
  2596. procedure TTestTypeParser.TestSimpleTypeInt64Platform;
  2597. begin
  2598. DoTestAliasType('INT64','platform');
  2599. end;
  2600. procedure TTestTypeParser.TestSimpleTypeLongInt;
  2601. begin
  2602. DoTestAliasType('LONGINT','');
  2603. end;
  2604. procedure TTestTypeParser.TestSimpleTypeLongIntDeprecated;
  2605. begin
  2606. DoTestAliasType('LONGINT','deprecated');
  2607. end;
  2608. procedure TTestTypeParser.TestSimpleTypeLongIntPlatform;
  2609. begin
  2610. DoTestAliasType('LONGINT','platform');
  2611. end;
  2612. procedure TTestTypeParser.TestSimpleTypeLongWord;
  2613. begin
  2614. DoTestAliasType('LONGWORD','');
  2615. end;
  2616. procedure TTestTypeParser.TestSimpleTypeLongWordDeprecated;
  2617. begin
  2618. DoTestAliasType('LONGWORD','deprecated');
  2619. end;
  2620. procedure TTestTypeParser.TestSimpleTypeLongWordPlatform;
  2621. begin
  2622. DoTestAliasType('LONGWORD','platform');
  2623. end;
  2624. procedure TTestTypeParser.TestSimpleTypeDouble;
  2625. begin
  2626. DoTestAliasType('Double','');
  2627. end;
  2628. procedure TTestTypeParser.TestSimpleTypeDoubleDeprecated;
  2629. begin
  2630. DoTestAliasType('Double','deprecated');
  2631. end;
  2632. procedure TTestTypeParser.TestSimpleTypeDoublePlatform;
  2633. begin
  2634. DoTestAliasType('Double','platform');
  2635. end;
  2636. procedure TTestTypeParser.TestSimpleTypeShortInt;
  2637. begin
  2638. DoTestAliasType('SHORTINT','');
  2639. end;
  2640. procedure TTestTypeParser.TestSimpleTypeShortIntDeprecated;
  2641. begin
  2642. DoTestAliasType('SHORTINT','deprecated');
  2643. end;
  2644. procedure TTestTypeParser.TestSimpleTypeShortIntPlatform;
  2645. begin
  2646. DoTestAliasType('SHORTINT','platform');
  2647. end;
  2648. procedure TTestTypeParser.TestSimpleTypeSmallInt;
  2649. begin
  2650. DoTestAliasType('SMALLINT','');
  2651. end;
  2652. procedure TTestTypeParser.TestSimpleTypeSmallIntDeprecated;
  2653. begin
  2654. DoTestAliasType('SMALLINT','deprecated');
  2655. end;
  2656. procedure TTestTypeParser.TestSimpleTypeSmallIntPlatform;
  2657. begin
  2658. DoTestAliasType('SMALLINT','platform');
  2659. end;
  2660. procedure TTestTypeParser.TestSimpleTypeString;
  2661. begin
  2662. DoTestAliasType('STRING','');
  2663. end;
  2664. procedure TTestTypeParser.TestSimpleTypeStringDeprecated;
  2665. begin
  2666. DoTestAliasType('STRING','deprecated');
  2667. end;
  2668. procedure TTestTypeParser.TestSimpleTypeStringPlatform;
  2669. begin
  2670. DoTestAliasType('STRING','platform');
  2671. end;
  2672. procedure TTestTypeParser.TestSimpleTypeStringSize;
  2673. begin
  2674. DoTestStringType('String[10]','');
  2675. end;
  2676. procedure TTestTypeParser.TestSimpleTypeStringSizeIncomplete;
  2677. begin
  2678. DoTypeError('Incomplete string: missing ]','string[10');
  2679. end;
  2680. procedure TTestTypeParser.TestSimpleTypeStringSizeWrong;
  2681. begin
  2682. DoTypeError('Incomplete string, ) instead of ]','string[10)');
  2683. end;
  2684. procedure TTestTypeParser.TestSimpleTypeStringSizeDeprecated;
  2685. begin
  2686. DoTestStringType('String[10]','deprecated');
  2687. end;
  2688. procedure TTestTypeParser.TestSimpleTypeStringSizePlatform;
  2689. begin
  2690. DoTestStringType('String[10]','Platform');
  2691. end;
  2692. procedure TTestTypeParser.TestSimpleTypeWord;
  2693. BEGIN
  2694. DoTestAliasType('WORD','');
  2695. end;
  2696. procedure TTestTypeParser.TestSimpleTypeWordDeprecated;
  2697. begin
  2698. DoTestAliasType('WORD','deprecated');
  2699. end;
  2700. procedure TTestTypeParser.TestSimpleTypeWordPlatform;
  2701. begin
  2702. DoTestAliasType('WORD','platform');
  2703. end;
  2704. procedure TTestTypeParser.TestSimpleTypeQWord;
  2705. BEGIN
  2706. DoTestAliasType('QWORD','');
  2707. end;
  2708. procedure TTestTypeParser.TestSimpleTypeQWordDeprecated;
  2709. begin
  2710. DoTestAliasType('QWORD','deprecated');
  2711. end;
  2712. procedure TTestTypeParser.TestSimpleTypeQWordPlatform;
  2713. begin
  2714. DoTestAliasType('QWORD','platform');
  2715. end;
  2716. procedure TTestTypeParser.TestSimpleTypeCardinal;
  2717. begin
  2718. DoTestAliasType('CARDINAL','');
  2719. end;
  2720. procedure TTestTypeParser.TestSimpleTypeCardinalDeprecated;
  2721. begin
  2722. DoTestAliasType('CARDINAL','deprecated');
  2723. end;
  2724. procedure TTestTypeParser.TestSimpleTypeCardinalPlatform;
  2725. begin
  2726. DoTestAliasType('CARDINAL','platform');
  2727. end;
  2728. procedure TTestTypeParser.TestSimpleTypeWideChar;
  2729. begin
  2730. DoTestAliasType('WIDECHAR','');
  2731. end;
  2732. procedure TTestTypeParser.TestSimpleTypeWideCharDeprecated;
  2733. begin
  2734. DoTestAliasType('WIDECHAR','deprecated');
  2735. end;
  2736. procedure TTestTypeParser.TestSimpleTypeWideCharPlatform;
  2737. begin
  2738. DoTestAliasType('WIDECHAR','platform');
  2739. end;
  2740. procedure TTestTypeParser.TestPointerSimple;
  2741. begin
  2742. DoParsePointer('integer','');
  2743. end;
  2744. procedure TTestTypeParser.TestPointerSimpleDeprecated;
  2745. begin
  2746. DoParsePointer('integer','deprecated');
  2747. end;
  2748. procedure TTestTypeParser.TestPointerSimplePlatform;
  2749. begin
  2750. DoParsePointer('integer','platform');
  2751. end;
  2752. procedure TTestTypeParser.TestStaticArray;
  2753. begin
  2754. DoParseArray('array [0..2] of integer','',Nil);
  2755. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2756. end;
  2757. procedure TTestTypeParser.TestStaticArrayComment;
  2758. begin
  2759. AddComment:=True;
  2760. TestStaticArray;
  2761. AssertComment;
  2762. end;
  2763. procedure TTestTypeParser.TestStaticArrayDeprecated;
  2764. begin
  2765. DoParseArray('array [0..2] of integer','deprecated',Nil);
  2766. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2767. end;
  2768. procedure TTestTypeParser.TestStaticArrayPlatform;
  2769. begin
  2770. DoParseArray('array [0..2] of integer','platform',Nil);
  2771. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2772. end;
  2773. procedure TTestTypeParser.TestStaticArrayPacked;
  2774. begin
  2775. DoParseArray('packed array [0..2] of integer','',Nil);
  2776. AssertEquals('Array type','0..2',TPasArrayType(TheType).IndexRange);
  2777. AssertEquals('Packed',True,TPasArrayType(TheType).IsPacked);
  2778. end;
  2779. procedure TTestTypeParser.TestStaticArrayTypedIndex;
  2780. begin
  2781. DoParseArray('array [Boolean] of integer','',Nil);
  2782. AssertEquals('Array type','Boolean',TPasArrayType(TheType).IndexRange);
  2783. end;
  2784. procedure TTestTypeParser.TestStaticArrayOfMethod;
  2785. begin
  2786. DoParseArray('array[0..127] of procedure of object','',TPasProcedureType);
  2787. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2788. end;
  2789. procedure TTestTypeParser.TestStaticArrayOfProcedure;
  2790. begin
  2791. DoParseArray('array[0..127] of procedure','',TPasProcedureType);
  2792. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2793. end;
  2794. procedure TTestTypeParser.TestDynamicArrayOfMethod;
  2795. begin
  2796. DoParseArray('array of procedure of object','',TPasProcedureType);
  2797. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2798. end;
  2799. procedure TTestTypeParser.TestDynamicArrayOfProcedure;
  2800. begin
  2801. DoParseArray('array of procedure ','',TPasProcedureType);
  2802. AssertEquals('Array element type',TPasProcedureType,TPasArrayType(TheType).ElType.ClassType);
  2803. end;
  2804. procedure TTestTypeParser.TestDynamicArray;
  2805. begin
  2806. DoParseArray('array of integer','',Nil);
  2807. AssertEquals('Array type','',TPasArrayType(TheType).IndexRange);
  2808. end;
  2809. procedure TTestTypeParser.TestDynamicArrayComment;
  2810. begin
  2811. AddComment:=True;
  2812. DoParseArray('array of integer','',Nil);
  2813. AssertEquals('Array type','',TPasArrayType(TheType).IndexRange);
  2814. AssertComment;
  2815. end;
  2816. procedure TTestTypeParser.TestGenericArray;
  2817. begin
  2818. Add('Type');
  2819. Add('generic TArray<T> = array of T;');
  2820. // Writeln(source.text);
  2821. ParseDeclarations;
  2822. AssertEquals('One type definition',1,Declarations.Types.Count);
  2823. AssertEquals('First declaration is type definition.',TPasArrayType,TObject(Declarations.Types[0]).ClassType);
  2824. FType:=TPasType(Declarations.Types[0]);
  2825. AssertEquals('First declaration has correct name.','TArray',FType.Name);
  2826. AssertEquals('Array type','',TPasArrayType(TheType).IndexRange);
  2827. AssertEquals('Generic Array type',True,TPasArrayType(TheType).IsGenericArray);
  2828. end;
  2829. procedure TTestTypeParser.TestSimpleEnumerated;
  2830. begin
  2831. DoParseEnumerated('(one,two,three)','',3);
  2832. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2833. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2834. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2835. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2836. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2837. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2838. end;
  2839. procedure TTestTypeParser.TestSimpleEnumeratedComment;
  2840. begin
  2841. AddComment:=True;
  2842. TestSimpleEnumerated;
  2843. AssertComment;
  2844. AssertEquals('No comment on enum 0','',TPasEnumValue(TPasEnumType(TheType).Values[0]).DocComment);
  2845. AssertEquals('No comment on enum 1','',TPasEnumValue(TPasEnumType(TheType).Values[1]).DocComment);
  2846. AssertEquals('No comment on enum 2','',TPasEnumValue(TPasEnumType(TheType).Values[2]).DocComment);
  2847. end;
  2848. procedure TTestTypeParser.TestSimpleEnumeratedComment2;
  2849. begin
  2850. AddComment:=True;
  2851. DoParseEnumerated('( {a} one, {b} two, {c} three)','',3);
  2852. AssertEquals('comment on enum 0','a'+sLineBreak,TPasEnumValue(TPasEnumType(TheType).Values[0]).DocComment);
  2853. AssertEquals('comment on enum 1','b'+sLineBreak,TPasEnumValue(TPasEnumType(TheType).Values[1]).DocComment);
  2854. AssertEquals('comment on enum 2','c'+sLineBreak,TPasEnumValue(TPasEnumType(TheType).Values[2]).DocComment);
  2855. end;
  2856. procedure TTestTypeParser.TestSimpleEnumeratedDeprecated;
  2857. begin
  2858. DoParseEnumerated('(one,two,three)','deprecated',3);
  2859. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2860. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2861. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2862. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2863. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2864. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2865. end;
  2866. procedure TTestTypeParser.TestSimpleEnumeratedPlatform;
  2867. begin
  2868. DoParseEnumerated('(one,two,three)','platform',3);
  2869. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2870. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2871. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2872. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2873. AssertEquals('Assigned value second enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2874. AssertEquals('Assigned value third enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2875. end;
  2876. procedure TTestTypeParser.TestAssignedEnumerated;
  2877. begin
  2878. DoParseEnumerated('(one,two:=2,three)','',3);
  2879. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2880. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2881. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2882. AssertEquals('Assigned value enumerated','2',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2883. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2884. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2885. end;
  2886. procedure TTestTypeParser.TestAssignedEnumeratedDeprecated;
  2887. begin
  2888. DoParseEnumerated('(one,two:=2,three)','',3);
  2889. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2890. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2891. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2892. AssertEquals('Assigned value enumerated','2',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2893. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2894. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2895. end;
  2896. procedure TTestTypeParser.TestAssignedEnumeratedPlatform;
  2897. begin
  2898. DoParseEnumerated('(one,two:=2,three)','',3);
  2899. AssertEquals('First enumerated value','one',TPasEnumValue(TPasEnumType(TheType).Values[0]).Name);
  2900. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[0]).AssignedValue);
  2901. AssertEquals('Second enumerated value','two',TPasEnumValue(TPasEnumType(TheType).Values[1]).Name);
  2902. AssertEquals('Assigned value enumerated','2',TPasEnumValue(TPasEnumType(TheType).Values[1]).AssignedValue);
  2903. AssertEquals('Third enumerated value','three',TPasEnumValue(TPasEnumType(TheType).Values[2]).Name);
  2904. AssertEquals('Assigned value first enumerated empty','',TPasEnumValue(TPasEnumType(TheType).Values[2]).AssignedValue);
  2905. end;
  2906. procedure TTestTypeParser.TestFileType;
  2907. begin
  2908. DoTestFileType('integer','');
  2909. end;
  2910. procedure TTestTypeParser.TestFileTypeDeprecated;
  2911. begin
  2912. DoTestFileType('integer','deprecated');
  2913. end;
  2914. procedure TTestTypeParser.TestFileTypePlatform;
  2915. begin
  2916. DoTestFileType('integer','platform');
  2917. end;
  2918. procedure TTestTypeParser.TestRangeType;
  2919. begin
  2920. DoTestRangeType('1','4','');
  2921. end;
  2922. procedure TTestTypeParser.TestCharRangeType;
  2923. begin
  2924. DoTestRangeType('#1','#4','');
  2925. end;
  2926. procedure TTestTypeParser.TestCharRangeType2;
  2927. begin
  2928. DoTestRangeType('''A''','''B''','');
  2929. end;
  2930. procedure TTestTypeParser.TestRangeTypeDeprecated;
  2931. begin
  2932. DoTestRangeType('1','4','deprecated');
  2933. end;
  2934. procedure TTestTypeParser.TestRangeTypePlatform;
  2935. begin
  2936. DoTestRangeType('1','4','platform');
  2937. end;
  2938. procedure TTestTypeParser.TestIdentifierRangeType;
  2939. begin
  2940. DoTestRangeType('tkFirst','tkLast','');
  2941. end;
  2942. procedure TTestTypeParser.TestIdentifierRangeTypeDeprecated;
  2943. begin
  2944. DoTestRangeType('tkFirst','tkLast','deprecated');
  2945. end;
  2946. procedure TTestTypeParser.TestIdentifierRangeTypePlatform;
  2947. begin
  2948. DoTestRangeType('tkFirst','tkLast','platform');
  2949. end;
  2950. procedure TTestTypeParser.TestNegativeIdentifierRangeType;
  2951. begin
  2952. DoTestRangeType('-tkLast','tkLast','');
  2953. end;
  2954. procedure TTestTypeParser.TestSimpleSet;
  2955. begin
  2956. DoParseSimpleSet('Byte','');
  2957. end;
  2958. procedure TTestTypeParser.TestSimpleSetDeprecated;
  2959. begin
  2960. DoParseSimpleSet('Byte','deprecated');
  2961. end;
  2962. procedure TTestTypeParser.TestSimpleSetPlatform;
  2963. begin
  2964. DoParseSimpleSet('Byte','platform');
  2965. end;
  2966. procedure TTestTypeParser.TestComplexSet;
  2967. begin
  2968. DoParseComplexSet('(one, two, three)','');
  2969. DoTestComplexSet;
  2970. end;
  2971. procedure TTestTypeParser.TestComplexSetDeprecated;
  2972. begin
  2973. DoParseComplexSet('(one, two, three)','deprecated');
  2974. DoTestComplexSet;
  2975. end;
  2976. procedure TTestTypeParser.TestComplexSetPlatform;
  2977. begin
  2978. DoParseComplexSet('(one, two, three)','platform');
  2979. DoTestComplexSet;
  2980. end;
  2981. procedure TTestTypeParser.TestPackedSet;
  2982. begin
  2983. DoParseSimpleSet('Byte','',True);
  2984. end;
  2985. procedure TTestTypeParser.TestRangeLowHigh;
  2986. begin
  2987. DoParseRangeSet('low(TRange)..high(TRange)','');
  2988. end;
  2989. procedure TTestTypeParser.TestRangeSet;
  2990. begin
  2991. // TRange = (rLow, rMiddle, rHigh);
  2992. DoParseRangeSet('rMiddle..high(TRange)','');
  2993. end;
  2994. procedure TTestTypeParser.TestSubRangeSet;
  2995. begin
  2996. DoParseRangeSet('0..SizeOf(Integer)*8-1','');
  2997. end;
  2998. procedure TTestTypeParser.TestRangeSetDeprecated;
  2999. begin
  3000. DoParseRangeSet('0..SizeOf(Integer)*8-1','deprecated');
  3001. end;
  3002. procedure TTestTypeParser.TestRangeSetPlatform;
  3003. begin
  3004. DoParseRangeSet('0..SizeOf(Integer)*8-1','platform');
  3005. end;
  3006. procedure TTestTypeParser.TestNegativeRangeType;
  3007. begin
  3008. DoTestRangeType('2-1','3','');
  3009. end;
  3010. procedure TTestTypeParser.TestClassOf;
  3011. begin
  3012. DoTestClassOf('');
  3013. end;
  3014. procedure TTestTypeParser.TestClassOfComment;
  3015. begin
  3016. AddComment:=True;
  3017. DoTestClassOf('');
  3018. AssertComment;
  3019. end;
  3020. procedure TTestTypeParser.TestClassOfDeprecated;
  3021. begin
  3022. DoTestClassOf('deprecated');
  3023. end;
  3024. procedure TTestTypeParser.TestClassOfPlatform;
  3025. begin
  3026. DoTestClassOf('Platform');
  3027. end;
  3028. procedure TTestTypeParser.TestReferenceAlias;
  3029. begin
  3030. Add('Type');
  3031. Add(' Type1 = Integer;');
  3032. Add(' Type2 = Type1;');
  3033. Add('end.');
  3034. ParseDeclarations;
  3035. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3036. AssertEquals('First declaration is type definition.',TPasAliasType,TObject(Declarations.Types[0]).ClassType);
  3037. AssertEquals('Second declaration is type definition.',TPasAliasType,TObject(Declarations.Types[1]).ClassType);
  3038. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3039. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3040. AssertSame('Second declaration references first.',Declarations.Types[0],TPasAliasType(Declarations.Types[1]).DestType);
  3041. end;
  3042. procedure TTestTypeParser.TestReferenceSet;
  3043. begin
  3044. Add('Type');
  3045. Add(' Type1 = (a,b,c);');
  3046. Add(' Type2 = set of Type1;');
  3047. Add('end.');
  3048. ParseDeclarations;
  3049. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3050. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3051. AssertEquals('Second declaration is type definition.',TPasSetType,TObject(Declarations.Types[1]).ClassType);
  3052. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3053. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3054. AssertSame('Second declaration references first.',Declarations.Types[0],TPasSetType(Declarations.Types[1]).EnumType);
  3055. end;
  3056. procedure TTestTypeParser.TestReferenceClassOf;
  3057. begin
  3058. Add('Type');
  3059. Add(' Type1 = Class(TObject);');
  3060. Add(' Type2 = Class of Type1;');
  3061. Add('end.');
  3062. ParseDeclarations;
  3063. AssertEquals('1 type definitions',1,Declarations.Types.Count);
  3064. AssertEquals('1 class definitions',1,Declarations.Classes.Count);
  3065. AssertEquals('First declaration is class definition.',TPasClassType,TObject(Declarations.Classes[0]).ClassType);
  3066. AssertEquals('Second declaration is type definition.',TPasClassOfType,TObject(Declarations.Types[0]).ClassType);
  3067. AssertEquals('First declaration has correct name.','Type2',TPasType(Declarations.Types[0]).Name);
  3068. AssertEquals('Second declaration has correct name.','Type1',TPasType(Declarations.Classes[0]).Name);
  3069. AssertSame('Second declaration references first.',Declarations.Classes[0],TPasClassOfType(Declarations.Types[0]).DestType);
  3070. end;
  3071. procedure TTestTypeParser.TestReferenceFile;
  3072. begin
  3073. Add('Type');
  3074. Add(' Type1 = (a,b,c);');
  3075. Add(' Type2 = File of Type1;');
  3076. Add('end.');
  3077. ParseDeclarations;
  3078. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3079. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3080. AssertEquals('Second declaration is type definition.',TPasFileType,TObject(Declarations.Types[1]).ClassType);
  3081. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3082. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3083. AssertSame('Second declaration references first.',Declarations.Types[0],TPasFileType(Declarations.Types[1]).elType);
  3084. end;
  3085. procedure TTestTypeParser.TestReferenceArray;
  3086. begin
  3087. Add('Type');
  3088. Add(' Type1 = (a,b,c);');
  3089. Add(' Type2 = Array of Type1;');
  3090. Add('end.');
  3091. ParseDeclarations;
  3092. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3093. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3094. AssertEquals('Second declaration is type definition.',TPasArrayType,TObject(Declarations.Types[1]).ClassType);
  3095. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3096. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3097. AssertSame('Second declaration references first.',Declarations.Types[0],TPasArrayType(Declarations.Types[1]).elType);
  3098. end;
  3099. procedure TTestTypeParser.TestReferencePointer;
  3100. begin
  3101. Add('Type');
  3102. Add(' Type1 = (a,b,c);');
  3103. Add(' Type2 = ^Type1;');
  3104. Add('end.');
  3105. ParseDeclarations;
  3106. AssertEquals('Two type definitions',2,Declarations.Types.Count);
  3107. AssertEquals('First declaration is type definition.',TPasEnumType,TObject(Declarations.Types[0]).ClassType);
  3108. AssertEquals('Second declaration is type definition.',TPasPointerType,TObject(Declarations.Types[1]).ClassType);
  3109. AssertEquals('First declaration has correct name.','Type1',TPasType(Declarations.Types[0]).Name);
  3110. AssertEquals('Second declaration has correct name.','Type2',TPasType(Declarations.Types[1]).Name);
  3111. AssertSame('Second declaration references first.',Declarations.Types[0],TPasPointerType(Declarations.Types[1]).DestType);
  3112. end;
  3113. procedure TTestTypeParser.TestInvalidColon;
  3114. var
  3115. ok: Boolean;
  3116. begin
  3117. ok:=false;
  3118. try
  3119. ParseType(':1..2',TPasSetType);
  3120. except
  3121. on E: EParserError do
  3122. ok:=true;
  3123. end;
  3124. AssertEquals('wrong colon in type raised an error',true,ok);
  3125. end;
  3126. procedure TTestTypeParser.StartTypeHelper(ForType: String; AParent: String);
  3127. Var
  3128. S : String;
  3129. begin
  3130. S:='TMyClass = Type Helper';
  3131. if (AParent<>'') then
  3132. begin
  3133. S:=S+'('+AParent;
  3134. S:=S+')';
  3135. end;
  3136. S:=S+' for '+ForType;
  3137. Add(S);
  3138. end;
  3139. procedure TTestTypeParser.TestTypeHelper;
  3140. begin
  3141. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msTypeHelpers];
  3142. ParseType('Type Helper for AnsiString end',TPasClassType,'');
  3143. end;
  3144. procedure TTestTypeParser.TestTypeHelperWithParent;
  3145. begin
  3146. Scanner.CurrentModeSwitches:=Scanner.CurrentModeSwitches+[msTypeHelpers];
  3147. ParseType('Type Helper(TOtherHelper) for AnsiString end',TPasClassType,'');
  3148. end;
  3149. procedure TTestTypeParser.TestPointerReference;
  3150. begin
  3151. Add('Type');
  3152. Add(' pReference = ^Reference;');
  3153. Add(' Reference = object');
  3154. Add(' end;');
  3155. ParseDeclarations;
  3156. AssertEquals('type definition count',1,Declarations.Types.Count);
  3157. AssertEquals('object definition count',1,Declarations.Classes.Count);
  3158. end;
  3159. procedure TTestTypeParser.TestPointerKeyWord;
  3160. begin
  3161. Add('type');
  3162. Add(' &file = object');
  3163. Add(' end;');
  3164. ParseDeclarations;
  3165. AssertEquals('object definition count',1,Declarations.Classes.Count);
  3166. end;
  3167. initialization
  3168. RegisterTests([TTestTypeParser,TTestRecordTypeParser,TTestProcedureTypeParser]);
  3169. end.