|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheetTests\Calculation\Engine; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Calculation\Engine\FormattedNumber; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class FormattedNumberTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider providerFractions |
| 12 | + */ |
| 13 | + public function testFraction(string $expected, string $value): void |
| 14 | + { |
| 15 | + $originalValue = $value; |
| 16 | + $result = FormattedNumber::convertToNumberIfFraction($value); |
| 17 | + if ($result === false) { |
| 18 | + self::assertSame($expected, $originalValue); |
| 19 | + self::assertSame($expected, $value); |
| 20 | + } else { |
| 21 | + self::assertSame($expected, (string) $value); |
| 22 | + self::assertNotEquals($value, $originalValue); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public function providerFractions(): array |
| 27 | + { |
| 28 | + return [ |
| 29 | + 'non-fraction' => ['1', '1'], |
| 30 | + 'common fraction' => ['1.5', '1 1/2'], |
| 31 | + 'fraction between -1 and 0' => ['-0.5', '-1/2'], |
| 32 | + 'fraction between -1 and 0 with space' => ['-0.5', ' - 1/2'], |
| 33 | + 'fraction between 0 and 1' => ['0.75', '3/4 '], |
| 34 | + 'fraction between 0 and 1 with space' => ['0.75', ' 3/4'], |
| 35 | + 'improper fraction' => ['1.75', '7/4'], |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider providerPercentages |
| 41 | + */ |
| 42 | + public function testPercentage(string $expected, string $value): void |
| 43 | + { |
| 44 | + $originalValue = $value; |
| 45 | + $result = FormattedNumber::convertToNumberIfPercent($value); |
| 46 | + if ($result === false) { |
| 47 | + self::assertSame($expected, $originalValue); |
| 48 | + self::assertSame($expected, $value); |
| 49 | + } else { |
| 50 | + self::assertSame($expected, (string) $value); |
| 51 | + self::assertNotEquals($value, $originalValue); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function providerPercentages(): array |
| 56 | + { |
| 57 | + return [ |
| 58 | + 'non-percentage' => ['10', '10'], |
| 59 | + 'single digit percentage' => ['0.02', '2%'], |
| 60 | + 'two digit percentage' => ['0.13', '13%'], |
| 61 | + 'negative single digit percentage' => ['-0.07', '-7%'], |
| 62 | + 'negative two digit percentage' => ['-0.75', '-75%'], |
| 63 | + 'large percentage' => ['98.45', '9845%'], |
| 64 | + 'small percentage' => ['0.0005', '0.05%'], |
| 65 | + 'percentage with decimals' => ['0.025', '2.5%'], |
| 66 | + 'trailing percent with space' => ['0.02', '2 %'], |
| 67 | + 'trailing percent with leading and trailing space' => ['0.02', ' 2 % '], |
| 68 | + 'leading percent with decimals' => ['0.025', ' % 2.5'], |
| 69 | + |
| 70 | + //These should all fail |
| 71 | + 'percent only' => ['%', '%'], |
| 72 | + 'nonsense percent' => ['2%2', '2%2'], |
| 73 | + 'negative leading percent' => ['-0.02', '-%2'], |
| 74 | + |
| 75 | + //Percent position permutations |
| 76 | + 'permutation_1' => ['0.02', '2%'], |
| 77 | + 'permutation_2' => ['0.02', ' 2%'], |
| 78 | + 'permutation_3' => ['0.02', '2% '], |
| 79 | + 'permutation_4' => ['0.02', ' 2 % '], |
| 80 | + 'permutation_5' => ['0.0275', '2.75% '], |
| 81 | + 'permutation_6' => ['0.0275', ' 2.75% '], |
| 82 | + 'permutation_7' => ['0.0275', ' 2.75 % '], |
| 83 | + 'permutation_8' => [' 2 . 75 %', ' 2 . 75 %'], |
| 84 | + 'permutation_9' => [' 2.7 5 % ', ' 2.7 5 % '], |
| 85 | + 'permutation_10' => ['-0.02', '-2%'], |
| 86 | + 'permutation_11' => ['-0.02', ' -2% '], |
| 87 | + 'permutation_12' => ['-0.02', '- 2% '], |
| 88 | + 'permutation_13' => ['-0.02', '-2 % '], |
| 89 | + 'permutation_14' => ['-0.0275', '-2.75% '], |
| 90 | + 'permutation_15' => ['-0.0275', ' -2.75% '], |
| 91 | + 'permutation_16' => ['-0.0275', '-2.75 % '], |
| 92 | + 'permutation_17' => ['-0.0275', ' - 2.75 % '], |
| 93 | + 'permutation_18' => ['0.02', '2%'], |
| 94 | + 'permutation_19' => ['0.02', '% 2 '], |
| 95 | + 'permutation_20' => ['0.02', ' %2 '], |
| 96 | + 'permutation_21' => ['0.02', ' % 2 '], |
| 97 | + 'permutation_22' => ['0.0275', '%2.75 '], |
| 98 | + 'permutation_23' => ['0.0275', ' %2.75 '], |
| 99 | + 'permutation_24' => ['0.0275', ' % 2.75 '], |
| 100 | + 'permutation_25' => [' %2 . 75 ', ' %2 . 75 '], |
| 101 | + 'permutation_26' => [' %2.7 5 ', ' %2.7 5 '], |
| 102 | + 'permutation_27' => [' % 2 . 75 ', ' % 2 . 75 '], |
| 103 | + 'permutation_28' => [' % 2.7 5 ', ' % 2.7 5 '], |
| 104 | + 'permutation_29' => ['-0.0275', '-%2.75 '], |
| 105 | + 'permutation_30' => ['-0.0275', ' - %2.75 '], |
| 106 | + 'permutation_31' => ['-0.0275', '- % 2.75 '], |
| 107 | + 'permutation_32' => ['-0.0275', ' - % 2.75 '], |
| 108 | + 'permutation_33' => ['0.02', '2%'], |
| 109 | + 'permutation_34' => ['0.02', '2 %'], |
| 110 | + 'permutation_35' => ['0.02', ' 2%'], |
| 111 | + 'permutation_36' => ['0.02', ' 2 % '], |
| 112 | + 'permutation_37' => ['0.0275', '2.75%'], |
| 113 | + 'permutation_38' => ['0.0275', ' 2.75 % '], |
| 114 | + 'permutation_39' => ['2 . 75 % ', '2 . 75 % '], |
| 115 | + 'permutation_40' => ['-0.0275', '-2.75% '], |
| 116 | + 'permutation_41' => ['-0.0275', '- 2.75% '], |
| 117 | + 'permutation_42' => ['-0.0275', ' - 2.75% '], |
| 118 | + 'permutation_43' => ['-0.0275', ' -2.75 % '], |
| 119 | + 'permutation_44' => ['-2. 75 % ', '-2. 75 % '], |
| 120 | + 'permutation_45' => ['%', '%'], |
| 121 | + 'permutation_46' => ['0.02', '%2 '], |
| 122 | + 'permutation_47' => ['0.02', '% 2 '], |
| 123 | + 'permutation_48' => ['0.02', ' %2 '], |
| 124 | + 'permutation_49' => ['0.02', '% 2 '], |
| 125 | + 'permutation_50' => ['0.02', ' % 2 '], |
| 126 | + 'permutation_51' => ['0.02', ' 2 % '], |
| 127 | + 'permutation_52' => ['-0.02', '-2%'], |
| 128 | + 'permutation_53' => ['-0.02', '- %2'], |
| 129 | + 'permutation_54' => ['-0.02', ' -%2 '], |
| 130 | + 'permutation_55' => ['2%2', '2%2'], |
| 131 | + 'permutation_56' => [' 2% %', ' 2% %'], |
| 132 | + 'permutation_57' => [' % 2 -', ' % 2 -'], |
| 133 | + 'permutation_58' => ['-0.02', '%-2'], |
| 134 | + 'permutation_59' => ['-0.02', ' % - 2'], |
| 135 | + 'permutation_60' => ['-0.0275', '%-2.75 '], |
| 136 | + 'permutation_61' => ['-0.0275', ' % - 2.75 '], |
| 137 | + 'permutation_62' => ['-0.0275', ' % - 2.75 '], |
| 138 | + 'permutation_63' => ['-0.0275', ' % - 2.75 '], |
| 139 | + 'permutation_64' => ['0.0275', ' % + 2.75 '], |
| 140 | + 'permutation_65' => ['0.0275', ' % + 2.75 '], |
| 141 | + 'permutation_66' => ['0.0275', ' % + 2.75 '], |
| 142 | + 'permutation_67' => ['0.02', '+2%'], |
| 143 | + 'permutation_68' => ['0.02', ' +2% '], |
| 144 | + 'permutation_69' => ['0.02', '+ 2% '], |
| 145 | + 'permutation_70' => ['0.02', '+2 % '], |
| 146 | + 'permutation_71' => ['0.0275', '+2.75% '], |
| 147 | + 'permutation_72' => ['0.0275', ' +2.75% '], |
| 148 | + 'permutation_73' => ['0.0275', '+2.75 % '], |
| 149 | + 'permutation_74' => ['0.0275', ' + 2.75 % '], |
| 150 | + 'permutation_75' => ['-2.5E-6', '-2.5E-4%'], |
| 151 | + 'permutation_76' => ['200', '2E4%'], |
| 152 | + 'permutation_77' => ['-2.5E-8', '-%2.50E-06'], |
| 153 | + 'permutation_78' => [' - % 2.50 E -06 ', ' - % 2.50 E -06 '], |
| 154 | + 'permutation_79' => ['-2.5E-8', ' - % 2.50E-06 '], |
| 155 | + 'permutation_80' => [' - % 2.50E- 06 ', ' - % 2.50E- 06 '], |
| 156 | + 'permutation_81' => [' - % 2.50E - 06 ', ' - % 2.50E - 06 '], |
| 157 | + 'permutation_82' => ['-2.5E-6', '-2.5e-4%'], |
| 158 | + 'permutation_83' => ['200', '2e4%'], |
| 159 | + 'permutation_84' => ['-2.5E-8', '-%2.50e-06'], |
| 160 | + 'permutation_85' => [' - % 2.50 e -06 ', ' - % 2.50 e -06 '], |
| 161 | + 'permutation_86' => ['-2.5E-8', ' - % 2.50e-06 '], |
| 162 | + 'permutation_87' => [' - % 2.50e- 06 ', ' - % 2.50e- 06 '], |
| 163 | + 'permutation_88' => [' - % 2.50e - 06 ', ' - % 2.50e - 06 '], |
| 164 | + ]; |
| 165 | + } |
| 166 | +} |
0 commit comments