Skip to content

Commit

Permalink
Implement ocean.core.Verify
Browse files Browse the repository at this point in the history
Fixes #213
  • Loading branch information
mihails-strasuns-sociomantic committed Aug 21, 2017
1 parent b087dae commit 5ed6c7a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions relnotes/verify.feature.md
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.
79 changes: 79 additions & 0 deletions src/ocean/core/Verify.d
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);
}
}

0 comments on commit 5ed6c7a

Please sign in to comment.