Browse Source

Add basic tests structure

- Intergrate TDD environment based on gtest
- Add simple test as example
- Add test build rules
Alexey Edelev 6 years ago
parent
commit
0e6b75497a

+ 17 - 0
CMakeLists.txt

@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8)
 
 project(qtprotobuf)
 
+set(testgeneration "test_generation")
+
 find_package(Protobuf)
 
 add_subdirectory("src/lib")
@@ -22,5 +24,20 @@ elseif (UNIX)
     target_link_libraries(${PROJECT_NAME} ${Protobuf_LIBRARIES} "-lprotoc")
 endif()
 
+if ($ENV{MAKE_TESTS})
+    set(TESTS_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/tests)
+    file(MAKE_DIRECTORY ${TESTS_OUT_DIR})
+
+    add_custom_target(${testgeneration})
+    add_custom_command(TARGET ${testgeneration} POST_BUILD
+        COMMAND protoc --plugin=protoc-gen-${PROJECT_NAME}=${CMAKE_BINARY_DIR}/qtprotobuf --qtprotobuf_out=${CMAKE_CURRENT_BINARY_DIR}/tests
+        simpletest.proto
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests/proto/
+        COMMENT "Generating test headers"
+    )
+    add_dependencies(${testgeneration} ${PROJECT_NAME})
+    add_subdirectory("tests")
+endif()
+
 install(TARGETS ${PROJECT_NAME}
     RUNTIME DESTINATION bin)

+ 39 - 4
src/generator/generator.cpp

@@ -80,7 +80,6 @@ public:
         printPublic();
         printFieldsOrderingDefinition();
         encloseClass();
-        printFieldsOrdering();
         enclose();
     }
 
@@ -94,6 +93,32 @@ public:
         Outdent();
     }
 
+
+};
+
+class QtSourcesGenerator : public ClassGeneratorBase
+{
+    std::string mPackage;
+    const Descriptor* mMessage;
+    std::set<std::string> mExtractedModels;
+public:
+    QtSourcesGenerator(const std::string &package, const Descriptor *message, std::unique_ptr<io::ZeroCopyOutputStream> out) :
+        ClassGeneratorBase(message->name(), std::move(out))
+      , mPackage(std::move(package))
+      , mMessage(message){}
+
+    void run() {
+        printClassHeaderInclude();
+        printNamespaces(mPackage);
+        printFieldsOrdering();
+        enclose();
+    }
+
+    void printClassHeaderInclude() {
+        std::string includeFileName = mClassName;
+        std::transform(std::begin(includeFileName), std::end(includeFileName), std::begin(includeFileName), ::tolower);
+        mPrinter.Print({{"type_lower", includeFileName}}, InternalIncludeTemplate);
+    }
     void printFieldsOrdering() {
         mPrinter.Print({{"type", mClassName}}, FieldsOrderingContainerTemplate);
         Indent();
@@ -102,8 +127,10 @@ public:
             if (i != 0) {
                 mPrinter.Print("\n,");
             }
+            //property_number is incremented by 1 because user properties stating from 1.
+            //Property with index 0 is "objectName"
             mPrinter.Print({{"field_number", std::to_string(field->number())},
-                            {"property_number", std::to_string(i)}}, FieldOrderTemplate);
+                            {"property_number", std::to_string(i + 1)}}, FieldOrderTemplate);
         }
         Outdent();
         mPrinter.Print(SemicolonBlockEnclosureTemplate);
@@ -144,12 +171,20 @@ bool QtGenerator::Generate(const FileDescriptor *file,
 
     for(int i = 0; i < file->message_type_count(); i++) {
         const Descriptor *message = file->message_type(i);
-        std::string filename = message->name() + ".h";
-        std::transform(std::begin(filename), std::end(filename), std::begin(filename), ::tolower);
+        std::string baseFilename(message->name());
+        std::transform(std::begin(baseFilename), std::end(baseFilename), std::begin(baseFilename), ::tolower);
+
+        std::string filename = baseFilename + ".h";
         QtClassGenerator classGen(file->package(), message,
                                   std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename))));
         classGen.run();
         extractedModels.insert(std::begin(classGen.extractedModels()), std::end(classGen.extractedModels()));
+
+        std::string sourceFileName = baseFilename + ".cpp";
+        QtSourcesGenerator classSourceGen(file->package(), message,
+                                  std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(sourceFileName))));
+        classSourceGen.run();
+
     }
 
     std::string globalEnumsFilename = "globalenums.h";

+ 1 - 1
src/lib/CMakeLists.txt

@@ -8,7 +8,7 @@ set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 if (Qt5_POSITION_INDEPENDENT_CODE)
-  SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
+  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 endif()
 
 add_library(qtprotobufsupport "universallistmodelbase.cpp" "universallistmodel.cpp" "protobufobject.cpp")

+ 31 - 0
tests/CMakeLists.txt

@@ -0,0 +1,31 @@
+set(testtarget "qtprotobuf_test")
+
+find_package(GTest REQUIRED)
+find_package(Qt5 COMPONENTS Core REQUIRED)
+
+include_directories(${Qt5Core_INCLUDE_DIRS})
+set(CMAKE_INCLUDE_CURRENT_DIR OFF)
+set(CMAKE_AUTOMOC OFF)
+set(CMAKE_AUTORCC ON)
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+if (Qt5_POSITION_INDEPENDENT_CODE)
+  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+endif()
+
+file(GLOB GENERATED_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/*.h)
+file(GLOB GENERATED_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/*.cpp)
+
+#Manual moc_ files generation for qt
+list(FILTER GENERATED_SOURCES EXCLUDE REGEX "moc_.*cpp")
+QT5_WRAP_CPP(MOC_SRCS ${GENERATED_HEADERS})
+
+include_directories(${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
+
+
+file(GLOB HEADERS ${TESTS_OUT_DIR})
+
+add_executable(${testtarget} "main.cpp" "simpletest.cpp" ${MOC_SRCS} ${GENERATED_SOURCES})
+target_link_libraries(${testtarget} ${GTEST_BOTH_LIBRARIES} Qt5::Core)
+add_dependencies(${testtarget} ${testgeneration})

+ 32 - 0
tests/main.cpp

@@ -0,0 +1,32 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
+ *
+ * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <gtest/gtest.h>
+
+int main(int argc, char *argv[])
+{
+    ::testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}

+ 7 - 0
tests/proto/simpletest.proto

@@ -0,0 +1,7 @@
+syntax = "proto3";
+
+package qtprotobuf.tests;
+
+message SimpleIntMessage {
+    int32 testField = 6;
+}

+ 44 - 0
tests/simpletest.cpp

@@ -0,0 +1,44 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
+ *
+ * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "simpletest.h"
+
+#include "simpleintmessage.h"
+#include <QMetaProperty>
+
+using namespace qtprotobuf::tests;
+
+SimpleTest::SimpleTest()
+{
+}
+
+TEST_F(SimpleTest, SimpleIntMessageTest)
+{
+    SimpleIntMessage test;
+    test.setTestField(1);
+    ASSERT_EQ(test.testField(), 1);
+    int propertyNumber = SimpleIntMessage::propertyOrdering.at(6);
+    ASSERT_STREQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).name(), "testField");
+}

+ 40 - 0
tests/simpletest.h

@@ -0,0 +1,40 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
+ *
+ * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <gtest/gtest.h>
+
+namespace qtprotobuf {
+namespace tests {
+
+class SimpleTest : public ::testing::Test
+{
+public:
+    SimpleTest();
+};
+
+}
+}