Skip to content
Permalink
Browse files
Unit tests for VectorList and VectorCollection
git-svn-id: https://svn.apache.org/repos/asf/incubator/flex/whiteboard@1311531 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Michael Labriola committed Apr 10, 2012
1 parent b4b5e35 commit 8766b6aec39c7aae21f740a0e5387741669c3985
Showing 13 changed files with 1,879 additions and 62 deletions.

Large diffs are not rendered by default.

@@ -24,16 +24,13 @@ import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.utils.IExternalizable;

import mx.collections.ICollectionView;
import mx.collections.ListCollectionView;
import mx.collections.VectorList;
import mx.core.mx_internal;

use namespace mx_internal;

[DefaultProperty("source")]

public class VectorCollection extends ListCollectionView implements ICollectionView, IExternalizable
public class VectorCollection extends ListCollectionView implements ICollectionView
{

//--------------------------------------------------------------------------
@@ -94,37 +91,6 @@ public class VectorCollection extends ListCollectionView implements ICollectionV
//Provides a default VectorList
list = new VectorList( s );
}

//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------

/**
* @private
* Ensures that only the source property is serialized.
*/
public function readExternal(input:IDataInput):void
{
if (list is IExternalizable)
IExternalizable(list).readExternal(input);
else
source = input.readObject();
}

/**
* @private
* Ensures that only the source property is serialized.
*/
public function writeExternal(output:IDataOutput):void
{
if (list is IExternalizable)
IExternalizable(list).writeExternal(output);
else
output.writeObject(source);
}

}

}
@@ -62,19 +62,21 @@ import mx.utils.UIDUtil;
* is possible that a subclass might.
*/
public class VectorList extends EventDispatcher
implements IList, IExternalizable, IPropertyChangeNotifier {
implements IList, IPropertyChangeNotifier {

//--------------------------------------------------------------------------
//
// Static Initializer
//
//--------------------------------------------------------------------------
{
/* {
//If one attempt to serialize a Vector in any way, the type of the Vector is not retained unless the types are also aliased
//This should really be handled in the VM in my opinion, however, it is being done here to make it more pay as you go
//Unfortunately the consequence is that one must either do this manually or in some way reference a VectorList to ensure
//proper serialization of their Vectors
registerClassAlias( "Boolean", Boolean );
//This is commented out pending the outcome of discussion abotu serialization
registerClassAlias( "Boolean", Boolean );
registerClassAlias( "int", int );
registerClassAlias( "Number", Number );
registerClassAlias( "String", String );
@@ -85,7 +87,7 @@ public class VectorList extends EventDispatcher
registerClassAlias( "Vector", Vector );
}

*/
private static const VECTOR_PREFIX:String = "__AS3__.vec::Vector.";
private var fixedLengthVector:Boolean = false;

@@ -233,11 +235,8 @@ public class VectorList extends EventDispatcher
* Get the item at the specified index.
*
* @param index the index in the list from which to retrieve the item
* @param prefetch int indicating both the direction and amount of items
* to fetch during the request should the item not be local.
* @param Not used in this implementation at this time
* @return the item at that index, null if there is none
* @throws ItemPendingError if the data for that index needs to be
* loaded from a remote location
* @throws RangeError if the index < 0 or index >= length
*/
public function getItemAt(index:int, prefetch:int = 0):Object
@@ -486,24 +485,6 @@ public class VectorList extends EventDispatcher

return ar;
}

/**
* Ensures that only the source property is seralized.
* @private
*/
public function readExternal(input:IDataInput):void
{
source = input.readObject();
}

/**
* Ensures that only the source property is serialized.
* @private
*/
public function writeExternal(output:IDataOutput):void
{
output.writeObject(_source);
}

/**
* Pretty prints the contents of this VectorList to a string and returns it.
@@ -588,7 +569,7 @@ public class VectorList extends EventDispatcher
if (_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
{
var objEvent:PropertyChangeEvent = PropertyChangeEvent(event.clone());
var index:uint = getItemIndex(event.target);
var index:int = getItemIndex(event.target);
objEvent.property = index.toString() + "." + event.property;
dispatchEvent(objEvent);
}
@@ -0,0 +1,14 @@
package mx.collections.tests
{
import mx.collections.tests.vectorCollection.BasicFunctionsTest;
import mx.collections.tests.vectorList.VectorListSuite;

[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;

}
}
@@ -0,0 +1,59 @@
package mx.collections.tests.vectorCollection {
import mx.collections.ICollectionView;
import mx.collections.IList;
import mx.collections.VectorCollection;
import mx.collections.VectorList;

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:VectorCollection = new VectorCollection();
assertThat( collection, instanceOf( ICollectionView ) );
}

[Test]
public function instantiatingWithoutSourceShouldBuildOwnVectorList():void {
var collection:VectorCollection = new VectorCollection();

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

[Test]
public function instantiatingWithVectorShouldYieldListWithVector():void {
var vector:Vector.<*> = new Vector.<*>;
var collection:VectorCollection = new VectorCollection( vector );

assertThat( vector, sameInstance( VectorList( collection.list ).source ) );
}

[Test(expects="TypeError")]
public function instantiatingWithTypeOtherThanVectorShouldThrowTypeError():void {
var collection:VectorCollection = new VectorCollection( [] );
}

[Test]
public function sourceShouldReturnVectorListSourceWhenListViable():void {
var vector:Vector.<*> = new Vector.<*>;
var collection:VectorCollection = new VectorCollection( vector );

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

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

assertNull( collection.source );
}

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

import mx.collections.VectorList;
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 addItemAtShouldInsertElementIntoVector():void {
var vector:Vector.<int> = new <int>[ 7, 8, 9 ];
var vectorList:VectorList = new VectorList( vector );
const value:int = 5;

vectorList.addItemAt( value, 2 );
assertEquals( 4, vectorList.length );
assertEquals( value, vector[ 2 ] );
}

public function addItemAtShouldAddEventListenerForEventDispatchers():void {
const value:EventDispatcher = new EventDispatcher();
var vector:Vector.<EventDispatcher> = new <EventDispatcher>[value];
var vectorList:VectorList = new VectorList( vector );

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

[Test(expects="RangeError")]
public function shouldReceiveRangeErrorAttemptingToAddItemAtNegativeIndex():void {
var vector:Vector.<int> = new <int>[];
var vectorList:VectorList = new VectorList( vector );
const value:int = 5;

vectorList.addItemAt( value, -1 );
}

[Test(expects="RangeError")]
public function shouldReceiveRangeErrorAttemptingToAddItemAtPastEndOfVector():void {
var vector:Vector.<int> = new <int>[];
var vectorList:VectorList = new VectorList( vector );
const value:int = 5;

vectorList.addItemAt( value, 2 );
}

[Test(expects="RangeError")]
public function shouldReceiveErrorAttemptingToAddItemAtToFixedLengthVector():void {
var vector:Vector.<int> = new Vector.<int>( 1, true );
var vectorList:VectorList = new VectorList( vector );
const value:int = 5;

vectorList.addItemAt( value, 0 );
}

[Test]
public function removeItemAtShouldRemoveItemFromVector():void {
const value:int = 5;
var vector:Vector.<int> = new <int>[ value ];
var vectorList:VectorList = new VectorList( vector );

assertThat( vector, hasItem( value ) );

vectorList.removeItemAt( 0 );
assertThat( vector, not( hasItem( value ) ) );
}

public function removeItemAtShouldRemoveEventListenerForEventDispatchers():void {
const value:EventDispatcher = new EventDispatcher();
var vector:Vector.<EventDispatcher> = new <EventDispatcher>[value];
var vectorList:VectorList = new VectorList( vector );

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

[Test]
public function removeItemAtShouldRemoveReturnRemovedItem():void {
const value:int = 5;
var vector:Vector.<int> = new <int>[ value ];
var vectorList:VectorList = new VectorList( vector );

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

[Test]
public function removeItemAtShouldRemoveArbitraryNode():void {
const value:int = 5;
var vector:Vector.<int> = new <int>[ 3, 7, 9, value, 11, 13 ];
var vectorList:VectorList = new VectorList( vector );

assertThat( vector, hasItem( value ) );

vectorList.removeItemAt( 3 );

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

[Test]
public function removeItemAtShouldRemoveLastNode():void {
const value:int = 5;
var vector:Vector.<int> = new <int>[ 3, 7, 9, 11, 13, value ];
var vectorList:VectorList = new VectorList( vector );

assertThat( vector, hasItem( value ) );

vectorList.removeItemAt( 5 );

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

[Test(expects="RangeError")]
public function removeItemAtShouldThrowRangeErrorIfOutOfRangeIndexSupplied():void {
const value:int = 5;
var vector:Vector.<int> = new <int>[ value ];
var vectorList:VectorList = new VectorList( vector );

vectorList.removeItemAt( 3 );
}

public function AddRemoveItemAtTest() {
}
}
}

0 comments on commit 8766b6a

Please sign in to comment.