tcidlparser.pp 52 KB

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