Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

143 lines
4.8KB

  1. <?php
  2. class HTMLPurifier_UnitConverterTest extends HTMLPurifier_Harness
  3. {
  4. protected function assertConversion($input, $expect, $unit = null, $test_negative = true)
  5. {
  6. $length = HTMLPurifier_Length::make($input);
  7. if ($expect !== false) $expectl = HTMLPurifier_Length::make($expect);
  8. else $expectl = false;
  9. $to_unit = $unit !== null ? $unit : $expectl->getUnit();
  10. $converter = new HTMLPurifier_UnitConverter(4, 10);
  11. $result = $converter->convert($length, $to_unit);
  12. if (!$result || !$expectl) $this->assertIdentical($result, $expectl);
  13. else $this->assertIdentical($result->toString(), $expectl->toString());
  14. $converter = new HTMLPurifier_UnitConverter(4, 10, true);
  15. $result = $converter->convert($length, $to_unit);
  16. if (!$result || !$expectl) $this->assertIdentical($result, $expectl);
  17. else $this->assertIdentical($result->toString(), $expectl->toString(), 'BCMath substitute: %s');
  18. if ($test_negative) {
  19. $this->assertConversion(
  20. "-$input",
  21. $expect === false ? false : "-$expect",
  22. $unit,
  23. false
  24. );
  25. }
  26. }
  27. public function testFail()
  28. {
  29. $this->assertConversion('1in', false, 'foo');
  30. $this->assertConversion('1foo', false, 'in');
  31. }
  32. public function testZero()
  33. {
  34. $this->assertConversion('0', '0', 'in', false);
  35. $this->assertConversion('-0', '0', 'in', false);
  36. $this->assertConversion('0in', '0', 'in', false);
  37. $this->assertConversion('-0in', '0', 'in', false);
  38. $this->assertConversion('0in', '0', 'pt', false);
  39. $this->assertConversion('-0in', '0', 'pt', false);
  40. }
  41. public function testEnglish()
  42. {
  43. $this->assertConversion('1in', '6pc');
  44. $this->assertConversion('6pc', '1in');
  45. $this->assertConversion('1in', '72pt');
  46. $this->assertConversion('72pt', '1in');
  47. $this->assertConversion('1pc', '12pt');
  48. $this->assertConversion('12pt', '1pc');
  49. $this->assertConversion('1pt', '0.01389in');
  50. $this->assertConversion('1.000pt', '0.01389in');
  51. $this->assertConversion('100000pt', '1389in');
  52. $this->assertConversion('1in', '96px');
  53. $this->assertConversion('96px', '1in');
  54. }
  55. public function testMetric()
  56. {
  57. $this->assertConversion('1cm', '10mm');
  58. $this->assertConversion('10mm', '1cm');
  59. $this->assertConversion('1mm', '0.1cm');
  60. $this->assertConversion('100mm', '10cm');
  61. }
  62. public function testEnglishMetric()
  63. {
  64. $this->assertConversion('2.835pt', '1mm');
  65. $this->assertConversion('1mm', '2.835pt');
  66. $this->assertConversion('0.3937in', '1cm');
  67. }
  68. public function testRoundingMinPrecision()
  69. {
  70. // One sig-fig, modified to be four, conversion rounds up
  71. $this->assertConversion('100pt', '1.389in');
  72. $this->assertConversion('1000pt', '13.89in');
  73. $this->assertConversion('10000pt', '138.9in');
  74. $this->assertConversion('100000pt', '1389in');
  75. $this->assertConversion('1000000pt', '13890in');
  76. }
  77. public function testRoundingUserPrecision()
  78. {
  79. // Five sig-figs, conversion rounds down
  80. $this->assertConversion('11112000pt', '154330in');
  81. $this->assertConversion('1111200pt', '15433in');
  82. $this->assertConversion('111120pt', '1543.3in');
  83. $this->assertConversion('11112pt', '154.33in');
  84. $this->assertConversion('1111.2pt', '15.433in');
  85. $this->assertConversion('111.12pt', '1.5433in');
  86. $this->assertConversion('11.112pt', '0.15433in');
  87. }
  88. public function testRoundingBigNumber()
  89. {
  90. $this->assertConversion('444400000000000000000000in', '42660000000000000000000000px');
  91. }
  92. protected function assertSigFig($n, $sigfigs)
  93. {
  94. $converter = new HTMLPurifier_UnitConverter();
  95. $result = $converter->getSigFigs($n);
  96. $this->assertIdentical($result, $sigfigs);
  97. }
  98. public function test_getSigFigs()
  99. {
  100. $this->assertSigFig('0', 0);
  101. $this->assertSigFig('1', 1);
  102. $this->assertSigFig('-1', 1);
  103. $this->assertSigFig('+1', 1);
  104. $this->assertSigFig('01', 1);
  105. $this->assertSigFig('001', 1);
  106. $this->assertSigFig('12', 2);
  107. $this->assertSigFig('012', 2);
  108. $this->assertSigFig('10', 1);
  109. $this->assertSigFig('10.', 2);
  110. $this->assertSigFig('100.', 3);
  111. $this->assertSigFig('103', 3);
  112. $this->assertSigFig('130', 2);
  113. $this->assertSigFig('.1', 1);
  114. $this->assertSigFig('0.1', 1);
  115. $this->assertSigFig('00.1', 1);
  116. $this->assertSigFig('0.01', 1);
  117. $this->assertSigFig('0.010', 2);
  118. $this->assertSigFig('0.012', 2);
  119. }
  120. }
  121. // vim: et sw=4 sts=4