| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | require_once dirname(__FILE__) . '/../include/test.inc.php'; |
| 23 | |
| 24 | class PlDictTest extends PlTestCase |
| 25 | { |
| 26 | private function checkEmpty(PlDict $dict, $key) |
| 27 | { |
| 28 | $this->assertSame(0, $dict->count()); |
| 29 | |
| 30 | $this->assertFalse($dict->has($key)); |
| 31 | $this->assertTrue($dict->blank($key)); |
| 32 | $this->assertNull($dict->v($key)); |
| 33 | $this->assertSame(0, $dict->i($key)); |
| 34 | $this->assertSame('', $dict->s($key)); |
| 35 | $this->assertSame('', $dict->t($key)); |
| 36 | $this->assertFalse($dict->b($key)); |
| 37 | $this->assertSame(array(), $dict->dict()); |
| 38 | } |
| 39 | |
| 40 | public function testEmpty() |
| 41 | { |
| 42 | $dict = new PlDict(); |
| 43 | $this->checkEmpty($dict, $key); |
| 44 | } |
| 45 | |
| 46 | public function testPrefilled() |
| 47 | { |
| 48 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 49 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 50 | $this->assertSame(6, $dict->count()); |
| 51 | $this->assertTrue($dict->has('a')); |
| 52 | $this->assertTrue($dict->has('b')); |
| 53 | $this->assertTrue($dict->has('c')); |
| 54 | $this->assertTrue($dict->has('d')); |
| 55 | $this->assertTrue($dict->has('e')); |
| 56 | $this->assertTrue($dict->has('f')); |
| 57 | $this->assertFalse($dict->has('g')); |
| 58 | $this->assertSame(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 59 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b')), $dict->dict()); |
| 60 | } |
| 61 | |
| 62 | public function testInsertion() |
| 63 | { |
| 64 | $dict = new PlDict(); |
| 65 | $this->assertFalse($dict->has('a')); |
| 66 | $dict->set('a', '12'); |
| 67 | $this->assertTrue($dict->has('a')); |
| 68 | $this->assertFalse($dict->has('b')); |
| 69 | $dict->set('b', false); |
| 70 | $this->assertTrue($dict->has('b')); |
| 71 | $this->assertFalse($dict->has('c')); |
| 72 | $dict->set('c', "\n\n hello world ! "); |
| 73 | $this->assertTrue($dict->has('c')); |
| 74 | $this->assertFalse($dict->has('d')); |
| 75 | $dict->set('d', ' '); |
| 76 | $this->assertTrue($dict->has('d')); |
| 77 | $this->assertFalse($dict->has('e')); |
| 78 | $dict->set('e', 42); |
| 79 | $this->assertTrue($dict->has('e')); |
| 80 | $this->assertFalse($dict->has('f')); |
| 81 | $dict->set('f', array('a', 'b')); |
| 82 | $this->assertTrue($dict->has('f')); |
| 83 | $this->assertFalse($dict->has('g')); |
| 84 | |
| 85 | $this->assertSame(6, $dict->count()); |
| 86 | $this->assertSame(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 87 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b')), $dict->dict()); |
| 88 | |
| 89 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ")); |
| 90 | $this->assertSame(3, $dict->count()); |
| 91 | $dict->set('d', ' '); |
| 92 | $dict->set('e', 42); |
| 93 | $dict->set('f', array('a', 'b')); |
| 94 | |
| 95 | $this->assertSame(6, $dict->count()); |
| 96 | $this->assertSame(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 97 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b')), $dict->dict()); |
| 98 | } |
| 99 | |
| 100 | public function testKill() |
| 101 | { |
| 102 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 103 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 104 | $this->assertSame(6, $dict->count()); |
| 105 | $this->assertTrue($dict->has('a')); |
| 106 | $this->assertTrue($dict->has('b')); |
| 107 | $this->assertTrue($dict->has('c')); |
| 108 | $this->assertTrue($dict->has('d')); |
| 109 | $this->assertTrue($dict->has('e')); |
| 110 | $this->assertTrue($dict->has('f')); |
| 111 | $this->assertFalse($dict->has('g')); |
| 112 | |
| 113 | $dict->kill('a'); |
| 114 | $this->assertSame(5, $dict->count()); |
| 115 | $this->assertFalse($dict->has('a')); |
| 116 | $this->assertTrue($dict->has('b')); |
| 117 | $this->assertTrue($dict->has('c')); |
| 118 | $this->assertTrue($dict->has('d')); |
| 119 | $this->assertTrue($dict->has('e')); |
| 120 | $this->assertTrue($dict->has('f')); |
| 121 | $this->assertFalse($dict->has('g')); |
| 122 | |
| 123 | $dict->kill('g'); |
| 124 | $this->assertSame(5, $dict->count()); |
| 125 | $this->assertFalse($dict->has('a')); |
| 126 | $this->assertTrue($dict->has('b')); |
| 127 | $this->assertTrue($dict->has('c')); |
| 128 | $this->assertTrue($dict->has('d')); |
| 129 | $this->assertTrue($dict->has('e')); |
| 130 | $this->assertTrue($dict->has('f')); |
| 131 | $this->assertFalse($dict->has('g')); |
| 132 | |
| 133 | $this->assertSame(array('b' => false, 'c' => "\n\n hello world ! ", |
| 134 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b')), $dict->dict()); |
| 135 | } |
| 136 | |
| 137 | public function testMerge() |
| 138 | { |
| 139 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ")); |
| 140 | $this->assertSame(3, $dict->count()); |
| 141 | $dict->merge(array('d' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 142 | |
| 143 | $this->assertSame(6, $dict->count()); |
| 144 | $this->assertSame(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 145 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b')), $dict->dict()); |
| 146 | } |
| 147 | |
| 148 | public function testInt() |
| 149 | { |
| 150 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 151 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 152 | $this->assertSame(6, $dict->count()); |
| 153 | |
| 154 | $this->assertSame(12, $dict->i('a', 42)); |
| 155 | $this->assertSame(42, $dict->i('b', 42)); |
| 156 | $this->assertSame(0, $dict->i('b')); |
| 157 | $this->assertSame(42, $dict->i('c', 42)); |
| 158 | $this->assertSame(0, $dict->i('c')); |
| 159 | $this->assertSame(42, $dict->i('d', 42)); |
| 160 | $this->assertSame(0, $dict->i('d')); |
| 161 | $this->assertSame(42, $dict->i('e', 42)); |
| 162 | $this->assertSame(42, $dict->i('e')); |
| 163 | $this->assertSame(42, $dict->i('f', 42)); |
| 164 | $this->assertSame(0, $dict->i('f')); |
| 165 | } |
| 166 | |
| 167 | public function testString() |
| 168 | { |
| 169 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 170 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 171 | $this->assertSame(6, $dict->count()); |
| 172 | |
| 173 | $this->assertSame('12', $dict->s('a', ' blah')); |
| 174 | $this->assertSame('12', $dict->s('a')); |
| 175 | $this->assertSame('12', $dict->t('a', ' blah')); |
| 176 | $this->assertSame('12', $dict->t('a')); |
| 177 | |
| 178 | $this->assertSame('', $dict->s('b', ' blah')); |
| 179 | $this->assertSame('', $dict->s('b', '')); |
| 180 | $this->assertSame('', $dict->t('b', ' blah')); |
| 181 | $this->assertSame('', $dict->t('b', '')); |
| 182 | |
| 183 | $this->assertSame("\n\n hello world ! ", $dict->s('c', ' blah')); |
| 184 | $this->assertSame("\n\n hello world ! ", $dict->s('c')); |
| 185 | $this->assertSame("hello world !", $dict->t('c', ' blah')); |
| 186 | $this->assertSame("hello world !", $dict->t('c')); |
| 187 | |
| 188 | $this->assertSame(' ', $dict->s('d', ' blah')); |
| 189 | $this->assertSame(' ', $dict->s('d')); |
| 190 | $this->assertSame('', $dict->t('d', ' blah')); |
| 191 | $this->assertSame('', $dict->t('d')); |
| 192 | |
| 193 | $this->assertSame('42', $dict->s('e', ' blah')); |
| 194 | $this->assertSame('42', $dict->s('e')); |
| 195 | $this->assertSame('42', $dict->t('e', ' blah')); |
| 196 | $this->assertSame('42', $dict->t('e')); |
| 197 | |
| 198 | $this->assertSame('Array', $dict->s('f', ' blah')); |
| 199 | $this->assertSame('Array', $dict->s('f')); |
| 200 | $this->assertSame('Array', $dict->t('f', ' blah')); |
| 201 | $this->assertSame('Array', $dict->t('f')); |
| 202 | |
| 203 | $this->assertSame(' blah', $dict->s('g', ' blah')); |
| 204 | $this->assertSame('', $dict->s('g')); |
| 205 | $this->assertSame('blah', $dict->t('g', ' blah')); |
| 206 | $this->assertSame('', $dict->t('g')); |
| 207 | } |
| 208 | |
| 209 | public function testValue() |
| 210 | { |
| 211 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 212 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 213 | $this->assertSame(6, $dict->count()); |
| 214 | |
| 215 | $this->assertSame('12', $dict->v('a')); |
| 216 | $this->assertSame('12', $dict->v('a', 'blah')); |
| 217 | $this->assertSame(false, $dict->v('b')); |
| 218 | $this->assertSame(false, $dict->v('b', 'blah')); |
| 219 | $this->assertSame("\n\n hello world ! ", $dict->v('c')); |
| 220 | $this->assertSame("\n\n hello world ! ", $dict->v('c', 'blah')); |
| 221 | $this->assertSame(' ', $dict->v('d')); |
| 222 | $this->assertSame(' ', $dict->v('d', 'blah')); |
| 223 | $this->assertSame(42, $dict->v('e')); |
| 224 | $this->assertSame(42, $dict->v('e', 'blah')); |
| 225 | $this->assertSame(array('a', 'b'), $dict->v('f')); |
| 226 | $this->assertSame(array('a', 'b'), $dict->v('f', 'blah')); |
| 227 | $this->assertNull($dict->v('g')); |
| 228 | $this->assertSame('blah', $dict->v('g', 'blah')); |
| 229 | } |
| 230 | |
| 231 | public function testBlank() |
| 232 | { |
| 233 | $dict = new PlDict(array('a' => '12', 'b' => false, 'c' => "\n\n hello world ! ", |
| 234 | 'd' => ' ', 'e' => 42, 'f' => array('a', 'b'))); |
| 235 | $this->assertSame(6, $dict->count()); |
| 236 | |
| 237 | $this->assertFalse($dict->blank('a')); |
| 238 | $this->assertFalse($dict->blank('a', true)); |
| 239 | $this->assertTrue($dict->blank('b')); |
| 240 | $this->assertTrue($dict->blank('b', true)); |
| 241 | $this->assertFalse($dict->blank('c')); |
| 242 | $this->assertFalse($dict->blank('c', true)); |
| 243 | $this->assertTrue($dict->blank('d')); |
| 244 | $this->assertFalse($dict->blank('d', true)); |
| 245 | $this->assertFalse($dict->blank('e')); |
| 246 | $this->assertFalse($dict->blank('e', true)); |
| 247 | $this->assertFalse($dict->blank('f')); |
| 248 | $this->assertFalse($dict->blank('f', true)); |
| 249 | $this->assertTrue($dict->blank('g')); |
| 250 | $this->assertTrue($dict->blank('g', true)); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 255 | ?> |