diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index d66b144..9fd306f 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -1,3 +1,5 @@ add_executable(${PROJECT_NAME} main.cpp) +target_link_libraries(${PROJECT_NAME} PUBLIC mylib) + target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}) \ No newline at end of file diff --git a/bin/main.cpp b/bin/main.cpp index 0ab7f9b..53149f9 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -1,6 +1,9 @@ #include +#include "lib/mylib/MyClass.h" + int main() { - std::cout << "Hello, World!" << std::endl; + MyClass printer(std::cout); + printer.Print("Hello, World!"); return 0; } diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 40bcb59..a2e0e27 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -13,3 +13,5 @@ else () endif() message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") + +add_subdirectory(mylib) diff --git a/lib/mylib/CMakeLists.txt b/lib/mylib/CMakeLists.txt new file mode 100644 index 0000000..4c59bc5 --- /dev/null +++ b/lib/mylib/CMakeLists.txt @@ -0,0 +1 @@ +add_library(mylib STATIC MyClass.cpp MyClass.h) \ No newline at end of file diff --git a/lib/mylib/MyClass.cpp b/lib/mylib/MyClass.cpp new file mode 100644 index 0000000..c4f920a --- /dev/null +++ b/lib/mylib/MyClass.cpp @@ -0,0 +1,7 @@ +#include "MyClass.h" + +MyClass::MyClass(std::ostream& out) : out_(out) {} + +void MyClass::Print(const std::string& str) { + out_ << str; +} diff --git a/lib/mylib/MyClass.h b/lib/mylib/MyClass.h new file mode 100644 index 0000000..e536fa6 --- /dev/null +++ b/lib/mylib/MyClass.h @@ -0,0 +1,16 @@ +#ifndef MYCLASS_H_ +#define MYCLASS_H_ + +#include + +class MyClass { + public: + explicit MyClass(std::ostream& out); + + void Print(const std::string& str); + + private: + std::ostream& out_; +}; + +#endif //MYCLASS_H_ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7353d0c..4c62957 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable( target_link_libraries( ${PROJECT_NAME}_tests # link used libraries from lib directory + mylib GTest::gtest_main ) diff --git a/tests/main_test.cpp b/tests/main_test.cpp index 04a7cd8..105243a 100644 --- a/tests/main_test.cpp +++ b/tests/main_test.cpp @@ -1,6 +1,7 @@ #include #include // include your library here +#include "lib/mylib/MyClass.h" std::vector SplitString(const std::string& str) { std::istringstream iss(str); @@ -8,8 +9,9 @@ std::vector SplitString(const std::string& str) { return {std::istream_iterator(iss), std::istream_iterator()}; } -TEST(BasicTestSuite, BasicTest1) { +TEST(MyLibUnitTestSuite, BasicTest1) { std::ostringstream out; - out << "Hello, World!"; + MyClass printer(out); + printer.Print("Hello, World!"); ASSERT_EQ(out.str(), "Hello, World!"); }