Add unit tests 33/161133/15
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 22 Nov 2017 01:18:21 +0000 (10:18 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 27 Nov 2017 03:10:39 +0000 (12:10 +0900)
- Adds a script for unit tests
+-----------------------------------------------------------+
| Usage: unit_tests.sh <command>                            |
| Commands:                                                 |
| - build               Build the unit tests                |
| - clean               Clean all artifacts                 |
| - run                 Run the unit tests                  |
| - full                Build & Run the unit tests          |
+-----------------------------------------------------------+

Change-Id: I2270356d7dd99aea07947b5a42fc8979c8ec823c
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
16 files changed:
CMakeLists.txt
packaging/tidl-tests.manifest [new file with mode: 0644]
packaging/tidl.spec
unit_tests/CMakeLists.txt [new file with mode: 0644]
unit_tests/attribute_unittest.cc [new file with mode: 0644]
unit_tests/block_unittest.cc [new file with mode: 0644]
unit_tests/declaration_unittest.cc [new file with mode: 0644]
unit_tests/document_unittest.cc [new file with mode: 0644]
unit_tests/generator_unittest.cc [new file with mode: 0644]
unit_tests/interface_unittest.cc [new file with mode: 0644]
unit_tests/main.cc [new file with mode: 0644]
unit_tests/proxy_gen_unittest.cc [new file with mode: 0644]
unit_tests/structure_unittest.cc [new file with mode: 0644]
unit_tests/stub_gen_unittest.cc [new file with mode: 0644]
unit_tests/type_unittest.cc [new file with mode: 0644]
unit_tests/unit_tests.sh [new file with mode: 0755]

index 424743f..ddd2498 100644 (file)
@@ -1,3 +1,4 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 
 ADD_SUBDIRECTORY(idlc)
+ADD_SUBDIRECTORY(unit_tests)
diff --git a/packaging/tidl-tests.manifest b/packaging/tidl-tests.manifest
new file mode 100644 (file)
index 0000000..97e8c31
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_"/>
+       </request>
+</manifest>
index 36d48fe..445f3d1 100644 (file)
@@ -6,6 +6,7 @@ Group:      Application Framework/Building
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
 Source1001: %{name}.manifest
+Source1002: %{name}-tests.manifest
 
 Requires(post):   /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
@@ -13,13 +14,22 @@ Requires(postun): /sbin/ldconfig
 BuildRequires:  cmake
 BuildRequires:  flex
 BuildRequires:  bison
+BuildRequires:  gtest-devel
 
 %description
 Tizen Interface Definition Language
 
+%package tests
+Summary:    Unit tests for TIDL
+Requires:   %{name} = %{version}
+
+%description tests
+Unit tests for TIDL
+
 %prep
 %setup -q
 cp %{SOURCE1001} .
+cp %{SOURCE1002} .
 
 %build
 MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
@@ -46,3 +56,6 @@ rm -rf %{buildroot}
 %manifest %{name}.manifest
 %{_bindir}/tidlc
 
+%files tests
+%manifest %{name}.manifest
+%{_bindir}/*
diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..6033e99
--- /dev/null
@@ -0,0 +1,61 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(TIDL_UNIT_TESTS CXX)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wno-unused-function -Wno-sign-compare")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs" )
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIE")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Werror")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -std=c++11")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}")
+
+FIND_PACKAGE(GTest REQUIRED)
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
+INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})
+
+SET(TIDLC_SOURCES
+       ../idlc/document.cc
+       ../idlc/declaration.cc
+       ../idlc/interface.cc
+       ../idlc/type.cc
+       ../idlc/parameter.cc
+       ../idlc/attribute.cc
+       ../idlc/structure.cc
+       ../idlc/block.cc
+       ../idlc/generator.cc
+       ../idlc/proxy_gen.cc
+       ../idlc/stub_gen.cc
+       )
+
+SET(UNIT_TESTS_SOURCES
+       document_unittest.cc
+       block_unittest.cc
+       type_unittest.cc
+       attribute_unittest.cc
+       declaration_unittest.cc
+       interface_unittest.cc
+       structure_unittest.cc
+       generator_unittest.cc
+       proxy_gen_unittest.cc
+       stub_gen_unittest.cc
+       main.cc
+       )
+
+ADD_EXECUTABLE(tidl-unit-tests
+       ${TIDLC_SOURCES}
+       ${UNIT_TESTS_SOURCES}
+       )
+
+TARGET_LINK_LIBRARIES(tidl-unit-tests ${GTEST_LIBRARIES} pthread)
+
+INSTALL(TARGETS tidl-unit-tests DESTINATION bin)
+
+SET(TIDL_UNIT_TESTS tidl-unit-tests)
+ADD_TEST(NAME ${TIDL_UNIT_TESTS} COMMAND ${TIDL_UNIT_TESTS})
+ADD_CUSTOM_COMMAND(
+       TARGET ${TIDL_UNIT_TESTS}
+       COMMENT "Run Unit Tests"
+       POST_BUILD
+       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+       COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/${TIDL_UNIT_TESTS})
diff --git a/unit_tests/attribute_unittest.cc b/unit_tests/attribute_unittest.cc
new file mode 100644 (file)
index 0000000..d4447d5
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/attribute.h"
+
+class AttributeTest : public testing::Test {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(AttributeTest, Attribute_Contstructor) {
+  tidl::Attribute* attr = new tidl::Attribute("test",
+      new tidl::BaseType("int"));
+  EXPECT_NE(attr, nullptr);
+  delete attr;
+}
+
+TEST_F(AttributeTest, Attribute_GetID) {
+  tidl::Attribute* attr = new tidl::Attribute("test",
+      new tidl::BaseType("int"));
+  EXPECT_NE(attr, nullptr);
+  EXPECT_EQ(attr->GetID(), "test");
+  delete attr;
+}
+
+TEST_F(AttributeTest, Attribute_GetType) {
+  tidl::Attribute* attr = new tidl::Attribute("test",
+      new tidl::BaseType("int"));
+  EXPECT_NE(attr, nullptr);
+  EXPECT_EQ(attr->GetType().ToString(), "int");
+  delete attr;
+}
+
+class AttributesTest : public testing::Test {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(AttributesTest, Attributes_Constructor) {
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  delete attrs;
+}
+
+TEST_F(AttributesTest, Attributes_Add) {
+  tidl::Attribute* attr = new tidl::Attribute("test",
+      new tidl::BaseType("int"));
+  EXPECT_NE(attr, nullptr);
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  attrs->Add(attr);
+
+  bool flag = false;
+  for (auto& attr : attrs->GetAttrs()) {
+    if (attr->GetID() == "test" &&
+        attr->GetType().ToString() == "int")
+      flag = true;
+  }
+  EXPECT_EQ(flag, true);
+  delete attrs;
+}
+
+TEST_F(AttributesTest, Attributes_GetAttrs) {
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  attrs->Add(new tidl::Attribute("test1", new tidl::BaseType("int")));
+  attrs->Add(new tidl::Attribute("test2", new tidl::BaseType("char *")));
+
+  int count = 0;
+  for (auto& attr : attrs->GetAttrs()) {
+    if (attr->GetID() == "test1" &&
+        attr->GetType().ToString() == "int")
+      count++;
+    if (attr->GetID() == "test2" &&
+        attr->GetType().ToString() == "char *")
+      count++;
+  }
+  EXPECT_EQ(count, 2);
+  delete attrs;
+}
diff --git a/unit_tests/block_unittest.cc b/unit_tests/block_unittest.cc
new file mode 100644 (file)
index 0000000..e6e31ab
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/block.h"
+
+class BlockTest : public testing::Test {
+ public:
+  tidl::Block* testBlock;
+
+  virtual void SetUp() {
+    testBlock = new tidl::Block("TestBlock", tidl::Block::TYPE_INTERFACE);
+  }
+  virtual void TearDown() {
+    delete testBlock;
+  }
+};
+
+TEST_F(BlockTest, Block_Constructor) {
+  tidl::Block* block = new tidl::Block("StructureBlock",
+      tidl::Block::TYPE_STRUCTURE);
+  EXPECT_NE(block, nullptr);
+  delete block;
+}
+
+TEST_F(BlockTest, Block_GetID) {
+  EXPECT_EQ(testBlock->GetID(), "TestBlock");
+}
+
+TEST_F(BlockTest, Block_GetType) {
+  EXPECT_EQ(testBlock->GetType(), tidl::Block::TYPE_INTERFACE);
+}
diff --git a/unit_tests/declaration_unittest.cc b/unit_tests/declaration_unittest.cc
new file mode 100644 (file)
index 0000000..f6d8ba7
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/declaration.h"
+#include "idlc/type.h"
+#include "idlc/parameter.h"
+
+class DeclarationTest : public testing::Test {
+ public:
+  tidl::Parameters* params;
+
+  virtual void SetUp() {
+    params = new tidl::Parameters();
+    EXPECT_NE(params, nullptr);
+    params->Add(new tidl::Parameter("test1",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+    params->Add(new tidl::Parameter("test2",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+  }
+  virtual void TearDown() {}
+};
+
+TEST_F(DeclarationTest, Declaration_Constructor) {
+  tidl::Declaration* decl = new tidl::Declaration("test",
+      new tidl::BaseType("int"), params);
+  EXPECT_NE(decl, nullptr);
+  delete decl;
+}
+
+TEST_F(DeclarationTest, Declaration_GetID) {
+  tidl::Declaration* decl = new tidl::Declaration("test",
+      new tidl::BaseType("int"), params);
+  EXPECT_NE(decl, nullptr);
+  EXPECT_EQ(decl->GetID(), "test");
+  delete decl;
+}
+
+TEST_F(DeclarationTest, Declaration_GetType) {
+  tidl::Declaration* decl = new tidl::Declaration("test",
+      new tidl::BaseType("int"), params);
+  EXPECT_NE(decl, nullptr);
+  EXPECT_EQ(decl->GetType().ToString(), "int");
+  delete decl;
+}
+
+TEST_F(DeclarationTest, Declaration_GetParameters) {
+  tidl::Declaration* decl = new tidl::Declaration("test",
+      new tidl::BaseType("int"), params);
+  EXPECT_NE(decl, nullptr);
+
+  int count = 0;
+  for (auto& param : decl->GetParameters().GetParams()) {
+    if (param->GetID() == "test1" &&
+        param->GetParameterType().GetBaseType().ToString() == "int")
+      count++;
+    if (param->GetID() == "test2" &&
+        param->GetParameterType().GetBaseType().ToString() == "int")
+      count++;
+  }
+  EXPECT_EQ(count, 2);
+
+  delete decl;
+}
+
+TEST_F(DeclarationTest, Declaration_IsAsync) {
+  tidl::Declaration* decl = new tidl::Declaration("test",
+      new tidl::BaseType("int"), params);
+  EXPECT_NE(decl, nullptr);
+  EXPECT_EQ(decl->IsAsync(), false);
+  delete decl;
+}
+
+class DeclarationsTest : public testing::Test {
+ public:
+  tidl::Declaration* decl;
+
+  virtual void SetUp() {}
+  virtual void TestDown() {}
+  void PrepareDeclaration() {
+    tidl::Parameters* params = new tidl::Parameters();
+    EXPECT_NE(params, nullptr);
+    params->Add(new tidl::Parameter("test1",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+    params->Add(new tidl::Parameter("test2",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+    decl = new tidl::Declaration("test",
+        new tidl::BaseType("int"), params);
+    EXPECT_NE(decl, nullptr);
+  }
+};
+
+TEST_F(DeclarationsTest, Declarations_Constructor) {
+  tidl::Declarations* decls = new tidl::Declarations();
+  EXPECT_NE(decls, nullptr);
+  delete decls;
+}
+
+TEST_F(DeclarationsTest, Declarations_Add) {
+  tidl::Declarations* decls = new tidl::Declarations();
+  EXPECT_NE(decls, nullptr);
+  PrepareDeclaration();
+  decls->Add(decl);
+
+  bool flag = false;
+  for (auto& d : decls->GetDecls()) {
+    if (d->GetID() == "test")
+      flag = true;
+  }
+  EXPECT_EQ(flag, true);
+  delete decls;
+}
+
+TEST_F(DeclarationsTest, Declarations_GetDecls) {
+  tidl::Declarations* decls = new tidl::Declarations();
+  EXPECT_NE(decls, nullptr);
+  PrepareDeclaration();
+  decls->Add(decl);
+
+  bool flag = false;
+  for (auto& d : decls->GetDecls()) {
+    if (d->GetID() == "test")
+      flag = true;
+  }
+  EXPECT_EQ(flag, true);
+  delete decls;
+}
diff --git a/unit_tests/document_unittest.cc b/unit_tests/document_unittest.cc
new file mode 100644 (file)
index 0000000..33f65f0
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/document.h"
+#include "idlc/block.h"
+
+class DocumentTest : public testing::Test {
+ public:
+  tidl::Block* block;
+  tidl::Document* document;
+
+  virtual void SetUp() {
+    document = new tidl::Document();
+    block = new tidl::Block("TestBlock", tidl::Block::TYPE_INTERFACE);
+  }
+  virtual void TearDown() {
+  }
+};
+
+TEST_F(DocumentTest, Document_Constructor) {
+  tidl::Document *doc = new tidl::Document();
+  EXPECT_NE(doc, nullptr);
+  delete doc;
+}
+
+TEST_F(DocumentTest, Document_AddBlock) {
+  document->AddBlock(block);
+  bool flag = false;
+  for (auto& blk : document->GetBlocks()) {
+    if (blk->GetID() == "TestBlock")
+      flag = true;
+  }
+  EXPECT_NE(flag, false);
+}
+
+TEST_F(DocumentTest, Document_GetBlocks) {
+  document->AddBlock(block);
+  bool flag = false;
+  for (auto& blk : document->GetBlocks()) {
+    if (blk->GetType() == tidl::Block::TYPE_INTERFACE)
+      flag = true;
+  }
+  EXPECT_NE(flag, false);
+}
diff --git a/unit_tests/generator_unittest.cc b/unit_tests/generator_unittest.cc
new file mode 100644 (file)
index 0000000..99754fa
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/generator.h"
+#include "idlc/interface.h"
+#include "idlc/structure.h"
+
+class SampleGenerator : public tidl::Generator {
+ public:
+  SampleGenerator(std::shared_ptr<tidl::Document> doc)
+      : tidl::Generator(doc), count_(0) {}
+  ~SampleGenerator() {}
+
+  void OnInterfaceBegin(const std::string& id) override {
+    interfaceID_ = id;
+    count_++;
+  }
+  void OnInterfaceEnd(const std::string& id) override {
+    count_++;
+  }
+  void OnDeclarationGen(std::ofstream& stream, const std::string& id,
+                        const tidl::Parameters& args,
+                        const tidl::BaseType& ret) override {
+    count_++;
+  }
+  void OnStructureBegin(std::ofstream& stream, const std::string& id) override {
+    structureID_ = id;
+    count_++;
+  }
+  void OnStructureEnd(std::ofstream& stream, const std::string& id) override {
+    count_++;
+  }
+  void OnAttributeGen(std::ofstream& stream, const std::string& id,
+                      const tidl::BaseType& type) override {
+    count_++;
+  }
+  void OnInitGen(std::ofstream& stream) override {
+    count_++;
+  }
+  void OnFiniGen(std::ofstream& stream) override {
+    count_++;
+  }
+
+  const int GetCount() const {
+    return count_;
+  }
+
+  const std::string& GetInterfaceID() const {
+    return interfaceID_;
+  }
+
+  const std::string& GetStructureID() const {
+    return structureID_;
+  }
+
+ private:
+  int count_;
+  std::string interfaceID_;
+  std::string structureID_;
+};
+
+class GeneratorTest : public testing::Test {
+ public:
+  tidl::Document* doc;
+
+  virtual void SetUp() {
+    tidl::Parameters* params = new tidl::Parameters();
+    EXPECT_NE(params, nullptr);
+    params->Add(new tidl::Parameter("test",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+
+    tidl::Declaration* decl = new tidl::Declaration("test",
+        new tidl::BaseType("int"), params);
+    EXPECT_NE(decl, nullptr);
+
+    tidl::Declarations* decls = new tidl::Declarations();
+    EXPECT_NE(decls, nullptr);
+    decls->Add(decl);
+
+    tidl::Attributes* attrs = new tidl::Attributes();
+    EXPECT_NE(attrs, nullptr);
+    attrs->Add(new tidl::Attribute("test", new tidl::BaseType("int")));
+
+    doc = new tidl::Document();
+    EXPECT_NE(doc, nullptr);
+    doc->AddBlock(new tidl::Interface("TestInterface", decls));
+    doc->AddBlock(new tidl::Structure("TestStructure", attrs));
+  }
+  virtual void TearDown() {}
+};
+
+TEST_F(GeneratorTest, Generator_Constructor) {
+  std::shared_ptr<tidl::Document> docPtr(doc);
+  SampleGenerator* sampleGen = new SampleGenerator(docPtr);
+  EXPECT_NE(sampleGen, nullptr);
+  delete sampleGen;
+}
+
+TEST_F(GeneratorTest, Generator_Run) {
+  std::shared_ptr<tidl::Document> docPtr(doc);
+  SampleGenerator* sampleGen = new SampleGenerator(docPtr);
+  EXPECT_NE(sampleGen, nullptr);
+  sampleGen->Run("test.txt");
+  EXPECT_EQ(sampleGen->GetCount(), 8);
+  EXPECT_EQ(sampleGen->GetInterfaceID(), "TestInterface");
+  EXPECT_EQ(sampleGen->GetStructureID(), "TestStructure");
+  delete sampleGen;
+}
diff --git a/unit_tests/interface_unittest.cc b/unit_tests/interface_unittest.cc
new file mode 100644 (file)
index 0000000..54d63ed
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/interface.h"
+
+class InterfaceTest : public testing::Test {
+ public:
+  tidl::Declarations* decls;
+
+  virtual void SetUp() {
+    tidl::Parameters* params = new tidl::Parameters();
+    EXPECT_NE(params, nullptr);
+    params->Add(new tidl::Parameter("test1",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+    params->Add(new tidl::Parameter("test2",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+    tidl::Declaration* decl = new tidl::Declaration("test",
+        new tidl::BaseType("int"), params);
+    EXPECT_NE(decl, nullptr);
+    decls = new tidl::Declarations();
+    EXPECT_NE(decls, nullptr);
+    decls->Add(decl);
+  }
+  virtual void TearDown() {}
+};
+
+TEST_F(InterfaceTest, Interface_Constructor) {
+  tidl::Interface* interface = new tidl::Interface("TestInterface", decls);
+  EXPECT_NE(interface, nullptr);
+  delete interface;
+}
+
+TEST_F(InterfaceTest, Interface_GetDeclrations) {
+  tidl::Interface* interface = new tidl::Interface("TestInterface", decls);
+  EXPECT_NE(interface, nullptr);
+
+  bool flag = false;
+  for (auto& d : interface->GetDeclarations().GetDecls()) {
+    if (d->GetID() == "test")
+      flag = true;
+  }
+  EXPECT_EQ(flag, true);
+  delete interface;
+}
+
+TEST_F(InterfaceTest, Interface_GetID) {
+  tidl::Interface* interface = new tidl::Interface("TestInterface", decls);
+  EXPECT_NE(interface, nullptr);
+  EXPECT_EQ(interface->GetID(), "TestInterface");
+  delete interface;
+}
+
+TEST_F(InterfaceTest, Interface_GetType) {
+  tidl::Interface* interface = new tidl::Interface("TestInterface", decls);
+  EXPECT_NE(interface, nullptr);
+  EXPECT_EQ(interface->GetType(), tidl::Interface::TYPE_INTERFACE);
+  delete interface;
+}
diff --git a/unit_tests/main.cc b/unit_tests/main.cc
new file mode 100644 (file)
index 0000000..b8dc0d5
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+int main(int argc, char** argv) {
+  testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}
diff --git a/unit_tests/proxy_gen_unittest.cc b/unit_tests/proxy_gen_unittest.cc
new file mode 100644 (file)
index 0000000..5e85421
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/proxy_gen.h"
+#include "idlc/interface.h"
+#include "idlc/structure.h"
+
+class ProxyGenTest : public testing::Test {
+ public:
+  tidl::Document* doc;
+
+  virtual void SetUp() {
+    tidl::Parameters* params = new tidl::Parameters();
+    EXPECT_NE(params, nullptr);
+    params->Add(new tidl::Parameter("test",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+
+    tidl::Declaration* decl = new tidl::Declaration("test",
+        new tidl::BaseType("int"), params);
+    EXPECT_NE(decl, nullptr);
+
+    tidl::Declarations* decls = new tidl::Declarations();
+    EXPECT_NE(decls, nullptr);
+    decls->Add(decl);
+
+    tidl::Attributes* attrs = new tidl::Attributes();
+    EXPECT_NE(attrs, nullptr);
+    attrs->Add(new tidl::Attribute("test", new tidl::BaseType("int")));
+
+    doc = new tidl::Document();
+    EXPECT_NE(doc, nullptr);
+    doc->AddBlock(new tidl::Interface("TestInterface", decls));
+    doc->AddBlock(new tidl::Structure("TestStructure", attrs));
+  }
+  virtual void TearDown() {}
+
+  bool IsExistence(const std::string& file) const {
+    if (std::ifstream(file))
+      return true;
+
+    return false;
+  }
+};
+
+TEST_F(ProxyGenTest, ProxyGen_Constructor) {
+  std::shared_ptr<tidl::Document> docPtr(doc);
+  tidl::ProxyGen* gen = new tidl::ProxyGen(docPtr);
+  EXPECT_NE(gen, nullptr);
+  delete gen;
+}
+
+TEST_F(ProxyGenTest, ProxyGen_Run) {
+  std::shared_ptr<tidl::Document> docPtr(doc);
+  tidl::ProxyGen* gen = new tidl::ProxyGen(docPtr);
+  EXPECT_NE(gen, nullptr);
+  gen->Run("ProxyGen.txt");
+  EXPECT_EQ(IsExistence("ProxyGen.txt"), true);
+  delete gen;
+}
diff --git a/unit_tests/structure_unittest.cc b/unit_tests/structure_unittest.cc
new file mode 100644 (file)
index 0000000..931575c
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/structure.h"
+
+class StructureTest : public testing::Test {
+ public:
+  tidl::Attributes* attrs;
+
+  virtual void SetUp() {
+    attrs = new tidl::Attributes();
+    EXPECT_NE(attrs, nullptr);
+    attrs->Add(new tidl::Attribute("test1", new tidl::BaseType("int")));
+    attrs->Add(new tidl::Attribute("test2", new tidl::BaseType("char *")));
+  }
+  virtual void TearDown() {}
+};
+
+TEST_F(StructureTest, Structure_Constructor) {
+  tidl::Structure* structure = new tidl::Structure("TestStructure", attrs);
+  EXPECT_NE(structure, nullptr);
+  delete structure;
+}
+
+TEST_F(StructureTest, Structure_GetAttributes) {
+  tidl::Structure* structure = new tidl::Structure("TestStructure", attrs);
+  EXPECT_NE(structure, nullptr);
+
+  int count = 0;
+  for (auto& attr : attrs->GetAttrs()) {
+    if (attr->GetID() == "test1" &&
+        attr->GetType().ToString() == "int")
+      count++;
+    if (attr->GetID() == "test2" &&
+        attr->GetType().ToString() == "char *")
+      count++;
+  }
+  EXPECT_EQ(count, 2);
+  delete structure;
+}
+
+TEST_F(StructureTest, Structure_GetID) {
+  tidl::Structure* structure = new tidl::Structure("TestStructure", attrs);
+  EXPECT_NE(structure, nullptr);
+  EXPECT_EQ(structure->GetID(), "TestStructure");
+  delete structure;
+}
+
+TEST_F(StructureTest, Structure_GetType) {
+  tidl::Structure* structure = new tidl::Structure("TestStructure", attrs);
+  EXPECT_NE(structure, nullptr);
+  EXPECT_EQ(structure->GetType(), tidl::Structure::TYPE_STRUCTURE);
+  delete structure;
+}
diff --git a/unit_tests/stub_gen_unittest.cc b/unit_tests/stub_gen_unittest.cc
new file mode 100644 (file)
index 0000000..5ea94ee
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/stub_gen.h"
+#include "idlc/interface.h"
+#include "idlc/structure.h"
+
+class StubGenTest : public testing::Test {
+ public:
+  tidl::Document* doc;
+
+  virtual void SetUp() {
+    tidl::Parameters* params = new tidl::Parameters();
+    EXPECT_NE(params, nullptr);
+    params->Add(new tidl::Parameter("test",
+          new tidl::ParameterType(new tidl::BaseType("int"))));
+
+    tidl::Declaration* decl = new tidl::Declaration("test",
+        new tidl::BaseType("int"), params);
+    EXPECT_NE(decl, nullptr);
+
+    tidl::Declarations* decls = new tidl::Declarations();
+    EXPECT_NE(decls, nullptr);
+    decls->Add(decl);
+
+    tidl::Attributes* attrs = new tidl::Attributes();
+    EXPECT_NE(attrs, nullptr);
+    attrs->Add(new tidl::Attribute("test", new tidl::BaseType("int")));
+
+    doc = new tidl::Document();
+    EXPECT_NE(doc, nullptr);
+    doc->AddBlock(new tidl::Interface("TestInterface", decls));
+    doc->AddBlock(new tidl::Structure("TestStructure", attrs));
+  }
+  virtual void TearDown() {}
+};
+
+TEST_F(StubGenTest, StubGen_Constructor) {
+  std::shared_ptr<tidl::Document> docPtr(doc);
+  tidl::StubGen* gen = new tidl::StubGen(docPtr);
+  EXPECT_NE(gen, nullptr);
+  delete gen;
+}
diff --git a/unit_tests/type_unittest.cc b/unit_tests/type_unittest.cc
new file mode 100644 (file)
index 0000000..b0a0629
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/type.h"
+
+class TokenTest : public testing::Test {
+ public:
+  tidl::Token* testToken;
+
+  virtual void SetUp() {
+    testToken = new tidl::Token("TestToken");
+  }
+  virtual void TearDown() {
+    delete testToken;
+  }
+};
+
+TEST_F(TokenTest, Token_Constructor) {
+  tidl::Token* token = new tidl::Token("TestToken");
+  EXPECT_NE(token, nullptr);
+  delete token;
+}
+
+TEST_F(TokenTest, Token_ToString) {
+  EXPECT_EQ(testToken->ToString(), "TestToken");
+}
+
+class BaseTypeTest : public testing::Test {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(BaseTypeTest, BaseType_Constructor) {
+  tidl::BaseType* baseType = new tidl::BaseType("BaseType");
+  EXPECT_NE(baseType, nullptr);
+  delete baseType;
+}
+
+TEST_F(BaseTypeTest, BaseType_SetMetaType) {
+  tidl::BaseType* customType = new tidl::BaseType("CustomType", true);
+  customType->SetMetaType(new tidl::BaseType("int"));
+  EXPECT_EQ(customType->GetMetaType().ToString(), "int");
+  delete customType;
+}
+
+TEST_F(BaseTypeTest, BaseType_GetMetaType) {
+  tidl::BaseType* customType = new tidl::BaseType("CustomType", true);
+  customType->SetMetaType(new tidl::BaseType("string"));
+  EXPECT_EQ(customType->GetMetaType().ToString(), "string");
+  delete customType;
+}
+
+TEST_F(BaseTypeTest, BaseType_GetFullName) {
+  tidl::BaseType* customType = new tidl::BaseType("CustomType", true);
+  customType->SetMetaType(new tidl::BaseType("string"));
+  EXPECT_EQ(customType->GetFullName(), "CustomType<string>");
+  delete customType;
+}
+
+TEST_F(BaseTypeTest, BaseType_IsUserDefinedType) {
+  tidl::BaseType* testType = new tidl::BaseType("TestType", true);
+  EXPECT_EQ(testType->IsUserDefinedType(), true);
+  delete testType;
+}
+
+class ParameterTypeTest : public testing::Test {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(ParameterTypeTest, ParameterType_Constructor) {
+  tidl::ParameterType *parameterType = new tidl::ParameterType(
+      new tidl::BaseType("int"));
+  EXPECT_NE(parameterType, nullptr);
+  delete parameterType;
+}
+
+TEST_F(ParameterTypeTest, ParameterType_Constructor_With_Direction) {
+  tidl::ParameterType *parameterType = new tidl::ParameterType(
+      new tidl::BaseType("int"), "in");
+  EXPECT_NE(parameterType, nullptr);
+  delete parameterType;
+}
+
+TEST_F(ParameterTypeTest, ParameterType_GetDirection) {
+  tidl::ParameterType *parameterType = new tidl::ParameterType(
+      new tidl::BaseType("int"), "out");
+  EXPECT_NE(parameterType, nullptr);
+  EXPECT_EQ(parameterType->GetDirection(), tidl::ParameterType::Direction::OUT);
+  delete parameterType;
+}
+
+TEST_F(ParameterTypeTest, ParameterType_GetBaseType) {
+ tidl::ParameterType *parameterType = new tidl::ParameterType(
+      new tidl::BaseType("string"), "ref");
+  EXPECT_NE(parameterType, nullptr);
+  EXPECT_EQ(parameterType->GetBaseType().ToString(),
+      "string");
+  delete parameterType;
+}
diff --git a/unit_tests/unit_tests.sh b/unit_tests/unit_tests.sh
new file mode 100755 (executable)
index 0000000..83c201b
--- /dev/null
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+#--------------------------------------------------------#
+# Tizen Interface Definition Langauge Compiler Unit Test #
+#--------------------------------------------------------#
+
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+SCRIPT_FILE=$(readlink -f $0)
+SCRIPT_DIR=$(dirname $SCRIPT_FILE)
+BUILD_DIR=build
+
+usage() {
+       echo "Usage: $0 [command]"
+       echo "Commands:"
+       echo "    build              Build the unit tests"
+       echo "    clean              Clean all artifacts"
+       echo "    run                Run the unit tests"
+       echo "    full               Build & Run the unit tests"
+}
+
+cmd_build() {
+       echo "[TIDL Unit Test] Build Unit Tests"
+       mkdir -p $SCRIPT_DIR/$BUILD_DIR
+       cd $SCRIPT_DIR/$BUILD_DIR
+       cmake ..
+       make
+}
+
+cmd_clean() {
+       echo "[TIDL Unit Test] Clean all artifacts"
+       rm -rf $SCRIPT_DIR/$BUILD_DIR
+}
+
+cmd_run() {
+       echo "[TIDL Unit Test] Run Unit Tests"
+       cd $SCRIPT_DIR/$BUILD_DIR
+       $SCRIPT_DIR/$BUILD_DIR/tidl-unit-tests
+       cd $SCRIPT_DIR
+}
+
+cmd_full() {
+       cmd_build
+       cmd_run
+       cmd_clean
+}
+
+cmd=$1; shift;
+case "$cmd" in
+       build|--build|-b) cmd_build $@ ;;
+       clean|--clean|-c) cmd_clean $@ ;;
+       run|--run|-r) cmd_run $@ ;;
+       full |--full |-f)  cmd_full $@ ;;
+       *)     usage ;;
+
+esac