tctypeparser.pas 128 KB

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