Browse Source

test: code coverage

Michele Caini 5 năm trước cách đây
mục cha
commit
bb43bb5508

+ 10 - 4
src/entt/signal/delegate.hpp

@@ -141,7 +141,9 @@ public:
      * @tparam Candidate Function or member to connect to the delegate.
      * @tparam Candidate Function or member to connect to the delegate.
      */
      */
     template<auto Candidate>
     template<auto Candidate>
-    delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
+    delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT
+        : delegate{}
+    {
         connect<Candidate>();
         connect<Candidate>();
     }
     }
 
 
@@ -153,7 +155,9 @@ public:
      * @param value_or_instance A valid object that fits the purpose.
      * @param value_or_instance A valid object that fits the purpose.
      */
      */
     template<auto Candidate, typename Type>
     template<auto Candidate, typename Type>
-    delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
+    delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT
+        : delegate{}
+    {
         connect<Candidate>(std::forward<Type>(value_or_instance));
         connect<Candidate>(std::forward<Type>(value_or_instance));
     }
     }
 
 
@@ -163,7 +167,9 @@ public:
      * @param function Function to connect to the delegate.
      * @param function Function to connect to the delegate.
      * @param payload User defined arbitrary data.
      * @param payload User defined arbitrary data.
      */
      */
-    delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
+    delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT
+        : delegate{}
+    {
         connect(function, payload);
         connect(function, payload);
     }
     }
 
 
@@ -288,7 +294,7 @@ public:
      * @return The value returned by the underlying function.
      * @return The value returned by the underlying function.
      */
      */
     Ret operator()(Args... args) const {
     Ret operator()(Args... args) const {
-        ENTT_ASSERT(fn);
+        ENTT_ASSERT(static_cast<bool>(*this));
         return fn(data, std::forward<Args>(args)...);
         return fn(data, std::forward<Args>(args)...);
     }
     }
 
 

+ 29 - 15
test/entt/signal/delegate.cpp

@@ -51,6 +51,9 @@ TEST(Delegate, Functionalities) {
     entt::delegate<int(int)> lf_del;
     entt::delegate<int(int)> lf_del;
     delegate_functor functor;
     delegate_functor functor;
 
 
+    ASSERT_DEATH(ff_del(42), "");
+    ASSERT_DEATH(mf_del(42), "");
+
     ASSERT_FALSE(ff_del);
     ASSERT_FALSE(ff_del);
     ASSERT_FALSE(mf_del);
     ASSERT_FALSE(mf_del);
     ASSERT_EQ(ff_del, mf_del);
     ASSERT_EQ(ff_del, mf_del);
@@ -369,21 +372,6 @@ TEST(Delegate, VoidVsNonVoidReturnType) {
     ASSERT_TRUE(cmember);
     ASSERT_TRUE(cmember);
 }
 }
 
 
-TEST(Delegate, TheLessTheBetter) {
-    entt::delegate<int(int, char)> delegate;
-    delegate_functor functor;
-
-    // int delegate_function(const int &);
-    delegate.connect<&delegate_function>();
-
-    ASSERT_EQ(delegate(3, 'c'), 9);
-
-    // int delegate_functor::operator()(int);
-    delegate.connect<&delegate_functor::operator()>(functor);
-
-    ASSERT_EQ(delegate(3, 'c'), 6);
-}
-
 TEST(Delegate, UnboundDataMember) {
 TEST(Delegate, UnboundDataMember) {
     entt::delegate<int(const delegate_functor &)> delegate;
     entt::delegate<int(const delegate_functor &)> delegate;
     delegate.connect<&delegate_functor::data_member>();
     delegate.connect<&delegate_functor::data_member>();
@@ -399,3 +387,29 @@ TEST(Delegate, UnboundMemberFunction) {
 
 
     ASSERT_EQ(delegate(&functor, 3), 6);
     ASSERT_EQ(delegate(&functor, 3), 6);
 }
 }
+
+TEST(Delegate, TheLessTheBetter) {
+    entt::delegate<int(int, char)> bound;
+    entt::delegate<int(delegate_functor &, int, char)> unbound;
+    delegate_functor functor;
+
+    // int delegate_function(const int &);
+    bound.connect<&delegate_function>();
+
+    ASSERT_EQ(bound(3, 'c'), 9);
+
+    // int delegate_functor::operator()(int);
+    bound.connect<&delegate_functor::operator()>(functor);
+
+    ASSERT_EQ(bound(3, 'c'), 6);
+
+    // int delegate_functor::operator()(int);
+    bound.connect<&delegate_functor::identity>(&functor);
+
+    ASSERT_EQ(bound(3, 'c'), 3);
+
+    // int delegate_functor::operator()(int);
+    unbound.connect<&delegate_functor::operator()>();
+
+    ASSERT_EQ(unbound(functor, 3, 'c'), 6);
+}