Fix INDEX() function when rows count less than row number (#233)

This commit is contained in:
Maxim 2017-09-30 06:13:20 +03:00 committed by Adrien Crivelli
parent 360db8dbcd
commit 810f174d6e
3 changed files with 95 additions and 2 deletions

View File

@ -627,7 +627,7 @@ class LookupRef
return Functions::VALUE();
}
if (!is_array($arrayValues)) {
if (!is_array($arrayValues) || ($rowNum > count($arrayValues))) {
return Functions::REF();
}
@ -647,7 +647,7 @@ class LookupRef
if (isset($arrayColumn[$rowNum])) {
$returnArray[] = $arrayColumn[$rowNum];
} else {
return $arrayValues[$rowNum];
return [$rowNum => $arrayValues[$rowNum]];
}
} else {
return $arrayValues[$rowNum];

View File

@ -66,4 +66,21 @@ class LookupRefTest extends PHPUnit_Framework_TestCase
{
return require 'data/Calculation/LookupRef/MATCH.php';
}
/**
* @dataProvider providerINDEX
* @group fail19
*
* @param mixed $expectedResult
*/
public function testINDEX($expectedResult, ...$args)
{
$result = LookupRef::INDEX(...$args);
self::assertEquals($expectedResult, $result);
}
public function providerINDEX()
{
return require 'data/Calculation/LookupRef/INDEX.php';
}
}

View File

@ -0,0 +1,76 @@
<?php
return [
[
[20 => ['R' => 1]], // Expected
// Input
[20 => ['R' => 1]],
],
[
'#VALUE!', // Expected
// Input
[
20 => ['R' => 1],
21 => ['R' => 2],
],
-1,
],
[
'#REF!', // Expected
// Input
[
20 => ['R' => 1],
21 => ['R' => 2],
],
10,
],
[
[21 => ['R' => 2]], // Expected
// Input
[
20 => ['R' => 1],
21 => ['R' => 2],
],
2,
],
[
[21 => ['R' => 2, 'S' => 4]], // Expected
// Input
[
'20' => ['R' => 1, 'S' => 3],
'21' => ['R' => 2, 'S' => 4],
],
2,
0,
],
[
'#VALUE!', // Expected
// Input
[
'20' => ['R' => 1, 'S' => 3],
'21' => ['R' => 2, 'S' => 4],
],
2,
-1,
],
[
'#VALUE!', // Expected
// Input
[
'20' => ['R' => 1, 'S' => 3],
'21' => ['R' => 2, 'S' => 4],
],
2,
10,
],
[
4, // Expected
// Input
[
'20' => ['R' => 1, 'S' => 3],
'21' => ['R' => 2, 'S' => 4],
],
2,
2,
],
];