Skip to content

Commit

Permalink
DnApp/Unity/asserts: functions changed into macros
Browse files Browse the repository at this point in the history
+ showing proper files/lines in the test output on failed tests
- polluting global namespace

 ThrowTheSwitch/Unity#645 (comment)
  • Loading branch information
MacDada committed Dec 7, 2022
1 parent 9a85c8a commit f949666
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
22 changes: 12 additions & 10 deletions lib/DnAppUnity/src/DnApp/Unity/asserts.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

#include "DnApp/Unity/functions.h"

namespace DnApp::Unity {
template<typename ExpectedBaseClass, typename ObjectClass>
inline void TEST_ASSERT_INSTANCE_OF(const ObjectClass *object) {
TEST_ASSERT_TRUE(instance_of<ExpectedBaseClass>(object));
}
#define TEST_ASSERT_INSTANCE_OF(expectedClass, actualObject) \
UNITY_TEST_ASSERT( \
(DnApp::Unity::instance_of<expectedClass>(actualObject)), \
__LINE__, \
"Expected `" #actualObject "` to be an instance of `" #expectedClass "`" \
)

template<typename ExpectedBaseClass, typename ObjectClass>
inline void TEST_ASSERT_NOT_INSTANCE_OF(const ObjectClass *object) {
TEST_ASSERT_FALSE(instance_of<ExpectedBaseClass>(object));
}
}
#define TEST_ASSERT_NOT_INSTANCE_OF(expectedClass, actualObject) \
UNITY_TEST_ASSERT( \
!(DnApp::Unity::instance_of<expectedClass>(actualObject)), \
__LINE__, \
"Expected `" #actualObject "` not to an instance of `" #expectedClass "`" \
)
28 changes: 13 additions & 15 deletions test/native/DnAppUnity/test_asserts/assertsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "DnApp/Unity/functions.h"

using DnApp::Unity::instance_of;
using DnApp::Unity::TEST_ASSERT_INSTANCE_OF;
using DnApp::Unity::TEST_ASSERT_NOT_INSTANCE_OF;

class Parent {} parent;

Expand All @@ -21,44 +19,44 @@ void test_instance_of_itself() {
TEST_ASSERT_TRUE(instance_of<Child>(&child));
TEST_ASSERT_TRUE(instance_of<GrandChild>(&grandChild));

TEST_ASSERT_INSTANCE_OF<Parent>(&parent);
TEST_ASSERT_INSTANCE_OF<OtherParent>(&otherParent);
TEST_ASSERT_INSTANCE_OF<Child>(&child);
TEST_ASSERT_INSTANCE_OF<GrandChild>(&grandChild);
TEST_ASSERT_INSTANCE_OF(Parent, &parent);
TEST_ASSERT_INSTANCE_OF(OtherParent, &otherParent);
TEST_ASSERT_INSTANCE_OF(Child, &child);
TEST_ASSERT_INSTANCE_OF(GrandChild, &grandChild);
}

void test_child_is_instance_of_parents() {
TEST_ASSERT_TRUE(instance_of<Parent>(&child));
TEST_ASSERT_TRUE(instance_of<OtherParent>(&child));

TEST_ASSERT_INSTANCE_OF<Parent>(&child);
TEST_ASSERT_INSTANCE_OF<OtherParent>(&child);
TEST_ASSERT_INSTANCE_OF(Parent, &child);
TEST_ASSERT_INSTANCE_OF(OtherParent, &child);
}

void test_grandchild_is_instance_of_child_and_parents() {
TEST_ASSERT_TRUE(instance_of<Parent>(&grandChild));
TEST_ASSERT_TRUE(instance_of<OtherParent>(&grandChild));
TEST_ASSERT_TRUE(instance_of<Child>(&grandChild));

TEST_ASSERT_INSTANCE_OF<Parent>(&grandChild);
TEST_ASSERT_INSTANCE_OF<OtherParent>(&grandChild);
TEST_ASSERT_INSTANCE_OF<Child>(&grandChild);
TEST_ASSERT_INSTANCE_OF(Parent, &grandChild);
TEST_ASSERT_INSTANCE_OF(OtherParent, &grandChild);
TEST_ASSERT_INSTANCE_OF(Child, &grandChild);
}

void test_parent_is_not_instance_of_other_parent() {
TEST_ASSERT_FALSE(instance_of<Parent>(&otherParent));
TEST_ASSERT_FALSE(instance_of<OtherParent>(&parent));

TEST_ASSERT_NOT_INSTANCE_OF<Parent>(&otherParent);
TEST_ASSERT_NOT_INSTANCE_OF<OtherParent>(&parent);
TEST_ASSERT_NOT_INSTANCE_OF(Parent, &otherParent);
TEST_ASSERT_NOT_INSTANCE_OF(OtherParent, &parent);
}

void test_parent_is_not_instance_of_child_and_grandchild() {
TEST_ASSERT_FALSE(instance_of<Child>(&parent));
TEST_ASSERT_FALSE(instance_of<GrandChild>(&parent));

TEST_ASSERT_NOT_INSTANCE_OF<Child>(&parent);
TEST_ASSERT_NOT_INSTANCE_OF<GrandChild>(&parent);
TEST_ASSERT_NOT_INSTANCE_OF(Child, &parent);
TEST_ASSERT_NOT_INSTANCE_OF(GrandChild, &parent);
}

int main() {
Expand Down
2 changes: 1 addition & 1 deletion test/native/DnLogger/test_NullLogger/NullLoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using DnApp::Logger::NullLogger;
NullLogger logger;

void test_it_is_a_Logger() {
DnApp::Unity::TEST_ASSERT_INSTANCE_OF<Logger>(&logger);
TEST_ASSERT_INSTANCE_OF(Logger, &logger);
}

void test_logging_chars() {
Expand Down

0 comments on commit f949666

Please sign in to comment.