| 1 | /** \file |
| 2 | * \brief Tests for the OGDF_ASSERT macro |
| 3 | * |
| 4 | * \author Stephan Beyer |
| 5 | * |
| 6 | * \par License: |
| 7 | * This file is part of the Open Graph Drawing Framework (OGDF). |
| 8 | * |
| 9 | * \par |
| 10 | * Copyright (C)<br> |
| 11 | * See README.md in the OGDF root directory for details. |
| 12 | * |
| 13 | * \par |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * Version 2 or 3 as published by the Free Software Foundation; |
| 17 | * see the file LICENSE.txt included in the packaging of this file |
| 18 | * for details. |
| 19 | * |
| 20 | * \par |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * \par |
| 27 | * You should have received a copy of the GNU General Public |
| 28 | * License along with this program; if not, see |
| 29 | * http://www.gnu.org/copyleft/gpl.html |
| 30 | */ |
| 31 | |
| 32 | #include <ogdf/basic/basic.h> |
| 33 | #include <testing.h> |
| 34 | |
| 35 | static void assert_positive(int); |
| 36 | |
| 37 | go_bandit([] { |
| 38 | describe("OGDF_ASSERT" , [] { |
| 39 | it("does not fail if the condition holds" , [] { |
| 40 | assert_positive(1); |
| 41 | }); |
| 42 | |
| 43 | #ifndef OGDF_DEBUG |
| 44 | it("does not fail if OGDF_DEBUG is not set" , [] { |
| 45 | assert_positive(-1); |
| 46 | }); |
| 47 | #endif |
| 48 | |
| 49 | #ifdef OGDF_USE_ASSERT_EXCEPTIONS |
| 50 | it("throws an AssertionFailed exception if the condition does not hold" , [] { |
| 51 | AssertThrows(AssertionFailed, assert_positive(-1)); |
| 52 | }); |
| 53 | |
| 54 | it("throws an exception with an explanatory what()" , [] { |
| 55 | int fails = false; |
| 56 | try { |
| 57 | assert_positive(-1); |
| 58 | } catch (AssertionFailed &e) { |
| 59 | fails = true; |
| 60 | std::string what(e.what()); |
| 61 | AssertThat(what, Contains("a > 0" )); |
| 62 | AssertThat(what, Contains("fail" )); |
| 63 | AssertThat(what, Contains(__FILE__ ":1000" )); |
| 64 | AssertThat(what, Contains("assert_positive" )); |
| 65 | } |
| 66 | AssertThat(fails, IsTrue()); |
| 67 | }); |
| 68 | #endif |
| 69 | |
| 70 | #ifdef OGDF_USE_ASSERT_EXCEPTIONS_WITH_STACKTRACE |
| 71 | it("throws an exception with a stack trace in what()" , [] { |
| 72 | int fails = false; |
| 73 | try { |
| 74 | assert_positive(-1); |
| 75 | } catch (AssertionFailed &e) { |
| 76 | fails = true; |
| 77 | AssertThat(std::string(e.what()), Contains("Stack trace" )); |
| 78 | } |
| 79 | AssertThat(fails, IsTrue()); |
| 80 | }); |
| 81 | #endif |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | static void assert_positive(int a) |
| 86 | { |
| 87 | #line 1000 |
| 88 | OGDF_ASSERT(a > 0); |
| 89 | } |
| 90 | |