Arbutils/tests/EnsureTests.cpp

67 lines
2.4 KiB
C++
Raw Normal View History

2022-02-05 12:37:47 +00:00
#include <doctest.h>
#include "../src/Ensure.hpp"
void TestWrapper(bool wrapperExpression) { Ensure(wrapperExpression) }
void TestWrapperNotNull(void* value) { EnsureNotNull(value) }
void TestWrapperEquals(i32 a, i32 b) { EnsureEquals(a, b) }
void TestWrapperGreater(i32 a, i32 b) { EnsureGreater(a, b) }
void TestWrapperGreaterEquals(i32 a, i32 b) { EnsureGreaterOrEquals(a, b) }
void TestWrapperLess(i32 a, i32 b) { EnsureLess(a, b) }
void TestWrapperLessEquals(i32 a, i32 b){EnsureLessOrEquals(a, b)}
TEST_CASE("Ensure succeeds if true") {
REQUIRE_NOTHROW(TestWrapper(true));
}
TEST_CASE("Ensure throws if false") { REQUIRE_THROWS(TestWrapper(false)); }
TEST_CASE("Ensure throws if false with message") {
try {
TestWrapper(false);
} catch (const ArbUt::Exception& e) {
REQUIRE(std::string(e.what()) == "[EnsureTests.cpp:3] ENSURE FAILED: \"wrapperExpression\"");
return;
}
throw ArbUt::Exception("Didn't throw.");
}
TEST_CASE("Ensure equals true") { REQUIRE_NOTHROW(TestWrapperEquals(10, 10)); }
TEST_CASE("Ensure equals throws if false") { REQUIRE_THROWS(TestWrapperEquals(5, 10)); }
TEST_CASE("Ensure greater true") { REQUIRE_NOTHROW(TestWrapperGreater(10, 5)); }
TEST_CASE("Ensure greater throws if less than") { REQUIRE_THROWS(TestWrapperGreater(5, 10)); }
TEST_CASE("Ensure greater throws if equals") { REQUIRE_THROWS(TestWrapperGreater(10, 10)); }
TEST_CASE("Ensure greater or equals true") {
REQUIRE_NOTHROW(TestWrapperGreaterEquals(10, 5));
REQUIRE_NOTHROW(TestWrapperGreaterEquals(10, 10));
}
TEST_CASE("Ensure greater or equals throws if less than") { REQUIRE_THROWS(TestWrapperGreaterEquals(5, 10)); }
TEST_CASE("Ensure less true") { REQUIRE_NOTHROW(TestWrapperLess(5, 10)); }
TEST_CASE("Ensure less throws if less than") { REQUIRE_THROWS(TestWrapperLess(10, 5)); }
TEST_CASE("Ensure less throws if equals") { REQUIRE_THROWS(TestWrapperLess(10, 10)); }
TEST_CASE("Ensure less or equals true") {
REQUIRE_NOTHROW(TestWrapperLessEquals(5, 10));
REQUIRE_NOTHROW(TestWrapperLessEquals(10, 10));
}
TEST_CASE("Ensure less or equals throws if less than") { REQUIRE_THROWS(TestWrapperLessEquals(10, 5)); }
TEST_CASE("Multiple asserts"){Ensure(true) Ensure(true) Ensure(true)}
TEST_CASE("EnsureNotNull throws if nullptr") {
REQUIRE_THROWS(TestWrapperNotNull(nullptr));
}
TEST_CASE("Ensure for each") {
auto i = {10, 500, 2300, 454};
EnsureForEach(i, item > 0)
}