|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheetTests\Calculation; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class ParseFormulaTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider providerBinaryOperations |
| 12 | + */ |
| 13 | + public function testParseOperations(array $expectedStack, string $formula): void |
| 14 | + { |
| 15 | + $parser = Calculation::getInstance(); |
| 16 | + $stack = $parser->parseFormula($formula); |
| 17 | + self::assertSame($expectedStack, $stack); |
| 18 | + } |
| 19 | + |
| 20 | + public function providerBinaryOperations(): array |
| 21 | + { |
| 22 | + return [ |
| 23 | + 'Unary negative with Value' => [ |
| 24 | + [ |
| 25 | + ['type' => 'Value', 'value' => 3, 'reference' => null], |
| 26 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 27 | + ], |
| 28 | + '=-3', |
| 29 | + ], |
| 30 | + 'Unary negative percentage with Value' => [ |
| 31 | + [ |
| 32 | + ['type' => 'Value', 'value' => 3, 'reference' => null], |
| 33 | + ['type' => 'Unary Operator', 'value' => '%', 'reference' => null], |
| 34 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 35 | + ], |
| 36 | + '=-3%', |
| 37 | + ], |
| 38 | + 'Binary minus with Values' => [ |
| 39 | + [ |
| 40 | + ['type' => 'Value', 'value' => 3, 'reference' => null], |
| 41 | + ['type' => 'Value', 'value' => 4, 'reference' => null], |
| 42 | + ['type' => 'Binary Operator', 'value' => '-', 'reference' => null], |
| 43 | + ], |
| 44 | + '=3-4', |
| 45 | + ], |
| 46 | + 'Unary negative with Cell Reference' => [ |
| 47 | + [ |
| 48 | + ['type' => 'Cell Reference', 'value' => 'A1', 'reference' => 'A1'], |
| 49 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 50 | + ], |
| 51 | + '=-A1', |
| 52 | + ], |
| 53 | + 'Unary negative with FQ Cell Reference' => [ |
| 54 | + [ |
| 55 | + ['type' => 'Cell Reference', 'value' => "'Sheet 1'!A1", 'reference' => "'Sheet 1'!A1"], |
| 56 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 57 | + ], |
| 58 | + "=-'Sheet 1'!A1", |
| 59 | + ], |
| 60 | + 'Unary negative percentage with Cell Reference' => [ |
| 61 | + [ |
| 62 | + ['type' => 'Cell Reference', 'value' => 'A1', 'reference' => 'A1'], |
| 63 | + ['type' => 'Unary Operator', 'value' => '%', 'reference' => null], |
| 64 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 65 | + ], |
| 66 | + '=-A1%', |
| 67 | + ], |
| 68 | + 'Unary negative with Defined Name' => [ |
| 69 | + [ |
| 70 | + ['type' => 'Defined Name', 'value' => 'DEFINED_NAME', 'reference' => 'DEFINED_NAME'], |
| 71 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 72 | + ], |
| 73 | + '=-DEFINED_NAME', |
| 74 | + ], |
| 75 | + 'Unary negative percentage with Defined Name' => [ |
| 76 | + [ |
| 77 | + ['type' => 'Defined Name', 'value' => 'DEFINED_NAME', 'reference' => 'DEFINED_NAME'], |
| 78 | + ['type' => 'Unary Operator', 'value' => '%', 'reference' => null], |
| 79 | + ['type' => 'Unary Operator', 'value' => '~', 'reference' => null], |
| 80 | + ], |
| 81 | + '=-DEFINED_NAME%', |
| 82 | + ], |
| 83 | + 'Cell Range' => [ |
| 84 | + [ |
| 85 | + ['type' => 'Cell Reference', 'value' => 'A1', 'reference' => 'A1'], |
| 86 | + ['type' => 'Cell Reference', 'value' => 'C3', 'reference' => 'C3'], |
| 87 | + ['type' => 'Binary Operator', 'value' => ':', 'reference' => null], |
| 88 | + ], |
| 89 | + '=A1:C3', |
| 90 | + ], |
| 91 | + 'Cell Range Intersection' => [ |
| 92 | + [ |
| 93 | + ['type' => 'Cell Reference', 'value' => 'A1', 'reference' => 'A1'], |
| 94 | + ['type' => 'Cell Reference', 'value' => 'C3', 'reference' => 'C3'], |
| 95 | + ['type' => 'Binary Operator', 'value' => ':', 'reference' => null], |
| 96 | + ['type' => 'Cell Reference', 'value' => 'B2', 'reference' => 'B2'], |
| 97 | + ['type' => 'Cell Reference', 'value' => 'D4', 'reference' => 'D4'], |
| 98 | + ['type' => 'Binary Operator', 'value' => ':', 'reference' => null], |
| 99 | + ['type' => 'Binary Operator', 'value' => '∩', 'reference' => null], |
| 100 | + ], |
| 101 | + '=A1:C3 B2:D4', |
| 102 | + ], |
| 103 | + 'Named Range Intersection' => [ |
| 104 | + [ |
| 105 | + ['type' => 'Defined Name', 'value' => 'DEFINED_NAME_1', 'reference' => 'DEFINED_NAME_1'], |
| 106 | + ['type' => 'Defined Name', 'value' => 'DEFINED_NAME_2', 'reference' => 'DEFINED_NAME_2'], |
| 107 | + ['type' => 'Binary Operator', 'value' => '∩', 'reference' => null], |
| 108 | + ], |
| 109 | + '=DEFINED_NAME_1 DEFINED_NAME_2', |
| 110 | + ], |
| 111 | + // 'Cell Range Union' => [ |
| 112 | + // [ |
| 113 | + // ['type' => 'Cell Reference', 'value' => 'A1', 'reference' => 'A1'], |
| 114 | + // ['type' => 'Cell Reference', 'value' => 'C3', 'reference' => 'C3'], |
| 115 | + // ['type' => 'Binary Operator', 'value' => ':', 'reference' => null], |
| 116 | + // ['type' => 'Cell Reference', 'value' => 'B2', 'reference' => 'B2'], |
| 117 | + // ['type' => 'Cell Reference', 'value' => 'D4', 'reference' => 'D4'], |
| 118 | + // ['type' => 'Binary Operator', 'value' => ':', 'reference' => null], |
| 119 | + // ['type' => 'Binary Operator', 'value' => '∪', 'reference' => null], |
| 120 | + // ], |
| 121 | + // '=A1:C3,B2:D4', |
| 122 | + // ], |
| 123 | + // 'Named Range Union' => [ |
| 124 | + // [ |
| 125 | + // ['type' => 'Defined Name', 'value' => 'DEFINED_NAME_1', 'reference' => 'DEFINED_NAME_1'], |
| 126 | + // ['type' => 'Defined Name', 'value' => 'DEFINED_NAME_2', 'reference' => 'DEFINED_NAME_2'], |
| 127 | + // ['type' => 'Binary Operator', 'value' => '∪', 'reference' => null], |
| 128 | + // ], |
| 129 | + // '=DEFINED_NAME_1,DEFINED_NAME_2', |
| 130 | + // ], |
| 131 | + ]; |
| 132 | + } |
| 133 | +} |
0 commit comments