-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
* `ocean.core.Verify` | ||
|
||
New module with a single function, `verify`, intended to serve as drop-in | ||
replacement for `assert` to comply to [Sociomantic assert/enforce | ||
policies](https://github.com/sociomantic-tsunami/sociomantic/blob/master/Code/assert-vs-enforce.rst) | ||
|
||
It works similar to `enforce` as it throws `Exception` instead of an `Error` | ||
and will remain even when built with `-release`. But it also uses specific | ||
pre-constructed `SanityException` type to indicate importance of the failure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/******************************************************************************* | ||
Utility intended as a replacement for `assert` to check for programming | ||
errors and sanity violations in situations when neither removing the check | ||
in -release mode nor bringing down the application by throwing an `Error` | ||
is acceptable. | ||
This module must have as few import dependencies as possible so that it can | ||
be used in place of `assert` freely without introducing cyclic imports. | ||
Copyright: Copyright (c) 2017 sociomantic labs GmbH. All rights reserved | ||
License: | ||
Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. | ||
Alternatively, this file may be distributed under the terms of the Tango | ||
3-Clause BSD License (see LICENSE_BSD.txt for details). | ||
*******************************************************************************/ | ||
|
||
module ocean.core.Verify; | ||
|
||
import ocean.meta.types.Qualifiers : istring; | ||
|
||
/******************************************************************************* | ||
Verifies that certain condition is met. | ||
Params: | ||
ok = boolean condition to check | ||
msg = optional exception message | ||
Throws: | ||
SanityException if `ok` condition is `false`. | ||
*******************************************************************************/ | ||
|
||
public void verify ( bool ok, lazy istring msg = "", | ||
istring file = __FILE__, int line = __LINE__ ) | ||
{ | ||
static SanityException exc; | ||
|
||
if (exc is null) | ||
exc = new SanityException(""); | ||
|
||
if (!ok) | ||
{ | ||
exc.file = file; | ||
exc.line = line; | ||
exc.msg = msg; | ||
|
||
throw exc; | ||
} | ||
} | ||
|
||
unittest | ||
{ | ||
try | ||
{ | ||
verify(false); | ||
} | ||
catch (SanityException e) { } | ||
|
||
verify(true); | ||
} | ||
|
||
/******************************************************************************* | ||
Indicates some internal sanity violation in the app, essentially a less | ||
fatal version of `AssertError`. | ||
*******************************************************************************/ | ||
|
||
public class SanityException : Exception | ||
{ | ||
public this ( istring msg, istring file = __FILE__, int line = __LINE__ ) | ||
{ | ||
super(msg, file, line); | ||
} | ||
} |