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

Commit

Permalink
Merge "Adding more GenericArrayObject tests"
Browse files Browse the repository at this point in the history
  • Loading branch information
filbertkm authored and Gerrit Code Review committed Jul 26, 2012
2 parents 662f01c + 41fcff4 commit 9bab036
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 13 deletions.
11 changes: 0 additions & 11 deletions Diff/includes/Diff.php
Expand Up @@ -203,17 +203,6 @@ public function unserialize( $serialization ) {
return $serializationData;
}

/**
* @see IDiff::isEmpty
*
* @since 0.1
*
* @return boolean
*/
public function isEmpty() {
return $this->count() === 0;
}

/**
* Returns the add operations.
*
Expand Down
11 changes: 11 additions & 0 deletions Diff/includes/GenericArrayObject.php
Expand Up @@ -219,4 +219,15 @@ public function unserialize( $serialization ) {
return $serializationData;
}

/**
* Returns if the ArrayObject has no elements.
*
* @since 0.1
*
* @return boolean
*/
public function isEmpty() {
return $this->count() === 0;
}

}
21 changes: 19 additions & 2 deletions Diff/tests/DiffTest.php
Expand Up @@ -21,8 +21,10 @@
*/
class DiffTest extends GenericArrayObjectTest {

public function stuffProvider() {
public function elementInstancesProvider() {
return array(
array( array(
) ),
array( array(
new DiffOpAdd( 'ohi' )
) ),
Expand All @@ -49,7 +51,7 @@ public function stuffProvider() {
}

/**
* @dataProvider stuffProvider
* @dataProvider elementInstancesProvider
*/
public function testStuff( array $operations ) {
$diff = new Diff( $operations );
Expand All @@ -75,5 +77,20 @@ public function testStuff( array $operations ) {
$this->assertEquals( $count, $diff->count() );
}

public function instanceProvider() {
$instances = array();

foreach ( $this->stuffProvider() as $args ) {
$diffOps = $args[0];
$instances[] = new Diff( $diffOps );
}

return $instances;
}

public function getInstanceClass() {
return '\Diff\Diff';
}

}

159 changes: 159 additions & 0 deletions Diff/tests/GenericArrayObjectTest.php
@@ -1,6 +1,7 @@
<?php

namespace Diff\Test;
use \Diff\GenericArrayObject as GenericArrayObject;

/**
* Tests for the Diff\GenericArrayObject and deriving classes.
Expand All @@ -17,6 +18,164 @@
*/
abstract class GenericArrayObjectTest extends \MediaWikiTestCase {

public abstract function elementInstancesProvider();

public abstract function instanceProvider();

public abstract function getInstanceClass();

/**
* @return GenericArrayObject
*/
protected function getNew( array $elements = array() ) {
$class = $this->getInstanceClass();
return new $class( $elements );
}

/**
* @dataProvider elementInstancesProvider
* @param array $elements
*/
public function testConstructor( array $elements ) {
$arrayObject = $this->getNew( $elements );

$this->assertEquals( count( $elements ), $arrayObject->count() );
}

/**
* @dataProvider elementInstancesProvider
* @param array $elements
*/
public function testIsEmpty( array $elements ) {
$arrayObject = $this->getNew( $elements );

$this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
}

/**
* @dataProvider instanceProvider
* @param GenericArrayObject $list
*/
public function testUnset( GenericArrayObject $list ) {
if ( !$list->isEmpty() ) {
$offset = $list->getIterator()->key();
$count = $list->count();
$list->offsetUnset( $offset );
$this->assertEquals( $count - 1, $list->count() );
}

if ( !$list->isEmpty() ) {
$offset = $list->getIterator()->key();
$count = $list->count();
unset( $list[$offset] );
$this->assertEquals( $count - 1, $list->count() );
}

$exception = null;
try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception $exception ){}
$this->assertInstanceOf( '\Exception', $exception );
}

/**
* @dataProvider elementInstancesProvider
* @param array $elements
*/
public function testAppend( array $elements ) {
$list = $this->getNew();

$listSize = count( $elements );

foreach ( $elements as $element ) {
$list->append( $element );
}

$this->assertEquals( $listSize, $list->count() );

$list = $this->getNew();

foreach ( $elements as $element ) {
$list[] = $element;
}

$this->assertEquals( $listSize, $list->count() );

$this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
$list->append( $element );
} );
}

protected function checkTypeChecks( $function ) {
$excption = null;
$list = $this->getNew();

$elementClass = $list->getObjectType();

foreach ( array( 42, 'foo', array(), new \stdClass(), 4.2 ) as $element ) {
$validValid = $element instanceof $elementClass;

try{
call_user_func( $function, $list, $element );
$valid = true;
}
catch ( \MWException $exception ) {
$valid = false;
}

$this->assertEquals(
$validValid,
$valid,
'Object of invalid type got successfully added to a GenericArrayObject'
);
}
}

/**
* @dataProvider elementInstancesProvider
* @param array $elements
*/
public function testOffsetSet( array $elements ) {
if ( $elements === array() ) {
$this->assertTrue( true );
return;
}

$list = $this->getNew();

$element = reset( $elements );
$list->offsetSet( 42, $element );
$this->assertEquals( $element, $list->offsetGet( 42 ) );

$list = $this->getNew();

$element = reset( $elements );
$list['oHai'] = $element;
$this->assertEquals( $element, $list['oHai'] );

$list = $this->getNew();

$element = reset( $elements );
$list->offsetSet( 9001, $element );
$this->assertEquals( $element, $list[9001] );

$list = $this->getNew();

$element = reset( $elements );
$list->offsetSet( null, $element );
$this->assertEquals( $element, $list[0] );

$list = $this->getNew();
$offset = 0;

foreach ( $elements as $element ) {
$list->offsetSet( null, $element );
$this->assertEquals( $element, $list[$offset++] );
}

$this->assertEquals( count( $elements ), $list->count() );

$this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
$list->offsetSet( mt_rand(), $element );
} );
}

}

0 comments on commit 9bab036

Please sign in to comment.