Commit 06f6053b by Qiang Xue

Finished dictionary and vector testing.

parent 71063374
......@@ -193,9 +193,9 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/
public function copyFrom($data)
{
if (is_array($data) || $data instanceof Traversable)
if (is_array($data) || $data instanceof \Traversable)
{
if (($this->_d !== array()) {
if ($this->_d !== array()) {
$this->clear();
}
if ($data instanceof self) {
......@@ -227,32 +227,33 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*
* @throws CException If data is neither an array nor an iterator.
*/
public function mergeWith($data,$recursive=true)
{
if (is_array($data) || $data instanceof Traversable)
{
if ($data instanceof Dictionary)
$data=$data->_d;
if ($recursive)
{
if ($data instanceof Traversable)
public function mergeWith($data, $recursive=true)
{
if (is_array($data) || $data instanceof \Traversable) {
if ($data instanceof self) {
$data = $data->_d;
}
if ($recursive) {
if ($data instanceof \Traversable) {
$d=array();
foreach($data as $key=>$value)
$d[$key]=$value;
$this->_d=self::mergeArray($this->_d,$d);
foreach($data as $key => $value) {
$d[$key] = $value;
}
else
$this->_d=self::mergeArray($this->_d,$data);
$this->_d = self::mergeArray($this->_d, $d);
}
else
{
foreach($data as $key=>$value)
$this->add($key,$value);
else {
$this->_d = self::mergeArray($this->_d, $data);
}
}
else if ($data!==null)
throw new CException(Yii::t('yii','Dictionary data must be an array or an object implementing Traversable.'));
else {
foreach($data as $key => $value) {
$this->add($key, $value);
}
}
}
else {
throw new Exception('Dictionary data must be an array or an object implementing Traversable.');
}
}
/**
......@@ -321,16 +322,17 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
* @return array the merged array (the original arrays are not changed.)
* @see mergeWith
*/
public static function mergeArray($a,$b)
{
foreach($b as $k=>$v)
public static function mergeArray($a, $b)
{
if(is_integer($k))
isset($a[$k]) ? $a[]=$v : $a[$k]=$v;
else if(is_array($v) && isset($a[$k]) && is_array($a[$k]))
$a[$k]=self::mergeArray($a[$k],$v);
foreach($b as $k=>$v) {
if(is_integer($k)) {
isset($a[$k]) ? $a[] = $v : $a[$k] = $v;
}
elseif(is_array($v) && isset($a[$k]) && is_array($a[$k])) {
$a[$k] = self::mergeArray($a[$k], $v);
}
else
$a[$k]=$v;
$a[$k] = $v;
}
return $a;
}
......
......@@ -259,7 +259,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/
public function copyFrom($data)
{
if (is_array($data) || ($data instanceof Traversable)) {
if (is_array($data) || $data instanceof \Traversable) {
if ($this->_c > 0) {
$this->clear();
}
......@@ -283,7 +283,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/
public function mergeWith($data)
{
if (is_array($data) || ($data instanceof Traversable)) {
if (is_array($data) || ($data instanceof \Traversable)) {
if ($data instanceof Vector) {
$data = $data->_d;
}
......
......@@ -49,7 +49,7 @@ class ComponentTest extends \yii\test\TestCase
public function testGetProperty()
{
$this->assertTrue('default'===$this->component->Text);
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$value2=$this->component->Caption;
}
......@@ -59,7 +59,7 @@ class ComponentTest extends \yii\test\TestCase
$this->component->Text=$value;
$text=$this->component->Text;
$this->assertTrue($value===$this->component->Text);
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$this->component->NewMember=$value;
}
......@@ -97,7 +97,7 @@ class ComponentTest extends \yii\test\TestCase
$this->assertEquals($list->getCount(),0);
$this->component->attachEventHandler('OnMyEvent','foo');
$this->assertEquals($list->getCount(),1);
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$list=$this->component->getEventHandlers('YourEvent');
}
......@@ -105,7 +105,7 @@ class ComponentTest extends \yii\test\TestCase
{
$this->component->attachEventHandler('OnMyEvent','foo');
$this->assertTrue($this->component->getEventHandlers('OnMyEvent')->getCount()===1);
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$this->component->attachEventHandler('YourEvent','foo');
}
......@@ -134,8 +134,8 @@ class ComponentTest extends \yii\test\TestCase
$this->component->raiseEvent('OnMyEvent',new \yii\base\Event($this));
$this->assertTrue($this->component->eventHandled);
//$this->setExpectedException('CException');
//$this->component->raiseEvent('OnUnknown',new CEvent($this));
$this->setExpectedException('yii\base\Exception');
$this->component->raiseEvent('OnUnknown',new \yii\base\Event($this));
}
public function testEventAccessor()
......@@ -165,14 +165,14 @@ class ComponentTest extends \yii\test\TestCase
public function testInvalidHandler1()
{
$this->component->onMyEvent=array(1,2,3);
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$this->component->onMyEvent();
}
public function testInvalidHandler2()
{
$this->component->onMyEvent=array($this->component,'nullHandler');
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$this->component->onMyEvent();
}
public function testDetachBehavior() {
......@@ -186,7 +186,7 @@ class ComponentTest extends \yii\test\TestCase
$behavior = new NewBehavior;
$component->attachBehavior('a',$behavior);
$component->detachBehaviors();
$this->setExpectedException('CException');
$this->setExpectedException('yii\base\Exception');
$component->test();
}
public function testEnablingBehavior() {
......
<?php
class MapItem
{
public $data='data';
}
class DictionaryTest extends \yii\test\TestCase
{
protected $dictionary;
protected $item1,$item2,$item3;
public function setUp()
{
$this->dictionary=new \yii\base\Dictionary;
$this->item1=new MapItem;
$this->item2=new MapItem;
$this->item3=new MapItem;
$this->dictionary->add('key1',$this->item1);
$this->dictionary->add('key2',$this->item2);
}
public function tearDown()
{
$this->dictionary=null;
$this->item1=null;
$this->item2=null;
$this->item3=null;
}
public function testConstruct()
{
$a=array(1,2,'key3'=>3);
$dictionary=new \yii\base\Dictionary($a);
$this->assertEquals(3,$dictionary->getCount());
$dictionary2=new \yii\base\Dictionary($this->dictionary);
$this->assertEquals(2,$dictionary2->getCount());
}
public function testReadOnly()
{
$dictionary = new \yii\base\Dictionary(array(), true);
self::assertEquals(true, $dictionary->readOnly, 'List is not read-only');
}
public function testGetCount()
{
$this->assertEquals(2,$this->dictionary->getCount());
}
public function testGetKeys()
{
$keys=$this->dictionary->getKeys();
$this->assertEquals(2,count($keys));
$this->assertEquals('key1',$keys[0]);
$this->assertEquals('key2',$keys[1]);
}
public function testAdd()
{
$this->dictionary->add('key3',$this->item3);
$this->assertEquals(3,$this->dictionary->getCount());
$this->assertTrue($this->dictionary->contains('key3'));
}
public function testCanNotAddWhenReadOnly()
{
$dictionary = new \yii\base\Dictionary(array(), true);
$this->setExpectedException('yii\base\Exception');
$dictionary->add('key', 'value');
}
public function testRemove()
{
$this->dictionary->remove('key1');
$this->assertEquals(1,$this->dictionary->getCount());
$this->assertTrue(!$this->dictionary->contains('key1'));
$this->assertTrue($this->dictionary->remove('unknown key')===null);
}
public function testCanNotRemoveWhenReadOnly()
{
$dictionary = new \yii\base\Dictionary(array('key' => 'value'), true);
$this->setExpectedException('yii\base\Exception');
$dictionary->remove('key');
}
public function testClear()
{
$this->dictionary->clear();
$this->assertEquals(0,$this->dictionary->getCount());
$this->assertTrue(!$this->dictionary->contains('key1') && !$this->dictionary->contains('key2'));
}
public function testContains()
{
$this->assertTrue($this->dictionary->contains('key1'));
$this->assertTrue($this->dictionary->contains('key2'));
$this->assertFalse($this->dictionary->contains('key3'));
}
public function testCopyFrom()
{
$array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->dictionary->copyFrom($array);
$this->assertEquals(2, $this->dictionary->getCount());
$this->assertEquals($this->item3, $this->dictionary['key3']);
$this->assertEquals($this->item1, $this->dictionary['key4']);
$this->setExpectedException('yii\base\Exception');
$this->dictionary->copyFrom($this);
}
public function testMergeWith()
{
$a=array('a'=>'v1','v2',array('2'),'c'=>array('3','c'=>'a'));
$b=array('v22','a'=>'v11',array('2'),'c'=>array('c'=>'3','a'));
$c=array('a'=>'v11','v2',array('2'),'c'=>array('3','c'=>'3','a'),'v22',array('2'));
$dictionary=new \yii\base\Dictionary($a);
$dictionary2=new \yii\base\Dictionary($b);
$dictionary->mergeWith($dictionary2);
$this->assertTrue($dictionary->toArray()===$c);
$array=array('key2'=>$this->item1,'key3'=>$this->item3);
$this->dictionary->mergeWith($array,false);
$this->assertEquals(3,$this->dictionary->getCount());
$this->assertEquals($this->item1,$this->dictionary['key2']);
$this->assertEquals($this->item3,$this->dictionary['key3']);
$this->setExpectedException('yii\base\Exception');
$this->dictionary->mergeWith($this,false);
}
public function testRecursiveMergeWithTraversable(){
$dictionary = new \yii\base\Dictionary();
$obj = new ArrayObject(array(
'k1' => $this->item1,
'k2' => $this->item2,
'k3' => new ArrayObject(array(
'k4' => $this->item3,
))
));
$dictionary->mergeWith($obj,true);
$this->assertEquals(3, $dictionary->getCount());
$this->assertEquals($this->item1, $dictionary['k1']);
$this->assertEquals($this->item2, $dictionary['k2']);
$this->assertEquals($this->item3, $dictionary['k3']['k4']);
}
public function testArrayRead()
{
$this->assertEquals($this->item1,$this->dictionary['key1']);
$this->assertEquals($this->item2,$this->dictionary['key2']);
$this->assertEquals(null,$this->dictionary['key3']);
}
public function testArrayWrite()
{
$this->dictionary['key3']=$this->item3;
$this->assertEquals(3,$this->dictionary->getCount());
$this->assertEquals($this->item3,$this->dictionary['key3']);
$this->dictionary['key1']=$this->item3;
$this->assertEquals(3,$this->dictionary->getCount());
$this->assertEquals($this->item3,$this->dictionary['key1']);
unset($this->dictionary['key2']);
$this->assertEquals(2,$this->dictionary->getCount());
$this->assertTrue(!$this->dictionary->contains('key2'));
unset($this->dictionary['unknown key']);
}
public function testArrayForeach()
{
$n=0;
$found=0;
foreach($this->dictionary as $index=>$item)
{
$n++;
if($index==='key1' && $item===$this->item1)
$found++;
if($index==='key2' && $item===$this->item2)
$found++;
}
$this->assertTrue($n==2 && $found==2);
}
public function testArrayMisc()
{
$this->assertEquals($this->dictionary->Count,count($this->dictionary));
$this->assertTrue(isset($this->dictionary['key1']));
$this->assertFalse(isset($this->dictionary['unknown key']));
}
public function testToArray()
{
$dictionary = new \yii\base\Dictionary(array('key' => 'value'));
$this->assertEquals(array('key' => 'value'), $dictionary->toArray());
}
public function testIteratorCurrent()
{
$dictionary = new \yii\base\Dictionary(array('key1' => 'value1', 'key2' => 'value2'));
$val = $dictionary->getIterator()->current();
$this->assertEquals('value1', $val);
}
}
<?php
class ListItem
{
public $data='data';
}
class VectorTest extends \yii\test\TestCase
{
protected $vector;
protected $item1, $item2, $item3;
public function setUp()
{
$this->vector=new \yii\base\Vector;
$this->item1=new ListItem;
$this->item2=new ListItem;
$this->item3=new ListItem;
$this->vector->add($this->item1);
$this->vector->add($this->item2);
}
public function tearDown()
{
$this->vector=null;
$this->item1=null;
$this->item2=null;
$this->item3=null;
}
public function testConstruct()
{
$a=array(1,2,3);
$vector=new \yii\base\Vector($a);
$this->assertEquals(3,$vector->getCount());
$vector2=new \yii\base\Vector($this->vector);
$this->assertEquals(2,$vector2->getCount());
}
public function testReadOnly()
{
$vector = new \yii\base\Vector(array(), true);
$this->assertEquals(true, $vector->readOnly, 'List is not read-only');
$vector = new \yii\base\Vector(array(), false);
$this->assertEquals(false, $vector->readOnly, 'List is read-only');
}
public function testGetCount()
{
$this->assertEquals(2,$this->vector->getCount());
$this->assertEquals(2,$this->vector->Count);
}
public function testAdd()
{
$this->vector->add(null);
$this->vector->add($this->item3);
$this->assertEquals(4,$this->vector->getCount());
$this->assertEquals(3,$this->vector->indexOf($this->item3));
}
public function testInsertAt()
{
$this->vector->insertAt(0,$this->item3);
$this->assertEquals(3,$this->vector->getCount());
$this->assertEquals(2,$this->vector->indexOf($this->item2));
$this->assertEquals(0,$this->vector->indexOf($this->item3));
$this->assertEquals(1,$this->vector->indexOf($this->item1));
$this->setExpectedException('yii\base\Exception');
$this->vector->insertAt(4,$this->item3);
}
public function testCanNotInsertWhenReadOnly()
{
$vector = new \yii\base\Vector(array(), true);
$this->setExpectedException('yii\base\Exception');
$vector->insertAt(1, 2);
}
public function testRemove()
{
$this->vector->remove($this->item1);
$this->assertEquals(1,$this->vector->getCount());
$this->assertEquals(-1,$this->vector->indexOf($this->item1));
$this->assertEquals(0,$this->vector->indexOf($this->item2));
$this->assertEquals(false,$this->vector->remove($this->item1));
}
public function testRemoveAt()
{
$this->vector->add($this->item3);
$this->vector->removeAt(1);
$this->assertEquals(-1,$this->vector->indexOf($this->item2));
$this->assertEquals(1,$this->vector->indexOf($this->item3));
$this->assertEquals(0,$this->vector->indexOf($this->item1));
$this->setExpectedException('yii\base\Exception');
$this->vector->removeAt(2);
}
public function testCanNotRemoveWhenReadOnly()
{
$vector = new \yii\base\Vector(array(1, 2, 3), true);
$this->setExpectedException('yii\base\Exception');
$vector->removeAt(2);
}
public function testClear()
{
$this->vector->clear();
$this->assertEquals(0,$this->vector->getCount());
$this->assertEquals(-1,$this->vector->indexOf($this->item1));
$this->assertEquals(-1,$this->vector->indexOf($this->item2));
}
public function testContains()
{
$this->assertTrue($this->vector->contains($this->item1));
$this->assertTrue($this->vector->contains($this->item2));
$this->assertFalse($this->vector->contains($this->item3));
}
public function testIndexOf()
{
$this->assertEquals(0,$this->vector->indexOf($this->item1));
$this->assertEquals(1,$this->vector->indexOf($this->item2));
$this->assertEquals(-1,$this->vector->indexOf($this->item3));
}
public function testCopyFrom()
{
$array=array($this->item3,$this->item1);
$this->vector->copyFrom($array);
$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
$this->setExpectedException('yii\base\Exception');
$this->vector->copyFrom($this);
}
public function testMergeWith()
{
$array=array($this->item3,$this->item1);
$this->vector->mergeWith($array);
$this->assertTrue($this->vector->getCount()==4 && $this->vector[0]===$this->item1 && $this->vector[3]===$this->item1);
$this->setExpectedException('yii\base\Exception');
$this->vector->mergeWith($this);
}
public function testToArray()
{
$array=$this->vector->toArray();
$this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
}
public function testArrayRead()
{
$this->assertTrue($this->vector[0]===$this->item1);
$this->assertTrue($this->vector[1]===$this->item2);
$this->setExpectedException('yii\base\Exception');
$a=$this->vector[2];
}
public function testGetIterator()
{
$n=0;
$found=0;
foreach($this->vector as $index=>$item)
{
foreach($this->vector 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->vector->Count,count($this->vector));
$this->assertTrue(isset($this->vector[1]));
$this->assertFalse(isset($this->vector[2]));
}
public function testOffsetSetAdd()
{
$vector = new \yii\base\Vector(array(1, 2, 3));
$vector->offsetSet(null, 4);
$this->assertEquals(array(1, 2, 3, 4), $vector->toArray());
}
public function testOffsetSetReplace()
{
$vector = new \yii\base\Vector(array(1, 2, 3));
$vector->offsetSet(1, 4);
$this->assertEquals(array(1, 4, 3), $vector->toArray());
}
public function testOffsetUnset()
{
$vector = new \yii\base\Vector(array(1, 2, 3));
$vector->offsetUnset(1);
$this->assertEquals(array(1, 3), $vector->toArray());
}
public function testIteratorCurrent()
{
$vector = new \yii\base\Vector(array('value1', 'value2'));
$val = $vector->getIterator()->current();
$this->assertEquals('value1', $val);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment