From 5ed6c7a21338924703afa55e2fe2927b15625c0d Mon Sep 17 00:00:00 2001 From: Mihails Strasuns Date: Wed, 16 Aug 2017 11:53:56 +0300 Subject: [PATCH] Implement `ocean.core.Verify` Fixes #213 --- relnotes/verify.feature.md | 9 +++++ src/ocean/core/Verify.d | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 relnotes/verify.feature.md create mode 100644 src/ocean/core/Verify.d diff --git a/relnotes/verify.feature.md b/relnotes/verify.feature.md new file mode 100644 index 000000000..6d6aedcbc --- /dev/null +++ b/relnotes/verify.feature.md @@ -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. diff --git a/src/ocean/core/Verify.d b/src/ocean/core/Verify.d new file mode 100644 index 000000000..3a6dac6bb --- /dev/null +++ b/src/ocean/core/Verify.d @@ -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); + } +}