Skip to content

Commit 7ce2c78

Browse files
committed
use assertThrown
1 parent dcb2f9a commit 7ce2c78

File tree

1 file changed

+14
-75
lines changed

1 file changed

+14
-75
lines changed

std/conv.d

Lines changed: 14 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,9 @@ unittest
518518
// Testing object conversions
519519
class A {} class B : A {} class C : A {}
520520
A a1 = new A, a2 = new B, a3 = new C;
521-
assert(to!(B)(a2) is a2);
522-
assert(to!(C)(a3) is a3);
523-
try
524-
{
525-
to!(B)(a3);
526-
assert(false);
527-
}
528-
catch (ConvException e)
529-
{
530-
//writeln(e);
531-
}
521+
assert(to!B(a2) is a2);
522+
assert(to!C(a3) is a3);
523+
assertThrown!ConvException(to!B(a3));
532524
}
533525

534526
/**
@@ -1712,17 +1704,10 @@ unittest
17121704
// boundary values
17131705
foreach (Int; TypeTuple!(byte, ubyte, short, ushort, int, uint))
17141706
{
1715-
try
1716-
{
1717-
assert(roundTo!Int(Int.min - 0.4L) == Int.min);
1718-
assert(roundTo!Int(Int.max + 0.4L) == Int.max);
1719-
}
1720-
catch (ConvOverflowException e)
1721-
{
1722-
assert(0);
1723-
}
1724-
try { roundTo!Int(Int.min - 0.5L); assert(0); } catch (ConvOverflowException e) {}
1725-
try { roundTo!Int(Int.max + 0.5L); assert(0); } catch (ConvOverflowException e) {}
1707+
assert(roundTo!Int(Int.min - 0.4L) == Int.min);
1708+
assert(roundTo!Int(Int.max + 0.4L) == Int.max);
1709+
assertThrown!ConvOverflowException(roundTo!Int(Int.min - 0.5L));
1710+
assertThrown!ConvOverflowException(roundTo!Int(Int.max + 0.5L));
17261711
}
17271712
}
17281713

@@ -2500,14 +2485,7 @@ unittest
25002485
assert(to!F("y"w) == F.y);
25012486
assert(to!F("z"d) == F.z);
25022487

2503-
try
2504-
{
2505-
to!E("d");
2506-
assert(0);
2507-
}
2508-
catch (ConvException e)
2509-
{
2510-
}
2488+
assertThrown!ConvException(to!E("d"));
25112489
}
25122490

25132491
version (none) // TODO: BUG4744
@@ -2873,16 +2851,7 @@ unittest
28732851
assert(to!string(f) == to!string(float.nan));
28742852
assert(isnan(f));
28752853

2876-
bool ok = false;
2877-
try
2878-
{
2879-
to!float("\x00");
2880-
}
2881-
catch (ConvException e)
2882-
{
2883-
ok = true;
2884-
}
2885-
assert(ok);
2854+
assertThrown!ConvException(to!float("\x00"));
28862855
}
28872856

28882857
/*
@@ -2936,16 +2905,7 @@ unittest
29362905
assert(to!string(d) == to!string(double.nan));
29372906
assert(isnan(d));
29382907

2939-
bool ok = false;
2940-
try
2941-
{
2942-
to!double("\x00");
2943-
}
2944-
catch (ConvException e)
2945-
{
2946-
ok = true;
2947-
}
2948-
assert(ok);
2908+
assertThrown!ConvException(to!double("\x00"));
29492909
}
29502910

29512911
/*
@@ -3004,16 +2964,7 @@ unittest
30042964
assert(to!string(r) == to!string(real.nan));
30052965
assert(isnan(r));
30062966

3007-
bool ok = false;
3008-
try
3009-
{
3010-
to!real("\x00");
3011-
}
3012-
catch (ConvException e)
3013-
{
3014-
ok = true;
3015-
}
3016-
assert(ok);
2967+
assertThrown!ConvException(to!real("\x00"));
30172968
}
30182969

30192970
unittest
@@ -3153,12 +3104,7 @@ unittest
31533104

31543105
assert (to!bool("TruE") == true);
31553106
assert (to!bool("faLse"d) == false);
3156-
try
3157-
{
3158-
to!bool("maybe");
3159-
assert (false);
3160-
}
3161-
catch (ConvException e) { }
3107+
assertThrown!ConvException(to!bool("maybe"));
31623108

31633109
auto t = "TrueType";
31643110
assert (parse!bool(t) == true);
@@ -3169,15 +3115,8 @@ unittest
31693115
assert (f == " killer whale"d);
31703116

31713117
auto m = "maybe";
3172-
try
3173-
{
3174-
parse!bool(m);
3175-
assert (false);
3176-
}
3177-
catch (ConvException e)
3178-
{
3179-
assert (m == "maybe"); // m shouldn't change on failure
3180-
}
3118+
assertThrown!ConvException(parse!bool(m));
3119+
assert (m == "maybe"); // m shouldn't change on failure
31813120

31823121
auto b = parse!(const(bool))("true");
31833122
assert(b == true);

0 commit comments

Comments
 (0)