2
0

tcidlparser.pp 48 KB

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