Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F101605983
PhutilUtilsTestCase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Feb 12, 00:28
Size
5 KB
Mime Type
text/x-php
Expires
Fri, Feb 14, 00:28 (2 d)
Engine
blob
Format
Raw Data
Handle
24199270
Attached To
rPHU libphutil
PhutilUtilsTestCase.php
View Options
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Test cases for functions in utils.php.
*
* @group testcase
*/
final
class
PhutilUtilsTestCase
extends
ArcanistPhutilTestCase
{
public
function
testMFilter_nullMethod_throwException
()
{
$caught
=
null
;
try
{
mfilter
(
array
(),
null
);
}
catch
(
InvalidArgumentException
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertEqual
(
true
,
(
$caught
instanceof
InvalidArgumentException
));
}
public
function
testMFilter_withEmptyValue_filtered
()
{
$a
=
new
MFilterTestHelper
(
'o'
,
'p'
,
'q'
);
$b
=
new
MFilterTestHelper
(
'o'
,
''
,
'q'
);
$c
=
new
MFilterTestHelper
(
'o'
,
'p'
,
'q'
);
$list
=
array
(
'a'
=>
$a
,
'b'
=>
$b
,
'c'
=>
$c
,
);
$actual
=
mfilter
(
$list
,
'getI'
);
$expected
=
array
(
'a'
=>
$a
,
'c'
=>
$c
,
);
$this
->
assertEqual
(
$expected
,
$actual
);
}
public
function
testMFilter_withEmptyValueNegate_filtered
()
{
$a
=
new
MFilterTestHelper
(
'o'
,
'p'
,
'q'
);
$b
=
new
MFilterTestHelper
(
'o'
,
''
,
'q'
);
$c
=
new
MFilterTestHelper
(
'o'
,
'p'
,
'q'
);
$list
=
array
(
'a'
=>
$a
,
'b'
=>
$b
,
'c'
=>
$c
,
);
$actual
=
mfilter
(
$list
,
'getI'
,
true
);
$expected
=
array
(
'b'
=>
$b
,
);
$this
->
assertEqual
(
$expected
,
$actual
);
}
public
function
testIFilter_invalidIndex_throwException
()
{
$caught
=
null
;
try
{
ifilter
(
array
(),
null
);
}
catch
(
InvalidArgumentException
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertEqual
(
true
,
(
$caught
instanceof
InvalidArgumentException
));
}
public
function
testIFilter_withEmptyValue_filtered
()
{
$list
=
array
(
'a'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'b'
=>
array
(
'h'
=>
'o'
,
'i'
=>
''
,
'j'
=>
'q'
,),
'c'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'd'
=>
array
(
'h'
=>
'o'
,
'i'
=>
0
,
'j'
=>
'q'
,),
'e'
=>
array
(
'h'
=>
'o'
,
'i'
=>
null
,
'j'
=>
'q'
,),
'f'
=>
array
(
'h'
=>
'o'
,
'i'
=>
false
,
'j'
=>
'q'
,),
);
$actual
=
ifilter
(
$list
,
'i'
);
$expected
=
array
(
'a'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'c'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
);
$this
->
assertEqual
(
$expected
,
$actual
);
}
public
function
testIFilter_indexNotExists_allFiltered
()
{
$list
=
array
(
'a'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'b'
=>
array
(
'h'
=>
'o'
,
'i'
=>
''
,
'j'
=>
'q'
,),
);
$actual
=
ifilter
(
$list
,
'NoneExisting'
);
$expected
=
array
();
$this
->
assertEqual
(
$expected
,
$actual
);
}
public
function
testIFilter_withEmptyValueNegate_filtered
()
{
$list
=
array
(
'a'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'b'
=>
array
(
'h'
=>
'o'
,
'i'
=>
''
,
'j'
=>
'q'
,),
'c'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'd'
=>
array
(
'h'
=>
'o'
,
'i'
=>
0
,
'j'
=>
'q'
,),
'e'
=>
array
(
'h'
=>
'o'
,
'i'
=>
null
,
'j'
=>
'q'
,),
'f'
=>
array
(
'h'
=>
'o'
,
'i'
=>
false
,
'j'
=>
'q'
,),
);
$actual
=
ifilter
(
$list
,
'i'
,
true
);
$expected
=
array
(
'b'
=>
array
(
'h'
=>
'o'
,
'i'
=>
''
,
'j'
=>
'q'
,),
'd'
=>
array
(
'h'
=>
'o'
,
'i'
=>
0
,
'j'
=>
'q'
,),
'e'
=>
array
(
'h'
=>
'o'
,
'i'
=>
null
,
'j'
=>
'q'
,),
'f'
=>
array
(
'h'
=>
'o'
,
'i'
=>
false
,
'j'
=>
'q'
,),
);
$this
->
assertEqual
(
$expected
,
$actual
);
}
public
function
testIFilter_indexNotExists_notFiltered
()
{
$list
=
array
(
'a'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'b'
=>
array
(
'h'
=>
'o'
,
'i'
=>
''
,
'j'
=>
'q'
,),
);
$actual
=
ifilter
(
$list
,
'NoneExisting'
,
true
);
$expected
=
array
(
'a'
=>
array
(
'h'
=>
'o'
,
'i'
=>
'p'
,
'j'
=>
'q'
,),
'b'
=>
array
(
'h'
=>
'o'
,
'i'
=>
''
,
'j'
=>
'q'
,),
);
$this
->
assertEqual
(
$expected
,
$actual
);
}
public
function
testmergev_merging_basicallyWorksCorrectly
()
{
$this
->
assertEqual
(
array
(),
array_mergev
(
array
(
// <empty>
)));
$this
->
assertEqual
(
array
(),
array_mergev
(
array
(
array
(),
array
(),
array
(),
)));
$this
->
assertEqual
(
array
(
1
,
2
,
3
,
4
,
5
),
array_mergev
(
array
(
array
(
1
,
2
),
array
(
3
),
array
(),
array
(
4
,
5
),
)));
}
public
function
testNonempty
()
{
$this
->
assertEqual
(
'zebra'
,
nonempty
(
false
,
null
,
0
,
''
,
array
(),
'zebra'
));
$this
->
assertEqual
(
null
,
nonempty
());
$this
->
assertEqual
(
false
,
nonempty
(
null
,
false
));
$this
->
assertEqual
(
null
,
nonempty
(
false
,
null
));
}
public
function
testCoalesce
()
{
$this
->
assertEqual
(
'zebra'
,
coalesce
(
null
,
'zebra'
));
$this
->
assertEqual
(
null
,
coalesce
());
$this
->
assertEqual
(
false
,
coalesce
(
false
,
null
));
$this
->
assertEqual
(
false
,
coalesce
(
null
,
false
));
}
public
function
testHeadLast
()
{
$this
->
assertEqual
(
'a'
,
head
(
explode
(
'.'
,
'a.b'
)));
$this
->
assertEqual
(
'b'
,
last
(
explode
(
'.'
,
'a.b'
)));
}
public
function
testHeadKeyLastKey
()
{
$this
->
assertEqual
(
'a'
,
head_key
(
array
(
'a'
=>
0
,
'b'
=>
1
))
);
$this
->
assertEqual
(
'b'
,
last_key
(
array
(
'a'
=>
0
,
'b'
=>
1
))
);
$this
->
assertEqual
(
NULL
,
head_key
(
array
()));
$this
->
assertEqual
(
NULL
,
last_key
(
array
()));
}
public
function
testID
()
{
$this
->
assertEqual
(
true
,
id
(
true
));
$this
->
assertEqual
(
false
,
id
(
false
));
}
}
Event Timeline
Log In to Comment