summaryrefslogtreecommitdiff
path: root/tests/unit/Collections/TMapTest.php
blob: a6f82570440e706d3cd931283ce2ad45f0f09aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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());
		}
	}
}

?>