diff options
| -rw-r--r-- | .gitattributes | 2 | ||||
| -rw-r--r-- | tests/unit/Collections/TMapTest.php | 239 | ||||
| -rw-r--r-- | tests/unit/I18N/TNumberFormatTest.php | 103 | 
3 files changed, 344 insertions, 0 deletions
| diff --git a/.gitattributes b/.gitattributes index 14233256..28e2ecda 100644 --- a/.gitattributes +++ b/.gitattributes @@ -906,6 +906,8 @@ tests/UnitTests/simpletest/user_agent.php -text  tests/UnitTests/simpletest/web_tester.php -text  tests/UnitTests/simpletest/xml.php -text  tests/unit/Collections/TListTest.php -text +tests/unit/Collections/TMapTest.php -text +tests/unit/I18N/TNumberFormatTest.php -text  tests/unit/TComponentTest.php -text  tests/unit/phpunit2.php -text  tools/.htaccess -text diff --git a/tests/unit/Collections/TMapTest.php b/tests/unit/Collections/TMapTest.php new file mode 100644 index 00000000..a6f82570 --- /dev/null +++ b/tests/unit/Collections/TMapTest.php @@ -0,0 +1,239 @@ +<?php +require_once dirname(__FILE__).'/../phpunit2.php'; + +class MapItem { +  public $data='data'; +} + +class NewMap extends TMap +{ +	private $_canAddItem=true; +	private $_canRemoveItem=true; +	private $_itemAdded=false; +	private $_itemRemoved=false; + +	protected function addedItem($key,$value) +	{ +		$this->_itemAdded=true; +	} + +	protected function removedItem($key,$value) +	{ +		$this->_itemRemoved=true; +	} + +	protected function canAddItem($key,$value) +	{ +		return $this->_canAddItem; +	} + +	protected function canRemoveItem($key,$value) +	{ +		return $this->_canRemoveItem; +	} + +	public function setCanAddItem($value) +	{ +		$this->_canAddItem=$value; +	} + +	public function setCanRemoveItem($value) +	{ +		$this->_canRemoveItem=$value; +	} + +	public function isItemAdded() +	{ +		return $this->_itemAdded; +	} + +	public function isItemRemoved() +	{ +		return $this->_itemRemoved; +	} +} + +/** + * @package System.Collections + */ +class TMapTest extends PHPUnit2_Framework_TestCase { +  protected $map; +  protected $item1,$item2,$item3; +   +  public function setUp() { +    $this->map=new TMap; +    $this->item1=new MapItem; +    $this->item2=new MapItem; +    $this->item3=new MapItem; +    $this->map->add('key1',$this->item1); +    $this->map->add('key2',$this->item2); +  } + +  public function tearDown() { +    $this->map=null; +    $this->item1=null; +    $this->item2=null; +    $this->item3=null; +  } +   +  public function testConstruct() { +    $a=array(1,2,'key3'=>3); +    $map=new TMap($a); +    $this->assertEquals(3,$map->getCount()); +    $map2=new TMap($this->map); +    $this->assertEquals(2,$map2->getCount()); +  } + +  public function testGetCount() { +    $this->assertEquals(2,$this->map->getCount()); +  } +   +  public function testGetKeys() { +    $keys=$this->map->getKeys(); +    $this->assertTrue(count($keys)===2 && $keys[0]==='key1' && $keys[1]==='key2'); +  } + +	public function testAdd() +	{ +		$this->map->add('key3',$this->item3); +		$this->assertTrue($this->map->getCount()==3 && $this->map->contains('key3')); +	} + +	public function testRemove() +	{ +		$this->map->remove('key1'); +		$this->assertTrue($this->map->getCount()==1 && !$this->map->contains('key1')); +		$this->assertTrue($this->map->remove('unknown key')===null); +	} + +	public function testClear() +	{ +		$this->map->clear(); +		$this->assertTrue($this->map->getCount()==0 && !$this->map->contains('key1') && !$this->map->contains('key2')); +	} + +	public function testContains() +	{ +		$this->assertTrue($this->map->contains('key1')); +		$this->assertTrue($this->map->contains('key2')); +		$this->assertFalse($this->map->contains('key3')); +	} + +	public function testCopyFrom() +	{ +		$array=array('key3'=>$this->item3,'key4'=>$this->item1); +		$this->map->copyFrom($array); +		$this->assertTrue($this->map->getCount()==2 && $this->map['key3']===$this->item3 && $this->map['key4']===$this->item1); +		try +		{ +			$this->map->copyFrom($this); +			$this->fail('no exception raised when copying a non-traversable object'); +		} +		catch(TInvalidDataTypeException $e) +		{ +			 +		} +	} + +	public function testMergeWith() +	{ +		$array=array('key2'=>$this->item1,'key3'=>$this->item3); +		$this->map->mergeWith($array); +		$this->assertTrue($this->map->getCount()==3 && $this->map['key2']===$this->item1 && $this->map['key3']===$this->item3); +		try +		{ +			$this->map->mergeWith($this); +			$this->fail('no exception raised when copying a non-traversable object'); +		} +		catch(TInvalidDataTypeException $e) +		{ +			 +		} +	} + +	public function testArrayRead() +	{ +		$this->assertTrue($this->map['key1']===$this->item1); +		$this->assertTrue($this->map['key2']===$this->item2); +		$this->assertEquals(null,$this->map['key3']); +	} + +	public function testArrayWrite() +	{ +		$this->map['key3']=$this->item3; +		$this->assertTrue($this->map['key3']===$this->item3 && $this->map->getCount()===3); +		$this->map['key1']=$this->item3; +		$this->assertTrue($this->map['key1']===$this->item3 && $this->map->getCount()===3); +		unset($this->map['key2']); +		$this->assertTrue($this->map->getCount()===2 && !$this->map->contains('key2')); +		try +		{ +			unset($this->map['unknown key']); +			 +		} +		catch(Exception $e) +		{ +			$this->fail('exception raised when unsetting element with unknown key'); +		} +	} + +	public function testArrayForeach() +	{ +		$n=0; +		$found=0; +		foreach($this->map 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(1,count($this->map)); +		$this->assertTrue(isset($this->map['key1'])); +		$this->assertFalse(isset($this->map['unknown key'])); +	} + +	public function testDerivedClasses() +	{ +		$newMap=new NewMap; +		$this->assertFalse($newMap->isItemAdded()); +		$newMap->add('key','value'); +		$this->assertTrue($newMap->isItemAdded()); +		$newMap->add('key2','value2'); + +		$newMap->setCanAddItem(false); +		try +		{ +			$newMap->add('new key','new value'); +			$this->fail('no exception raised when adding an item that is disallowed'); +		} +		catch(TInvalidOperationException $e) +		{ +			$this->assertEquals(2,$newMap->getCount()); +			 +		} + +		$this->assertFalse($newMap->isItemRemoved()); +		$newMap->remove('key'); +		$this->assertTrue($newMap->isItemRemoved()); + +		$newMap->setCanRemoveItem(false); +		try +		{ +			$newMap->remove('key2'); +			$this->fail('no exception raised when removing an item that is disallowed'); +		} +		catch(TInvalidOperationException $e) +		{ +			$this->assertEquals(1,$newMap->getCount()); +		} +	} +} + +?>
\ No newline at end of file diff --git a/tests/unit/I18N/TNumberFormatTest.php b/tests/unit/I18N/TNumberFormatTest.php new file mode 100644 index 00000000..18e5f3ff --- /dev/null +++ b/tests/unit/I18N/TNumberFormatTest.php @@ -0,0 +1,103 @@ +<?php +require_once dirname(__FILE__).'/../phpunit2.php'; + +//NOTE: This page require UTF-8 aware editors +Prado::using('System.I18N.core.NumberFormat'); + +/** + * @package System.I18N + */ +class TNumberFormatTest extends PHPUnit2_Framework_TestCase { +  function testDefaultFormats() { +    $formatter = new NumberFormat(); +    $number = '123456789.125156'; +    $wanted = '123,456,789.125156'; +    $this->assertEquals($wanted, $formatter->format($number)); +     +    //currency +    $wanted = 'US$123,456,789.13'; +    $this->assertEquals($wanted, $formatter->format($number,'c')); +  } + +  function testLocalizedCurrencyFormats() { +    $fr = new NumberFormat('fr'); +    $de = new NumberFormat('de'); +    $ja = new NumberFormat('ja_JP'); +     +    $number = '123456789.125156'; +     +    //french +    $wanted = '123 456 789,13 F'; +    $this->assertEquals($wanted, $fr->format($number,'c','FRF')); +     +    //german +    $wanted = 'DES 123.456.789,13'; +    $this->assertEquals($wanted, $de->format($number,'c','DES')); +     +    //japanese +    $wanted = '¥123,456,789'; +    $this->assertEquals($wanted, $ja->format($number,'c','JPY')); +     +    //custom/unkown currency +    $wanted = 'DLL123,456,789'; +    $this->assertEquals($wanted, $ja->format($number,'c','DLL')); +  } + +  function testCustomFormat() { +    $formatter = new NumberFormat(); +    $number = '123456789.125156'; +     +    //primay and secondary grouping test +    $pattern = '#,###,##.###'; +    $wanted = '1,234,567,89.125156'; +    $this->assertEquals($wanted, $formatter->format($number, $pattern)); +     +    //4 digits grouping test +    $pattern = '#,####.###'; +    $wanted = '1,2345,6789.125156'; +    $this->assertEquals($wanted, $formatter->format($number, $pattern)); +     +    //custom percentage +    $pattern = '#,###.00%'; +    $wanted = '123,456,789.13%'; +    $this->assertEquals($wanted, $formatter->format($number, $pattern)); +  } +   +  function testPercentageFormat() { +    $formatter = new NumberFormat(); +    $number = '0.125156'; +    $wanted = '12%'; +    $this->assertEquals($wanted, $formatter->format($number, 'p')); +  } +   +  function testQuotes() { +    $formatter = new NumberFormat(); +    $number = '123456789.125156'; +     +    $pattern = "# o'clock"; +    $wanted = "123456789 o'clock"; +    $this->assertEquals($wanted, $formatter->format($number, $pattern)); +     +  } +   +  function testPadding() { +    $formatter = new NumberFormat(); +    $number = '5'; +     +    $pattern = '0000'; +    $wanted = '0005'; +     +    //this should fail!!! +    $this->assertNotEquals($wanted, $formatter->format($number, $pattern)); +  } +   +  function testNegativeValue() { +    $formatter = new NumberFormat(); +    $number = "-1.2"; +     +    $wanted = "-1.2"; +    $this->assertEquals($wanted, $formatter->format($number)); +  } +} + +?>
\ No newline at end of file | 
