tcwebidldefs.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. unit tcwebidldefs;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, webidldefs;
  6. Type
  7. { TTestParser }
  8. { TTestDefinition }
  9. TTestDefinition = Class(TTestCase)
  10. private
  11. FDef: TIDLDefinition;
  12. function CreateUnionTypeDef(Types : Array of UTF8String; withAttrs: Boolean=False): TIDLUnionTypeDefDefinition;
  13. function CreateArgument(isOptional : Boolean; DefaultValue : string = ''; withAttrs: Boolean=False): TIDLArgumentDefinition;
  14. function CreateFunction(Options: TFunctionOptions; Args: Array of UTF8String; withAttrs: Boolean=False): TIDLFunctionDefinition;
  15. function CreateAttribute(Options: TAttributeOptions; withAttrs: Boolean=False): TIDLAttributeDefinition;
  16. function CreateConst(withAttrs: Boolean=False): TIDLConstDefinition;
  17. function CreateImplements(Const ATypeName : String; withAttrs: Boolean=False): TIDLImplementsDefinition;
  18. function CreateIncludes(Const ATypeName : String; withAttrs: Boolean=False): TIDLIncludesDefinition;
  19. function CreateTypeDef(Const ATypeName : String; withAttrs: Boolean=False): TIDLTypeDefDefinition;
  20. function CreateInterface(Const AParentName : String; aMembers : Array of TIDLDefinition; withAttrs: Boolean=False): TIDLInterfaceDefinition;
  21. function CreateDictionaryMember(Const AName,aTypeName,aDefault : String; aRequired : Boolean; withAttrs: Boolean=False): TIDLDictionaryMemberDefinition;
  22. function CreateDictionary(Const AParentName : String; aMembers : Array of TIDLDictionaryMemberDefinition; withAttrs: Boolean=False): TIDLDictionaryDefinition;
  23. function CreateSequence(Const AElementName : String; withAttrs: Boolean=False): TIDLSequenceTypeDefDefinition;
  24. function CreatePromise(Const AReturnTypeName: String; withAttrs: Boolean=False): TIDLPromiseTypeDefDefinition;
  25. function CreateSetLike(Const AElementName: String; withAttrs: Boolean=False): TIDLSetLikeDefinition;
  26. function CreateMapLike(Const AKeyTypeName,AValueTypeName: String; withAttrs: Boolean=False): TIDLMapLikeDefinition;
  27. function CreateRecord(Const AKeyTypeName,AValueTypeName: String; withAttrs: Boolean=False): TIDLRecordDefinition;
  28. Public
  29. Procedure TearDown; override;
  30. function CreateDef(aClass: TIDLDefinitionClass; WithAttrs: Boolean=False): TIDLDefinition;
  31. Procedure TestDef(Const aDef : String; AFull : Boolean);
  32. Property Def : TIDLDefinition Read FDef Write FDef;
  33. published
  34. Procedure TestConst;
  35. procedure TestAttribute;
  36. procedure TestStringifierAttribute;
  37. procedure TestStringifierFunction;
  38. procedure TestFunction;
  39. procedure TestCallBackFunction;
  40. procedure TestArgument;
  41. procedure TestImplements;
  42. procedure TestIncludes;
  43. procedure TestTypeDef;
  44. procedure TestUnionTypeDef;
  45. procedure TestInterface;
  46. procedure TestDictionaryMember;
  47. procedure TestDictionary;
  48. procedure TestCallbackInterface;
  49. procedure TestSequence;
  50. procedure TestPromise;
  51. procedure TestMapLike;
  52. procedure TestSetLike;
  53. procedure TestRecord;
  54. end;
  55. implementation
  56. { TTestDefinition }
  57. procedure TTestDefinition.TearDown;
  58. begin
  59. FreeAndNil(FDef);
  60. inherited TearDown;
  61. end;
  62. function TTestDefinition.CreateDef(aClass: TIDLDefinitionClass; WithAttrs : Boolean = False): TIDLDefinition;
  63. begin
  64. FreeAndNil(FDef);
  65. FDef:=aClass.Create(Nil,'A');
  66. if WithAttrs then
  67. FDef.Attributes.Add('Me');
  68. Result:=FDef;
  69. end;
  70. procedure TTestDefinition.TestDef(const aDef: String; AFull: Boolean);
  71. begin
  72. AssertEquals('Def '+FDef.ClassName +'.AsString('+BoolToStr(aFull,'True','False')+')',ADef,FDef.AsString(afull));
  73. end;
  74. function TTestDefinition.CreateConst(withAttrs: Boolean): TIDLConstDefinition;
  75. begin
  76. Result:=CreateDef(TIDLConstDefinition,WithAttrs) as TIDLConstDefinition;
  77. Result.TypeName:='short';
  78. Result.Value:='0x8080';
  79. end;
  80. function TTestDefinition.CreateImplements(const ATypeName: String;
  81. withAttrs: Boolean): TIDLImplementsDefinition;
  82. begin
  83. Result:=CreateDef(TIDLImplementsDefinition,WithAttrs) as TIDLImplementsDefinition;
  84. Result.ImplementedInterface:=ATypeName;
  85. end;
  86. function TTestDefinition.CreateIncludes(const ATypeName: String;
  87. withAttrs: Boolean): TIDLIncludesDefinition;
  88. begin
  89. Result:=CreateDef(TIDLIncludesDefinition,WithAttrs) as TIDLIncludesDefinition;
  90. Result.IncludedInterface:=ATypeName;
  91. end;
  92. function TTestDefinition.CreateTypeDef(const ATypeName: String;
  93. withAttrs: Boolean): TIDLTypeDefDefinition;
  94. begin
  95. Result:=CreateDef(TIDLTypeDefDefinition,WithAttrs) as TIDLTypeDefDefinition;
  96. Result.TypeName:=ATypeName;
  97. end;
  98. function TTestDefinition.CreateInterface(const AParentName: String;
  99. aMembers: array of TIDLDefinition; withAttrs: Boolean
  100. ): TIDLInterfaceDefinition;
  101. Var
  102. M : TIDLDefinition;
  103. begin
  104. Result:=CreateDef(TIDLInterfaceDefinition,WithAttrs) as TIDLInterfaceDefinition;
  105. Result.ParentName:=AParentName;
  106. For M in aMembers do
  107. Result.members.Add(M);
  108. end;
  109. function TTestDefinition.CreateDictionaryMember(const AName, aTypeName,
  110. aDefault: String; aRequired: Boolean; withAttrs: Boolean
  111. ): TIDLDictionaryMemberDefinition;
  112. begin
  113. Result:=CreateDef(TIDLDictionaryMemberDefinition,WithAttrs) as TIDLDictionaryMemberDefinition;
  114. Result.Name:=aName;
  115. Result.MemberType:=TIDLTypeDefDefinition.Create(Result,'');
  116. Result.MemberType.TypeName:=aTypeName;
  117. Result.IsRequired:=aRequired;
  118. if (aDefault<>'') then
  119. begin
  120. Result.DefaultValue:=TIDLConstDefinition.Create(Result,'');
  121. Result.DefaultValue.Value:=aDefault;
  122. end;
  123. end;
  124. function TTestDefinition.CreateDictionary(const AParentName: String;
  125. aMembers: array of TIDLDictionaryMemberDefinition; withAttrs: Boolean
  126. ): TIDLDictionaryDefinition;
  127. Var
  128. M : TIDLDictionaryMemberDefinition;
  129. begin
  130. Result:=CreateDef(TIDLDictionaryDefinition,WithAttrs) as TIDLDictionaryDefinition;
  131. Result.ParentName:=aParentName;
  132. for M in aMembers do
  133. Result.Members.Add(M);
  134. end;
  135. function TTestDefinition.CreateSequence(const AElementName: String;
  136. withAttrs: Boolean): TIDLSequenceTypeDefDefinition;
  137. begin
  138. Result:=CreateDef(TIDLSequenceTypeDefDefinition,WithAttrs) as TIDLSequenceTypeDefDefinition;
  139. Result.ElementType:=TIDLTypeDefDefinition.Create(Result,'');
  140. Result.ElementType.TypeName:=AElementName;
  141. end;
  142. function TTestDefinition.CreatePromise(const AReturnTypeName: String;
  143. withAttrs: Boolean): TIDLPromiseTypeDefDefinition;
  144. begin
  145. Result:=CreateDef(TIDLPromiseTypeDefDefinition,withAttrs) as TIDLPromiseTypeDefDefinition;
  146. Result.ReturnType:=TIDLTypeDefDefinition.Create(Result,'');
  147. Result.ReturnType.TypeName:=AReturnTypeName;
  148. end;
  149. function TTestDefinition.CreateSetLike(const AElementName: String;
  150. withAttrs: Boolean): TIDLSetLikeDefinition;
  151. begin
  152. Result:=CreateDef(TIDLSetLikeDefinition,WithAttrs) as TIDLSetLikeDefinition;
  153. Result.ElementType:=TIDLTypeDefDefinition.Create(Result,'');
  154. Result.ElementType.TypeName:=AElementName;
  155. end;
  156. function TTestDefinition.CreateMapLike(const AKeyTypeName,
  157. AValueTypeName: String; withAttrs: Boolean): TIDLMapLikeDefinition;
  158. begin
  159. Result:=CreateDef(TIDLMapLikeDefinition,WithAttrs) as TIDLMapLikeDefinition;
  160. Result.KeyType:=TIDLTypeDefDefinition.Create(Result,'');
  161. Result.KeyType.TypeName:=AKeyTypeName;
  162. Result.ValueType:=TIDLTypeDefDefinition.Create(Result,'');
  163. Result.ValueType.TypeName:=AValueTypeName;
  164. end;
  165. function TTestDefinition.CreateRecord(const AKeyTypeName,
  166. AValueTypeName: String; withAttrs: Boolean): TIDLRecordDefinition;
  167. begin
  168. Result:=CreateDef(TIDLRecordDefinition,WithAttrs) as TIDLRecordDefinition;
  169. Result.KeyType:=TIDLTypeDefDefinition.Create(Result,'');
  170. Result.KeyType.TypeName:=AKeyTypeName;
  171. Result.ValueType:=TIDLTypeDefDefinition.Create(Result,'');
  172. Result.ValueType.TypeName:=AValueTypeName;
  173. end;
  174. procedure TTestDefinition.TestConst;
  175. begin
  176. CreateConst(False);
  177. TestDef('const short A 0x8080',true);
  178. TestDef('short A 0x8080',False);
  179. CreateConst(True);
  180. TestDef('[Me] const short A 0x8080',true);
  181. TestDef('short A 0x8080',False);
  182. end;
  183. function TTestDefinition.CreateUnionTypeDef(Types: array of UTF8String;
  184. withAttrs: Boolean): TIDLUnionTypeDefDefinition;
  185. Var
  186. S : UTF8String;
  187. T : TIDLTypeDefDefinition;
  188. begin
  189. Result:=CreateDef(TIDLUnionTypeDefDefinition,WithAttrs) as TIDLUnionTypeDefDefinition;
  190. for S in Types do
  191. begin
  192. T:=TIDLTypeDefDefinition.Create(Result,'');
  193. T.TypeName:=S;
  194. Result.Union.Add(T);
  195. end;
  196. end;
  197. function TTestDefinition.CreateArgument(isOptional: Boolean; DefaultValue: string; withAttrs: Boolean): TIDLArgumentDefinition;
  198. begin
  199. Result:=CreateDef(TIDLArgumentDefinition,WithAttrs) as TIDLArgumentDefinition;
  200. Result.ArgumentType:=TIDLTypeDefDefinition.Create(Result,'AN');
  201. Result.ArgumentType.TypeName:='short';
  202. Result.HasDefaultValue:=(DefaultValue<>'');
  203. Result.DefaultValue:=DefaultValue;
  204. Result.IsOptional:=IsOptional;
  205. end;
  206. function TTestDefinition.CreateFunction(Options: TFunctionOptions; Args: array of UTF8String; withAttrs: Boolean): TIDLFunctionDefinition;
  207. Var
  208. I : integer;
  209. begin
  210. Result:=CreateDef(TIDLFunctionDefinition,WithAttrs) as TIDLFunctionDefinition;
  211. Result.ReturnType:=TIDLTypeDefDefinition.Create(Result,'AN');
  212. Result.ReturnType.TypeName:='short';
  213. Result.Options:=Options;
  214. I:=0;
  215. While I<Length(Args)-1 do
  216. begin
  217. Result.Arguments.Add(TIDLArgumentDefinition,args[I+1]);
  218. Result.Argument[I div 2].ArgumentType:=TIDLTypeDefDefinition.Create(Result,'AN'+IntToStr(i));
  219. Result.Argument[I div 2].ArgumentType.TypeName:=args[i];
  220. // With Result.Argument[I div 2] do
  221. // Writeln(I,': ',Name+'->',ArgumentType.TypeName);
  222. Inc(I,2);
  223. end;
  224. end;
  225. function TTestDefinition.CreateAttribute(Options: TAttributeOptions;
  226. withAttrs: Boolean): TIDLAttributeDefinition;
  227. begin
  228. Result:=CreateDef(TIDLAttributeDefinition,WithAttrs) as TIDLAttributeDefinition;
  229. Result.AttributeType:=TIDLTypeDefDefinition.Create(Result,'AN');
  230. Result.AttributeType.TypeName:='short';
  231. Result.Options:=Options;
  232. end;
  233. procedure TTestDefinition.TestAttribute;
  234. begin
  235. CreateAttribute([],False);
  236. TestDef('attribute short A',true);
  237. TestDef('short A',False);
  238. CreateAttribute([],True);
  239. TestDef('[Me] attribute short A',true);
  240. TestDef('short A',False);
  241. CreateAttribute([aoReadonly],false);
  242. TestDef('readonly attribute short A',true);
  243. TestDef('short A',False);
  244. CreateAttribute([aoStatic],false);
  245. TestDef('static attribute short A',true);
  246. TestDef('short A',False);
  247. end;
  248. procedure TTestDefinition.TestStringifierAttribute;
  249. begin
  250. CreateAttribute([aoStringifier],false);
  251. TestDef('stringifier attribute short A',true);
  252. TestDef('short A',False);
  253. CreateAttribute([aoStringifier,aoReadOnly],false);
  254. TestDef('stringifier readonly attribute short A',true);
  255. TestDef('short A',False);
  256. end;
  257. procedure TTestDefinition.TestStringifierFunction;
  258. begin
  259. CreateFunction([foStringifier],[],False);
  260. TestDef('stringifier short A ()',True);
  261. end;
  262. procedure TTestDefinition.TestFunction;
  263. Var
  264. F : TIDLFunctionDefinition;
  265. begin
  266. CreateFunction([],[],False);
  267. TestDef('short A ()',True);
  268. CreateFunction([],['short','B'],False);
  269. TestDef('short A (short B)',True);
  270. TestDef('short A (short B)',False);
  271. CreateFunction([],['short','B'],True);
  272. TestDef('[Me] short A (short B)',True);
  273. F:=CreateFunction([],['short','B','long','C'],False);
  274. F.Argument[1].IsOptional:=True;
  275. TestDef('short A (short B, optional long C)',True);
  276. F.Argument[1].HasDefaultValue:=True;
  277. F.Argument[1].DefaultValue:='123';
  278. TestDef('short A (short B, optional long C = 123)',True);
  279. CreateFunction([foStatic],[],False);
  280. TestDef('static short A ()',True);
  281. CreateFunction([foGetter],[],False);
  282. TestDef('getter short A ()',True);
  283. CreateFunction([foSetter],[],False);
  284. TestDef('setter short A ()',True);
  285. end;
  286. procedure TTestDefinition.TestCallBackFunction;
  287. begin
  288. CreateFunction([foCallback],[],False);
  289. TestDef('callback A = short ()',True);
  290. end;
  291. procedure TTestDefinition.TestArgument;
  292. begin
  293. CreateArgument(False,'',False);
  294. TestDef('short A',true);
  295. CreateArgument(False,'',False).ArgumentType.AllowNull:=True;
  296. TestDef('short? A',true);
  297. CreateArgument(true,'',False);
  298. TestDef('optional short A',true);
  299. CreateArgument(true,'',true);
  300. TestDef('[Me] optional short A',true);
  301. CreateArgument(true,'1',true);
  302. TestDef('[Me] optional short A = 1',true);
  303. end;
  304. procedure TTestDefinition.TestImplements;
  305. begin
  306. CreateImplements('IME',False);
  307. TestDef('A implements IME',False);
  308. TestDef('A implements IME',True);
  309. CreateImplements('IME',True);
  310. TestDef('A implements IME',False);
  311. TestDef('[Me] A implements IME',True);
  312. end;
  313. procedure TTestDefinition.TestIncludes;
  314. begin
  315. CreateIncludes('IME',False);
  316. TestDef('A includes IME',False);
  317. TestDef('A includes IME',True);
  318. CreateIncludes('IME',True);
  319. TestDef('A includes IME',False);
  320. TestDef('[Me] A includes IME',True);
  321. end;
  322. procedure TTestDefinition.TestTypeDef;
  323. begin
  324. CreateTypeDef('IME',False);
  325. TestDef('IME',False);
  326. TestDef('typedef IME A',true);
  327. CreateTypeDef('IME',True);
  328. TestDef('IME',False);
  329. TestDef('[Me] typedef IME A',true);
  330. CreateTypeDef('IME',True).AllowNull:=True;
  331. TestDef('IME?',False);
  332. TestDef('[Me] typedef IME? A',true);
  333. end;
  334. procedure TTestDefinition.TestUnionTypeDef;
  335. begin
  336. CreateUnionTypeDef(['string','short'],False);
  337. TestDef('(string or short)',False);
  338. TestDef('typedef (string or short) A',true);
  339. CreateUnionTypeDef(['string','short','unsigned long long'],true);
  340. TestDef('(string or short or unsigned long long)',False);
  341. TestDef('[Me] typedef (string or short or unsigned long long) A',true);
  342. end;
  343. procedure TTestDefinition.TestInterface;
  344. Var
  345. C : TIDLConstDefinition;
  346. D : TIDLFunctionDefinition;
  347. begin
  348. CreateInterface('',[],False);
  349. TestDef('interface A {'+sLinebreak+'}',True);
  350. CreateInterface('B',[]);
  351. TestDef('interface A : B {'+sLinebreak+'}',True);
  352. C:=CreateConst(False);
  353. Def:=Nil;
  354. CreateInterface('B',[C]);
  355. TestDef('interface A : B {'+sLinebreak+' const short A 0x8080;'+sLineBreak+'}',True);
  356. C:=CreateConst(False);
  357. C.Name:='D';
  358. Def:=Nil;
  359. D:=CreateFunction([],[],True);
  360. D.Name:='C';
  361. Def:=Nil;
  362. CreateInterface('B',[C,D]);
  363. TestDef('interface A : B {'+sLinebreak
  364. +' const short D 0x8080;'+sLineBreak
  365. +' [Me] short C ();'+sLineBreak
  366. +'}',True);
  367. CreateInterface('',[],False).IsPartial:=True;
  368. TestDef('partial interface A {'+sLinebreak+'}',True);
  369. end;
  370. procedure TTestDefinition.TestDictionaryMember;
  371. begin
  372. CreateDictionaryMember('A','short','',False,False);
  373. TestDef('short A',False);
  374. TestDef('short A',True);
  375. CreateDictionaryMember('A','short','""',False,False);
  376. TestDef('short A = ""',False);
  377. TestDef('short A = ""',True);
  378. CreateDictionaryMember('A','short','',True,False);
  379. TestDef('required short A',False);
  380. TestDef('required short A',True);
  381. CreateDictionaryMember('A','short','',False,True);
  382. TestDef('short A',False);
  383. TestDef('[Me] short A',True);
  384. CreateDictionaryMember('A','short','',true,True);
  385. TestDef('required short A',False);
  386. TestDef('[Me] required short A',True);
  387. end;
  388. procedure TTestDefinition.TestDictionary;
  389. Var
  390. m1,m2 : TIDLDictionaryMemberDefinition;
  391. begin
  392. CreateDictionary('',[],False);
  393. TestDef('dictionary A {'+sLinebreak+'}',True);
  394. CreateDictionary('B',[],False);
  395. TestDef('dictionary A : B {'+sLinebreak+'}',True);
  396. m1:=CreateDictionaryMember('B','short','',False,False);
  397. Def:=Nil;
  398. CreateDictionary('',[m1],False);
  399. TestDef('dictionary A {'+sLinebreak+
  400. ' short B;'+sLinebreak+
  401. '}',True);
  402. m1:=CreateDictionaryMember('C','short','',False,False);
  403. Def:=Nil;
  404. m2:=CreateDictionaryMember('D','short','',true,True);
  405. Def:=Nil;
  406. CreateDictionary('B',[m1,m2],False);
  407. TestDef('dictionary A : B {'+sLinebreak+
  408. ' short C;'+sLinebreak+
  409. ' [Me] required short D;'+sLinebreak+
  410. '}',True);
  411. end;
  412. procedure TTestDefinition.TestCallbackInterface;
  413. begin
  414. CreateInterface('',[],False).IsCallBack:=True;
  415. TestDef('callback interface A {'+sLinebreak+'}',True);
  416. end;
  417. procedure TTestDefinition.TestSequence;
  418. begin
  419. CreateSequence('short',false);
  420. TestDef('typedef sequence <short> A',True);
  421. TestDef('sequence <short>',False);
  422. CreateSequence('short',True);
  423. TestDef('[Me] typedef sequence <short> A',True);
  424. TestDef('sequence <short>',False);
  425. end;
  426. procedure TTestDefinition.TestPromise;
  427. begin
  428. CreatePromise('short',false);
  429. TestDef('typedef promise <short> A',True);
  430. TestDef('promise <short>',False);
  431. CreatePromise('short',true);
  432. TestDef('[Me] typedef promise <short> A',True);
  433. TestDef('promise <short>',False);
  434. CreatePromise('short',False).AllowNull:=True;
  435. TestDef('typedef promise <short>? A',True);
  436. TestDef('promise <short>?',False);
  437. end;
  438. procedure TTestDefinition.TestMapLike;
  439. begin
  440. CreateMapLike('short','string',false);
  441. TestDef('maplike <short,string>',True);
  442. TestDef('maplike <short,string>',False);
  443. CreateMapLike('short','string', True);
  444. TestDef('[Me] maplike <short,string>',True);
  445. TestDef('maplike <short,string>',False);
  446. CreateMapLike('short','string' ,false).IsReadOnly:=True;
  447. TestDef('readonly maplike <short,string>',True);
  448. TestDef('readonly maplike <short,string>',False);
  449. end;
  450. procedure TTestDefinition.TestSetLike;
  451. begin
  452. CreateSetLike('string',false);
  453. TestDef('setlike <string>',True);
  454. TestDef('setlike <string>',False);
  455. CreateSetLike('string', True);
  456. TestDef('[Me] setlike <string>',True);
  457. TestDef('setlike <string>',False);
  458. CreateSetLike('string' ,false).IsReadOnly:=True;
  459. TestDef('readonly setlike <string>',True);
  460. TestDef('readonly setlike <string>',False);
  461. end;
  462. procedure TTestDefinition.TestRecord;
  463. begin
  464. CreateRecord('short','string',false);
  465. TestDef('typedef record <short,string>',True);
  466. TestDef('record <short,string>',False);
  467. CreateRecord('short','string', True);
  468. TestDef('[Me] typedef record <short,string>',True);
  469. TestDef('record <short,string>',False);
  470. end;
  471. initialization
  472. RegisterTests([TTestDefinition])
  473. end.