Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Expand as-expression test coverage, part 1
Summary: From D8708371, we learned that our test coverage for as-expressions is very sparse. This adds tests for the simple types; a followup will test classes, type aliases, and type constants. Reviewed By: ricklavoie Differential Revision: D8710617 fbshipit-source-id: c19011bc92a1375e871e2606e55df9a20b89628d
- Loading branch information
Showing
with
471 additions
and 0 deletions.
- +51 −0 hphp/test/slow/as_expression/class.php
- +21 −0 hphp/test/slow/as_expression/class.php.expect
- +11 −0 hphp/test/slow/as_expression/darray.php
- +1 −0 hphp/test/slow/as_expression/darray.php.expectf
- +20 −0 hphp/test/slow/as_expression/dynamic.php
- +14 −0 hphp/test/slow/as_expression/dynamic.php.expect
- +47 −0 hphp/test/slow/as_expression/enum.php
- +17 −0 hphp/test/slow/as_expression/enum.php.expect
- +11 −0 hphp/test/slow/as_expression/function.php
- +1 −0 hphp/test/slow/as_expression/function.php.expectf
- +20 −0 hphp/test/slow/as_expression/keyset.php
- +11 −0 hphp/test/slow/as_expression/keyset.php.expect
- +20 −0 hphp/test/slow/as_expression/mixed.php
- +14 −0 hphp/test/slow/as_expression/mixed.php.expect
- +20 −0 hphp/test/slow/as_expression/nonnull.php
- +14 −0 hphp/test/slow/as_expression/nonnull.php.expect
- +20 −0 hphp/test/slow/as_expression/noreturn.php
- +10 −0 hphp/test/slow/as_expression/noreturn.php.expect
- +41 −0 hphp/test/slow/as_expression/primitive_union.php
- +21 −0 hphp/test/slow/as_expression/primitive_union.php.expect
- +11 −0 hphp/test/slow/as_expression/soft.php
- +1 −0 hphp/test/slow/as_expression/soft.php.expectf
- +11 −0 hphp/test/slow/as_expression/varray.php
- +1 −0 hphp/test/slow/as_expression/varray.php.expectf
- +20 −0 hphp/test/slow/as_expression/vec_or_dict.php
- +12 −0 hphp/test/slow/as_expression/vec_or_dict.php.expect
- +20 −0 hphp/test/slow/as_expression/void.php
- +10 −0 hphp/test/slow/as_expression/void.php.expect
@@ -0,0 +1,51 @@ | ||
<?hh // strict | ||
|
||
interface I {} | ||
abstract class C implements I {} | ||
final class D extends C {} | ||
final class E extends C {} | ||
final class F implements I {} | ||
|
||
function as_I(mixed $x): void { | ||
try { | ||
var_dump($x as I); | ||
} catch (TypeAssertionException $_) { | ||
echo "not I: ".get_class($x)."\n"; | ||
} | ||
} | ||
|
||
function as_C(mixed $x): void { | ||
try { | ||
var_dump($x as C); | ||
} catch (TypeAssertionException $_) { | ||
echo "not C: ".get_class($x)."\n"; | ||
} | ||
} | ||
|
||
function as_D(mixed $x): void { | ||
try { | ||
var_dump($x as D); | ||
} catch (TypeAssertionException $_) { | ||
echo "not D: ".get_class($x)."\n"; | ||
} | ||
} | ||
|
||
$d = new D(); | ||
as_D($d); | ||
as_C($d); | ||
as_I($d); | ||
echo "\n"; | ||
$s = new stdClass(); | ||
as_D($s); | ||
as_C($s); | ||
as_I($s); | ||
echo "\n"; | ||
$e = new E(); | ||
as_D($e); | ||
as_C($e); | ||
as_I($e); | ||
echo "\n"; | ||
$f = new F(); | ||
as_D($f); | ||
as_C($f); | ||
as_I($f); |
@@ -0,0 +1,21 @@ | ||
object(D)#1 (0) { | ||
} | ||
object(D)#1 (0) { | ||
} | ||
object(D)#1 (0) { | ||
} | ||
|
||
not D: stdClass | ||
not C: stdClass | ||
not I: stdClass | ||
|
||
not D: E | ||
object(E)#3 (0) { | ||
} | ||
object(E)#3 (0) { | ||
} | ||
|
||
not D: F | ||
not C: F | ||
object(F)#4 (0) { | ||
} |
@@ -0,0 +1,11 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as darray); | ||
} catch (TypeAssertionException $_) { | ||
echo "not darray: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(darray[]); |
@@ -0,0 +1 @@ | ||
Fatal error: "is" and "as" operators cannot be used with an array in %s on line %d |
@@ -0,0 +1,20 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as dynamic); | ||
} catch (TypeAssertionException $_) { | ||
echo "not dynamic: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(1); | ||
f(false); | ||
f(1.5); | ||
f('foo'); | ||
f(STDIN); | ||
f(new stdClass()); | ||
f(vec[]); | ||
f(dict[]); | ||
f(keyset[]); | ||
f(null); |
@@ -0,0 +1,14 @@ | ||
int(1) | ||
bool(false) | ||
float(1.5) | ||
string(3) "foo" | ||
resource(1) of type (stream) | ||
object(stdClass)#1 (0) { | ||
} | ||
vec(0) { | ||
} | ||
dict(0) { | ||
} | ||
keyset(0) { | ||
} | ||
NULL |
@@ -0,0 +1,47 @@ | ||
<?hh | ||
|
||
enum Foo: int { | ||
A = 1; | ||
B = 2; | ||
} | ||
|
||
enum Bar: string { | ||
A = '1'; | ||
B = '2'; | ||
} | ||
|
||
function as_foo(mixed $x) { | ||
try { | ||
var_dump($x as Foo); | ||
} catch (TypeAssertionException $_) { | ||
echo "not Foo: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
function as_bar(mixed $x) { | ||
try { | ||
var_dump($x as Bar); | ||
} catch (TypeAssertionException $_) { | ||
echo "not Bar: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
as_foo(1); | ||
as_foo('1'); // TODO(T29283057) | ||
as_foo(2); | ||
as_foo(1.5); | ||
as_foo(true); | ||
as_foo(null); | ||
as_foo(STDIN); | ||
as_foo(new stdClass()); | ||
|
||
echo "\n"; | ||
|
||
as_bar(1); // TODO(T29283057) | ||
as_bar('1'); | ||
as_bar('2'); | ||
as_bar(1.5); | ||
as_bar(true); | ||
as_bar(null); | ||
as_bar(STDIN); | ||
as_bar(new stdClass()); |
@@ -0,0 +1,17 @@ | ||
int(1) | ||
string(1) "1" | ||
int(2) | ||
not Foo: double | ||
not Foo: boolean | ||
not Foo: NULL | ||
not Foo: resource | ||
not Foo: object | ||
|
||
int(1) | ||
string(1) "1" | ||
string(1) "2" | ||
not Bar: double | ||
not Bar: boolean | ||
not Bar: NULL | ||
not Bar: resource | ||
not Bar: object |
@@ -0,0 +1,11 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as (function(): int)); | ||
} catch (TypeAssertionException $_) { | ||
echo "not function: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(() ==> 1); |
@@ -0,0 +1 @@ | ||
Fatal error: Callable typehints cannot be used with as-expressions in %s on line %d |
@@ -0,0 +1,20 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as keyset); | ||
} catch (TypeAssertionException $_) { | ||
echo "not keyset: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(1); | ||
f(false); | ||
f(1.5); | ||
f('foo'); | ||
f(STDIN); | ||
f(new stdClass()); | ||
f(vec[]); | ||
f(dict[]); | ||
f(keyset[]); | ||
f(null); |
@@ -0,0 +1,11 @@ | ||
not keyset: integer | ||
not keyset: boolean | ||
not keyset: double | ||
not keyset: string | ||
not keyset: resource | ||
not keyset: object | ||
not keyset: vec | ||
not keyset: dict | ||
keyset(0) { | ||
} | ||
not keyset: NULL |
@@ -0,0 +1,20 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as mixed); | ||
} catch (TypeAssertionException $_) { | ||
echo "not mixed: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(1); | ||
f(false); | ||
f(1.5); | ||
f('foo'); | ||
f(STDIN); | ||
f(new stdClass()); | ||
f(vec[]); | ||
f(dict[]); | ||
f(keyset[]); | ||
f(null); |
@@ -0,0 +1,14 @@ | ||
int(1) | ||
bool(false) | ||
float(1.5) | ||
string(3) "foo" | ||
resource(1) of type (stream) | ||
object(stdClass)#1 (0) { | ||
} | ||
vec(0) { | ||
} | ||
dict(0) { | ||
} | ||
keyset(0) { | ||
} | ||
NULL |
@@ -0,0 +1,20 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as nonnull); | ||
} catch (TypeAssertionException $_) { | ||
echo "not nonnull: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(1); | ||
f(false); | ||
f(1.5); | ||
f('foo'); | ||
f(STDIN); | ||
f(new stdClass()); | ||
f(vec[]); | ||
f(dict[]); | ||
f(keyset[]); | ||
f(null); |
@@ -0,0 +1,14 @@ | ||
int(1) | ||
bool(false) | ||
float(1.5) | ||
string(3) "foo" | ||
resource(1) of type (stream) | ||
object(stdClass)#1 (0) { | ||
} | ||
vec(0) { | ||
} | ||
dict(0) { | ||
} | ||
keyset(0) { | ||
} | ||
not nonnull: NULL |
@@ -0,0 +1,20 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as noreturn); | ||
} catch (TypeAssertionException $_) { | ||
echo "not noreturn: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(1); | ||
f(false); | ||
f(1.5); | ||
f('foo'); | ||
f(STDIN); | ||
f(new stdClass()); | ||
f(vec[]); | ||
f(dict[]); | ||
f(keyset[]); | ||
f(null); |
@@ -0,0 +1,10 @@ | ||
not noreturn: integer | ||
not noreturn: boolean | ||
not noreturn: double | ||
not noreturn: string | ||
not noreturn: resource | ||
not noreturn: object | ||
not noreturn: vec | ||
not noreturn: dict | ||
not noreturn: keyset | ||
not noreturn: NULL |
@@ -0,0 +1,41 @@ | ||
<?hh | ||
|
||
function f(mixed $x): void { | ||
try { | ||
var_dump($x as num); | ||
} catch (TypeAssertionException $_) { | ||
echo "not num: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
function g(mixed $x): void { | ||
try { | ||
var_dump($x as arraykey); | ||
} catch (TypeAssertionException $_) { | ||
echo "not arraykey: ".gettype($x)."\n"; | ||
} | ||
} | ||
|
||
f(1); | ||
f(false); | ||
f(1.5); | ||
f('foo'); | ||
f(STDIN); | ||
f(new stdClass()); | ||
f(vec[]); | ||
f(dict[]); | ||
f(keyset[]); | ||
f(null); | ||
|
||
echo "\n"; | ||
|
||
g(1); | ||
g(false); | ||
g(1.5); | ||
g('foo'); | ||
g(STDIN); | ||
g(new stdClass()); | ||
g(vec[]); | ||
g(dict[]); | ||
g(keyset[]); | ||
g(null); |
Oops, something went wrong.