tccsstree.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. unit tcCSSTree;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. TypInfo, Classes, SysUtils, fpcunit, testregistry, fpCSSTree;
  6. type
  7. { TBaseCSSTreeTest }
  8. TAppendMode = (amReplace,amNone,amChild,amSelector,amKey);
  9. TBaseCSSTreeTest = Class(TTestCase)
  10. private
  11. FElement: TCSSElement;
  12. Protected
  13. Procedure SetUp; override;
  14. Procedure TearDown; override;
  15. procedure AssertEquals(AMessage: String; AExpected, AActual: TCSSType); overload;
  16. Function CreateElement(aClass : TCSSElementClass; aAppend : TAppendMode = amReplace) : TCSSElement;
  17. Function CreateIdentifier(const avalue : string; aAppend : TAppendMode = amNone): TCSSIdentifierElement;
  18. Function CreateDeclaration(const aKey,avalue : string; aAppend : TAppendMode = amNone): TCSSDeclarationElement;
  19. Function CreateBinaryOperation(aOp : TCSSBinaryOperation;const aLeft,aRight : string; aAppend : TAppendMode = amNone): TCSSBinaryElement;
  20. Function CreateUnaryOperation(aOp : TCSSUnaryOperation;const aRight : string; aAppend : TAppendMode = amNone): TCSSUnaryElement;
  21. Property Element : TCSSElement Read FElement Write FElement;
  22. end;
  23. { TCSSTreeTypeTest }
  24. TCSSTreeTypeTest = Class(TBaseCSSTreeTest)
  25. Published
  26. Procedure TestINTEGER;
  27. Procedure TestSTRING;
  28. Procedure TestFLOAT;
  29. Procedure TestIDENTIFIER;
  30. Procedure TestCLASSNAME;
  31. Procedure TestPSEUDOCLASS;
  32. Procedure TestCOMPOUND;
  33. Procedure TestRULE;
  34. Procedure TestDECLARATION;
  35. Procedure TestBINARYOP;
  36. Procedure TestCALL;
  37. Procedure TestUNARYOP;
  38. Procedure TestARRAY;
  39. Procedure TestURL;
  40. Procedure TestUNICODERANGE;
  41. end;
  42. { TCSSTreeAsStringTest }
  43. TCSSTreeAsStringTest = Class(TBaseCSSTreeTest)
  44. Published
  45. Procedure TestINTEGER;
  46. Procedure TestSTRING;
  47. Procedure TestFLOAT;
  48. Procedure TestIDENTIFIER;
  49. Procedure TestCLASSNAME;
  50. Procedure TestPSEUDOCLASS;
  51. Procedure TestCOMPOUND;
  52. Procedure TestRULE;
  53. Procedure TestRULE2Declarations;
  54. Procedure TestRULESelector;
  55. Procedure TestRULE2Selectors;
  56. Procedure TestRULE2SelectorCombined;
  57. Procedure TestAtRULE;
  58. Procedure TestDECLARATION;
  59. Procedure TestDECLARATIONList;
  60. Procedure TestBINARYOP;
  61. Procedure TestCALL;
  62. Procedure TestUNARYOP;
  63. Procedure TestARRAY;
  64. Procedure TestURL;
  65. Procedure TestUNICODERANGE;
  66. end;
  67. { TEnumVisitor }
  68. TEnumVisitor = Class (TCSSTreeVisitor)
  69. Private
  70. FList: TFPList;
  71. Public
  72. Constructor Create(aList : TFPList);
  73. procedure Visit(obj: TCSSElement); override;
  74. end;
  75. { TCSSTreeVisitorTest }
  76. TCSSTreeVisitorTest = Class(TBaseCSSTreeTest)
  77. Private
  78. FList: TFPList;
  79. FVisitor: TCSSTreeVisitor;
  80. Public
  81. Procedure Setup; override;
  82. Procedure TearDown; override;
  83. Procedure CheckElement(aIndex : Integer; aElement : TCSSElement);
  84. Procedure CheckCount(aCount : Integer);
  85. Property List : TFPList Read FList;
  86. Property Visitor : TCSSTreeVisitor Read FVisitor;
  87. Published
  88. Procedure TestElement;
  89. Procedure TestINTEGER;
  90. Procedure TestSTRING;
  91. Procedure TestFLOAT;
  92. Procedure TestIDENTIFIER;
  93. Procedure TestCLASSNAME;
  94. Procedure TestPSEUDOCLASS;
  95. Procedure TestCOMPOUND;
  96. Procedure TestRULE;
  97. Procedure TestRULE2Declarations;
  98. Procedure TestRULESelector;
  99. Procedure TestRULE2Selectors;
  100. Procedure TestRULE2SelectorCombined;
  101. Procedure TestAtRULE;
  102. Procedure TestDECLARATION;
  103. Procedure TestDECLARATIONList;
  104. Procedure TestBINARYOP;
  105. Procedure TestCALL;
  106. Procedure TestUNARYOP;
  107. Procedure TestARRAY;
  108. Procedure TestURL;
  109. Procedure TestUNICODERANGE;
  110. end;
  111. { TCSSTreeOtherTest }
  112. TCSSTreeOtherTest = Class(TBaseCSSTreeTest)
  113. Published
  114. Procedure TestStringToIdentifier;
  115. end;
  116. implementation
  117. { TCSSTreeOtherTest }
  118. procedure TCSSTreeOtherTest.TestStringToIdentifier;
  119. begin
  120. AssertEquals('Normal','abc',StringToIdentifier('abc'));
  121. AssertEquals('dash','-abc',StringToIdentifier('-abc'));
  122. AssertEquals('dashdash','--abc',StringToIdentifier('--abc'));
  123. AssertEquals('Underscore','abc_d',StringToIdentifier('abc_d'));
  124. AssertEquals('Numerical','abc_1',StringToIdentifier('abc_1'));
  125. AssertEquals('Weird','abc\(1\)',StringToIdentifier('abc(1)'));
  126. end;
  127. { TCSSTreeVisitorTest }
  128. procedure TCSSTreeVisitorTest.Setup;
  129. begin
  130. inherited Setup;
  131. FList:=TFPList.Create;
  132. FVisitor:=TEnumVisitor.Create(FList);
  133. end;
  134. procedure TCSSTreeVisitorTest.TearDown;
  135. begin
  136. FreeAndNil(FVisitor);
  137. FreeAndNil(FList);
  138. inherited TearDown;
  139. end;
  140. procedure TCSSTreeVisitorTest.CheckElement(aIndex: Integer; aElement: TCSSElement);
  141. begin
  142. AssertTrue(Format('Index in range: %d in [0..%d[',[aIndex,FList.Count]),(aIndex>=0) and (aIndex<FList.Count));
  143. AssertSame(Format('Element %d is correct',[aIndex]),aElement,TObject(FList[aindex]));
  144. end;
  145. procedure TCSSTreeVisitorTest.CheckCount(aCount: Integer);
  146. begin
  147. AssertEquals('Count is correct',aCount,FList.Count);
  148. end;
  149. procedure TCSSTreeVisitorTest.TestElement;
  150. begin
  151. CreateElement(TCSSElement);
  152. Element.Iterate(Visitor);
  153. CheckCount(1);
  154. CheckElement(0,Element);
  155. end;
  156. procedure TCSSTreeVisitorTest.TestINTEGER;
  157. begin
  158. CreateElement(TCSSIntegerElement);
  159. Element.Iterate(Visitor);
  160. CheckCount(1);
  161. CheckElement(0,Element);
  162. end;
  163. procedure TCSSTreeVisitorTest.TestSTRING;
  164. begin
  165. CreateElement(TCSSStringElement);
  166. Element.Iterate(Visitor);
  167. CheckCount(1);
  168. CheckElement(0,Element);
  169. end;
  170. procedure TCSSTreeVisitorTest.TestFLOAT;
  171. begin
  172. CreateElement(TCSSFloatElement);
  173. Element.Iterate(Visitor);
  174. CheckCount(1);
  175. CheckElement(0,Element);
  176. end;
  177. procedure TCSSTreeVisitorTest.TestIDENTIFIER;
  178. begin
  179. CreateElement(TCSSIdentifierElement);
  180. Element.Iterate(Visitor);
  181. CheckCount(1);
  182. CheckElement(0,Element);
  183. end;
  184. procedure TCSSTreeVisitorTest.TestCLASSNAME;
  185. begin
  186. CreateElement(TCSSClassNameElement);
  187. Element.Iterate(Visitor);
  188. CheckCount(1);
  189. CheckElement(0,Element);
  190. end;
  191. procedure TCSSTreeVisitorTest.TestPSEUDOCLASS;
  192. begin
  193. CreateElement(TCSSPseudoClassElement);
  194. Element.Iterate(Visitor);
  195. CheckCount(1);
  196. CheckElement(0,Element);
  197. end;
  198. procedure TCSSTreeVisitorTest.TestCOMPOUND;
  199. begin
  200. CreateElement(TCSSCompoundElement);
  201. Element.Iterate(Visitor);
  202. CheckCount(1);
  203. CheckElement(0,Element);
  204. end;
  205. procedure TCSSTreeVisitorTest.TestRULE;
  206. begin
  207. CreateElement(TCSSRuleElement);
  208. Element.Iterate(Visitor);
  209. CheckCount(1);
  210. CheckElement(0,Element);
  211. end;
  212. procedure TCSSTreeVisitorTest.TestRULE2Declarations;
  213. begin
  214. end;
  215. procedure TCSSTreeVisitorTest.TestRULESelector;
  216. begin
  217. end;
  218. procedure TCSSTreeVisitorTest.TestRULE2Selectors;
  219. begin
  220. end;
  221. procedure TCSSTreeVisitorTest.TestRULE2SelectorCombined;
  222. begin
  223. end;
  224. procedure TCSSTreeVisitorTest.TestAtRULE;
  225. begin
  226. CreateElement(TCSSAtRuleElement);
  227. Element.Iterate(Visitor);
  228. CheckCount(1);
  229. CheckElement(0,Element);
  230. end;
  231. procedure TCSSTreeVisitorTest.TestDECLARATION;
  232. Var
  233. Decl: TCSSDeclarationElement;
  234. begin
  235. Decl:=CreateDeclaration('a','b',amReplace);
  236. Element.Iterate(Visitor);
  237. CheckCount(3);
  238. CheckElement(0,Element);
  239. CheckElement(1,Decl.Keys[0]);
  240. CheckElement(2,Decl.Children[0]);
  241. end;
  242. procedure TCSSTreeVisitorTest.TestDECLARATIONList;
  243. Var
  244. Decl: TCSSDeclarationElement;
  245. begin
  246. Decl:=CreateDeclaration('a','b',amReplace);
  247. CreateIdentifier('c',amChild);
  248. Element.Iterate(Visitor);
  249. CheckCount(4);
  250. CheckElement(0,Element);
  251. CheckElement(1,Decl.Keys[0]);
  252. CheckElement(2,Decl.Children[0]);
  253. CheckElement(3,Decl.Children[1]);
  254. end;
  255. procedure TCSSTreeVisitorTest.TestBINARYOP;
  256. Var
  257. Bin : TCSSBinaryElement;
  258. begin
  259. Bin:=CreateBinaryOperation(boAnd,'a','b',amReplace);
  260. Element.Iterate(Visitor);
  261. CheckCount(3);
  262. CheckElement(0,Element);
  263. CheckElement(1,Bin.Right);
  264. CheckElement(2,Bin.Left);
  265. end;
  266. procedure TCSSTreeVisitorTest.TestCALL;
  267. Var
  268. aEl : TCSSElement;
  269. begin
  270. CreateElement(TCSSCallElement);
  271. aEl:=CreateIdentifier('a',amChild);
  272. Element.Iterate(Visitor);
  273. CheckCount(2);
  274. CheckElement(0,Element);
  275. CheckElement(1,aEl);
  276. end;
  277. procedure TCSSTreeVisitorTest.TestUNARYOP;
  278. begin
  279. CreateUnaryOperation(uoDoubleColon,'a',amReplace);
  280. Element.Iterate(Visitor);
  281. CheckCount(2);
  282. CheckElement(0,Element);
  283. CheckElement(1,TCSSUnaryElement(Element).Right);
  284. end;
  285. procedure TCSSTreeVisitorTest.TestARRAY;
  286. begin
  287. CreateElement(TCSSArrayElement);
  288. CreateIdentifier('a',amChild);
  289. CreateIdentifier('b',amChild);
  290. Element.Iterate(Visitor);
  291. CheckCount(3);
  292. CheckElement(0,Element);
  293. CheckElement(1,TCSSArrayElement(Element).Children[0]);
  294. CheckElement(2,TCSSArrayElement(Element).Children[1]);
  295. end;
  296. procedure TCSSTreeVisitorTest.TestURL;
  297. begin
  298. CreateElement(TCSSURLElement);
  299. Element.Iterate(Visitor);
  300. CheckCount(1);
  301. CheckElement(0,Element);
  302. end;
  303. procedure TCSSTreeVisitorTest.TestUNICODERANGE;
  304. begin
  305. CreateElement(TCSSUnicodeRangeElement);
  306. Element.Iterate(Visitor);
  307. CheckCount(1);
  308. CheckElement(0,Element);
  309. end;
  310. { TEnumVisitor }
  311. constructor TEnumVisitor.Create(aList: TFPList);
  312. begin
  313. FList:=AList;
  314. end;
  315. procedure TEnumVisitor.Visit(obj: TCSSElement);
  316. begin
  317. FList.Add(obj);
  318. end;
  319. { TCSSTreeAsStringTest }
  320. procedure TCSSTreeAsStringTest.TestINTEGER;
  321. begin
  322. TCSSIntegerElement(CreateElement(TCSSIntegerElement)).Value:=123;
  323. AssertEquals('Value','123',Element.AsString);
  324. end;
  325. procedure TCSSTreeAsStringTest.TestSTRING;
  326. begin
  327. TCSSStringElement(CreateElement(TCSSStringElement)).Value:='abc';
  328. AssertEquals('Value','"abc"',Element.AsString);
  329. end;
  330. procedure TCSSTreeAsStringTest.TestFLOAT;
  331. begin
  332. TCSSFloatElement(CreateElement(TCSSFloatElement)).Value:=1.23;
  333. AssertEquals('Value','1.23',Element.AsString);
  334. end;
  335. procedure TCSSTreeAsStringTest.TestIDENTIFIER;
  336. begin
  337. TCSSIdentifierElement(CreateElement(TCSSIdentifierElement)).Value:='abc';
  338. AssertEquals('Value','abc',Element.AsString);
  339. end;
  340. procedure TCSSTreeAsStringTest.TestCLASSNAME;
  341. begin
  342. TCSSClassNameElement(CreateElement(TCSSClassNameElement)).Value:='.abc';
  343. AssertEquals('Value','.abc',Element.AsString);
  344. end;
  345. procedure TCSSTreeAsStringTest.TestPSEUDOCLASS;
  346. begin
  347. TCSSPseudoClassElement(CreateElement(TCSSPseudoClassElement)).Value:=':abc';
  348. AssertEquals('Value',':abc',Element.AsString);
  349. TCSSPseudoClassElement(CreateElement(TCSSPseudoClassElement)).Value:='::abc';
  350. AssertEquals('Value','::abc',Element.AsString);
  351. end;
  352. procedure TCSSTreeAsStringTest.TestCOMPOUND;
  353. Var
  354. aRule : TCSSRuleElement;
  355. begin
  356. CreateElement(TCSSCompoundElement);
  357. aRule:=TCSSRuleElement(CreateElement(TCSSRuleElement,amChild));
  358. aRule.AddChild(CreateDeclaration('a','b',amNone));
  359. aRule:=TCSSRuleElement(CreateElement(TCSSRuleElement,amChild));
  360. aRule.AddChild(CreateDeclaration('c','d',amNone));
  361. aRule.AddSelector(CreateIdentifier('p',amNone));
  362. AssertEquals('Value','{ a : b; }p { c : d; }',Element.AsString);
  363. AssertEquals('Value','{'+sLineBreak+' a : b;'+sLineBreak+'}'+sLineBReak+'p {'+sLineBReak+' c : d;'+sLineBreak+'}',Element.AsFormattedString);
  364. end;
  365. procedure TCSSTreeAsStringTest.TestRULE;
  366. begin
  367. CreateElement(TCSSRuleElement);
  368. CreateDeclaration('a','b',amChild);
  369. AssertEquals('Value','{ a : b; }',Element.AsString);
  370. AssertEquals('Value','{'+sLineBreak+' a : b;'+sLineBreak+'}',Element.AsFormattedString);
  371. end;
  372. procedure TCSSTreeAsStringTest.TestRULE2Declarations;
  373. begin
  374. CreateElement(TCSSRuleElement);
  375. CreateDeclaration('a','b',amChild);
  376. CreateDeclaration('c','d',amChild);
  377. AssertEquals('Value','{ a : b; c : d; }',Element.AsString);
  378. AssertEquals('Value','{'+sLineBreak+' a : b;'+sLineBreak+' c : d;'+sLineBreak+'}',Element.AsFormattedString);
  379. end;
  380. procedure TCSSTreeAsStringTest.TestRULESelector;
  381. Var
  382. aIdent : TCSSIdentifierElement;
  383. begin
  384. CreateElement(TCSSRuleElement);
  385. CreateDeclaration('a','b',amChild);
  386. aIdent:=CreateIdentifier('c',amSelector);
  387. AssertEquals('Value','c { a : b; }',Element.AsString);
  388. AssertEquals('Value','c {'+sLineBreak+' a : b;'+sLineBreak+'}',Element.AsFormattedString);
  389. end;
  390. procedure TCSSTreeAsStringTest.TestRULE2Selectors;
  391. Var
  392. aIdent : TCSSIdentifierElement;
  393. begin
  394. CreateElement(TCSSRuleElement);
  395. CreateDeclaration('a','b',amChild);
  396. aIdent:=CreateIdentifier('c',amSelector);
  397. aIdent:=CreateIdentifier('d',amSelector);
  398. AssertEquals('Value','c, d { a : b; }',Element.AsString);
  399. AssertEquals('Value','c,'+sLineBreak+'d {'+sLineBreak+' a : b;'+sLineBreak+'}',Element.AsFormattedString);
  400. end;
  401. procedure TCSSTreeAsStringTest.TestRULE2SelectorCombined;
  402. Var
  403. aList : TCSSListElement;
  404. aIdent : TCSSIdentifierElement;
  405. begin
  406. CreateElement(TCSSRuleElement);
  407. CreateDeclaration('a','b',amChild);
  408. aList:=TCSSListElement(CreateElement(TCSSListElement,amSelector));
  409. aIdent:=CreateIdentifier('c',amNone);
  410. aList.AddChild(aIdent);
  411. aIdent:=CreateIdentifier('d',amNone);
  412. aList.AddChild(aIdent);
  413. aIdent:=CreateIdentifier('e',amSelector);
  414. AssertEquals('Value','c d, e { a : b; }',Element.AsString);
  415. AssertEquals('Value','c d,'+sLineBreak+'e {'+sLineBreak+' a : b;'+sLineBreak+'}',Element.AsFormattedString);
  416. end;
  417. procedure TCSSTreeAsStringTest.TestAtRULE;
  418. Var
  419. aATRule : TCSSAtRuleElement;
  420. aURL : TCSSURLElement;
  421. begin
  422. aATRule:=TCSSAtRuleElement(CreateElement(TCSSAtRuleElement));
  423. aATRule.atKeyWord:='@import';
  424. aURL:=TCSSURLElement(CreateElement(TCSSURLElement,amSelector));
  425. aURL.Value:='url("me.css")';
  426. AssertEquals('Value','@import url("me.css");',Element.AsFormattedString);
  427. end;
  428. procedure TCSSTreeAsStringTest.TestDECLARATION;
  429. begin
  430. CreateDeclaration('a','b',amReplace);
  431. AssertEquals('Value','a : b',Element.AsString)
  432. end;
  433. procedure TCSSTreeAsStringTest.TestDECLARATIONList;
  434. Var
  435. aList: TCSSListElement;
  436. begin
  437. CreateElement(TCSSDeclarationElement);
  438. CreateIdentifier('a',amKey);
  439. aList:=TCSSListElement(CreateElement(TCSSListElement,amChild));
  440. aList.AddChild(CreateIdentifier('b',amNone));
  441. aList.AddChild(CreateIdentifier('c',amNone));
  442. CreateIdentifier('d',amChild);
  443. AssertEquals('Value','a : b c, d',Element.AsString)
  444. end;
  445. procedure TCSSTreeAsStringTest.TestBINARYOP;
  446. Const
  447. MyBinaryOperators : Array[TCSSBinaryOperation] of string =
  448. ('=','+','-','and','<','>','/','*','~',':','::','^');
  449. Var
  450. Op : TCSSBinaryOperation;
  451. Sop : String;
  452. begin
  453. For Op in TCSSBinaryOperation do
  454. begin
  455. CreateBinaryOperation(Op,'a','b',amReplace);
  456. Sop:=MyBinaryOperators[Op];
  457. if Not (Op in [boColon,boDoubleColon]) then
  458. Sop:=' '+Sop+' ';
  459. AssertEquals('Value '+Sop,'a'+sop+'b',Element.AsString)
  460. end;
  461. end;
  462. procedure TCSSTreeAsStringTest.TestCALL;
  463. Var
  464. aCall : TCSSCallElement;
  465. begin
  466. aCall:=TCSSCallElement(CreateElement(TCSSCallElement));
  467. aCall.Name:='me';
  468. CreateIdentifier('a',amChild);
  469. AssertEquals('Value','me(a)',Element.AsString);
  470. end;
  471. procedure TCSSTreeAsStringTest.TestUNARYOP;
  472. Const
  473. MyUnaryOperators : Array[TCSSUnaryOperation] of string =
  474. ('::','-','+','/');
  475. Var
  476. Op : TCSSUnaryOperation;
  477. Sop : String;
  478. begin
  479. For Op in TCSSUnaryOperation do
  480. begin
  481. CreateUnaryOperation(op,'a',amReplace);
  482. Sop:=MyUnaryOperators[Op];
  483. if Not (Op in [uoDoubleColon]) then
  484. Sop:=Sop+' ';
  485. AssertEquals('Value '+Sop,sop+'a',Element.AsString)
  486. end;
  487. end;
  488. procedure TCSSTreeAsStringTest.TestARRAY;
  489. begin
  490. CreateElement(TCSSArrayElement);
  491. CreateIdentifier('a',amChild);
  492. CreateIdentifier('b',amChild);
  493. AssertEquals('Value','[a b]',Element.AsString);
  494. AssertEquals('Value','[a b]',Element.AsFormattedString);
  495. end;
  496. procedure TCSSTreeAsStringTest.TestURL;
  497. Var
  498. Url : TCSSURLElement;
  499. begin
  500. Url:=TCSSURLElement(CreateElement(TCSSURLElement));
  501. Url.Value:='url("a.png")';
  502. AssertEquals('Value','url("a.png")',Element.AsString);
  503. end;
  504. procedure TCSSTreeAsStringTest.TestUNICODERANGE;
  505. Var
  506. Url : TCSSUnicodeRangeElement;
  507. begin
  508. Url:=TCSSUnicodeRangeElement(CreateElement(TCSSUnicodeRangeElement));
  509. Url.Value:='U+3588-488';
  510. AssertEquals('Value','U+3588-488',Element.AsString);
  511. end;
  512. { TCSSTreeTypeTest }
  513. procedure TCSSTreeTypeTest.TestINTEGER;
  514. begin
  515. AssertEquals('Type',csstINTEGER,CreateElement(TCSSIntegerElement).CSSType);
  516. end;
  517. procedure TCSSTreeTypeTest.TestSTRING;
  518. begin
  519. AssertEquals('Type',csstString,CreateElement(TCSSStringElement).CSSType);
  520. end;
  521. procedure TCSSTreeTypeTest.TestFLOAT;
  522. begin
  523. AssertEquals('Type',csstFloat,CreateElement(TCSSFloatElement).CSSType);
  524. end;
  525. procedure TCSSTreeTypeTest.TestIDENTIFIER;
  526. begin
  527. AssertEquals('Type',csstINTEGER,CreateElement(TCSSIntegerElement).CSSType);
  528. end;
  529. procedure TCSSTreeTypeTest.TestCLASSNAME;
  530. begin
  531. AssertEquals('Type',csstClassName,CreateElement(TCSSClassNameElement).CSSType);
  532. end;
  533. procedure TCSSTreeTypeTest.TestPSEUDOCLASS;
  534. begin
  535. AssertEquals('Type',csstPseudoClass,CreateElement(TCSSPseudoClassElement).CSSType);
  536. end;
  537. procedure TCSSTreeTypeTest.TestCOMPOUND;
  538. begin
  539. AssertEquals('Type',csstCompound,CreateElement(TCSSCompoundElement).CSSType);
  540. end;
  541. procedure TCSSTreeTypeTest.TestRULE;
  542. begin
  543. AssertEquals('Type',csstRule,CreateElement(TCSSRuleElement).CSSType);
  544. end;
  545. procedure TCSSTreeTypeTest.TestDECLARATION;
  546. begin
  547. AssertEquals('Type',csstDeclaration,CreateElement(TCSSDeclarationElement).CSSType);
  548. end;
  549. procedure TCSSTreeTypeTest.TestBINARYOP;
  550. begin
  551. AssertEquals('Type',csstBinaryOp,CreateElement(TCSSBinaryElement).CSSType);
  552. end;
  553. procedure TCSSTreeTypeTest.TestCALL;
  554. begin
  555. AssertEquals('Type',csstCall,CreateElement(TCSSCallElement).CSSType);
  556. end;
  557. procedure TCSSTreeTypeTest.TestUNARYOP;
  558. begin
  559. AssertEquals('Type',csstUnaryOp,CreateElement(TCSSUnaryElement).CSSType);
  560. end;
  561. procedure TCSSTreeTypeTest.TestARRAY;
  562. begin
  563. AssertEquals('Type',csstArray,CreateElement(TCSSArrayElement).CSSType);
  564. end;
  565. procedure TCSSTreeTypeTest.TestURL;
  566. begin
  567. AssertEquals('Type',csstURL,CreateElement(TCSSURLElement).CSSType);
  568. end;
  569. procedure TCSSTreeTypeTest.TestUNICODERANGE;
  570. begin
  571. AssertEquals('Type',csstUnicodeRange,CreateElement(TCSSUnicodeRangeElement).CSSType);
  572. end;
  573. { TBaseCSSTreeTest }
  574. procedure TBaseCSSTreeTest.SetUp;
  575. begin
  576. inherited SetUp;
  577. FreeAndNil(Felement);
  578. end;
  579. procedure TBaseCSSTreeTest.TearDown;
  580. begin
  581. FreeAndNil(Felement);
  582. inherited TearDown;
  583. end;
  584. procedure TBaseCSSTreeTest.AssertEquals(AMessage : String; AExpected, AActual: TCSSType);
  585. Var
  586. S,EN1,EN2 : String;
  587. begin
  588. If (AActual<>AExpected) then
  589. begin
  590. EN1:=GetEnumName(TypeINfo(TCSSType),Ord(AExpected));
  591. EN2:=GetEnumName(TypeINfo(TCSSType),Ord(AActual));
  592. S:=Format('%s : %s <> %s',[AMessage,EN1,EN2]);
  593. Fail(S);
  594. end;
  595. end;
  596. function TBaseCSSTreeTest.CreateElement(aClass: TCSSElementClass; aAppend : TAppendMode = amReplace): TCSSElement;
  597. begin
  598. Result:=aClass.Create(TestName+'.css',1,1);
  599. Case aAppend of
  600. amNone : ;
  601. amReplace :
  602. begin
  603. FreeAndNil(FElement);
  604. FElement:=Result;
  605. end;
  606. amChild:
  607. begin
  608. if FElement is TCSSChildrenElement then
  609. TCSSChildrenElement(FElement).AddChild(Result);
  610. end;
  611. amSelector:
  612. begin
  613. if FElement is TCSSRuleElement then
  614. TCSSRuleElement(FElement).AddSelector(Result);
  615. end;
  616. amKey:
  617. begin
  618. if FElement is TCSSDeclarationElement then
  619. TCSSDeclarationElement(FElement).AddKey(Result);
  620. end;
  621. end;
  622. end;
  623. function TBaseCSSTreeTest.CreateIdentifier(const avalue: string;
  624. aAppend: TAppendMode): TCSSIdentifierElement;
  625. begin
  626. Result:=TCSSIdentifierElement(CreateElement(TCSSIdentifierElement,aAppend));
  627. Result.Value:=aValue;
  628. end;
  629. function TBaseCSSTreeTest.CreateDeclaration(const aKey, avalue: string; aAppend : TAppendMode = amNone): TCSSDeclarationElement;
  630. var
  631. aIdent : TCSSIdentifierElement;
  632. begin
  633. Result:=TCSSDeclarationElement(CreateElement(TCSSDeclarationElement,aAppend));
  634. aIdent:=CreateIdentifier(aKey,amNone);
  635. Result.AddKey(aIdent);
  636. aIdent:=CreateIdentifier(aValue,amNone);
  637. Result.AddChild(aIdent);
  638. end;
  639. function TBaseCSSTreeTest.CreateBinaryOperation(aOp : TCSSBinaryOperation; const aLeft, aRight: string;
  640. aAppend: TAppendMode): TCSSBinaryElement;
  641. var
  642. aIdent : TCSSIdentifierElement;
  643. begin
  644. Result:=TCSSBinaryElement(CreateElement(TCSSBinaryElement,aAppend));
  645. Result.Operation:=aOp;
  646. aIdent:=CreateIdentifier(aLeft,amNone);
  647. Result.Left:=aIdent;
  648. aIdent:=CreateIdentifier(aRight,amNone);
  649. Result.Right:=aIdent;
  650. end;
  651. function TBaseCSSTreeTest.CreateUnaryOperation(aOp: TCSSUnaryOperation;
  652. const aRight: string; aAppend: TAppendMode): TCSSUnaryElement;
  653. var
  654. aIdent : TCSSIdentifierElement;
  655. begin
  656. Result:=TCSSUnaryElement(CreateElement(TCSSUnaryElement,aAppend));
  657. Result.Operation:=aOp;
  658. aIdent:=CreateIdentifier(aRight,amNone);
  659. Result.Right:=aIdent;
  660. end;
  661. initialization
  662. RegisterTests([TCSSTreeTypeTest,TCSSTreeAsStringTest,TCSSTreeVisitorTest,TCSSTreeOtherTest]);
  663. end.