1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-21 22:32:41 +01:00

Add explicit tests for phutil_string_cast

Summary: Test (and document) what phutil_string_cast actually does to some things.

Test Plan: `arc unit`

Reviewers: O1 Blessed Committers, speck

Reviewed By: O1 Blessed Committers, speck

Subscribers: speck, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Differential Revision: https://we.phorge.it/D25326
This commit is contained in:
Aviv Eyal 2023-07-03 13:42:25 -07:00
parent 8426ebc053
commit 152ba1f02a

View file

@ -1070,4 +1070,50 @@ final class PhutilUtilsTestCase extends PhutilTestCase {
} }
} }
public function testStringCasting() {
$cases = array(
array(123, '123', 'number'),
array(null, '', 'null to empty string'),
array('text', 'text', 'string'),
array(17.4, '17.4', 'float'),
array(true, '1', 'boolean true (well done php?)'),
array(false, '', 'boolean false (to empty string)'),
array(0, '0', 'zero (int)'),
array(0.0, '0', 'zero (float'),
array(
new PhutilURI('http://www.example.com'),
'http://www.example.com',
'Object with toString()',
),
);
$exception_cases = array(
array(array(), 'array'),
);
foreach ($cases as $test_case) {
list($input, $expected_output, $test_name) = $test_case;
$actual = phutil_string_cast($input);
$this->assertEqual($expected_output, $actual, $test_name);
}
$expect_exceptions = array('Exception');
foreach ($exception_cases as $test_case) {
list($input, $test_name) = $test_case;
try {
phutil_string_cast($input);
} catch (Exception $ex) {
$caught = $ex;
} catch (Throwable $ex) {
$caught = $ex;
}
$this->assertCaught($expect_exceptions, $caught, $test_name);
}
}
} }