Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Make isNotNull return refined type
Browse files Browse the repository at this point in the history
  • Loading branch information
fredemmott committed Jul 3, 2017
1 parent d00406d commit 82dc11d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
26 changes: 14 additions & 12 deletions src/ExpectObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Facebook\FBExpect;

final class ExpectObj extends Assert {
final class ExpectObj<T> extends Assert {
public function __construct(private ImmVector<mixed> $vars) { }

/**************************************
Expand Down Expand Up @@ -152,11 +152,11 @@ public function toBeGreaterThanOrEqualTo(
}

// Asserts: $actual instanceof $type
public function toBeInstanceOf<T>(
classname<T> $class_or_interface,
public function toBeInstanceOf<Tclass>(
classname<Tclass> $class_or_interface,
string $msg = '',
...
): T {
): Tclass {
$msg = vsprintf($msg, array_slice(func_get_args(), 2));
$this->assertSingleArg(__FUNCTION__);
$obj = $this->vars->firstValue();
Expand Down Expand Up @@ -324,10 +324,12 @@ public function toNotEqual($expected, string $msg = '', ...): void {
}

// Asserts: $actual !== null
public function toNotBeNull(string $msg = '', ...) {
public function toNotBeNull<Tv>(string $msg = '', ...): Tv where T = ?Tv {
$msg = vsprintf($msg, array_slice(func_get_args(), 1));
$this->assertSingleArg(__FUNCTION__);
$this->assertNotNull($this->vars->firstValue(), $msg);
$val = $this->vars->firstValue();
$this->assertNotNull($val, $msg);
return /* HH_IGNORE_ERROR[4110] */ $val;
}

/**
Expand Down Expand Up @@ -470,8 +472,8 @@ public function notToThrow(
* expect(function() { invariant_violation('fail'); })
* ->toThrow(InvariantViolationException::class, 'fail');
*/
public function toThrow<T as \Exception>(
classname<T> $exception_class,
public function toThrow<Tclass as \Exception>(
classname<Tclass> $exception_class,
?string $expected_exception_message = null,
?string $msg = null,
...
Expand All @@ -495,9 +497,9 @@ classname<T> $exception_class,
* function($a) { if ($a == 'foo') { invariant_violation('fail'); }}
* )->toThrowWhenCalledWith(array('foo'), 'InvariantViolationException');
*/
public function toThrowWhenCalledWith<T as \Exception>(
public function toThrowWhenCalledWith<Tclass as \Exception>(
array $args,
classname<T> $exception_class,
classname<Tclass> $exception_class,
?string $expected_exception_message = null,
?string $desc = null
): void {
Expand Down Expand Up @@ -534,9 +536,9 @@ private function assertSingleArg(string $method) {
);
}

private function tryCallWithArgsReturnException<T as \Exception>(
private function tryCallWithArgsReturnException<Tclass as \Exception>(
array $args,
classname<T> $expected_exception_type,
classname<Tclass> $expected_exception_type,
) {
try {
$callable = count($this->vars) == 1 ? $this->vars->firstValue() : $this->vars;
Expand Down
4 changes: 2 additions & 2 deletions src/expect.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
* - Function Exception Assertions
* - Mock Object Asserts
*/
function expect(mixed ...$args): ExpectObj {
return new ExpectObj(new ImmVector($args));
function expect<T>(T $obj, mixed ...$args): ExpectObj<T> {
return new ExpectObj(new ImmVector(func_get_args()));
}
6 changes: 6 additions & 0 deletions tests/ExpectObjTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,10 @@ public function testInstanceOfTyping(): void {
$x = expect($x)->toBeInstanceOf(\Exception::class);
expect($x->getMessage())->toBeSame('foo');
}

public function testNotNull(): void {
$x = ((): ?\Exception ==> new \Exception('foo'))();
$x = expect($x)->toNotBeNull();
expect($x->getMessage())->toBeSame('foo');
}
}

0 comments on commit 82dc11d

Please sign in to comment.