LengthTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. class HTMLPurifier_LengthTest extends HTMLPurifier_Harness
  3. {
  4. function testConstruct() {
  5. $l = new HTMLPurifier_Length('23', 'in');
  6. $this->assertIdentical($l->getN(), '23');
  7. $this->assertIdentical($l->getUnit(), 'in');
  8. }
  9. function testMake() {
  10. $l = HTMLPurifier_Length::make('+23.4in');
  11. $this->assertIdentical($l->getN(), '+23.4');
  12. $this->assertIdentical($l->getUnit(), 'in');
  13. }
  14. function testToString() {
  15. $l = new HTMLPurifier_Length('23', 'in');
  16. $this->assertIdentical($l->toString(), '23in');
  17. }
  18. protected function assertValidate($string, $expect = true) {
  19. if ($expect === true) $expect = $string;
  20. $l = HTMLPurifier_Length::make($string);
  21. $result = $l->isValid();
  22. if ($result === false) $this->assertIdentical($expect, false);
  23. else $this->assertIdentical($l->toString(), $expect);
  24. }
  25. function testValidate() {
  26. $this->assertValidate('0');
  27. $this->assertValidate('+0', '0');
  28. $this->assertValidate('-0', '0');
  29. $this->assertValidate('0px');
  30. $this->assertValidate('4.5px');
  31. $this->assertValidate('-4.5px');
  32. $this->assertValidate('3ex');
  33. $this->assertValidate('3em');
  34. $this->assertValidate('3in');
  35. $this->assertValidate('3cm');
  36. $this->assertValidate('3mm');
  37. $this->assertValidate('3pt');
  38. $this->assertValidate('3pc');
  39. $this->assertValidate('3PX', '3px');
  40. $this->assertValidate('3', false);
  41. $this->assertValidate('3miles', false);
  42. }
  43. /**
  44. * @param $s1 First string to compare
  45. * @param $s2 Second string to compare
  46. * @param $expect 0 for $s1 == $s2, 1 for $s1 > $s2 and -1 for $s1 < $s2
  47. */
  48. protected function assertComparison($s1, $s2, $expect = 0) {
  49. $l1 = HTMLPurifier_Length::make($s1);
  50. $l2 = HTMLPurifier_Length::make($s2);
  51. $r1 = $l1->compareTo($l2);
  52. $r2 = $l2->compareTo($l1);
  53. $this->assertIdentical($r1 == 0 ? 0 : ($r1 > 0 ? 1 : -1), $expect);
  54. $this->assertIdentical($r2 == 0 ? 0 : ($r2 > 0 ? 1 : -1), - $expect);
  55. }
  56. function testCompareTo() {
  57. $this->assertComparison('12in', '12in');
  58. $this->assertComparison('12in', '12mm', 1);
  59. $this->assertComparison('1px', '1mm', -1);
  60. $this->assertComparison(str_repeat('2', 38) . 'in', '100px', 1);
  61. }
  62. }
  63. // vim: et sw=4 sts=4