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