소스 검색

Update fix of non-notifiable operator=

- Replace signals emiting by setter
- Implement tests
- Add notification for movable object
TODO: Notification for moved property is called all
      the time event if it was 0 already
Alexey Edelev 5 년 전
부모
커밋
5ab6a3275f
3개의 변경된 파일42개의 추가작업 그리고 5개의 파일을 삭제
  1. 2 1
      cmake/QtProtobufCommon.cmake
  2. 3 4
      src/generator/templates.cpp
  3. 37 0
      tests/test_protobuf/simpletest.cpp

+ 2 - 1
cmake/QtProtobufCommon.cmake

@@ -47,6 +47,7 @@ function(add_test_target)
     cmake_parse_arguments(add_test_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
 
     find_package(Threads REQUIRED)
+    find_package(Qt5 COMPONENTS Test REQUIRED)
     ## test sources build
     # policy enables automoc for generated files
     if(${CMAKE_VERSION} VERSION_GREATER "3.10.0")
@@ -74,7 +75,7 @@ function(add_test_target)
         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
     endif()
     add_dependencies(${add_test_target_TARGET} ${QtProtobuf_GENERATED})
-    target_link_libraries(${add_test_target_TARGET} gtest_main gtest ${QtProtobuf_GENERATED} ${QTPROTOBUF_COMMON_NAMESPACE}::QtProtobuf ${QTPROTOBUF_COMMON_NAMESPACE}::QtGrpc Qt5::Core Qt5::Qml Qt5::Network ${CMAKE_THREAD_LIBS_INIT})
+    target_link_libraries(${add_test_target_TARGET} gtest_main gtest ${QtProtobuf_GENERATED} ${QTPROTOBUF_COMMON_NAMESPACE}::QtProtobuf ${QTPROTOBUF_COMMON_NAMESPACE}::QtGrpc Qt5::Core Qt5::Test Qt5::Qml Qt5::Network ${CMAKE_THREAD_LIBS_INIT})
 endfunction(add_test_target)
 
 function(add_target_qml)

+ 3 - 4
src/generator/templates.cpp

@@ -92,11 +92,10 @@ const char *Templates::CopyConstructorTemplate = "$classname$(const $classname$
 const char *Templates::MoveConstructorTemplate = "$classname$($classname$ &&other) : QObject() {\n";
 const char *Templates::DeletedCopyConstructorTemplate = "$classname$(const $classname$ &) = delete;\n";
 const char *Templates::DeletedMoveConstructorTemplate = "$classname$($classname$ &&) = delete;\n";
-const char *Templates::CopyFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n"
-                                           "$property_name$Changed();\n";
+const char *Templates::CopyFieldTemplate = "set$property_name_cap$(other.m_$property_name$);\n";
 const char *Templates::MoveComplexFieldTemplate = "m_$property_name$ = std::move(other.m_$property_name$);\n";
-const char *Templates::MoveFieldTemplate = "m_$property_name$ = std::exchange(other.m_$property_name$, 0);\n"
-                                           "$property_name$Changed();\n";
+const char *Templates::MoveFieldTemplate = "set$property_name_cap$(std::exchange(other.m_$property_name$, 0));\n"
+                                           "other.$property_name$Changed();\n";
 const char *Templates::EnumMoveFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
 const char *Templates::AssignmentOperatorTemplate = "$classname$ &operator =(const $classname$ &other) {\n";
 const char *Templates::AssignmentOperatorReturnTemplate = "return *this;\n";

+ 37 - 0
tests/test_protobuf/simpletest.cpp

@@ -68,6 +68,7 @@
 #include "globalenums.h"
 #include <QVariantList>
 #include <QMetaProperty>
+#include <QSignalSpy>
 
 #include <gtest/gtest.h>
 
@@ -743,5 +744,41 @@ TEST_F(SimpleTest, NullPointerMessageTest)
     ASSERT_TRUE(msg.testComplexField().testFieldString().isEmpty());
 }
 
+TEST_F(SimpleTest, AssignmentOperatorTest)
+{
+    const char *propertyName = "testFieldInt";
+    SimpleIntMessage test;
+    SimpleIntMessage test2{35};
+
+    QSignalSpy updateSpy(&test, &SimpleIntMessage::testFieldIntChanged);
+    test.setProperty(propertyName, QVariant::fromValue<int32>(15));
+    test.setTestFieldInt(25);
+    test = test2;
+    test = test;
+    test = test2;
+    ASSERT_EQ(test2.testFieldInt(), test.testFieldInt());
+    ASSERT_EQ(3, updateSpy.count());
+}
+
+TEST_F(SimpleTest, MoveOperatorTest)
+{
+    const char *propertyName = "testFieldInt";
+    SimpleIntMessage test;
+    SimpleIntMessage test2{35};
+
+    QSignalSpy updateSpy(&test, &SimpleIntMessage::testFieldIntChanged);
+    QSignalSpy movedUpdateSpy(&test2, &SimpleIntMessage::testFieldIntChanged);
+
+    SimpleIntMessage test3(std::move(test2));
+    test2.setTestFieldInt(35);
+
+    test.setProperty(propertyName, QVariant::fromValue<int32>(15));
+    test.setTestFieldInt(25);
+    test = std::move(test2);
+    ASSERT_EQ(35, test.testFieldInt());
+    ASSERT_EQ(0, test2.testFieldInt());
+    ASSERT_EQ(3, updateSpy.count());
+    ASSERT_EQ(3, movedUpdateSpy.count());
+}
 } // tests
 } // qtprotobuf