Skip to content

Commit 131ddee

Browse files
committed
Added expect and expectErr override variants accepting custom handler
1 parent 49cf20f commit 131ddee

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

source/expected.d

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,8 +1449,9 @@ unittest
14491449
If there is none, or error value, it throws $(D assert(0)) with the provided message.
14501450
14511451
Params:
1452-
res = $(LREF Expected) to check the result of
1453-
msg = message to use with assert
1452+
res = $(LREF Expected) to check the result of
1453+
msg = message to use with assert
1454+
handler = custom handler to be called on error
14541455
+/
14551456
T expect(EX : Expected!(T, E, H), T, E, H)(auto ref EX res, lazy string msg)
14561457
{
@@ -1468,6 +1469,21 @@ T expect(EX : Expected!(T, E, H), T, E, H)(auto ref EX res, lazy string msg)
14681469
}
14691470
}
14701471

1472+
/// ditto
1473+
T expect(alias handler, EX : Expected!(T, E, H), T, E, H)(auto ref EX res)
1474+
{
1475+
static if (!is(T == void)) { if (res.hasValue) return res.value; }
1476+
else { if (!res.hasError) return; }
1477+
1478+
static if (!is(typeof(handler(res.error)) == void))
1479+
return handler(res.error);
1480+
else
1481+
{
1482+
handler(res.error);
1483+
return T.init;
1484+
}
1485+
}
1486+
14711487
version (D_Exceptions)
14721488
{
14731489
///
@@ -1487,6 +1503,7 @@ version (D_Exceptions)
14871503
Params:
14881504
res = $(LREF Expected) to check the result of
14891505
msg = message to use with assert
1506+
handler = custom handler to be called on value
14901507
+/
14911508
E expectErr(EX : Expected!(T, E, H), T, E, H)(auto ref EX res, lazy string msg)
14921509
{
@@ -1506,17 +1523,43 @@ E expectErr(EX : Expected!(T, E, H), T, E, H)(auto ref EX res, lazy string msg)
15061523
}
15071524
}
15081525

1509-
version (D_Exceptions)
1526+
/// ditto
1527+
E expectErr(alias handler, EX : Expected!(T, E, H), T, E, H)(auto ref EX res)
15101528
{
1511-
///
1512-
@("expectErr")
1513-
@system unittest
1529+
if (res.hasError) return res.error;
1530+
1531+
static if (!is(typeof(handler(T.init)) == void))
1532+
{
1533+
static if (!is(T == void)) return handler(res.hasValue ? res.value : T.init);
1534+
else return handler();
1535+
}
1536+
else
1537+
{
1538+
static if (!is(T == void)) handler(res.hasValue ? res.value : T.init);
1539+
else handler();
1540+
return T.init;
1541+
}
1542+
}
1543+
1544+
///
1545+
@("expectErr")
1546+
@system unittest
1547+
{
1548+
assert(err("foo").expectErr("oops") == "foo");
1549+
version (D_Exceptions)
15141550
{
1515-
assert(err("foo").expectErr("oops") == "foo");
15161551
assert(collectExceptionMsg!Throwable(Expected!int.init.expectErr("oops")) == "oops: empty");
15171552
assert(collectExceptionMsg!Throwable(ok(42).expectErr("oops")) == "oops: 42");
15181553
assert(collectExceptionMsg!Throwable(ok().expectErr("oops")) == "oops: empty"); // void value
15191554
}
1555+
1556+
assert(ok("foo").expect!(a => "bar") == "foo");
1557+
assert(err!string("foo").expect!(a => "bar") == "bar");
1558+
assert(err!string("foo").expect!((a) {}) is null);
1559+
1560+
assert(ok("foo").expectErr!(a => "bar") == "bar");
1561+
assert(err!string("foo").expectErr!(a => "bar") == "foo");
1562+
assert(ok!string("foo").expectErr!((a) {}) is null);
15201563
}
15211564

15221565
/++

0 commit comments

Comments
 (0)