Skip to content

Commit c2c21c6

Browse files
committed
Fix string representation when using callables inside Condition
1 parent 0ac752b commit c2c21c6

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/main/php/test/verify/Condition.class.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*
1010
* - `Condition('self::processExecutionEnabled()')`
1111
* - `Condition(assert: 'function_exists("bcadd")')`
12+
* - `Condition(assert: fn() => PHP_VERSION_ID >= 80500)`
1213
*
1314
* @test test.unittest.ConditionTest
1415
*/
@@ -27,14 +28,17 @@ public function __construct($assert) {
2728
* @return iterable
2829
*/
2930
public function assertions(Context $context) {
30-
$assertion= $this->assert instanceof Closure
31-
? $this->assert
32-
: $context->type->evaluate("fn() => {$this->assert};")
33-
;
31+
if ($this->assert instanceof Closure) {
32+
$string= '<function()>';
33+
$assertion= $this->assert;
34+
} else {
35+
$string= (string)$this->assert;
36+
$assertion= $context->type->evaluate("fn() => {$this->assert};");
37+
}
3438

3539
yield new Assertion(
36-
$assertion->bindTo($context->instance, $context->type->literal())->__invoke(),
37-
new Verify($this->assert)
40+
$assertion->bindTo($context->instance, $context->type->literal())(),
41+
new Verify($string)
3842
);
3943
}
4044
}

src/test/php/test/unittest/ConditionTest.class.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use lang\Reflection;
44
use test\execution\Context;
55
use test\verify\Condition;
6-
use test\{Assert, Test, Values};
6+
use test\{Assert, Test};
77

88
class ConditionTest {
99

@@ -29,11 +29,13 @@ public function can_create() {
2929
}
3030

3131
#[Test]
32-
public function success() {
33-
Assert::that(new Condition('true'))
34-
->mappedBy([$this, 'failures'])
35-
->isNull()
36-
;
32+
public function success_string() {
33+
Assert::that(new Condition('true'))->mappedBy([$this, 'failures'])->isNull();
34+
}
35+
36+
#[Test]
37+
public function success_closure() {
38+
Assert::that(new Condition(fn() => true))->mappedBy([$this, 'failures'])->isNull();
3739
}
3840

3941
#[Test]
@@ -44,6 +46,14 @@ public function failure_includes_assertion_expression() {
4446
;
4547
}
4648

49+
#[Test]
50+
public function failure_includes_assertion_string() {
51+
Assert::that(new Condition(fn() => false))
52+
->mappedBy([$this, 'failures'])
53+
->isEqualTo('Failed verifying <function()>')
54+
;
55+
}
56+
4757
#[Test]
4858
public function failure_can_access_context_scope() {
4959
Assert::that(new Condition('self::verify()'))

0 commit comments

Comments
 (0)