add more tests (#22)

This commit is contained in:
Saif Eddin G 2020-07-07 15:17:36 +02:00 committed by GitHub
parent 723a4f208e
commit c3d0cdc062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 145 additions and 32 deletions

View File

@ -42,7 +42,7 @@ foo([95, 96, null, 98]);
## Installation
This package doesn't not have a stable release yet, but you can still install it using composer :
This package doesn't have a stable release yet, but you can still install it using composer :
```console
$ composer require azjezz/psl:dev-develop

View File

@ -2,6 +2,11 @@
<psalm
totallyTyped="true"
resolveFromConfigFile="true"
forbidEcho="true"
strictBinaryOperands="true"
phpVersion="7.4"
allowPhpStormGenerics="true"
allowStringToStandInForClass="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"

View File

@ -56,4 +56,14 @@ class CountValuesTest extends TestCase
],
];
}
public function testCountThrowsForNonArrayKeyValues(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Expected all values to be of type array-key, value of type (object) provided.');
Arr\count_values([
new Collection\Map([]),
]);
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Psl\Tests;
use PHPUnit\Framework\TestCase;
use Psl;
use Psl\Exception\InvariantViolationException;
class InvariantTest extends TestCase
{
public function testInvariant(): void
{
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('Something went wrong!');
Psl\invariant(false, 'Something %s %s!', 'went', 'wrong');
}
public function testInvariantDoesNotThrowWhenTheFactIsTrue(): void
{
Psl\invariant(2 === (1 + 1), 'Unless?');
$this->addToAssertionCount(1);
}
}

View File

@ -5,8 +5,31 @@ declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
use Psl\Iter;
class AllTest extends TestCase
{
// TODO: add tests.
/**
* @dataProvider provideData
*/
public function testAll(bool $expected, iterable $iterable, callable $predicate): void
{
$this->assertSame($expected, Iter\all($iterable, $predicate));
}
public function provideData(): iterable
{
yield [false, [false, true, true], fn (bool $value): bool => $value];
yield [false, [false, true, true], fn (bool $value): bool => !$value];
yield [true, [true, true, true], fn (bool $value): bool => $value];
yield [false, [true, true, true], fn (bool $value): bool => !$value];
yield [false, [false, false, false], fn (bool $value): bool => $value];
yield [true, [false, false, false], fn (bool $value): bool => !$value];
yield [true, [false, false, false], fn (bool $value): bool => true];
yield [false, [false, false, false], fn (bool $value): bool => false];
yield [false, [1, 2, 3], fn (int $i): bool => $i > 3];
yield [true, [4, 5, 6], fn (int $i): bool => $i > 3];
yield [false, [1, 2, 3, 4, 5, 6], fn (int $i): bool => $i > 3];
yield [true, [], fn (bool $value): bool => false];
}
}

View File

@ -5,8 +5,31 @@ declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
use Psl\Iter;
class AnyTest extends TestCase
{
// TODO: add tests.
/**
* @dataProvider provideData
*/
public function testAny(bool $expected, iterable $iterable, callable $predicate): void
{
$this->assertSame($expected, Iter\any($iterable, $predicate));
}
public function provideData(): iterable
{
yield [true, [false, true, true], fn (bool $value): bool => $value];
yield [true, [false, true, true], fn (bool $value): bool => !$value];
yield [true, [true, true, true], fn (bool $value): bool => $value];
yield [false, [true, true, true], fn (bool $value): bool => !$value];
yield [false, [false, false, false], fn (bool $value): bool => $value];
yield [true, [false, false, false], fn (bool $value): bool => !$value];
yield [true, [false, false, false], fn (bool $value): bool => true];
yield [false, [false, false, false], fn (bool $value): bool => false];
yield [false, [1, 2, 3], fn (int $i): bool => $i > 3];
yield [true, [4, 5, 6], fn (int $i): bool => $i > 3];
yield [true, [1, 2, 3, 4, 5, 6], fn (int $i): bool => $i > 3];
yield [false, [], fn (bool $value): bool => false];
}
}

View File

@ -5,8 +5,16 @@ declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
use Psl\Collection\MutableVector;
use Psl\Iter;
class ApplyTest extends TestCase
{
// TODO: add tests.
public function testApply(): void
{
$vec = new MutableVector([]);
Iter\apply([1, 2, 3], fn (int $i) => $vec->add($i));
$this->assertSame([1, 2, 3], $vec->toArray());
}
}

View File

@ -5,41 +5,31 @@ declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
use Psl\Exception\InvariantViolationException;
use Psl\Iter;
class IteratorTest extends TestCase
{
public function testCurrent(): void
{
// TODO: write tests for Iterator::current
}
public function testNext(): void
{
// TODO: write tests for Iterator::next
}
public function testKey(): void
{
// TODO: write tests for Iterator::key
}
public function testValid(): void
{
// TODO: write tests for Iterator::valid
}
public function testRewind(): void
{
// TODO: write tests for Iterator::rewind
}
public function testSeek(): void
{
// TODO: write tests for Iterator::seek
$iterator = new Iter\Iterator([1, 2, 3, 4, 5]);
$this->assertSame(1, $iterator->current());
$iterator->next();
$this->assertSame(2, $iterator->current());
$iterator->next();
$iterator->seek(0);
$this->assertSame(1, $iterator->current());
}
public function testCount(): void
public function testSeekThrowsForOutOfBoundIndex(): void
{
// TODO: write tests for Iterator::count
$iterator = new Iter\Iterator([1, 2, 3, 4, 5]);
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('Position (30) is out-of-bound.');
$iterator->seek(30);
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Psl\Tests;
use PHPUnit\Framework\TestCase;
use Psl;
class SequenceTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testSequence(array $args, $expected): void
{
$this->assertSame($expected, Psl\sequence(...$args));
}
public function provideData(): iterable
{
yield [[], null];
yield [[null], null];
yield [[1, 2, 3], 3];
yield [['foo', 'bar', 'baz', 'qux'], 'qux'];
}
}