summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjavalizard <>2010-09-27 04:52:42 +0000
committerjavalizard <>2010-09-27 04:52:42 +0000
commitac27dc3f3db931cb59e9318a1d69bc683d45b332 (patch)
treebe1d2adb6c4285be1d997f316f353165bc8c8a26
parent546221db4bcf502c3b5744a438d09cbf9df43070 (diff)
#291 Added new unit tests for all edit operations on read-only lists. Added a read-only conditional prior to the item search within three functions that search for an item index in the TList prior to editing.
-rw-r--r--framework/Collections/TList.php47
-rw-r--r--tests/unit/Collections/TListTest.php455
2 files changed, 290 insertions, 212 deletions
diff --git a/framework/Collections/TList.php b/framework/Collections/TList.php
index 8d9fd033..aff21d33 100644
--- a/framework/Collections/TList.php
+++ b/framework/Collections/TList.php
@@ -175,13 +175,18 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess,Countabl
*/
public function remove($item)
{
- if(($index=$this->indexOf($item))>=0)
+ if(!$this->_r)
{
- $this->removeAt($index);
- return $index;
+ if(($index=$this->indexOf($item))>=0)
+ {
+ $this->removeAt($index);
+ return $index;
+ }
+ else
+ throw new TInvalidDataValueException('list_item_inexistent');
}
else
- throw new TInvalidDataValueException('list_item_inexistent');
+ throw new TInvalidOperationException('list_readonly',get_class($this));
}
/**
@@ -253,12 +258,17 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess,Countabl
*/
public function insertBefore($baseitem, $item)
{
- if(($index = $this->indexOf($baseitem)) == -1)
- throw new TInvalidDataValueException('list_item_inexistent');
-
- $this->insertAt($index, $item);
-
- return $index;
+ if(!$this->_r)
+ {
+ if(($index = $this->indexOf($baseitem)) == -1)
+ throw new TInvalidDataValueException('list_item_inexistent');
+
+ $this->insertAt($index, $item);
+
+ return $index;
+ }
+ else
+ throw new TInvalidOperationException('list_readonly',get_class($this));
}
/**
@@ -270,12 +280,17 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess,Countabl
*/
public function insertAfter($baseitem, $item)
{
- if(($index = $this->indexOf($baseitem)) == -1)
- throw new TInvalidDataValueException('list_item_inexistent');
-
- $this->insertAt($index + 1, $item);
-
- return $index + 1;
+ if(!$this->_r)
+ {
+ if(($index = $this->indexOf($baseitem)) == -1)
+ throw new TInvalidDataValueException('list_item_inexistent');
+
+ $this->insertAt($index + 1, $item);
+
+ return $index + 1;
+ }
+ else
+ throw new TInvalidOperationException('list_readonly',get_class($this));
}
/**
diff --git a/tests/unit/Collections/TListTest.php b/tests/unit/Collections/TListTest.php
index e2ba654c..a97273dc 100644
--- a/tests/unit/Collections/TListTest.php
+++ b/tests/unit/Collections/TListTest.php
@@ -2,105 +2,85 @@
require_once dirname(__FILE__).'/../phpunit.php';
class ListItem {
- public $data='data';
+ public $data='data';
}
/**
* @package System.Collections
*/
class TListTest extends PHPUnit_Framework_TestCase {
+
+ protected $list;
+ protected $item1, $item2, $item3, $item4;
+
+ public function setUp() {
+ $this->list=new TList;
+ $this->item1=new ListItem;
+ $this->item2=new ListItem;
+ $this->item3=new ListItem;
+ $this->item4=new ListItem;
+ $this->list->add($this->item1);
+ $this->list->add($this->item2);
+ }
+
+ public function tearDown() {
+ $this->list=null;
+ $this->item1=null;
+ $this->item2=null;
+ $this->item3=null;
+ $this->item4=null;
+ }
+
+ public function testConstruct() {
+ $a=array(1,2,3);
+ $list=new TList($a);
+ $this->assertEquals(3,$list->getCount());
+ $list2=new TList($this->list);
+ $this->assertEquals(2,$list2->getCount());
+ }
- protected $list;
- protected $item1, $item2, $item3, $item4;
-
- public function setUp() {
- $this->list=new TList;
- $this->item1=new ListItem;
- $this->item2=new ListItem;
- $this->item3=new ListItem;
- $this->item4=new ListItem;
- $this->list->add($this->item1);
- $this->list->add($this->item2);
- }
-
- public function tearDown() {
- $this->list=null;
- $this->item1=null;
- $this->item2=null;
- $this->item3=null;
- $this->item4=null;
- }
-
- public function testConstruct() {
- $a=array(1,2,3);
- $list=new TList($a);
- $this->assertEquals(3,$list->getCount());
- $list2=new TList($this->list);
- $this->assertEquals(2,$list2->getCount());
- }
-
public function testGetReadOnly() {
$list = new TList(null, true);
self::assertEquals(true, $list->getReadOnly(), 'List is not read-only');
$list = new TList(null, false);
self::assertEquals(false, $list->getReadOnly(), 'List is read-only');
}
-
- public function testGetCount() {
- $this->assertEquals(2,$this->list->getCount());
- $this->assertEquals(2,$this->list->Count);
- }
-
- public function testAdd() {
- $this->assertEquals(2,$this->list->add(null));
- $this->assertEquals(3,$this->list->add($this->item3));
- $this->assertEquals(4,$this->list->getCount());
- $this->assertEquals(3,$this->list->indexOf($this->item3));
- }
-
- public function testInsertAt() {
- $this->assertNull($this->list->insertAt(0,$this->item3));
- $this->assertEquals(3,$this->list->getCount());
- $this->assertEquals(2,$this->list->indexOf($this->item2));
- $this->assertEquals(0,$this->list->indexOf($this->item3));
- $this->assertEquals(1,$this->list->indexOf($this->item1));
- try {
- $this->list->insertAt(4,$this->item3);
- $this->fail('exception not raised when adding item at an out-of-range index');
- } catch(TInvalidDataValueException $e) {
-
- }
- }
-
- public function testInsertBefore() {
- try {
- $this->list->insertBefore($this->item4,$this->item3);
- $this->fail('exception not raised when adding item before a non-existant base item');
- } catch(TInvalidDataValueException $e) {
- }
- $this->assertEquals(2,$this->list->getCount());
- $this->assertEquals(0,$this->list->insertBefore($this->item1,$this->item3));
- $this->assertEquals(3,$this->list->getCount());
- $this->assertEquals(0,$this->list->indexOf($this->item3));
- $this->assertEquals(1,$this->list->indexOf($this->item1));
- $this->assertEquals(2,$this->list->indexOf($this->item2));
- }
-
- public function testInsertAfter() {
- try {
- $this->list->insertAfter($this->item4,$this->item3);
- $this->fail('exception not raised when adding item after a non-existant base item');
- } catch(TInvalidDataValueException $e) {
- }
- $this->assertEquals(2,$this->list->getCount());
- $this->assertEquals(2,$this->list->insertAfter($this->item2,$this->item3));
- $this->assertEquals(3,$this->list->getCount());
- $this->assertEquals(0,$this->list->indexOf($this->item1));
- $this->assertEquals(1,$this->list->indexOf($this->item2));
- $this->assertEquals(2,$this->list->indexOf($this->item3));
- }
-
- public function testCanNotInsertWhenReadOnly() {
+
+ public function testGetCount() {
+ $this->assertEquals(2,$this->list->getCount());
+ $this->assertEquals(2,$this->list->Count);
+ }
+
+ public function testAdd() {
+ $this->assertEquals(2,$this->list->add(null));
+ $this->assertEquals(3,$this->list->add($this->item3));
+ $this->assertEquals(4,$this->list->getCount());
+ $this->assertEquals(3,$this->list->indexOf($this->item3));
+ }
+
+ public function testCanNotAddWhenReadOnly() {
+ $list = new TList(array(), true);
+ try {
+ $list->add(1);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testInsertAt() {
+ $this->assertNull($this->list->insertAt(0,$this->item3));
+ $this->assertEquals(3,$this->list->getCount());
+ $this->assertEquals(2,$this->list->indexOf($this->item2));
+ $this->assertEquals(0,$this->list->indexOf($this->item3));
+ $this->assertEquals(1,$this->list->indexOf($this->item1));
+ try {
+ $this->list->insertAt(4,$this->item3);
+ $this->fail('exception not raised when adding item at an out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+ }
+ }
+
+ public function testCanNotInsertAtWhenReadOnly() {
$list = new TList(array(), true);
try {
$list->insertAt(1, 2);
@@ -113,129 +93,212 @@ class TListTest extends PHPUnit_Framework_TestCase {
} catch(TInvalidOperationException $e) {
}
}
-
- public function testRemove() {
- $this->assertEquals(0,$this->list->remove($this->item1));
- $this->assertEquals(1,$this->list->getCount());
- $this->assertEquals(-1,$this->list->indexOf($this->item1));
- $this->assertEquals(0,$this->list->indexOf($this->item2));
- try {
- $this->list->remove($this->item1);
- $this->fail('exception not raised when removing nonexisting item');
- } catch(Exception $e) {
-
- }
- }
-
- public function testRemoveAt() {
- $this->list->add($this->item3);
- $this->assertEquals($this->item2, $this->list->removeAt(1));
- $this->assertEquals(-1,$this->list->indexOf($this->item2));
- $this->assertEquals(1,$this->list->indexOf($this->item3));
- $this->assertEquals(0,$this->list->indexOf($this->item1));
- try {
- $this->list->removeAt(2);
- $this->fail('exception not raised when removing item with invalid index');
- } catch(TInvalidDataValueException $e) {
-
- }
- }
-
+
+ public function testInsertBefore() {
+ try {
+ $this->list->insertBefore($this->item4,$this->item3);
+ $this->fail('exception not raised when adding item before a non-existant base item');
+ } catch(TInvalidDataValueException $e) {
+ }
+ $this->assertEquals(2,$this->list->getCount());
+ $this->assertEquals(0,$this->list->insertBefore($this->item1,$this->item3));
+ $this->assertEquals(3,$this->list->getCount());
+ $this->assertEquals(0,$this->list->indexOf($this->item3));
+ $this->assertEquals(1,$this->list->indexOf($this->item1));
+ $this->assertEquals(2,$this->list->indexOf($this->item2));
+ }
+
+ public function testCanNotInsertBeforeWhenReadOnly() {
+ $list = new TList(array(5), true);
+ try {
+ $list->insertBefore(5, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ try {
+ $list->insertBefore(8, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testInsertAfter() {
+ try {
+ $this->list->insertAfter($this->item4,$this->item3);
+ $this->fail('exception not raised when adding item after a non-existant base item');
+ } catch(TInvalidDataValueException $e) {
+ }
+ $this->assertEquals(2,$this->list->getCount());
+ $this->assertEquals(2,$this->list->insertAfter($this->item2,$this->item3));
+ $this->assertEquals(3,$this->list->getCount());
+ $this->assertEquals(0,$this->list->indexOf($this->item1));
+ $this->assertEquals(1,$this->list->indexOf($this->item2));
+ $this->assertEquals(2,$this->list->indexOf($this->item3));
+ }
+
+ public function testCanNotInsertAfterWhenReadOnly() {
+ $list = new TList(array(5), true);
+ try {
+ $list->insertAfter(5, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ try {
+ $list->insertAfter(8, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testRemove() {
+ $this->assertEquals(0,$this->list->remove($this->item1));
+ $this->assertEquals(1,$this->list->getCount());
+ $this->assertEquals(-1,$this->list->indexOf($this->item1));
+ $this->assertEquals(0,$this->list->indexOf($this->item2));
+ try {
+ $this->list->remove($this->item1);
+ $this->fail('exception not raised when removing nonexisting item');
+ } catch(Exception $e) {
+ }
+ }
+
public function testCanNotRemoveWhenReadOnly() {
$list = new TList(array(1, 2, 3), true);
try {
+ $list->remove(2);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+
+ $list = new TList(array(1, 2, 3), true);
+ try {
+ $list->remove(10);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testRemoveAt() {
+ $this->list->add($this->item3);
+ $this->assertEquals($this->item2, $this->list->removeAt(1));
+ $this->assertEquals(-1,$this->list->indexOf($this->item2));
+ $this->assertEquals(1,$this->list->indexOf($this->item3));
+ $this->assertEquals(0,$this->list->indexOf($this->item1));
+ try {
+ $this->list->removeAt(2);
+ $this->fail('exception not raised when removing item with invalid index');
+ } catch(TInvalidDataValueException $e) {
+ }
+ }
+
+ public function testCanNotRemoveAtWhenReadOnly() {
+ $list = new TList(array(1, 2, 3), true);
+ try {
$list->removeAt(2);
+ self::fail('An expected TInvalidOperationException was not raised');
} catch(TInvalidOperationException $e) {
- return;
+ }
+
+ $list = new TList(array(1, 2, 3), true);
+ try {
+ $list->removeAt(10);
+ self::fail('An expected TInvalidOperationException was not raised');
+ } catch(TInvalidOperationException $e) {
+ }
+ }
+
+ public function testClear() {
+ $this->list->clear();
+ $this->assertEquals(0,$this->list->getCount());
+ $this->assertEquals(-1,$this->list->indexOf($this->item1));
+ $this->assertEquals(-1,$this->list->indexOf($this->item2));
+ }
+
+ public function testCanNotClearWhenReadOnly() {
+ $list = new TList(array(1, 2, 3), true);
+ try {
+ $list->clear();
+ } catch(TInvalidOperationException $e) {
+ return;
}
self::fail('An expected TInvalidOperationException was not raised');
}
-
- public function testClear() {
- $this->list->clear();
- $this->assertEquals(0,$this->list->getCount());
- $this->assertEquals(-1,$this->list->indexOf($this->item1));
- $this->assertEquals(-1,$this->list->indexOf($this->item2));
- }
-
- public function testContains() {
- $this->assertTrue($this->list->contains($this->item1));
- $this->assertTrue($this->list->contains($this->item2));
- $this->assertFalse($this->list->contains($this->item3));
- }
-
- public function testIndexOf() {
- $this->assertEquals(0,$this->list->indexOf($this->item1));
- $this->assertEquals(1,$this->list->indexOf($this->item2));
- $this->assertEquals(-1,$this->list->indexOf($this->item3));
- }
-
- public function testCopyFrom() {
- $array=array($this->item3,$this->item1);
- $this->list->copyFrom($array);
- $this->assertTrue(count($array)==2 && $this->list[0]===$this->item3 && $this->list[1]===$this->item1);
- try {
- $this->list->copyFrom($this);
- $this->fail('exception not raised when copying from non-traversable object');
- } catch(TInvalidDataTypeException $e) {
-
- }
- }
-
- public function testMergeWith() {
- $array=array($this->item3,$this->item1);
- $this->list->mergeWith($array);
- $this->assertTrue($this->list->getCount()==4 && $this->list[0]===$this->item1 && $this->list[3]===$this->item1);
- try {
- $this->list->mergeWith($this);
- $this->fail('exception not raised when copying from non-traversable object');
- } catch(TInvalidDataTypeException $e) {
-
- }
- }
-
- public function testToArray() {
- $array=$this->list->toArray();
- $this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
- }
-
- public function testArrayRead() {
- $this->assertTrue($this->list[0]===$this->item1);
- $this->assertTrue($this->list[1]===$this->item2);
- try {
- $a=$this->list[2];
- $this->fail('exception not raised when accessing item with out-of-range index');
- } catch(TInvalidDataValueException $e) {
-
- }
- }
-
- public function testGetIterator() {
- $n=0;
- $found=0;
- foreach($this->list as $index=>$item) {
- foreach($this->list as $a=>$b); // test of iterator
- $n++;
- if($index===0 && $item===$this->item1)
- $found++;
- if($index===1 && $item===$this->item2)
- $found++;
- }
- $this->assertTrue($n==2 && $found==2);
- }
-
- public function testArrayMisc() {
- $this->assertEquals($this->list->Count,count($this->list));
- $this->assertTrue(isset($this->list[1]));
- $this->assertFalse(isset($this->list[2]));
- }
-
+
+ public function testContains() {
+ $this->assertTrue($this->list->contains($this->item1));
+ $this->assertTrue($this->list->contains($this->item2));
+ $this->assertFalse($this->list->contains($this->item3));
+ }
+
+ public function testIndexOf() {
+ $this->assertEquals(0,$this->list->indexOf($this->item1));
+ $this->assertEquals(1,$this->list->indexOf($this->item2));
+ $this->assertEquals(-1,$this->list->indexOf($this->item3));
+ }
+
+ public function testCopyFrom() {
+ $array=array($this->item3,$this->item1);
+ $this->list->copyFrom($array);
+ $this->assertTrue(count($array)==2 && $this->list[0]===$this->item3 && $this->list[1]===$this->item1);
+ try {
+ $this->list->copyFrom($this);
+ $this->fail('exception not raised when copying from non-traversable object');
+ } catch(TInvalidDataTypeException $e) {
+ }
+ }
+
+ public function testMergeWith() {
+ $array=array($this->item3,$this->item1);
+ $this->list->mergeWith($array);
+ $this->assertTrue($this->list->getCount()==4 && $this->list[0]===$this->item1 && $this->list[3]===$this->item1);
+ try {
+ $this->list->mergeWith($this);
+ $this->fail('exception not raised when copying from non-traversable object');
+ } catch(TInvalidDataTypeException $e) {
+ }
+ }
+
+ public function testToArray() {
+ $array=$this->list->toArray();
+ $this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
+ }
+
+ public function testArrayRead() {
+ $this->assertTrue($this->list[0]===$this->item1);
+ $this->assertTrue($this->list[1]===$this->item2);
+ try {
+ $a=$this->list[2];
+ $this->fail('exception not raised when accessing item with out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+ }
+ }
+
+ public function testGetIterator() {
+ $n=0;
+ $found=0;
+ foreach($this->list as $index=>$item) {
+ foreach($this->list as $a=>$b); // test of iterator
+ $n++;
+ if($index===0 && $item===$this->item1)
+ $found++;
+ if($index===1 && $item===$this->item2)
+ $found++;
+ }
+ $this->assertTrue($n==2 && $found==2);
+ }
+
+ public function testArrayMisc() {
+ $this->assertEquals($this->list->Count,count($this->list));
+ $this->assertTrue(isset($this->list[1]));
+ $this->assertFalse(isset($this->list[2]));
+ }
+
public function testOffsetSetAdd() {
$list = new TList(array(1, 2, 3));
$list->offsetSet(null, 4);
self::assertEquals(array(1, 2, 3, 4), $list->toArray());
}
-
+
public function testOffsetSetReplace() {
$list = new TList(array(1, 2, 3));
$list->offsetSet(1, 4);