tcidlparser.pp 60 KB

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