Skip to content

Commit

Permalink
Merge pull request #2288 from Dicebot/enforce-unify
Browse files Browse the repository at this point in the history
Merge enforceEx with enforce
  • Loading branch information
dnadlinger committed Jul 10, 2014
2 parents dc97d14 + 815c8e3 commit 41421af
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions std/exception.d
Expand Up @@ -10,7 +10,7 @@
{
FILE* f = enforce(fopen("some/file"));
// f is not null from here on
FILE* g = enforceEx!WriteException(fopen("some/other/file", "w"));
FILE* g = enforce!WriteException(fopen("some/other/file", "w"));
// g is not null from here on
Exception e = collectException(write(g, readln(f)));
Expand Down Expand Up @@ -90,27 +90,27 @@ void assertNotThrown(T : Throwable = Exception, E)
///
unittest
{
assertNotThrown!StringException(enforceEx!StringException(true, "Error!"));
assertNotThrown!StringException(enforce!StringException(true, "Error!"));

//Exception is the default.
assertNotThrown(enforceEx!StringException(true, "Error!"));
assertNotThrown(enforce!StringException(true, "Error!"));

assert(collectExceptionMsg!AssertError(assertNotThrown!StringException(
enforceEx!StringException(false, "Error!"))) ==
enforce!StringException(false, "Error!"))) ==
`assertNotThrown failed: StringException was thrown: Error!`);
}
unittest
{
assert(collectExceptionMsg!AssertError(assertNotThrown!StringException(
enforceEx!StringException(false, ""), "Error!")) ==
enforce!StringException(false, ""), "Error!")) ==
`assertNotThrown failed: StringException was thrown: Error!`);

assert(collectExceptionMsg!AssertError(assertNotThrown!StringException(
enforceEx!StringException(false, ""))) ==
enforce!StringException(false, ""))) ==
`assertNotThrown failed: StringException was thrown.`);

assert(collectExceptionMsg!AssertError(assertNotThrown!StringException(
enforceEx!StringException(false, ""), "")) ==
enforce!StringException(false, ""), "")) ==
`assertNotThrown failed: StringException was thrown.`);
}

Expand Down Expand Up @@ -225,13 +225,13 @@ void assertThrown(T : Throwable = Exception, E)
///
unittest
{
assertThrown!StringException(enforceEx!StringException(false, "Error!"));
assertThrown!StringException(enforce!StringException(false, "Error!"));

//Exception is the default.
assertThrown(enforceEx!StringException(false, "Error!"));
assertThrown(enforce!StringException(false, "Error!"));

assert(collectExceptionMsg!AssertError(assertThrown!StringException(
enforceEx!StringException(true, "Error!"))) ==
enforce!StringException(true, "Error!"))) ==
`assertThrown failed: No StringException was thrown.`);
}

Expand Down Expand Up @@ -330,10 +330,10 @@ unittest
enforce(line.length, "Expected a non-empty line.");
--------------------
+/
T enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__)
T enforce(E : Throwable = Exception, T)(T value, lazy const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__)
if (is(typeof({ if (!value) {} })))
{
if (!value) bailOut(file, line, msg);
if (!value) bailOut!E(file, line, msg);
return value;
}

Expand Down Expand Up @@ -367,9 +367,21 @@ T enforce(T, Dg, string file = __FILE__, size_t line = __LINE__)
return value;
}

private void bailOut(string file, size_t line, in char[] msg) @safe pure
private void bailOut(E : Throwable = Exception)(string file, size_t line, in char[] msg)
{
throw new Exception(msg.ptr ? msg.idup : "Enforcement failed", file, line);
static if (is(typeof(new E(string.init, string.init, size_t.init))))
{
throw new E(msg.ptr ? msg.idup : "Enforcement failed", file, line);
}
else static if (is(typeof(new E(string.init, size_t.init))))
{
throw new E(file, line);
}
else
{
static assert("Expected this(string, string, size_t) or this(string, size_t)" ~
" constructor for " ~ __traits(identifier, E));
}
}

unittest
Expand Down Expand Up @@ -461,8 +473,8 @@ unittest
{
this(string msg) { super(msg, __FILE__, __LINE__); }
}
enforceEx!E1(s);
enforceEx!E2(s);
enforce!E1(s);
enforce!E2(s);
}

deprecated unittest
Expand Down Expand Up @@ -530,6 +542,8 @@ T errnoEnforce(T, string file = __FILE__, size_t line = __LINE__)
and can be constructed with $(D new E(file, line)), then
$(D new E(file, line)) will be thrown.
This is legacy name, it is recommended to use $(D enforce!E) instead.
Example:
--------------------
auto f = enforceEx!FileMissingException(fopen("data.txt"));
Expand Down

0 comments on commit 41421af

Please sign in to comment.