2
2
3
3
namespace Ahc \Underscore \Tests ;
4
4
5
- use Ahc \Underscore \UnderscoreArray as _ ;
5
+ use Ahc \Underscore \Underscore as _ ;
6
6
7
7
class UnderscoreArrayTest extends \PHPUnit_Framework_TestCase
8
8
{
9
9
public function test_first_last ()
10
10
{
11
11
$ array = range (rand (5 , 10 ), rand (15 , 20 ));
12
12
13
- $ this ->assertSame ($ array [0 ], _:: _ ($ array )->first (), 'first ' );
14
- $ this ->assertSame (array_reverse ($ array )[0 ], _:: _ ($ array )->last (), 'last ' );
13
+ $ this ->assertSame ($ array [0 ], underscore ($ array )->first (), 'first ' );
14
+ $ this ->assertSame (array_reverse ($ array )[0 ], underscore ($ array )->last (), 'last ' );
15
15
16
16
$ array = ['x ' => ['first ' ], 'z ' => 'last ' ];
17
17
18
- $ this ->assertSame ($ array ['x ' ], _:: _ ($ array )->head (), 'first ' );
19
- $ this ->assertSame ($ array ['z ' ], _:: _ ($ array )->tail (), 'last ' );
18
+ $ this ->assertSame ($ array ['x ' ], underscore ($ array )->head (), 'first ' );
19
+ $ this ->assertSame ($ array ['z ' ], underscore ($ array )->tail (), 'last ' );
20
20
21
21
$ array = range (1 , 5 );
22
22
23
- $ this ->assertSame ([1 , 2 , 3 ], _:: _ ($ array )->take (3 ), 'first 3 ' );
24
- $ this ->assertSame ([1 , 2 , 3 , 4 , 5 ], _:: _ ($ array )->first (6 ), 'first 6 (n + 1) ' );
25
- $ this ->assertSame ([2 => 3 , 3 => 4 , 4 => 5 ], _:: _ ($ array )->drop (3 ), 'last 3 ' );
26
- $ this ->assertSame ([1 , 2 , 3 , 4 , 5 ], _:: _ ($ array )->last (6 ), 'last 6 (n + 1) ' );
23
+ $ this ->assertSame ([1 , 2 , 3 ], underscore ($ array )->take (3 ), 'first 3 ' );
24
+ $ this ->assertSame ([1 , 2 , 3 , 4 , 5 ], underscore ($ array )->first (6 ), 'first 6 (n + 1) ' );
25
+ $ this ->assertSame ([2 => 3 , 3 => 4 , 4 => 5 ], underscore ($ array )->drop (3 ), 'last 3 ' );
26
+ $ this ->assertSame ([1 , 2 , 3 , 4 , 5 ], underscore ($ array )->last (6 ), 'last 6 (n + 1) ' );
27
27
}
28
28
29
29
public function test_compact ()
30
30
{
31
31
$ array = [0 , 'a ' , '' , [], 2 , [1 ]];
32
32
33
- $ this ->assertSame ([1 => 'a ' , 4 => 2 , 5 => [1 ]], _:: _ ($ array )->compact ()->get (), 'first ' );
33
+ $ this ->assertSame ([1 => 'a ' , 4 => 2 , 5 => [1 ]], underscore ($ array )->compact ()->get (), 'first ' );
34
34
}
35
35
36
36
public function test_flatten ()
@@ -39,7 +39,7 @@ public function test_flatten()
39
39
40
40
$ this ->assertSame (
41
41
[0 , 'a ' , '' , 1 , 2 , 'b ' , 3 , 4 , 'c ' , 5 , 'd ' ],
42
- _:: _ ($ array )->flatten ()->get (),
42
+ underscore ($ array )->flatten ()->get (),
43
43
'flatten '
44
44
);
45
45
}
@@ -50,15 +50,15 @@ public function test_unique_uniq()
50
50
51
51
$ this ->assertSame (
52
52
[0 , 'a ' , '' , 1 , 6 => 2 , 8 => 3 ],
53
- _:: _ ($ array )->unique ()->get (),
53
+ underscore ($ array )->unique ()->get (),
54
54
'unique '
55
55
);
56
56
57
57
$ array = ['a ' , '' , 'a ' , 1 , '' , 0 , 1 , 'b ' , 3 , 2 ];
58
58
59
59
$ this ->assertSame (
60
60
['a ' , '' , 3 => 1 , 5 => 0 , 7 => 'b ' , 8 => 3 , 9 => 2 ],
61
- _:: _ ($ array )->uniq (function ($ i ) {
61
+ underscore ($ array )->uniq (function ($ i ) {
62
62
return $ i ;
63
63
})->get (),
64
64
'uniq '
@@ -71,13 +71,13 @@ public function test_difference_without()
71
71
72
72
$ this ->assertSame (
73
73
[1 => 2 , 'a ' => 3 ],
74
- _:: _ ($ array )->difference ([1 , [4 ]])->get (),
74
+ underscore ($ array )->difference ([1 , [4 ]])->get (),
75
75
'difference '
76
76
);
77
77
78
78
$ this ->assertSame (
79
79
['a ' => 3 , 'b ' => [4 ]],
80
- _:: _ ($ array )->without ([1 , 2 ])->get (),
80
+ underscore ($ array )->without ([1 , 2 ])->get (),
81
81
'without '
82
82
);
83
83
}
@@ -88,7 +88,7 @@ public function test_union()
88
88
89
89
$ this ->assertSame (
90
90
[1 , 2 , 'a ' => 4 , 3 , 'b ' => [5 ]],
91
- _:: _ ($ array )->union ([3 , 'a ' => 4 , 'b ' => [5 ]])->get (),
91
+ underscore ($ array )->union ([3 , 'a ' => 4 , 'b ' => [5 ]])->get (),
92
92
'union '
93
93
);
94
94
}
@@ -99,7 +99,7 @@ public function test_intersection()
99
99
100
100
$ this ->assertSame (
101
101
[1 => 2 , 'a ' => 3 ],
102
- _:: _ ($ array )->intersection ([2 , 'a ' => 3 , 3 ])->get (),
102
+ underscore ($ array )->intersection ([2 , 'a ' => 3 , 3 ])->get (),
103
103
'intersection '
104
104
);
105
105
}
@@ -110,7 +110,7 @@ public function test_zip()
110
110
111
111
$ this ->assertSame (
112
112
[[1 , 2 ], [2 , 4 ], 'a ' => [3 , 5 ], 'b ' => ['B ' , null ]],
113
- _:: _ ($ array )->zip ([2 , 4 , 'a ' => 5 ])->get (),
113
+ underscore ($ array )->zip ([2 , 4 , 'a ' => 5 ])->get (),
114
114
'zip '
115
115
);
116
116
}
@@ -119,7 +119,7 @@ public function test_object()
119
119
{
120
120
$ array = [[1 , 2 ], 'a ' => 3 , 'b ' => 'B ' ];
121
121
122
- foreach (_:: _ ($ array )->object () as $ index => $ value ) {
122
+ foreach (underscore ($ array )->object () as $ index => $ value ) {
123
123
$ this ->assertTrue (is_object ($ value ));
124
124
$ this ->assertSame ($ index , $ value ->index );
125
125
$ this ->assertSame ($ array [$ index ], $ value ->value );
@@ -128,7 +128,7 @@ public function test_object()
128
128
129
129
public function test_findIndex_findLastIndex ()
130
130
{
131
- $ array = _:: _ ([[1 , 2 ], 'a ' => 3 , 'x ' => 4 , 'y ' => 2 , 'b ' => 'B ' ]);
131
+ $ array = underscore ([[1 , 2 ], 'a ' => 3 , 'x ' => 4 , 'y ' => 2 , 'b ' => 'B ' ]);
132
132
133
133
$ this ->assertSame (0 , $ array ->findIndex ());
134
134
$ this ->assertSame ('b ' , $ array ->findLastIndex ());
@@ -143,25 +143,25 @@ public function test_findIndex_findLastIndex()
143
143
144
144
public function test_indexOf_lastIndexOf ()
145
145
{
146
- $ array = _:: _ ([[1 , 2 ], 'a ' => 2 , 'x ' => 4 , 'y ' => 2 , 'b ' => 'B ' ]);
146
+ $ array = underscore ([[1 , 2 ], 'a ' => 2 , 'x ' => 4 , 'y ' => 2 , 'b ' => 'B ' ]);
147
147
148
148
$ this ->assertSame ('a ' , $ array ->indexOf (2 ));
149
149
$ this ->assertSame ('y ' , $ array ->lastIndexOf (2 ));
150
150
}
151
151
152
152
public function test_range ()
153
153
{
154
- $ this ->assertSame ([4 , 5 , 6 , 7 , 8 , 9 ], _:: _ ()->range (4 , 9 )->get ());
155
- $ this ->assertSame ([10 , 12 , 14 , 16 , 18 ], _:: _ ()->range (10 , 18 , 2 )->get ());
156
- $ this ->assertSame ([20 , 19 , 18 , 17 , 16 ], _:: _ ()->range (20 , 16 , -1 )->get ());
154
+ $ this ->assertSame ([4 , 5 , 6 , 7 , 8 , 9 ], underscore ()->range (4 , 9 )->get ());
155
+ $ this ->assertSame ([10 , 12 , 14 , 16 , 18 ], underscore ()->range (10 , 18 , 2 )->get ());
156
+ $ this ->assertSame ([20 , 19 , 18 , 17 , 16 ], underscore ()->range (20 , 16 , -1 )->get ());
157
157
}
158
158
159
159
public function test_sortedIndex ()
160
160
{
161
161
$ nums = [1 , 3 , 5 , 8 , 11 ];
162
162
$ new = 9 ;
163
163
164
- $ newIdx = _:: _ ($ nums )->sortedIndex ($ new , null );
164
+ $ newIdx = underscore ($ nums )->sortedIndex ($ new , null );
165
165
166
166
$ this ->assertSame (4 , $ newIdx );
167
167
@@ -174,7 +174,7 @@ public function test_sortedIndex()
174
174
];
175
175
176
176
$ new = ['x ' => 3 , 'y ' => 2 ];
177
- $ newIdx = _:: _ ($ data )->sortedIndex ($ new , function ($ row ) {
177
+ $ newIdx = underscore ($ data )->sortedIndex ($ new , function ($ row ) {
178
178
return $ row ['x ' ] + $ row ['y ' ];
179
179
});
180
180
0 commit comments