tctypeparser.pas 119 KB

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