Arbutils/tests/EnsureTests.cpp

67 lines
2.4 KiB
C++

#include <doctest.h>
#include "../src/Ensure.hpp"
void TestWrapper(bool wrapperExpression) { Ensure(wrapperExpression) }
void TestWrapperNotNull(void* value) { EnsureNotNull(value) }
void TestWrapperEquals(int32_t a, int32_t b) { EnsureEquals(a, b) }
void TestWrapperGreater(int32_t a, int32_t b) { EnsureGreater(a, b) }
void TestWrapperGreaterEquals(int32_t a, int32_t b) { EnsureGreaterOrEquals(a, b) }
void TestWrapperLess(int32_t a, int32_t b) { EnsureLess(a, b) }
void TestWrapperLessEquals(int32_t a, int32_t 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)
}