tcidlparser.pp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. unit tcidlparser;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, webidldefs, webidlparser, webidlscanner;
  6. Type
  7. { TTestParser }
  8. TTestParser = Class(TTestCase)
  9. private
  10. FContext: TWebIDLContext;
  11. FParser: TWebIDLParser;
  12. FVersion: TWebIDLVersion;
  13. function GetList: TIDLDefinitionList;
  14. procedure SetVersion(AValue: TWebIDLVersion);
  15. Protected
  16. Procedure Setup; override;
  17. Procedure TearDown; override;
  18. Procedure InitSource(Const aSource: UTF8String);
  19. procedure AssertParserError(const Msg: String; const aSource: UTF8String);
  20. Class Procedure AssertEquals(Msg : String; AExpected,AActual : TConstType); overload;
  21. Class Procedure AssertEquals(Msg : String; AExpected,AActual : TAttributeOption); overload;
  22. Class Procedure AssertEquals(Msg : String; AExpected,AActual : TFunctionOption); overload;
  23. Class Procedure AssertEquals(Msg : String; AExpected,AActual : TAttributeOptions); overload;
  24. Class Procedure AssertEquals(Msg : String; AExpected,AActual : TFunctionOptions); overload;
  25. Public
  26. Property Parser : TWebIDLParser Read FParser;
  27. Property Context : TWebIDLContext Read FContext;
  28. Property Definitions : TIDLDefinitionList Read GetList;
  29. Property Version : TWebIDLVersion Read FVersion Write SetVersion;
  30. end;
  31. { TTestEnumParser }
  32. TTestEnumParser = Class(TTestParser)
  33. Public
  34. Procedure TestEnum(Const aSource,AName : UTF8String; AValues : Array of UTF8String);
  35. Published
  36. Procedure TestSingle;
  37. Procedure TestTwo;
  38. Procedure TestMissingIdent;
  39. Procedure TestMissingOpening;
  40. Procedure TestMissingClosing;
  41. Procedure TestMissingSemicolon;
  42. Procedure TestMissingComma;
  43. end;
  44. { TTestTypeDefParser }
  45. TTestTypeDefParser = Class(TTestParser)
  46. private
  47. function DoTestPromise(aDef: UTF8String; AReturnType: String=''): TIDLPromiseTypeDefDefinition;
  48. function DoTestSequence(aDef: UTF8String): TIDLSequenceTypeDefDefinition;
  49. function DoTestRecord(aDef: UTF8String; const aKeyTypeName,
  50. aValueTypeName: String): TIDLRecordDefinition;
  51. function DoTestUnion(aDef: String): TIDLUnionTypeDefDefinition;
  52. Public
  53. function TestTypeDef(const aSource, AName, aType: UTF8String): TIDLTypeDefDefinition;
  54. Published
  55. Procedure TestSimpleBoolean;
  56. Procedure TestSimpleBooleanNull;
  57. Procedure TestSimpleInt;
  58. procedure TestSimpleIntNull;
  59. Procedure TestSimpleLongint;
  60. procedure TestSimpleLongintNull;
  61. Procedure TestSimpleLongLongint;
  62. Procedure TestSimpleLongLongintNull;
  63. Procedure TestSimpleUnsignedShortint;
  64. Procedure TestSimpleUnsignedShortintNull;
  65. Procedure TestSimpleUnsignedLongint;
  66. Procedure TestSimpleUnsignedLongintNull;
  67. Procedure TestSimpleUnsignedLongLongint;
  68. Procedure TestSimpleUnsignedLongLongintNull;
  69. Procedure TestUnrestrictedFloat;
  70. Procedure TestSimpleFloat;
  71. Procedure TestSimpleFloatNull;
  72. Procedure TestSimpleDouble;
  73. Procedure TestSimpleDoubleNull;
  74. Procedure TestSimpleOctet;
  75. Procedure TestSimpleOctetNull;
  76. Procedure TestSimpleByte;
  77. procedure TestSimpleByteNull;
  78. Procedure TestSimpleIdentifier;
  79. Procedure TestSimpleIdentifierNull;
  80. Procedure TestAnyType;
  81. Procedure TestAnyTypeNull;
  82. Procedure TestUnion;
  83. Procedure TestUnionNull;
  84. Procedure TestSequence;
  85. Procedure TestSequenceNull;
  86. Procedure TestPromise;
  87. Procedure TestPromiseVoid;
  88. Procedure TestPromiseNull;
  89. Procedure TestPromiseReturnNull;
  90. Procedure TestRecord;
  91. end;
  92. { TTestInterfaceParser }
  93. { TTestBaseInterfaceParser }
  94. TTestBaseInterfaceParser = Class(TTestParser)
  95. private
  96. FCustAttributes: String;
  97. FisMixin: Boolean;
  98. Protected
  99. Procedure Setup; override;
  100. Public
  101. Function ParseInterface(AName,aInheritance : UTF8String; AMembers : Array of UTF8String) : TIDLInterfaceDefinition;
  102. Property isMixin : Boolean Read FisMixin Write FisMixin;
  103. Property CustAttributes : String Read FCustAttributes Write FCustAttributes;
  104. end;
  105. TTestInterfaceParser = Class(TTestBaseInterfaceParser)
  106. Published
  107. Procedure ParseEmpty;
  108. Procedure ParseEmptyInheritance;
  109. Procedure ParseMixinEmpty;
  110. Procedure ParseMixinEmptyInheritance;
  111. Procedure ParseCustomAttributes1;
  112. end;
  113. { TTestMapLikeInterfaceParser }
  114. TTestMapLikeInterfaceParser = Class(TTestBaseInterfaceParser)
  115. Public
  116. function ParseMapLike(const AKeyTypeName, aValueTypeName: UTF8String; IsReadOnly: Boolean): TIDLMapLikeDefinition;
  117. Published
  118. Procedure Parse;
  119. Procedure ParseReadOnly;
  120. end;
  121. { TTestSetLikeInterfaceParser }
  122. TTestSetLikeInterfaceParser = Class(TTestBaseInterfaceParser)
  123. Public
  124. Function ParseSetLike(const aElementTypeName : UTF8String; IsReadOnly : Boolean) : TIDLSetlikeDefinition;
  125. Published
  126. Procedure Parse;
  127. Procedure ParseReadOnly;
  128. end;
  129. { TTestConstInterfaceParser }
  130. TTestConstInterfaceParser = Class(TTestBaseInterfaceParser)
  131. Public
  132. Function ParseConst(AName,ATypeName,aValue : UTF8String; AType : TConstType) : TIDLConstDefinition;
  133. Published
  134. Procedure ParseConstInt;
  135. Procedure Parse2ConstInt;
  136. Procedure ParseConstIntHex;
  137. Procedure ParseConstLongint;
  138. Procedure ParseConstLongLongint;
  139. Procedure ParseConstUnsignedShortint;
  140. Procedure ParseConstUnsignedLongint;
  141. Procedure ParseConstUnsignedLongLongint;
  142. Procedure ParseConstFloat;
  143. Procedure ParseConstNan;
  144. Procedure ParseConstInfinity;
  145. Procedure ParseConstNegInfinity;
  146. Procedure ParseConstNull;
  147. Procedure ParseConstOctet;
  148. Procedure ParseConstByte;
  149. Procedure ParseConstBooleantrue;
  150. Procedure ParseConstBooleanFalse;
  151. Procedure ParseConstIdentifier;
  152. end;
  153. { TTestAttributeInterfaceParser }
  154. TTestAttributeInterfaceParser = Class(TTestBaseInterfaceParser)
  155. private
  156. Fattr: TIDLAttributeDefinition;
  157. Public
  158. Function ParseAttribute(ADef,AName,ATypeName : UTF8String; Options : TAttributeOptions = []) : TIDLAttributeDefinition;
  159. Property Attr : TIDLAttributeDefinition Read Fattr;
  160. Published
  161. Procedure ParseSimpleAttribute;
  162. Procedure ParseSimpleAttributeWithExtendedAttrs;
  163. Procedure ParseSimpleStaticAttribute;
  164. Procedure ParseSimpleStringifierAttribute;
  165. Procedure ParseSimpleReadonlyAttribute;
  166. Procedure ParseSimpleInheritedAttribute;
  167. Procedure ParseSimpleReadonlyInheritedAttribute;
  168. Procedure ParseSimpleReadonlyStaticAttribute;
  169. Procedure ParseSimpleReadonlyStringifierAttribute;
  170. Procedure ParseComplexReadonlyStaticAttribute;
  171. Procedure ParseIdentifierAttribute;
  172. Procedure Parse2IdentifierAttributes;
  173. end;
  174. { TTestSerializerInterfaceParser }
  175. TTestSerializerInterfaceParser = Class(TTestBaseInterfaceParser)
  176. private
  177. FSer: TIDLSerializerDefinition;
  178. Public
  179. Function ParseSerializer(ADef : UTF8String; Attrs : Array of UTF8String) : TIDLSerializerDefinition;
  180. Property Ser : TIDLSerializerDefinition Read FSer;
  181. Published
  182. Procedure TestSimpleIdentifier;
  183. Procedure TestSimpleFunction;
  184. Procedure TestMap;
  185. Procedure TestMapWithInherited;
  186. Procedure TestMapWithGetter;
  187. Procedure TestList;
  188. Procedure TestListWithGetter;
  189. end;
  190. { TTestOperationInterfaceParser }
  191. TTestOperationInterfaceParser = Class(TTestBaseInterfaceParser)
  192. private
  193. FFunc: TIDLFunctionDefinition;
  194. Public
  195. Function ParseFunction(ADef,aName,aReturnType : UTF8String; aArguments : Array of UTF8String) : TIDLFunctionDefinition;
  196. Property Func : TIDLFunctionDefinition Read FFunc;
  197. Published
  198. Procedure TestSimpleFunction;
  199. Procedure TestSimpleGetterFunction;
  200. Procedure TestSimpleSetterFunction;
  201. Procedure TestSimpleLegacyCallerFunction;
  202. Procedure TestSimpleDeleterFunction;
  203. Procedure TestAttrFunctionFunction;
  204. Procedure TestOptionalDefaultArgFunction;
  205. end;
  206. { TTestDictionaryParser }
  207. TTestDictionaryParser = Class(TTestParser)
  208. private
  209. FDict: TIDLDictionaryDefinition;
  210. FisPartial: Boolean;
  211. procedure AssertMember(aIndex: Integer; Aname, ATypeName, aDefaultValue: String; aDefaultType: TConstType=ctNull; isRequired: Boolean=False);
  212. Protected
  213. Property isPartial : Boolean Read FisPartial Write FisPartial;
  214. Public
  215. Function ParseDictionary(AName,aInheritance : UTF8String; AMembers : Array of UTF8String) : TIDLDictionaryDefinition;
  216. Property Dict : TIDLDictionaryDefinition read FDict;
  217. Published
  218. Procedure ParseSingleSimpleElement;
  219. Procedure ParseSingleSimpleElementInheritance;
  220. Procedure ParseSingleSimpleElementAttributes;
  221. Procedure ParseSingleSimpleElementAttributes2;
  222. Procedure ParseSingleSimpleElementRequired;
  223. Procedure ParseSingleSimpleElementDefaultString;
  224. Procedure ParseSingleSimpleElementRequiredDefaultString;
  225. Procedure ParseSingleSimpleElementRequiredDefaultEmptyArray;
  226. Procedure ParseSingleSimpleElementRequiredDefaultNull;
  227. Procedure ParseSingleSimpleElementUnsignedLongLong;
  228. Procedure ParseTwoSimpleElements;
  229. Procedure ParseThreeElements;
  230. Procedure ParsePartialSingleSimpleElement;
  231. end;
  232. { TTestFunctionCallbackParser }
  233. TTestFunctionCallbackParser = Class(TTestParser)
  234. private
  235. FFunction: TIDLFunctionDefinition;
  236. Public
  237. function ParseCallback(Const AName, aReturnType: UTF8String; AArguments: array of UTF8String): TIDLFunctionDefinition;
  238. Property Func : TIDLFunctionDefinition Read FFunction;
  239. Published
  240. Procedure ParseNoArgumentsReturnVoid;
  241. Procedure ParseOneArgumentReturnVoid;
  242. Procedure ParseOneUnsignedLongLongArgumentReturnVoid;
  243. Procedure ParseOneUnsignedLongLongArgumentReturnUnsignedLongLong;
  244. Procedure ParseOneArgumentWithAttrsReturnVoid;
  245. Procedure ParseOneOptionalArgumentReturnVoid;
  246. Procedure ParseOneOptionalArgumentWithAttrsReturnVoid;
  247. Procedure ParseTwoArgumentsReturnVoid;
  248. Procedure ParseTwoArgumentsAttrsReturnVoid;
  249. Procedure ParseThreeArgumentsAttrsReturnVoid;
  250. end;
  251. { TTestImplementsParser }
  252. TTestImplementsParser = Class(TTestParser)
  253. private
  254. FImpl: TIDLImplementsDefinition;
  255. Public
  256. Function ParseImplements(Const AName,aImplements: UTF8String) : TIDLImplementsDefinition;
  257. Property Impl: TIDLImplementsDefinition Read FImpl;
  258. Published
  259. Procedure ParseImplementsSimple;
  260. end;
  261. { TTestIncludesParser }
  262. TTestIncludesParser = Class(TTestParser)
  263. private
  264. FImpl: TIDLIncludesDefinition;
  265. Public
  266. Function ParseIncludes(Const AName,aIncludes: UTF8String) : TIDLIncludesDefinition;
  267. Property Impl: TIDLIncludesDefinition Read FImpl;
  268. Published
  269. Procedure ParseIncludesSimple;
  270. end;
  271. { TTestIterableInterfaceParser }
  272. TTestIterableInterfaceParser = Class(TTestBaseInterfaceParser)
  273. private
  274. Fiter: TIDLIterableDefinition;
  275. Public
  276. Function ParseIterable(Const AValueTypeName,AKeyTypeName : UTF8String) : TIDLIterableDefinition;
  277. Property Iter : TIDLIterableDefinition Read FIter;
  278. Published
  279. Procedure ParseSimpleIter;
  280. Procedure ParseKeyValueIter;
  281. end;
  282. implementation
  283. uses typinfo;
  284. { TTestSetLikeInterfaceParser }
  285. function TTestSetLikeInterfaceParser.ParseSetLike(
  286. const aElementTypeName: UTF8String; IsReadOnly: Boolean
  287. ): TIDLSetlikeDefinition;
  288. Var
  289. Id : TIDLInterfaceDefinition;
  290. S : UTF8String;
  291. begin
  292. Version:=V2;
  293. S:=Format('setlike <%s>',[aElementTypeName]);
  294. if isReadOnly then
  295. S:='readonly '+S;
  296. Id:=ParseInterFace('IA','',[S]);
  297. AssertEquals('Correct class',TIDLSetLikeDefinition,Id.Members[0].ClassType);
  298. Result:=Id.Members[0] as TIDLSetLikeDefinition;
  299. AssertNotNull('Have key type',Result.ElementType);
  300. AssertEquals('key type',TIDLTypeDefDefinition, Result.ElementType.ClassType);
  301. AssertEquals('Key type Name',AElementTypeName,Result.ElementType.TypeName);
  302. AssertEquals('Readonly',IsReadOnly,Result.IsReadOnly);
  303. end;
  304. procedure TTestSetLikeInterfaceParser.Parse;
  305. begin
  306. ParseSetLike('short',False);
  307. end;
  308. procedure TTestSetLikeInterfaceParser.ParseReadOnly;
  309. begin
  310. ParseSetLike('short',True);
  311. end;
  312. { TTestMapLikeInterfaceParser }
  313. function TTestMapLikeInterfaceParser.ParseMapLike(const AKeyTypeName,
  314. aValueTypeName: UTF8String; IsReadOnly : Boolean): TIDLMapLikeDefinition;
  315. Var
  316. Id : TIDLInterfaceDefinition;
  317. S : UTF8String;
  318. begin
  319. Version:=V2;
  320. S:=Format('maplike <%s,%s>',[aKeyTypeName,aValueTypeName]);
  321. if isReadOnly then
  322. S:='readonly '+S;
  323. Id:=ParseInterFace('IA','',[S]);
  324. AssertEquals('Correct class',TIDLMapLikeDefinition,Id.Members[0].ClassType);
  325. Result:=Id.Members[0] as TIDLMapLikeDefinition;
  326. AssertNotNull('Have key type',Result.KeyType);
  327. AssertEquals('key type',TIDLTypeDefDefinition, Result.KeyType.ClassType);
  328. AssertEquals('Key type Name',AKeyTypeName,Result.KeyType.TypeName);
  329. AssertNotNull('Have value type',Result.ValueType);
  330. AssertEquals('key value',TIDLTypeDefDefinition, Result.ValueType.ClassType);
  331. AssertEquals('Key value Name',AValueTypeName,Result.ValueType.TypeName);
  332. AssertEquals('Readonly',IsReadOnly,Result.IsReadOnly);
  333. end;
  334. procedure TTestMapLikeInterfaceParser.Parse;
  335. begin
  336. ParseMapLike('short','string',False);
  337. end;
  338. procedure TTestMapLikeInterfaceParser.ParseReadOnly;
  339. begin
  340. ParseMapLike('short','string',True);
  341. end;
  342. { TTestIncludesParser }
  343. function TTestIncludesParser.ParseIncludes(const AName, aIncludes: UTF8String
  344. ): TIDLIncludesDefinition;
  345. Var
  346. Src : UTF8String;
  347. begin
  348. Src:=AName+' includes '+aIncludes+';'+sLineBreak;
  349. InitSource(Src);
  350. Parser.Version:=v2;
  351. Parser.Parse;
  352. AssertEquals('Correct class',TIDLIncludesDefinition,Definitions[0].ClassType);
  353. Result:=Definitions[0] as TIDLIncludesDefinition;
  354. AssertEquals('Correct name ',AName,Result.Name);
  355. AssertEquals('Correct implements ',aIncludes,Result.IncludedInterface);
  356. FImpl:=Result;
  357. end;
  358. procedure TTestIncludesParser.ParseIncludesSimple;
  359. begin
  360. end;
  361. { TTestOperationInterfaceParser }
  362. function TTestOperationInterfaceParser.ParseFunction(ADef, aName,
  363. aReturnType: UTF8String; aArguments: array of UTF8String): TIDLFunctionDefinition;
  364. Var
  365. TN,Src : UTF8String;
  366. P,I,Idx : integer;
  367. Arg : TIDLArgumentDefinition;
  368. ID : TIDLInterfaceDefinition;
  369. begin
  370. ID:=ParseInterface('IA','',[aDef]);
  371. Parser.Parse;
  372. AssertEquals('Correct class',TIDLFunctionDefinition,ID.Members[0].ClassType);
  373. Result:=ID.Members[0] as TIDLFunctionDefinition;
  374. AssertEquals('Name',AName,Result.Name);
  375. AssertNotNull('Have return type',Result.ReturnType);
  376. AssertEquals('Return type name',aReturnType,Result.ReturnType.TypeName);
  377. AssertEquals('Have arguments',Length(aArguments)>0,Result.HasArguments);
  378. AssertEquals('Argument count',Length(aArguments) div 2,Result.Arguments.Count);
  379. I:=0;
  380. While I<Length(aArguments)-1 do
  381. begin
  382. Idx:=I div 2;
  383. Arg:=Result.Argument[idx];
  384. AssertEquals('Argument '+IntToStr(Idx)+' name',aArguments[I+1],Arg.Name);
  385. AssertNotNull('Argument '+IntToStr(Idx)+' have type',Arg.ArgumentType);
  386. TN:=aArguments[I];
  387. P:=Pos(']',TN);
  388. If P>0 then
  389. TN:=Trim(Copy(TN,P+1,Length(TN)-P));
  390. if Pos('optional',TN)=1 then
  391. TN:=Trim(Copy(TN,9,Length(TN)-8));
  392. AssertEquals('Argument '+IntToStr(I div 2)+' type name',TN,Arg.ArgumentType.TypeName);
  393. Inc(I,2);
  394. end;
  395. FFunc:=Result;
  396. end;
  397. procedure TTestOperationInterfaceParser.TestSimpleFunction;
  398. begin
  399. ParseFunction('short A()','A','short',[]);
  400. end;
  401. procedure TTestOperationInterfaceParser.TestSimpleGetterFunction;
  402. begin
  403. AssertEquals('Options',[foGetter],ParseFunction('getter short A()','A','short',[]).Options);
  404. end;
  405. procedure TTestOperationInterfaceParser.TestSimpleSetterFunction;
  406. begin
  407. AssertEquals('Options',[foSetter],ParseFunction('setter short A()','A','short',[]).Options);
  408. end;
  409. procedure TTestOperationInterfaceParser.TestSimpleLegacyCallerFunction;
  410. begin
  411. AssertEquals('Options',[foLegacyCaller],ParseFunction('legacycaller short A()','A','short',[]).Options);
  412. end;
  413. procedure TTestOperationInterfaceParser.TestSimpleDeleterFunction;
  414. begin
  415. AssertEquals('Options',[foDeleter],ParseFunction('deleter short A()','A','short',[]).Options);
  416. end;
  417. procedure TTestOperationInterfaceParser.TestAttrFunctionFunction;
  418. begin
  419. AssertTrue('HasAttribute',ParseFunction('[Me] short A()','A','short',[]).HasSimpleAttribute('Me'));
  420. end;
  421. procedure TTestOperationInterfaceParser.TestOptionalDefaultArgFunction;
  422. begin
  423. ParseFunction('void A(optional short me = 0,optional short you = 0)','A','void',['short','me','short','you'])
  424. end;
  425. { TTestSerializerInterfaceParser }
  426. function TTestSerializerInterfaceParser.ParseSerializer(ADef: UTF8String; Attrs: array of UTF8String): TIDLSerializerDefinition;
  427. Var
  428. Id : TIDLInterfaceDefinition;
  429. i : Integer;
  430. begin
  431. Id:=ParseInterFace('IA','',['serializer '+ADef]);
  432. AssertEquals('Correct class',TIDLSerializerDefinition,Id.Members[0].ClassType);
  433. Result:=Id.Members[0] as TIDLSerializerDefinition;
  434. if (Length(Attrs)>0) then
  435. begin
  436. AssertTrue('Kind is object or array',Result.Kind in [skObject,skArray,skSingle]);
  437. AssertEquals('Identifier count',Length(Attrs),Result.Identifiers.Count);
  438. For I:=0 to Length(Attrs)-1 do
  439. AssertEquals('Identifier',Attrs[I],Result.Identifiers[i]);
  440. end
  441. else if (Result.SerializerFunction<>Nil) then
  442. AssertTrue('Kind is function',Result.Kind=skFunction);
  443. FSer:=Result;
  444. end;
  445. procedure TTestSerializerInterfaceParser.TestSimpleIdentifier;
  446. begin
  447. ParseSerializer('= A',['A']);
  448. end;
  449. procedure TTestSerializerInterfaceParser.TestSimpleFunction;
  450. Var
  451. D : TIDLFunctionDefinition;
  452. begin
  453. AssertNotNull(ParseSerializer('string A ()',[]).SerializerFunction);
  454. D:=Ser.SerializerFunction;
  455. AssertEquals('Function name','A',D.Name);
  456. end;
  457. procedure TTestSerializerInterfaceParser.TestMap;
  458. begin
  459. ParseSerializer('= {A, B, C}',['A','B','C']);
  460. AssertTrue('Map',Ser.Kind=skObject);
  461. end;
  462. procedure TTestSerializerInterfaceParser.TestMapWithInherited;
  463. begin
  464. ParseSerializer('= {inherit, B, C}',['inherit','B','C']);
  465. AssertTrue('Map',Ser.Kind=skObject);
  466. end;
  467. procedure TTestSerializerInterfaceParser.TestMapWithGetter;
  468. begin
  469. ParseSerializer('= {getter, B, C}',['getter','B','C']);
  470. AssertTrue('Map',Ser.Kind=skObject);
  471. end;
  472. procedure TTestSerializerInterfaceParser.TestList;
  473. begin
  474. ParseSerializer('= [A, B, C]',['A','B','C']);
  475. AssertTrue('Map',Ser.Kind=skArray);
  476. end;
  477. procedure TTestSerializerInterfaceParser.TestListWithGetter;
  478. begin
  479. ParseSerializer('= [getter, B, C]',['getter','B','C']);
  480. AssertTrue('Map',Ser.Kind=skArray);
  481. end;
  482. { TTestIterableInterfaceParser }
  483. function TTestIterableInterfaceParser.ParseIterable(const AValueTypeName,
  484. AKeyTypeName: UTF8String): TIDLIterableDefinition;
  485. Var
  486. Id : TIDLInterfaceDefinition;
  487. Src : UTF8String;
  488. begin
  489. Src:='iterable <';
  490. if AKeyTypeName<>'' then
  491. Src:=Src+aKeyTypeName+',';
  492. Src:=Src+aValueTypeName+'>';
  493. Id:=ParseInterFace('IA','',[Src]);
  494. AssertEquals('Correct class',TIDLIterableDefinition,Id.Members[0].ClassType);
  495. Result:=Id.Members[0] as TIDLIterableDefinition;
  496. AssertNotNull('Have value type',Result.ValueType);
  497. AssertEquals('Attr type',AValueTypeName,Result.ValueType.TypeName);
  498. if AKeyTypeName='' then
  499. AssertNull('No key type',Result.KeyType)
  500. else
  501. begin
  502. AssertNotNull('Have key type',Result.KeyType);
  503. AssertEquals('Attr type',AKeyTypeName,Result.KeyType.TypeName);
  504. end;
  505. Fiter:=Result;
  506. end;
  507. procedure TTestIterableInterfaceParser.ParseSimpleIter;
  508. begin
  509. ParseIterable('short','');
  510. end;
  511. procedure TTestIterableInterfaceParser.ParseKeyValueIter;
  512. begin
  513. ParseIterable('short','long');
  514. end;
  515. { TTestAttributeInterfaceParser }
  516. function TTestAttributeInterfaceParser.ParseAttribute(ADef, AName,
  517. ATypeName: UTF8String; Options: TAttributeOptions): TIDLAttributeDefinition;
  518. Var
  519. Id : TIDLInterfaceDefinition;
  520. begin
  521. Id:=ParseInterFace('IA','',[aDef]);
  522. AssertEquals('Correct class',TIDLAttributeDefinition,Id.Members[0].ClassType);
  523. Result:=Id.Members[0] as TIDLAttributeDefinition;
  524. AssertEquals('Attr name',AName,Result.Name);
  525. AssertNotNull('Have type',Result.AttributeType);
  526. AssertEquals('Attr type',ATypeName,Result.AttributeType.TypeName);
  527. AssertEquals('Attr options',Options,Result.Options);
  528. FAttr:=Result;
  529. end;
  530. procedure TTestAttributeInterfaceParser.ParseSimpleAttribute;
  531. begin
  532. ParseAttribute('attribute short A','A','short',[]);
  533. end;
  534. procedure TTestAttributeInterfaceParser.ParseSimpleAttributeWithExtendedAttrs;
  535. begin
  536. AssertTrue('Have attribute',ParseAttribute('[Me] attribute short A','A','short',[]).HasSimpleAttribute('Me'));
  537. end;
  538. procedure TTestAttributeInterfaceParser.ParseSimpleStaticAttribute;
  539. begin
  540. ParseAttribute('static attribute short A','A','short',[aoStatic]);
  541. end;
  542. procedure TTestAttributeInterfaceParser.ParseSimpleStringifierAttribute;
  543. begin
  544. ParseAttribute('stringifier attribute short A','A','short',[aoStringifier]);
  545. end;
  546. procedure TTestAttributeInterfaceParser.ParseSimpleReadonlyAttribute;
  547. begin
  548. ParseAttribute('readonly attribute short A','A','short',[aoReadOnly]);
  549. end;
  550. procedure TTestAttributeInterfaceParser.ParseSimpleInheritedAttribute;
  551. begin
  552. ParseAttribute('inherit attribute short A','A','short',[aoInherit]);
  553. end;
  554. procedure TTestAttributeInterfaceParser.ParseSimpleReadonlyInheritedAttribute;
  555. begin
  556. ParseAttribute('inherit readonly attribute short A','A','short',[aoInherit,aoReadonly]);
  557. end;
  558. procedure TTestAttributeInterfaceParser.ParseSimpleReadonlyStaticAttribute;
  559. begin
  560. ParseAttribute('static readonly attribute short A','A','short',[aoStatic,aoReadOnly]);
  561. end;
  562. procedure TTestAttributeInterfaceParser.ParseSimpleReadonlyStringifierAttribute;
  563. begin
  564. ParseAttribute('stringifier readonly attribute short A','A','short',[aoStringifier,aoReadOnly]);
  565. end;
  566. procedure TTestAttributeInterfaceParser.ParseComplexReadonlyStaticAttribute;
  567. begin
  568. ParseAttribute('static readonly attribute unsigned long long A','A','unsigned long long',[aoStatic,aoReadOnly]);
  569. end;
  570. procedure TTestAttributeInterfaceParser.ParseIdentifierAttribute;
  571. begin
  572. ParseAttribute('attribute B A','A','B',[]);
  573. end;
  574. procedure TTestAttributeInterfaceParser.Parse2IdentifierAttributes;
  575. Var
  576. Id : TIDLInterfaceDefinition;
  577. begin
  578. Id:=ParseInterFace('IA','',['attribute B A','readonly attribute C D']);
  579. AssertEquals('Correct class',TIDLAttributeDefinition,Id.Members[0].ClassType);
  580. FAttr:=Id.Members[0] as TIDLAttributeDefinition;
  581. AssertEquals('Attr name','A',FAttr.Name);
  582. AssertNotNull('Have type',FAttr.AttributeType);
  583. AssertEquals('Attr type','B',FAttr.AttributeType.TypeName);
  584. AssertEquals('Attr options',[],FAttr.Options);
  585. FAttr:=Id.Members[1] as TIDLAttributeDefinition;
  586. AssertEquals('Attr name','D',FAttr.Name);
  587. AssertNotNull('Have type',FAttr.AttributeType);
  588. AssertEquals('Attr type','C',FAttr.AttributeType.TypeName);
  589. AssertEquals('Attr options',[aoReadonly],FAttr.Options);
  590. end;
  591. { TTestImplementsParser }
  592. function TTestImplementsParser.ParseImplements(const AName,
  593. aImplements: UTF8String): TIDLImplementsDefinition;
  594. Var
  595. Src : UTF8String;
  596. begin
  597. Src:=AName+' implements '+aImplements+';'+sLineBreak;
  598. InitSource(Src);
  599. Parser.Version:=V1;
  600. Parser.Parse;
  601. AssertEquals('Correct class',TIDLImplementsDefinition,Definitions[0].ClassType);
  602. Result:=Definitions[0] as TIDLImplementsDefinition;
  603. AssertEquals('Correct name ',AName,Result.Name);
  604. AssertEquals('Correct implements ',aImplements,Result.ImplementedInterface);
  605. FImpl:=Result;
  606. end;
  607. procedure TTestImplementsParser.ParseImplementsSimple;
  608. begin
  609. ParseImplements('A','B');
  610. end;
  611. { TTestFunctionCallbackParser }
  612. function TTestFunctionCallbackParser.ParseCallback(const AName,
  613. aReturnType: UTF8String; AArguments: array of UTF8String
  614. ): TIDLFunctionDefinition;
  615. Var
  616. TN,Src : UTF8String;
  617. P,I,Idx : integer;
  618. Arg : TIDLArgumentDefinition;
  619. begin
  620. Src:='callback '+aName+' = '+AReturnType+' (';
  621. I:=0;
  622. While I<Length(aArguments) do
  623. begin
  624. if I>0 then
  625. Src:=Src+', ';
  626. Src:=Src+aArguments[I]+ ' '+aArguments[I+1];
  627. Inc(I,2);
  628. end;
  629. Src:=Src+');'+sLineBreak;
  630. InitSource(Src);
  631. Parser.Parse;
  632. AssertEquals('Correct class',TIDLFunctionDefinition,Definitions[0].ClassType);
  633. Result:=Definitions[0] as TIDLFunctionDefinition;
  634. AssertEquals('Name',AName,Result.Name);
  635. AssertNotNull('Have return type',Result.ReturnType);
  636. AssertEquals('Return type name',aReturnType,Result.ReturnType.TypeName);
  637. AssertEquals('Have arguments',Length(aArguments)>0,Result.HasArguments);
  638. AssertEquals('Argument count',Length(aArguments) div 2,Result.Arguments.Count);
  639. I:=0;
  640. While I<Length(aArguments)-1 do
  641. begin
  642. Idx:=I div 2;
  643. Arg:=Result.Argument[idx];
  644. AssertEquals('Argument '+IntToStr(Idx)+' name',aArguments[I+1],Arg.Name);
  645. AssertNotNull('Argument '+IntToStr(Idx)+' have type',Arg.ArgumentType);
  646. TN:=aArguments[I];
  647. P:=Pos(']',TN);
  648. If P>0 then
  649. TN:=Trim(Copy(TN,P+1,Length(TN)-P));
  650. if Pos('optional',TN)=1 then
  651. TN:=Trim(Copy(TN,9,Length(TN)-8));
  652. AssertEquals('Argument '+IntToStr(I div 2)+' type name',TN,Arg.ArgumentType.TypeName);
  653. Inc(I,2);
  654. end;
  655. FFunction:=Result;
  656. end;
  657. procedure TTestFunctionCallbackParser.ParseNoArgumentsReturnVoid;
  658. begin
  659. ParseCallback('A','void',[]);
  660. end;
  661. procedure TTestFunctionCallbackParser.ParseOneArgumentReturnVoid;
  662. begin
  663. ParseCallback('A','void',['short','A']);
  664. end;
  665. procedure TTestFunctionCallbackParser.ParseOneUnsignedLongLongArgumentReturnVoid;
  666. begin
  667. ParseCallback('A','void',['unsigned long long','A']);
  668. end;
  669. procedure TTestFunctionCallbackParser.ParseOneUnsignedLongLongArgumentReturnUnsignedLongLong;
  670. begin
  671. ParseCallback('A','unsigned long long',['unsigned long long','A']);
  672. end;
  673. procedure TTestFunctionCallbackParser.ParseOneArgumentWithAttrsReturnVoid;
  674. begin
  675. ParseCallback('A','void',['[Me] unsigned long long','A']);
  676. AssertTrue('Have attribute',Func.Arguments[0].HasSimpleAttribute('Me'));
  677. end;
  678. procedure TTestFunctionCallbackParser.ParseOneOptionalArgumentReturnVoid;
  679. begin
  680. ParseCallback('A','void',['optional unsigned long long','A']);
  681. AssertTrue('is optional',Func.Argument[0].IsOptional);
  682. AssertEquals('Type name','unsigned long long',Func.Argument[0].ArgumentType.TypeName);
  683. end;
  684. procedure TTestFunctionCallbackParser.ParseOneOptionalArgumentWithAttrsReturnVoid;
  685. begin
  686. ParseCallback('A','void',['[Me] optional unsigned long long','A']);
  687. AssertTrue('is optional',Func.Argument[0].IsOptional);
  688. AssertEquals('Type name','unsigned long long',Func.Argument[0].ArgumentType.TypeName);
  689. AssertTrue('Have attribute',Func.Arguments[0].HasSimpleAttribute('Me'));
  690. end;
  691. procedure TTestFunctionCallbackParser.ParseTwoArgumentsReturnVoid;
  692. begin
  693. ParseCallback('A','void',['short','B','short','C']);
  694. end;
  695. procedure TTestFunctionCallbackParser.ParseTwoArgumentsAttrsReturnVoid;
  696. begin
  697. ParseCallback('A','void',['[Me] short','B','[Me] short','C']);
  698. AssertTrue('Have attribute',Func.Arguments[0].HasSimpleAttribute('Me'));
  699. AssertTrue('Have attribute',Func.Arguments[1].HasSimpleAttribute('Me'));
  700. end;
  701. procedure TTestFunctionCallbackParser.ParseThreeArgumentsAttrsReturnVoid;
  702. begin
  703. ParseCallback('A','void',['[Me] short','B','[Me] short','C','[Me] optional unsigned long long','D']);
  704. AssertTrue('Have attribute',Func.Arguments[0].HasSimpleAttribute('Me'));
  705. AssertTrue('Have attribute',Func.Arguments[1].HasSimpleAttribute('Me'));
  706. AssertTrue('Have attribute',Func.Arguments[2].HasSimpleAttribute('Me'));
  707. AssertTrue('Have attribute',Func.Argument[2].IsOptional);
  708. end;
  709. { TTestDictionaryParser }
  710. function TTestDictionaryParser.ParseDictionary(AName, aInheritance: UTF8String;
  711. AMembers: array of UTF8String): TIDLDictionaryDefinition;
  712. Var
  713. Src : UTF8String;
  714. I : integer;
  715. begin
  716. Src:='dictionary '+aName+' ';
  717. If IsPartial then
  718. Src:='partial '+Src;
  719. if (aInheritance<>'') then
  720. Src:=Src+': '+aInheritance+' ';
  721. Src:=Src+'{'+sLineBreak;
  722. For I:=0 to Length(AMembers)-1 do
  723. Src:=Src+AMembers[I]+';'+sLineBreak;
  724. Src:=Src+'};'+sLineBreak;
  725. InitSource(Src);
  726. Parser.Parse;
  727. AssertEquals('Correct class',TIDLDictionaryDefinition,Definitions[0].ClassType);
  728. Result:=Definitions[0] as TIDLDictionaryDefinition;
  729. AssertEquals('Name',AName,Result.Name);
  730. AssertEquals('Inheritance : ',aInheritance,Result.ParentName);
  731. AssertEquals('Member count',Length(AMembers),Result.Members.Count);
  732. FDict:=Result;
  733. end;
  734. procedure TTestDictionaryParser.AssertMember(aIndex : Integer; Aname, ATypeName,aDefaultValue : String; aDefaultType : TConstType = ctNull; isRequired : Boolean = False);
  735. Var
  736. m : TIDLDictionaryMemberDefinition;
  737. S : string;
  738. begin
  739. S:=Format('Member %d (Name %s)',[aIndex,AName]);
  740. AssertNotNull(S+' have dict',Dict);
  741. AssertTrue(S+' dict has members',Dict.HasMembers);
  742. AssertTrue(S+' index in range',(aIndex>=0) and (aIndex<Dict.Members.Count));
  743. AssertEquals(S+' element has correct class',TIDLDictionaryMemberDefinition,Dict.Members[aIndex].ClassType);
  744. M:=Dict[aIndex];
  745. AssertEquals(S+' isRequired : ',isRequired,M.IsRequired);
  746. AssertEquals(S+' Name : ',aName,M.Name);
  747. AssertNotNull(S+' Have type',M.MemberType);
  748. AssertEquals(S+' type name',aTypeName,M.MemberType.TypeName);
  749. if (aDefaultValue='') then
  750. AssertNull(S+' Have no default value',M.DefaultValue)
  751. else
  752. begin
  753. AssertNotNull(S+' Have default value',M.DefaultValue);
  754. AssertEquals(S+' default value',aDefaultValue,M.DefaultValue.Value);
  755. AssertEquals(S+' default value type',aDefaultType,M.DefaultValue.ConstType);
  756. end;
  757. end;
  758. procedure TTestDictionaryParser.ParseSingleSimpleElement;
  759. begin
  760. ParseDictionary('A','',['string B']);
  761. AssertMember(0,'B','string','');
  762. end;
  763. procedure TTestDictionaryParser.ParseSingleSimpleElementInheritance;
  764. begin
  765. ParseDictionary('A','C',['string B']);
  766. AssertMember(0,'B','string','');
  767. end;
  768. procedure TTestDictionaryParser.ParseSingleSimpleElementAttributes;
  769. begin
  770. ParseDictionary('A','',['[Replaceable] required string B']);
  771. AssertMember(0,'B','string','',ctNull,True);
  772. AssertTrue('Has attributes',Dict[0].HasAttributes);
  773. AssertEquals('Attribute count',1,Dict[0].Attributes.Count);
  774. AssertEquals('Has attributes','Replaceable',Dict[0].Attributes[0]);
  775. end;
  776. procedure TTestDictionaryParser.ParseSingleSimpleElementAttributes2;
  777. begin
  778. ParseDictionary('A','',['[Replaceable] octet B']);
  779. AssertMember(0,'B','octet','',ctNull,False);
  780. AssertTrue('Has attributes',Dict[0].HasAttributes);
  781. AssertEquals('Attribute count',1,Dict[0].Attributes.Count);
  782. AssertEquals('Has attributes','Replaceable',Dict[0].Attributes[0]);
  783. end;
  784. procedure TTestDictionaryParser.ParseSingleSimpleElementRequired;
  785. begin
  786. ParseDictionary('A','',['required string B']);
  787. AssertMember(0,'B','string','',ctNull,True);
  788. end;
  789. procedure TTestDictionaryParser.ParseSingleSimpleElementDefaultString;
  790. begin
  791. ParseDictionary('A','',['string B = "abc"']);
  792. AssertMember(0,'B','string','abc',ctString);
  793. end;
  794. procedure TTestDictionaryParser.ParseSingleSimpleElementRequiredDefaultString;
  795. begin
  796. ParseDictionary('A','',['required string B = "abc"']);
  797. AssertMember(0,'B','string','abc',ctString,true);
  798. end;
  799. procedure TTestDictionaryParser.ParseSingleSimpleElementRequiredDefaultEmptyArray;
  800. begin
  801. ParseDictionary('A','',['required string B = []']);
  802. AssertMember(0,'B','string','[]',ctEmptyArray,true);
  803. end;
  804. procedure TTestDictionaryParser.ParseSingleSimpleElementRequiredDefaultNull;
  805. begin
  806. ParseDictionary('A','',['string B = null']);
  807. AssertMember(0,'B','string','null',ctNull,False);
  808. end;
  809. procedure TTestDictionaryParser.ParseSingleSimpleElementUnsignedLongLong;
  810. begin
  811. ParseDictionary('A','',['required unsigned long long B']);
  812. AssertMember(0,'B','unsigned long long','',ctNull,True);
  813. end;
  814. procedure TTestDictionaryParser.ParseTwoSimpleElements;
  815. begin
  816. ParseDictionary('A','',['string B','short C']);
  817. AssertMember(0,'B','string','');
  818. AssertMember(1,'C','short','');
  819. end;
  820. procedure TTestDictionaryParser.ParseThreeElements;
  821. begin
  822. ParseDictionary('A','',['string B','short C','required unsigned long long D']);
  823. AssertMember(0,'B','string','');
  824. AssertMember(1,'C','short','');
  825. AssertMember(2,'D','unsigned long long','',ctNull,true);
  826. end;
  827. procedure TTestDictionaryParser.ParsePartialSingleSimpleElement;
  828. begin
  829. isPartial:=True;
  830. ParseDictionary('A','',['string B']);
  831. AssertMember(0,'B','string','');
  832. AssertTrue('Partial',Dict.IsPartial);
  833. end;
  834. { TTestTypeDefParser }
  835. function TTestTypeDefParser.TestTypeDef(const aSource, AName, aType: UTF8String
  836. ): TIDLTypeDefDefinition;
  837. Var
  838. E : TIDLTypeDefDefinition;
  839. begin
  840. InitSource('typedef '+ASource+';');
  841. Parser.Parse;
  842. AssertEquals('Definition count',1,Definitions.Count);
  843. AssertTrue('Correct class',Definitions[0].ClassType.InheritsFrom(TIDLTypeDefDefinition));
  844. E:=Definitions[0] as TIDLTypeDefDefinition;
  845. AssertEquals('Name',AName,E.Name);
  846. AssertEquals('Type name',AType,E.TypeName);
  847. if Pos('?',aSource)=0 then
  848. AssertEquals('Not Null',False,E.AllowNull);
  849. Result:=E;
  850. end;
  851. procedure TTestTypeDefParser.TestSimpleBoolean;
  852. begin
  853. TestTypeDef('boolean A','A','boolean');
  854. end;
  855. procedure TTestTypeDefParser.TestSimpleBooleanNull;
  856. begin
  857. AssertTrue('AllowNull',TestTypeDef('boolean ? A','A','boolean').AllowNull);
  858. end;
  859. procedure TTestTypeDefParser.TestSimpleInt;
  860. begin
  861. TestTypeDef('short A','A','short');
  862. end;
  863. procedure TTestTypeDefParser.TestSimpleIntNull;
  864. begin
  865. AssertTrue('AllowNull',TestTypeDef('short ? A','A','short').AllowNull);
  866. end;
  867. procedure TTestTypeDefParser.TestSimpleLongint;
  868. begin
  869. TestTypeDef('long A','A','long');
  870. end;
  871. procedure TTestTypeDefParser.TestSimpleLongintNull;
  872. begin
  873. AssertTrue('AllowNull',TestTypeDef('long ? A','A','long').AllowNull);
  874. end;
  875. procedure TTestTypeDefParser.TestSimpleLongLongint;
  876. begin
  877. TestTypeDef('long long A','A','long long');
  878. end;
  879. procedure TTestTypeDefParser.TestSimpleLongLongintNull;
  880. begin
  881. AssertTrue('AllowNull',TestTypeDef('long long ? A','A','long long').AllowNull);
  882. end;
  883. procedure TTestTypeDefParser.TestSimpleUnsignedShortint;
  884. begin
  885. TestTypeDef('unsigned short A','A','unsigned short');
  886. end;
  887. procedure TTestTypeDefParser.TestSimpleUnsignedShortintNull;
  888. begin
  889. AssertTrue('AllowNull',TestTypeDef('unsigned short ? A','A','unsigned short').AllowNull);
  890. end;
  891. procedure TTestTypeDefParser.TestSimpleUnsignedLongint;
  892. begin
  893. TestTypeDef('unsigned long A','A','unsigned long');
  894. end;
  895. procedure TTestTypeDefParser.TestSimpleUnsignedLongintNull;
  896. begin
  897. AssertTrue('AllowNull',TestTypeDef('unsigned long ? A','A','unsigned long').AllowNull);
  898. end;
  899. procedure TTestTypeDefParser.TestSimpleUnsignedLongLongint;
  900. begin
  901. TestTypeDef('unsigned long long A','A','unsigned long long');
  902. end;
  903. procedure TTestTypeDefParser.TestSimpleUnsignedLongLongintNull;
  904. begin
  905. AssertTrue('AllowNull',TestTypeDef('unsigned long long ? A','A','unsigned long long').AllowNull);
  906. end;
  907. procedure TTestTypeDefParser.TestUnrestrictedFloat;
  908. begin
  909. TestTypeDef('unrestricted float A','A','unrestricted float');
  910. end;
  911. procedure TTestTypeDefParser.TestSimpleFloat;
  912. begin
  913. TestTypeDef('float A','A','float');
  914. end;
  915. procedure TTestTypeDefParser.TestSimpleFloatNull;
  916. begin
  917. AssertTrue('AllowNull',TestTypeDef('float ? A','A','float').AllowNull)
  918. end;
  919. procedure TTestTypeDefParser.TestSimpleDouble;
  920. begin
  921. TestTypeDef('double A','A','double');
  922. end;
  923. procedure TTestTypeDefParser.TestSimpleDoubleNull;
  924. begin
  925. AssertTrue('AllowNull',TestTypeDef('double ? A','A','double').AllowNull);
  926. end;
  927. procedure TTestTypeDefParser.TestSimpleOctet;
  928. begin
  929. TestTypeDef('octet A','A','octet');
  930. end;
  931. procedure TTestTypeDefParser.TestSimpleOctetNull;
  932. begin
  933. AssertTrue('AllowNull',TestTypeDef('octet ? A','A','octet').AllowNull);
  934. end;
  935. procedure TTestTypeDefParser.TestSimpleByte;
  936. begin
  937. TestTypeDef('byte A','A','byte');
  938. end;
  939. procedure TTestTypeDefParser.TestSimpleByteNull;
  940. begin
  941. AssertTrue('AllowNull',TestTypeDef('byte ? A','A','byte').AllowNull);
  942. end;
  943. procedure TTestTypeDefParser.TestSimpleIdentifier;
  944. begin
  945. TestTypeDef('Zaza A','A','Zaza');
  946. end;
  947. procedure TTestTypeDefParser.TestSimpleIdentifierNull;
  948. begin
  949. AssertTrue('AllowNull',TestTypeDef('Zaza ? A','A','Zaza').AllowNull);
  950. end;
  951. procedure TTestTypeDefParser.TestAnyType;
  952. begin
  953. TestTypeDef('any A','A','any');
  954. end;
  955. procedure TTestTypeDefParser.TestAnyTypeNull;
  956. begin
  957. AssertTrue('AllowNull',TestTypeDef('any ? A','A','any').AllowNull);
  958. end;
  959. function TTestTypeDefParser.DoTestUnion(aDef: String): TIDLUnionTypeDefDefinition;
  960. Var
  961. D : TIDLTypeDefDefinition;
  962. U : TIDLUnionTypeDefDefinition;
  963. begin
  964. D:=TestTypeDef(aDef,'A','union');
  965. AssertEquals('Correct class',TIDLUnionTypeDefDefinition,D.ClassType);
  966. U:=TIDLUnionTypeDefDefinition(D);
  967. AssertEquals('Union types',2,U.Union.Count);
  968. AssertNotNull('Have type 1',U.Union[0]);
  969. AssertEquals('1: Correct class',TIDLTypeDefDefinition,U.Union[0].ClassType);
  970. D:=TIDLTypeDefDefinition(U.Union[0]);
  971. AssertEquals('1: Correct type name','byte',D.TypeName);
  972. AssertNotNull('Have type 2',U.Union[1]);
  973. AssertEquals('2: Correct class',TIDLTypeDefDefinition,U.Union[1].ClassType);
  974. D:=TIDLTypeDefDefinition(U.Union[1]);
  975. AssertEquals('2: Correct type name','octet',D.TypeName);
  976. Result:=U;
  977. end;
  978. procedure TTestTypeDefParser.TestUnion;
  979. begin
  980. DoTestUnion('(byte or octet) A');
  981. end;
  982. procedure TTestTypeDefParser.TestUnionNull;
  983. begin
  984. AssertTrue('Is null',DoTestUnion('(byte or octet) ? A').AllowNull);
  985. end;
  986. function TTestTypeDefParser.DoTestSequence(aDef: UTF8String
  987. ): TIDLSequenceTypeDefDefinition;
  988. Var
  989. D : TIDLTypeDefDefinition;
  990. S : TIDLSequenceTypeDefDefinition;
  991. begin
  992. D:=TestTypeDef(aDef ,'A','sequence');
  993. AssertEquals('Correct class',TIDLSequenceTypeDefDefinition,D.ClassType);
  994. S:=TIDLSequenceTypeDefDefinition(D);
  995. AssertNotNull('Have element type',S.ElementType);
  996. D:=TIDLTypeDefDefinition(S.ElementType);
  997. AssertEquals('1: Correct type name','byte',D.TypeName);
  998. Result:=S;
  999. end;
  1000. function TTestTypeDefParser.DoTestRecord(aDef: UTF8String; const aKeyTypeName,
  1001. aValueTypeName: String): TIDLRecordDefinition;
  1002. Var
  1003. D : TIDLTypeDefDefinition;
  1004. R : TIDLRecordDefinition;
  1005. begin
  1006. Version:=v2;
  1007. D:=TestTypeDef(aDef ,'A','record');
  1008. AssertEquals('Correct class',TIDLRecordDefinition,D.ClassType);
  1009. R:=TIDLRecordDefinition(D);
  1010. AssertNotNull('Have key type',R.KeyType);
  1011. D:=TIDLTypeDefDefinition(R.KeyType);
  1012. AssertEquals('1: Correct type name',aKeyTypeName,D.TypeName);
  1013. AssertNotNull('Have value type',R.ValueType);
  1014. D:=TIDLTypeDefDefinition(R.ValueType);
  1015. AssertEquals('1: Correct type name',aValueTypeName,D.TypeName);
  1016. Result:=R;
  1017. end;
  1018. procedure TTestTypeDefParser.TestSequence;
  1019. begin
  1020. DoTestSequence('sequence<byte> A');
  1021. end;
  1022. procedure TTestTypeDefParser.TestSequenceNull;
  1023. begin
  1024. AssertTrue('Is Null ',DoTestSequence('sequence<byte> ? A').AllowNull);
  1025. end;
  1026. function TTestTypeDefParser.DoTestPromise(aDef: UTF8String; AReturnType : String = ''): TIDLPromiseTypeDefDefinition;
  1027. Var
  1028. D : TIDLTypeDefDefinition;
  1029. S : TIDLPromiseTypeDefDefinition;
  1030. begin
  1031. D:=TestTypeDef(ADef,'A','Promise');
  1032. AssertEquals('Correct class',TIDLPromiseTypeDefDefinition,D.ClassType);
  1033. S:=TIDLPromiseTypeDefDefinition(D);
  1034. AssertNotNull('Have element type',S.ReturnType);
  1035. D:=TIDLTypeDefDefinition(S.ReturnType);
  1036. if aReturnType='' then
  1037. aReturnType:='byte';
  1038. AssertEquals('1: Correct type name',aReturnType,D.TypeName);
  1039. Result:=S;
  1040. end;
  1041. procedure TTestTypeDefParser.TestPromise;
  1042. begin
  1043. DoTestPromise('Promise<byte> A');
  1044. end;
  1045. procedure TTestTypeDefParser.TestPromiseVoid;
  1046. begin
  1047. DoTestPromise('Promise<void> A','void');
  1048. end;
  1049. procedure TTestTypeDefParser.TestPromiseNull;
  1050. begin
  1051. AssertTrue('Is Null',DoTestPromise('Promise<byte> ? A').AllowNull);
  1052. end;
  1053. procedure TTestTypeDefParser.TestPromiseReturnNull;
  1054. begin
  1055. AssertTrue('ReturnType Is Null',DoTestPromise('Promise<byte ?> A').ReturnType.AllowNull);
  1056. end;
  1057. procedure TTestTypeDefParser.TestRecord;
  1058. begin
  1059. DoTestRecord('record <short,string> A','short','string');
  1060. end;
  1061. { TTestInterfaceParser }
  1062. procedure TTestBaseInterfaceParser.Setup;
  1063. begin
  1064. inherited Setup;
  1065. FIsMixin:=False
  1066. end;
  1067. function TTestBaseInterfaceParser.ParseInterface(AName,aInheritance: UTF8String;
  1068. AMembers: array of UTF8String): TIDLInterfaceDefinition;
  1069. Var
  1070. Src : UTF8String;
  1071. I : integer;
  1072. begin
  1073. if IsMixin then
  1074. Src:='interface mixin '+aName+' '
  1075. else
  1076. Src:='interface '+aName+' ';
  1077. if (FCustAttributes<>'') then
  1078. Src:=FCustAttributes+' '+Src;
  1079. if (aInheritance<>'') then
  1080. Src:=Src+': '+aInheritance+' ';
  1081. Src:=Src+'{'+sLineBreak;
  1082. For I:=0 to Length(AMembers)-1 do
  1083. Src:=Src+' '+AMembers[I]+';'+sLineBreak;
  1084. Src:=Src+'};'+sLineBreak;
  1085. InitSource(Src);
  1086. Parser.Parse;
  1087. AssertEquals('Correct class',TIDLInterfaceDefinition,Definitions[0].ClassType);
  1088. Result:=Definitions[0] as TIDLInterfaceDefinition;
  1089. AssertEquals('Name',AName,Result.Name);
  1090. AssertEquals('Inheritance : ',aInheritance,Result.ParentName);
  1091. AssertEquals('Member count',Length(AMembers),Result.Members.Count);
  1092. AssertEquals('Mixin correct',IsMixin,Result.IsMixin);
  1093. end;
  1094. function TTestConstInterfaceParser.ParseConst(AName, ATypeName, aValue: UTF8String;
  1095. AType: TConstType): TIDLConstDefinition;
  1096. Var
  1097. Id : TIDLInterfaceDefinition;
  1098. P : Integer;
  1099. isNull : Boolean;
  1100. begin
  1101. Id:=ParseInterFace('IA','',['const '+aTypeName+' '+AName+' = '+AValue]);
  1102. AssertEquals('Correct class',TIDLConstDefinition,Id.Members[0].ClassType);
  1103. Result:=Id.Members[0] as TIDLConstDefinition;
  1104. AssertEquals('Const Name',AName,Result.Name);
  1105. P:=Pos('?',ATypeName);
  1106. isNull:=P>0;
  1107. if IsNull then
  1108. ATypeName:=Trim(Copy(ATypeName,1,P-1));
  1109. AssertEquals('Const type',ATypeName,Result.TypeName);
  1110. AssertEquals('Const consttype',AType,Result.ConstType);
  1111. AssertEquals('Const value',AValue,Result.Value);
  1112. AssertEquals('Const null allowed',IsNull,Result.AllowNull);
  1113. end;
  1114. procedure TTestInterfaceParser.ParseEmpty;
  1115. begin
  1116. ParseInterface('A','',[]);
  1117. end;
  1118. procedure TTestInterfaceParser.ParseEmptyInheritance;
  1119. begin
  1120. ParseInterface('A','B',[]);
  1121. end;
  1122. procedure TTestInterfaceParser.ParseMixinEmpty;
  1123. begin
  1124. IsMixin:=true;
  1125. Version:=v2;
  1126. ParseInterface('A','',[]);
  1127. end;
  1128. procedure TTestInterfaceParser.ParseMixinEmptyInheritance;
  1129. begin
  1130. IsMixin:=true;
  1131. Version:=v2;
  1132. ParseInterface('A','B',[]);
  1133. end;
  1134. procedure TTestInterfaceParser.ParseCustomAttributes1;
  1135. begin
  1136. CustAttributes:='[Constructor(DOMString type,optional WebGLContextEventInit eventInit)]';
  1137. AssertEquals('Attributes',CustAttributes,ParseInterface('A','B',[]).Attributes.AsString(True));
  1138. end;
  1139. procedure TTestConstInterfaceParser.ParseConstInt;
  1140. begin
  1141. ParseConst('A','short','123',ctInteger);
  1142. end;
  1143. procedure TTestConstInterfaceParser.Parse2ConstInt;
  1144. Var
  1145. Id : TIDLInterfaceDefinition;
  1146. CD : TIDLConstDefinition;
  1147. begin
  1148. Id:=ParseInterFace('IA','',['const GLenum READ_BUFFER = 0x0C02','const GLenum UNPACK_ROW_LENGTH = 0x0CF2']);
  1149. AssertEquals('Correct class',TIDLConstDefinition,Id.Members[0].ClassType);
  1150. CD:=Id.Members[0] as TIDLConstDefinition;
  1151. AssertEquals('Const Name','READ_BUFFER',CD.Name);
  1152. AssertEquals('Const type','GLenum',CD.TypeName);
  1153. AssertEquals('Const consttype',ctInteger,CD.ConstType);
  1154. AssertEquals('Const value','0x0C02',CD.Value);
  1155. AssertEquals('Const null allowed',False,CD.AllowNull);
  1156. CD:=Id.Members[1] as TIDLConstDefinition;
  1157. AssertEquals('Const Name','UNPACK_ROW_LENGTH',CD.Name);
  1158. AssertEquals('Const type','GLenum',CD.TypeName);
  1159. AssertEquals('Const consttype',ctInteger,CD.ConstType);
  1160. AssertEquals('Const value','0x0CF2',CD.Value);
  1161. AssertEquals('Const null allowed',False,CD.AllowNull);
  1162. end;
  1163. procedure TTestConstInterfaceParser.ParseConstIntHex;
  1164. begin
  1165. ParseConst('A','short','0xABCDEF',ctInteger);
  1166. end;
  1167. procedure TTestConstInterfaceParser.ParseConstLongint;
  1168. begin
  1169. ParseConst('A','long','123',ctInteger);
  1170. end;
  1171. procedure TTestConstInterfaceParser.ParseConstLongLongint;
  1172. begin
  1173. ParseConst('A','long long','123',ctInteger);
  1174. end;
  1175. procedure TTestConstInterfaceParser.ParseConstUnsignedShortint;
  1176. begin
  1177. ParseConst('A','unsigned short','123',ctInteger);
  1178. end;
  1179. procedure TTestConstInterfaceParser.ParseConstUnsignedLongint;
  1180. begin
  1181. ParseConst('A','unsigned long','123',ctInteger);
  1182. end;
  1183. procedure TTestConstInterfaceParser.ParseConstUnsignedLongLongint;
  1184. begin
  1185. ParseConst('A','unsigned long long','123',ctInteger);
  1186. end;
  1187. procedure TTestConstInterfaceParser.ParseConstFloat;
  1188. begin
  1189. ParseConst('A','float','1.23',ctFloat);
  1190. end;
  1191. procedure TTestConstInterfaceParser.ParseConstNan;
  1192. begin
  1193. ParseConst('A','float','NaN',ctNaN);
  1194. end;
  1195. procedure TTestConstInterfaceParser.ParseConstInfinity;
  1196. begin
  1197. ParseConst('A','float','Infinity',ctInfinity);
  1198. end;
  1199. procedure TTestConstInterfaceParser.ParseConstNegInfinity;
  1200. begin
  1201. ParseConst('A','float','-Infinity',ctNegInfinity);
  1202. end;
  1203. procedure TTestConstInterfaceParser.ParseConstNull;
  1204. begin
  1205. ParseConst('A','short ?','123',ctInteger);
  1206. end;
  1207. procedure TTestConstInterfaceParser.ParseConstOctet;
  1208. begin
  1209. ParseConst('A','octet','123',ctInteger);
  1210. end;
  1211. procedure TTestConstInterfaceParser.ParseConstByte;
  1212. begin
  1213. ParseConst('A','byte','123',ctInteger);
  1214. end;
  1215. procedure TTestConstInterfaceParser.ParseConstBooleantrue;
  1216. begin
  1217. ParseConst('A','boolean','true',ctBoolean);
  1218. end;
  1219. procedure TTestConstInterfaceParser.ParseConstBooleanFalse;
  1220. begin
  1221. ParseConst('A','boolean','false',ctBoolean);
  1222. end;
  1223. procedure TTestConstInterfaceParser.ParseConstIdentifier;
  1224. begin
  1225. ParseConst('A','Zaza','false',ctBoolean);
  1226. end;
  1227. { TTestEnumParser }
  1228. procedure TTestEnumParser.TestEnum(const aSource, AName: UTF8String;
  1229. AValues: array of UTF8String);
  1230. Var
  1231. E : TIDLEnumDefinition;
  1232. i : Integer;
  1233. begin
  1234. InitSource('enum '+ASource+';');
  1235. Parser.Parse;
  1236. AssertEquals('Definition count',1,Definitions.Count);
  1237. AssertEquals('Correct class',TIDLEnumDefinition,Definitions[0].ClassType);
  1238. E:=Definitions[0] as TIDLEnumDefinition;
  1239. AssertEquals('Name',AName,E.Name);
  1240. AssertEquals('Value count',Length(AValues),E.Values.Count);
  1241. For I:=0 to E.Values.Count-1 do
  1242. AssertEquals('Value '+IntToStr(i),AValues[i],E.Values[i]);
  1243. end;
  1244. procedure TTestEnumParser.TestSingle;
  1245. begin
  1246. TestEnum('A { "one" }','A',['one']);
  1247. end;
  1248. procedure TTestEnumParser.TestTwo;
  1249. begin
  1250. TestEnum('A { "one", "two" }','A',['one','two']);
  1251. end;
  1252. procedure TTestEnumParser.TestMissingIdent;
  1253. begin
  1254. AssertParserError('No ident','enum { "one" };');
  1255. end;
  1256. procedure TTestEnumParser.TestMissingOpening;
  1257. begin
  1258. AssertParserError('No {','enum A "one" };');
  1259. end;
  1260. procedure TTestEnumParser.TestMissingClosing;
  1261. begin
  1262. AssertParserError('No }','enum A { "one" ;');
  1263. end;
  1264. procedure TTestEnumParser.TestMissingSemicolon;
  1265. begin
  1266. AssertParserError('No ; ','enum A { "one" }');
  1267. end;
  1268. procedure TTestEnumParser.TestMissingComma;
  1269. begin
  1270. AssertParserError('No ; ','enum A { "one" "two"}');
  1271. end;
  1272. { TTestParser }
  1273. function TTestParser.GetList: TIDLDefinitionList;
  1274. begin
  1275. Result:=Context.Definitions;
  1276. end;
  1277. procedure TTestParser.SetVersion(AValue: TWebIDLVersion);
  1278. begin
  1279. if FVersion=AValue then Exit;
  1280. FVersion:=AValue;
  1281. if Assigned(FParser) then
  1282. FParser.Version:=aValue;
  1283. end;
  1284. procedure TTestParser.Setup;
  1285. begin
  1286. FContext:=TWebIDLContext.Create;
  1287. FVersion:=v1;
  1288. inherited Setup;
  1289. end;
  1290. procedure TTestParser.TearDown;
  1291. begin
  1292. FreeAndNil(FParser);
  1293. FreeAndNil(FContext);
  1294. inherited TearDown;
  1295. end;
  1296. procedure TTestParser.InitSource(const aSource: UTF8String);
  1297. begin
  1298. Writeln(TestName+' source : ');
  1299. Writeln(aSource);
  1300. FParser:=TWebIDLParser.Create(Context,aSource);
  1301. FParser.Version:=Version;
  1302. end;
  1303. procedure TTestParser.AssertParserError(const Msg: String;
  1304. const aSource: UTF8String);
  1305. begin
  1306. InitSource(aSource);
  1307. AssertException(Msg,EWebIDLParser,@Parser.Parse);
  1308. end;
  1309. class procedure TTestParser.AssertEquals(Msg: String; AExpected,
  1310. AActual: TConstType);
  1311. begin
  1312. AssertEQuals(Msg,GetEnumName(TypeInfo(TConstType),Ord(AExpected)),GetEnumName(TypeInfo(TConstType),Ord(AActual)));
  1313. end;
  1314. class procedure TTestParser.AssertEquals(Msg: String; AExpected,
  1315. AActual: TAttributeOption);
  1316. begin
  1317. AssertEquals(Msg,GetEnumName(TypeInfo(TAttributeOption),Ord(AExpected)),GetEnumName(TypeInfo(TAttributeOption),Ord(AActual)));
  1318. end;
  1319. class procedure TTestParser.AssertEquals(Msg: String; AExpected, AActual: TFunctionOption);
  1320. begin
  1321. AssertEquals(Msg,GetEnumName(TypeInfo(TFunctionOption),Ord(AExpected)),GetEnumName(TypeInfo(TFunctionOption),Ord(AActual)));
  1322. end;
  1323. class procedure TTestParser.AssertEquals(Msg: String; AExpected,
  1324. AActual: TAttributeOptions);
  1325. begin
  1326. AssertEquals(Msg,SetToString(PTypeInfo(TypeInfo(TAttributeOptions)),Integer(AExpected),True),
  1327. SetToString(PTypeInfo(TypeInfo(TAttributeOptions)),Integer(AActual),True));
  1328. end;
  1329. class procedure TTestParser.AssertEquals(Msg: String; AExpected,
  1330. AActual: TFunctionOptions);
  1331. begin
  1332. AssertEquals(Msg,SetToString(PTypeInfo(TypeInfo(TFunctionOptions)),Integer(AExpected),True),
  1333. SetToString(PTypeInfo(TypeInfo(TFunctionOptions)),Integer(AActual),True));
  1334. end;
  1335. initialization
  1336. RegisterTests([TTestEnumParser,
  1337. TTestInterfaceParser,
  1338. TTestConstInterfaceParser,
  1339. TTestTypeDefParser,
  1340. TTestDictionaryParser,
  1341. TTestFunctionCallbackParser,
  1342. TTestImplementsParser,
  1343. TTestIncludesParser,
  1344. TTestAttributeInterfaceParser,
  1345. TTestIterableInterfaceParser,
  1346. TTestSerializerInterfaceParser,
  1347. TTestOperationInterfaceParser,
  1348. TTestMapLikeInterfaceParser,
  1349. TTestSetLikeInterfaceParser]);
  1350. end.