Skip to content

Commit

Permalink
Add better assertion error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Oct 14, 2018
1 parent 09f983a commit 31938f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 22 additions & 7 deletions source/macros.yay
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
));
}
}
}
Expand All @@ -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"])
));
}
}
}
Expand Down Expand Up @@ -224,7 +239,7 @@ $(macro :recursion) {
$(functionArgumentTypeRemove ? {
assert($this->validGenericType(
$(functionArgumentName), $$(stringify($(functionArgumentType)))
)); })
), new \UnexpectedValueException($$(stringify($(functionArgumentTypeError))))); })
}) })


Expand All @@ -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));
})
Expand Down
6 changes: 3 additions & 3 deletions tests/GenericsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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);
}

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();
}
Expand Down Expand Up @@ -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]);
}
Expand Down

0 comments on commit 31938f7

Please sign in to comment.