Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
add more tests for scalar-list-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Hansel23 committed Jun 14, 2016
1 parent 67705a4 commit dc9ea26
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/BooleanList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@ public function getItemType()
{
return $this->typeName;
}

/**
* @param array $booleans
*
* @return static
*/
public static function fromArray( array $booleans )
{
$list = new static();

foreach( $booleans as $boolean )
{
$list->add( $boolean );
}

return $list;
}
}
78 changes: 78 additions & 0 deletions tests/unit/ArrayListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Hansel23\GenericLists\Tests\Unit;

use Hansel23\GenericLists\ArrayList;
use Hansel23\GenericLists\Tests\Unit\Fixtures\Stringable;

class ArrayListTest extends \Codeception\TestCase\Test
{
public function InvalidItemProvider()
{
return [
[ 1, 1.0, true, false, new \stdClass(), new Stringable(), 'string' ],
];
}

/**
* @dataProvider InvalidItemProvider
*
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfInvalidItemsThrowsException( $item )
{
$stringList = new ArrayList();
$stringList->add( $item );
}

public function ValidItemProvider()
{
return [
[ [ 'true' ], [ [ '1' ] ], [ '1.0', '2.0', '3.0' ], [ ['false'], ['true'] ] ],
];
}

/**
* @dataProvider ValidItemProvider
*/
public function testAddingValidItems( $item )
{
$stringList = new ArrayList();
$stringList->add( $item );

$this->assertNotEquals( -1, $stringList->indexOf( $item ) );
}

public function ValidStringArrayProvider()
{
return [
[ [ ['here', 'is', 'a', 'testarray' ], [ '1', 2.0, 3 ] ], [ ['I like some'], ['more tests'] ] ],
];
}

/**
* @dataProvider ValidStringArrayProvider
*/
public function testCreatingStringListFromArray( array $subArrays )
{
$stringList = ArrayList::fromArray( $subArrays );

$this->assertEquals( count( $subArrays ), $stringList->count() );
$this->assertEquals( $subArrays, $stringList->toArray() );
}

public function InvalidArrayProvider()
{
return [
[ [ 1, false ], [ 'string', 'one more string', true ], [ [ ] ] ],
];
}

/**
* @dataProvider InvalidArrayProvider
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfCreatingFromInvalidArrayThrowsException( array $invalidArray )
{
ArrayList::fromArray( $invalidArray );
}
}
34 changes: 34 additions & 0 deletions tests/unit/BooleanListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,38 @@ public function testAddingValidItems( $item )

$this->assertNotEquals( -1, $boolList->indexOf( $item ) );
}

public function ValidBoolArrayProvider()
{
return [
[ [ false, true, false, false ], [ true, true, false ] ],
];
}

/**
* @dataProvider ValidBoolArrayProvider
*/
public function testCreatingNumericListFromArray( array $boolArray )
{
$stringList = BooleanList::fromArray( $boolArray );

$this->assertEquals( count( $boolArray ), $stringList->count() );
$this->assertEquals( $boolArray, $stringList->toArray() );
}

public function InvalidArrayProvider()
{
return [
[ [ 1, false ], [ true, 0 ], [ [ ] ] ],
];
}

/**
* @dataProvider InvalidArrayProvider
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfCreatingFromInvalidArrayThrowsException( array $invalidArray )
{
BooleanList::fromArray( $invalidArray );
}
}
34 changes: 34 additions & 0 deletions tests/unit/FloatListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,38 @@ public function testAddingValidItems( $item )

$this->assertNotEquals( -1, $floatList->indexOf( $item ) );
}

public function ValidFloatArrayProvider()
{
return [
[ [ 1.2, -1.0, 100.0, -999.999 ], [ 2.2, 1.0, -1.5 ] ],
];
}

/**
* @dataProvider ValidFloatArrayProvider
*/
public function testCreatingNumericListFromArray( array $floatArray )
{
$stringList = FloatList::fromArray( $floatArray );

$this->assertEquals( count( $floatArray ), $stringList->count() );
$this->assertEquals( $floatArray, $stringList->toArray() );
}

public function InvalidArrayProvider()
{
return [
[ [ 1.0, false ], [ true, 0.0 ], [ 1, 1.0 ], [ [ ] ] ],
];
}

/**
* @dataProvider InvalidArrayProvider
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfCreatingFromInvalidArrayThrowsException( array $invalidArray )
{
FloatList::fromArray( $invalidArray );
}
}
36 changes: 35 additions & 1 deletion tests/unit/IntegerListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testIfInvalidItemsThrowsException( $item )
public function ValidItemProvider()
{
return [
[ 1, 0, 10000, -324234 ],
[ 1, 0, 10000, -324234, PHP_INT_MAX ],
];
}

Expand All @@ -40,4 +40,38 @@ public function testAddingValidItems( $item )

$this->assertNotEquals( -1, $integerList->indexOf( $item ) );
}

public function ValidIntegerArrayProvider()
{
return [
[ [ 1, -1, 100, 999999 ], [ PHP_INT_MAX, 1, -1 ] ],
];
}

/**
* @dataProvider ValidIntegerArrayProvider
*/
public function testCreatingNumericListFromArray( array $integerArray )
{
$stringList = IntegerList::fromArray( $integerArray );

$this->assertEquals( count( $integerArray ), $stringList->count() );
$this->assertEquals( $integerArray, $stringList->toArray() );
}

public function InvalidArrayProvider()
{
return [
[ [ 1, false ], [ true, 0 ], [ 1, 1.0 ], [ [ ] ] ],
];
}

/**
* @dataProvider InvalidArrayProvider
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfCreatingFromInvalidArrayThrowsException( array $invalidArray )
{
IntegerList::fromArray( $invalidArray );
}
}
34 changes: 34 additions & 0 deletions tests/unit/NumericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,38 @@ public function testAddingValidItems( $item )

$this->assertNotEquals( -1, $numericList->indexOf( $item ) );
}

public function ValidNumericArrayProvider()
{
return [
[ [ '1.0', '1', '0.0', '0' ], [ 1.0, 0.0, 100.00, -324.234 ], [ 11.23, 1, 0, -100, '-100', '-1.02' ] ],
];
}

/**
* @dataProvider ValidNumericArrayProvider
*/
public function testCreatingNumericListFromArray( array $numericArray )
{
$stringList = NumericList::fromArray( $numericArray );

$this->assertEquals( count( $numericArray ), $stringList->count() );
$this->assertEquals( $numericArray, $stringList->toArray() );
}

public function InvalidArrayProvider()
{
return [
[ [ 1, false ], [ 'string', 1.0, 0.0, true ], [ [ ] ] ],
];
}

/**
* @dataProvider InvalidArrayProvider
* @expectedException \Hansel23\GenericLists\Exceptions\InvalidTypeException
*/
public function testIfCreatingFromInvalidArrayThrowsException( array $invalidArray )
{
NumericList::fromArray( $invalidArray );
}
}

0 comments on commit dc9ea26

Please sign in to comment.