tccsstree.pp 21 KB

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