* Write a unittest for RootControler
[hoplite.git] / testing / tests / base / functions_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\base as base;
19
20 require_once HOPLITE_ROOT . '/base/functions.php';
21
22 class FunctionsTest extends \PHPUnit_Framework_TestCase
23 {
24 public function testArrayStripEmpty()
25 {
26 $array = array(1, 4, 6);
27 base\ArrayStripEmpty($array);
28 $this->assertEquals(3, count($array));
29
30 $array = array(1, 0, 5, '');
31 base\ArrayStripEmpty($array);
32 $this->assertEquals(2, count($array));
33
34 $array = array('', 'test' => array('', 6));
35 base\ArrayStripEmpty($array);
36 $this->assertEquals(1, count($array));
37 $this->assertEquals(1, count($array['test']));
38
39 $array = array('foo', NULL, 'bar');
40 base\ArrayStripEmpty($array);
41 $this->assertEquals(2, count($array));
42 }
43
44 public function testUnderscoreToCamelCase()
45 {
46 $str = 'under_score';
47 $this->assertEquals('UnderScore', base\UnderscoreToCamelCase($str));
48 $this->assertEquals('underScore', base\UnderscoreToCamelCase($str, FALSE));
49
50 $str = 'many_many_under_scores';
51 $this->assertEquals('ManyManyUnderScores', base\UnderscoreToCamelCase($str));
52 }
53
54 public function testCamelCaseToUnderscore()
55 {
56 $str = 'CamelCase';
57 $this->assertEquals('camel_case', base\CamelCaseToUnderscore($str));
58
59 $str = 'camelCase';
60 $this->assertEquals('camel_case', base\CamelCaseToUnderscore($str));
61
62 $str = 'AVeryLongTitleCase';
63 $this->assertEquals('a_very_long_title_case', base\CamelCaseToUnderscore($str));
64 }
65
66 protected function _RandomHelper($arg, $lower, $upper)
67 {
68 $list = array();
69 for ($i = 0; $i < 200; $i++)
70 {
71 $rand = base\Random($arg);
72 $this->assertNotContains($rand, $list, 'Duplicate random string!');
73 $list[] = $rand;
74 $this->assertGreaterThanOrEqual($lower, strlen($rand), 'Random string not in lower bound');
75 $this->assertLessThanOrEqual($upper, strlen($rand), 'Random string not in upper bound');
76 }
77 }
78
79 public function testRandomDefault()
80 {
81 $this->_RandomHelper(NULL, 20, 100);
82 }
83
84 public function testRandomArgument()
85 {
86 $this->_RandomHelper(20, 20, 20);
87 }
88 }