Adding unit tests.

This commit is contained in:
2023-11-01 23:52:36 -05:00
parent c4032b4a84
commit f573ff8366
14 changed files with 158 additions and 17 deletions

View File

@ -6,4 +6,10 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
assert.cpp
)
target_sources(dawntests
PRIVATE
assert.cpp
_test_.assert.cpp
)

View File

@ -0,0 +1,77 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include <catch2/catch_test_macros.hpp>
#include "assert.hpp"
TEST_CASE("assertTrue", "[assert]") {
SECTION("true") {
assertTrue(true, "This should not fail");
}
SECTION("false") {
REQUIRE_THROWS(assertTrue(false, "This should fail"));
}
}
TEST_CASE("assertFalse", "[assert]") {
SECTION("true") {
REQUIRE_THROWS(assertFalse(true, "This should fail"));
}
SECTION("false") {
assertFalse(false, "This should not fail");
}
}
TEST_CASE("assertUnreachable", "[assert]") {
REQUIRE_THROWS(assertUnreachable("This should fail all the time."));
}
TEST_CASE("assertNotNull", "[assert]") {
SECTION("nullptr") {
REQUIRE_THROWS(assertNotNull(nullptr, "This should fail"));
}
SECTION("NULL") {
REQUIRE_THROWS(assertNotNull(NULL, "This should fail"));
}
SECTION("not nullptr or NULL") {
assertNotNull((void*)0x1, "This should not fail");
}
}
TEST_CASE("assertNull", "[assert]") {
SECTION("nullptr") {
assertNull(nullptr, "This should not fail");
}
SECTION("NULL") {
assertNull(NULL, "This should not fail");
}
SECTION("not nullptr or NULL") {
REQUIRE_THROWS(assertNull((void*)0x1, "This should fail"));
}
}
TEST_CASE("assertMapHasKey", "[assert]") {
SECTION("has key") {
std::map<std::string, std::string> map;
map["key"] = "value";
assertMapHasKey(map, "key", "This should not fail");
}
SECTION("does not have key") {
std::map<std::string, std::string> map;
map["different_key"] = "Some other value";
REQUIRE_THROWS(assertMapHasKey(map, "key", "This should fail"));
}
}
TEST_CASE("assertDeprecated", "[assert]") {
REQUIRE_THROWS(assertDeprecated("This should fail"));
}

View File

@ -31,5 +31,5 @@ void assertTrueImplement(
vfprintf(stderr, message, argptr);
va_end(argptr);
fprintf(stderr, "\n");
abort();
throw std::runtime_error("Assert failed.");
}