From 31938f7e0c8df91bd3ad262bc5f488c57c835761 Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Sun, 14 Oct 2018 05:25:19 +0200 Subject: [PATCH] Add better assertion error messages --- source/macros.yay | 29 ++++++++++++++++++++++------- tests/GenericsTest.php | 6 +++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/source/macros.yay b/source/macros.yay index 01bda4b..6eac52d 100644 --- a/source/macros.yay +++ b/source/macros.yay @@ -123,6 +123,14 @@ $(macro :recursion) { if (isset($argument["functionArgument"]["functionArgumentType"])) { if (in_array($stringFromNs($argument["functionArgument"]["functionArgumentType"]), $types)) { $argument["functionArgument"]["functionArgumentTypeRemove"] = true; + + $argument["functionArgument"]["functionArgumentTypeError"] = new \Yay\Token(T_CONSTANT_ENCAPSED_STRING, sprintf( + "%s in %s::%s() is not a valid %s", + (string) $argument["functionArgument"]["functionArgumentName"], + $stringFromNs($internal["className"]), + $stringFromNs($member["function"]["functionName"]), + $stringFromNs($argument["functionArgument"]["functionArgumentType"]) + )); } } } @@ -134,6 +142,13 @@ $(macro :recursion) { if (in_array($stringFromNs($member["function"]["functionReturn"]["functionReturnType"]), $types)) { $member["function"]["functionReturnComplex"] = true; $member["function"]["functionReturn"]["functionReturnTypeRemove"] = true; + + $member["function"]["functionReturn"]["functionReturnTypeError"] = new \Yay\Token(T_CONSTANT_ENCAPSED_STRING, sprintf( + "%s::%s() does not return a valid %s", + $stringFromNs($internal["className"]), + $stringFromNs($member["function"]["functionName"]), + $stringFromNs($member["function"]["functionReturn"]["functionReturnType"]) + )); } } } @@ -224,7 +239,7 @@ $(macro :recursion) { $(functionArgumentTypeRemove ? { assert($this->validGenericType( $(functionArgumentName), $$(stringify($(functionArgumentType))) - )); }) + ), new \UnexpectedValueException($$(stringify($(functionArgumentTypeError))))); }) }) }) @@ -237,12 +252,12 @@ $(macro :recursion) { }) { $(functionBody) })(); - - assert($this->validGenericType( - $$(unsafe($GENERIC_RESULT)), $(functionReturn ... { - $$(stringify($(functionReturnType))) - }) - )); + + $(functionReturn ... { + assert($this->validGenericType( + $$(unsafe($GENERIC_RESULT)), $$(stringify($(functionReturnType))) + ), new \UnexpectedValueException($$(stringify($(functionReturnTypeError))))); + }) return $$(unsafe($GENERIC_RESULT)); }) diff --git a/tests/GenericsTest.php b/tests/GenericsTest.php index 90bdd32..1403502 100644 --- a/tests/GenericsTest.php +++ b/tests/GenericsTest.php @@ -22,7 +22,7 @@ public function test_can_create_basic_generics() public function test_can_detect_invalid_argument_types() { $combiner = process(__DIR__ . "/fixtures/can-create-basic-generics.pre"); - $this->expectException(AssertionError::class); + $this->expectException(UnexpectedValueException::class); $combiner->combine('hello', 1); } @@ -30,7 +30,7 @@ public function test_can_detect_invalid_argument_types() public function test_can_detect_invalid_return_types() { $combiner = process(__DIR__ . "/fixtures/can-create-basic-generics.pre"); - $this->expectException(AssertionError::class); + $this->expectException(UnexpectedValueException::class); $combiner->badReturn(); } @@ -68,7 +68,7 @@ public function test_can_handle_interface_types() public function test_can_detect_invalid_interface_types() { $counter = process(__DIR__ . "/fixtures/can-handle-interface-types.pre"); - $this->expectException(AssertionError::class); + $this->expectException(UnexpectedValueException::class); $counter->badCount([1, 2, 3]); }