tccsstree.pp 20 KB

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