Skip to content
Permalink
Browse files
Added a series of tests for ArrayCollection and ArrayList
git-svn-id: https://svn.apache.org/repos/asf/incubator/flex/whiteboard@1311563 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Michael Labriola committed Apr 10, 2012
1 parent 8766b6a commit c445d3b89365846951730b30ccc8f1e0a1782f57
Showing 12 changed files with 988 additions and 3 deletions.
@@ -92,7 +92,7 @@ import mx.utils.UIDUtil;
public class ArrayList extends EventDispatcher
implements IList, IExternalizable, IPropertyChangeNotifier
{
include "../core/Version.as";
//include "../core/Version.as";

//--------------------------------------------------------------------------
//
@@ -397,7 +397,12 @@ public class VectorList extends EventDispatcher
*/
public function removeItemAt(index:int):Object
{
if (index < 0 || index >= length)
if ( fixedLengthVector ) {
//Make a message in manager
throw new RangeError( "Fixed Length Vector");
}

if (index < 0 || index >= length)
{
var message:String = resourceManager.getString(
"collections", "outOfBounds", [ index ]);
@@ -2,13 +2,17 @@ package mx.collections.tests
{
import mx.collections.tests.vectorCollection.BasicFunctionsTest;
import mx.collections.tests.vectorList.VectorListSuite;
import mx.collections.tests.arrayCollection.BasicFunctionsTest;
import mx.collections.tests.arrayList.ArrayListSuite;

[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class CollectionTests
{
public var test1:mx.collections.tests.vectorCollection.BasicFunctionsTest;
public var test2:mx.collections.tests.vectorList.VectorListSuite;
public var test3:mx.collections.tests.arrayCollection.BasicFunctionsTest;
public var test4:mx.collections.tests.arrayList.ArrayListSuite;

}
}
@@ -0,0 +1,54 @@
package mx.collections.tests.arrayCollection {
import mx.collections.ICollectionView;
import mx.collections.IList;
import mx.collections.ArrayCollection;
import mx.collections.ArrayList;

import org.flexunit.assertThat;
import org.flexunit.asserts.assertNull;
import org.hamcrest.object.instanceOf;
import org.hamcrest.object.sameInstance;

public class BasicFunctionsTest {

[Test]
public function shouldImplementICollectionView():void {
var collection:ArrayCollection = new ArrayCollection();
assertThat( collection, instanceOf( ICollectionView ) );
}

[Test]
public function instantiatingWithoutSourceShouldBuildOwnArrayList():void {
var collection:ArrayCollection = new ArrayCollection();

assertThat( collection.list, instanceOf( ArrayList ) );
}

[Test]
public function instantiatingWithArrayShouldYieldListWithArray():void {
var array:Array = [];
var collection:ArrayCollection = new ArrayCollection( array );

assertThat( array, sameInstance( ArrayList( collection.list ).source ) );
}

[Test]
public function sourceShouldReturnArrayListSourceWhenListViable():void {
var array:Array = [];
var collection:ArrayCollection = new ArrayCollection( array );

assertThat( array, sameInstance( collection.source ) );
}

[Test]
public function sourceShouldReturnNullWhenListIsNull():void {
var collection:ArrayCollection = new ArrayCollection();
collection.list = null;

assertNull( collection.source );
}

public function BasicFunctionsTest() {
}
}
}
@@ -0,0 +1,124 @@
package mx.collections.tests.arrayList {
import flash.events.EventDispatcher;

import mx.collections.ArrayList;
import mx.events.PropertyChangeEvent;

import org.flexunit.assertThat;
import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertFalse;
import org.flexunit.asserts.assertTrue;
import org.hamcrest.collection.hasItem;
import org.hamcrest.core.not;

public class AddRemoveItemAtTest {

[Test]
public function addItemAtShouldInsertElementIntoArray():void {
var array:Array = [ 7, 8, 9 ];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

arrayList.addItemAt( value, 2 );
assertEquals( 4, arrayList.length );
assertEquals( value, array[ 2 ] );
}

public function addItemAtShouldAddEventListenerForEventDispatchers():void {
const value:EventDispatcher = new EventDispatcher();
var array:Array = [value];
var arrayList:ArrayList = new ArrayList( array );

assertFalse( value.hasEventListener( PropertyChangeEvent.PROPERTY_CHANGE ) );
arrayList.addItemAt( value, 0 );
assertTrue( value.hasEventListener( PropertyChangeEvent.PROPERTY_CHANGE ) );
}

[Test(expects="RangeError")]
public function shouldReceiveRangeErrorAttemptingToAddItemAtNegativeIndex():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

arrayList.addItemAt( value, -1 );
}

[Test(expects="RangeError")]
public function shouldReceiveRangeErrorAttemptingToAddItemAtPastEndOfArray():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

arrayList.addItemAt( value, 2 );
}

[Test]
public function removeItemAtShouldRemoveItemFromArray():void {
const value:int = 5;
var array:Array = [ value ];
var arrayList:ArrayList = new ArrayList( array );

assertThat( array, hasItem( value ) );

arrayList.removeItemAt( 0 );
assertThat( array, not( hasItem( value ) ) );
}

public function removeItemAtShouldRemoveEventListenerForEventDispatchers():void {
const value:EventDispatcher = new EventDispatcher();
var array:Array = [value];
var arrayList:ArrayList = new ArrayList( array );

assertTrue( value.hasEventListener( PropertyChangeEvent.PROPERTY_CHANGE ) );
arrayList.removeItemAt( 0 );
assertFalse( value.hasEventListener( PropertyChangeEvent.PROPERTY_CHANGE ) );
}

[Test]
public function removeItemAtShouldRemoveReturnRemovedItem():void {
const value:int = 5;
var array:Array = [ value ];
var arrayList:ArrayList = new ArrayList( array );

assertEquals( value, arrayList.removeItemAt( 0 ) );
}

[Test]
public function removeItemAtShouldRemoveArbitraryNode():void {
const value:int = 5;
var array:Array = [ 3, 7, 9, value, 11, 13 ];
var arrayList:ArrayList = new ArrayList( array );

assertThat( array, hasItem( value ) );

arrayList.removeItemAt( 3 );

assertThat( array, not( hasItem( value ) ) );
}

[Test]
public function removeItemAtShouldRemoveLastNode():void {
const value:int = 5;
var array:Array = [ 3, 7, 9, 11, 13, value ];
var arrayList:ArrayList = new ArrayList( array );

assertThat( array, hasItem( value ) );

arrayList.removeItemAt( 5 );

assertThat( array, not( hasItem( value ) ) );
}

[Test(expects="RangeError")]
public function removeItemAtShouldThrowRangeErrorIfOutOfRangeIndexSupplied():void {
const value:int = 5;
var array:Array = [ value ];
var arrayList:ArrayList = new ArrayList( array );

arrayList.removeItemAt( 3 );
}

public function AddRemoveItemAtTest() {
}
}
}
@@ -0,0 +1,121 @@
package mx.collections.tests.arrayList {
import flash.display.Sprite;
import flash.events.EventDispatcher;

import mx.collections.ArrayList;

import org.flexunit.assertThat;
import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertFalse;
import org.flexunit.asserts.assertStrictlyEquals;
import org.flexunit.asserts.assertTrue;
import org.hamcrest.collection.hasItem;
import org.hamcrest.core.not;
import org.hamcrest.object.sameInstance;

public class AddRemoveItemTest {

private static const EMPTY:int = 0;

[Test]
public function addItemShouldAddFirstItemToEmptyArray():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

assertThat( array, not( hasItem( value ) ) );

arrayList.addItem( value );
assertEquals( 1, arrayList.length );
assertThat( array, hasItem( value ) );
}

[Test]
public function addItemShouldAddLastItemToPopulatedArray():void {
var array:Array = [ 7, 8, 9 ];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

assertEquals( 3, arrayList.length );

arrayList.addItem( value );
assertEquals( 4, arrayList.length );
assertEquals( value, array[ 3 ] );
}

[Test]
public function removeItemShouldRemoveSimpleItemFromArray():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

arrayList.addItem( value );
assertThat( array, hasItem( value ) );

arrayList.removeItem( value );
assertThat( array, not( hasItem( value ) ) );
}

[Test]
public function removeItemShouldRemoveSingleSimpleItemFromArray():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:int = 5;

arrayList.addItem( value );
arrayList.addItem( value );
assertThat( array, hasItem( value ) );

arrayList.removeItem( value );
assertThat( array, hasItem( value ) );
assertEquals( 1, array.length );
}

[Test]
public function removeItemShouldRemoveComplexItemFromArray():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:EventDispatcher = new EventDispatcher();

arrayList.addItem( value );
assertThat( array, hasItem( value ) );

arrayList.removeItem( value );
assertThat( array, not( hasItem( value ) ) );
}

[Test]
public function removeItemShouldRemoveReturnTrueIfItemFound():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:EventDispatcher = new EventDispatcher();

arrayList.addItem( value );
assertTrue( arrayList.removeItem( value ) );
}

[Test]
public function removeItemShouldRemoveReturnFalseIfItemNotFound():void {
var array:Array = [];
var arrayList:ArrayList = new ArrayList( array );
const value:EventDispatcher = new EventDispatcher();

assertFalse( arrayList.removeItem( value ) );
}

[Test]
public function removeAllShouldEmptyArray():void {
var array:Array = [ 5, 7, 9, 11, 13 ];
var arrayList:ArrayList = new ArrayList( array );

assertEquals( 5, array.length );

arrayList.removeAll();

assertEquals( EMPTY, array.length );
}

public function AddRemoveItemTest() {
}
}
}
@@ -0,0 +1,15 @@
package mx.collections.tests.arrayList
{

[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class ArrayListSuite
{
public var test1:mx.collections.tests.arrayList.BasicFunctionsTest;
public var test2:mx.collections.tests.arrayList.AddRemoveItemTest;
public var test3:mx.collections.tests.arrayList.AddRemoveItemAtTest;
public var test4:mx.collections.tests.arrayList.ConversionTest;
public var test5:mx.collections.tests.arrayList.EventsTest;
public var test6:mx.collections.tests.arrayList.ItemRetrievalTest;
}
}

0 comments on commit c445d3b

Please sign in to comment.