From 82dc11d776ae56164b13f1f74d4e7cba138f5e28 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Thu, 15 Jun 2017 14:53:48 -0700 Subject: [PATCH] Make isNotNull return refined type --- src/ExpectObj.php | 26 ++++++++++++++------------ src/expect.php | 4 ++-- tests/ExpectObjTest.php | 6 ++++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/ExpectObj.php b/src/ExpectObj.php index 0338d13..3e5ea52 100644 --- a/src/ExpectObj.php +++ b/src/ExpectObj.php @@ -11,7 +11,7 @@ namespace Facebook\FBExpect; -final class ExpectObj extends Assert { +final class ExpectObj extends Assert { public function __construct(private ImmVector $vars) { } /************************************** @@ -152,11 +152,11 @@ public function toBeGreaterThanOrEqualTo( } // Asserts: $actual instanceof $type - public function toBeInstanceOf( - classname $class_or_interface, + public function toBeInstanceOf( + classname $class_or_interface, string $msg = '', ... - ): T { + ): Tclass { $msg = vsprintf($msg, array_slice(func_get_args(), 2)); $this->assertSingleArg(__FUNCTION__); $obj = $this->vars->firstValue(); @@ -324,10 +324,12 @@ public function toNotEqual($expected, string $msg = '', ...): void { } // Asserts: $actual !== null - public function toNotBeNull(string $msg = '', ...) { + public function toNotBeNull(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; } /** @@ -470,8 +472,8 @@ public function notToThrow( * expect(function() { invariant_violation('fail'); }) * ->toThrow(InvariantViolationException::class, 'fail'); */ - public function toThrow( - classname $exception_class, + public function toThrow( + classname $exception_class, ?string $expected_exception_message = null, ?string $msg = null, ... @@ -495,9 +497,9 @@ classname $exception_class, * function($a) { if ($a == 'foo') { invariant_violation('fail'); }} * )->toThrowWhenCalledWith(array('foo'), 'InvariantViolationException'); */ - public function toThrowWhenCalledWith( + public function toThrowWhenCalledWith( array $args, - classname $exception_class, + classname $exception_class, ?string $expected_exception_message = null, ?string $desc = null ): void { @@ -534,9 +536,9 @@ private function assertSingleArg(string $method) { ); } - private function tryCallWithArgsReturnException( + private function tryCallWithArgsReturnException( array $args, - classname $expected_exception_type, + classname $expected_exception_type, ) { try { $callable = count($this->vars) == 1 ? $this->vars->firstValue() : $this->vars; diff --git a/src/expect.php b/src/expect.php index 6fde22e..a03af17 100644 --- a/src/expect.php +++ b/src/expect.php @@ -31,6 +31,6 @@ * - Function Exception Assertions * - Mock Object Asserts */ -function expect(mixed ...$args): ExpectObj { - return new ExpectObj(new ImmVector($args)); +function expect(T $obj, mixed ...$args): ExpectObj { + return new ExpectObj(new ImmVector(func_get_args())); } diff --git a/tests/ExpectObjTest.php b/tests/ExpectObjTest.php index 4fb4988..9cbfb2d 100644 --- a/tests/ExpectObjTest.php +++ b/tests/ExpectObjTest.php @@ -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'); + } }