Adding tests for conversion of string percentage to numeric

Adding two test cases:
1) Test the function in `StringHelper.php`.  Modeled this test on the test for the `convertToNumberIfFraction` function.
2) Test a spreadsheet with a string percentage in a formula to see if it  calculates the formula correctly.
This commit is contained in:
fjohnston@avatarasoftware.com
2022-11-03 22:03:24 -04:00
parent 1e3f579f37
commit 21772479e1
2 changed files with 41 additions and 0 deletions
@@ -180,6 +180,18 @@ class CalculationTest extends TestCase
self::assertEquals('9', $cell3->getCalculatedValue());
}
public function testCellWithStringPercentage(): void
{
$spreadsheet = new Spreadsheet();
$workSheet = $spreadsheet->getActiveSheet();
$cell1 = $workSheet->getCell('A1');
$cell1->setValue('2%');
$cell2 = $workSheet->getCell('B1');
$cell2->setValue('=100*A1');
self::assertEquals('2', $cell2->getCalculatedValue());
}
public function testBranchPruningFormulaParsingSimpleCase(): void
{
$calculation = Calculation::getInstance();
@@ -148,4 +148,33 @@ class StringHelperTest extends TestCase
'improper fraction' => ['1.75', '7/4'],
];
}
/**
* @dataProvider providerPercentages
*/
public function testPercentage(string $expected, string $value): void
{
$originalValue = $value;
$result = StringHelper::convertToNumberIfPercent($value);
if ($result === false) {
self::assertSame($expected, $originalValue);
self::assertSame($expected, $value);
} else {
self::assertSame($expected, (string) $value);
self::assertNotEquals($value, $originalValue);
}
}
public function providerPercentages(): array
{
return [
'non-percentage' => ['10', '10'],
'single digit percentage' => ['0.02', '2%'],
'two digit percentage' => ['0.13', '13%'],
'negative single digit percentage' => ['-0.07', '-7%'],
'negative two digit percentage' => ['-0.75', '-75%'],
'large percentage' => ['98.45', '9845%'],
'small percentage' => ['0.0005', '0.05%'],
];
}
}