Skip to content

Commit

Permalink
Added library example
Browse files Browse the repository at this point in the history
  • Loading branch information
bialger committed Jan 18, 2024
1 parent 763997b commit ef6c9d2
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions bin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
5 changes: 4 additions & 1 deletion bin/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <iostream>

#include "lib/mylib/MyClass.h"

int main() {
std::cout << "Hello, World!" << std::endl;
MyClass printer(std::cout);
printer.Print("Hello, World!");
return 0;
}
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ else ()
endif()

message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

add_subdirectory(mylib)
1 change: 1 addition & 0 deletions lib/mylib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_library(mylib STATIC MyClass.cpp MyClass.h)
7 changes: 7 additions & 0 deletions lib/mylib/MyClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "MyClass.h"

MyClass::MyClass(std::ostream& out) : out_(out) {}

void MyClass::Print(const std::string& str) {
out_ << str;
}
16 changes: 16 additions & 0 deletions lib/mylib/MyClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <iostream>

class MyClass {
public:
explicit MyClass(std::ostream& out);

void Print(const std::string& str);

private:
std::ostream& out_;
};

#endif //MYCLASS_H_
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_executable(

target_link_libraries(
${PROJECT_NAME}_tests # link used libraries from lib directory
mylib
GTest::gtest_main
)

Expand Down
6 changes: 4 additions & 2 deletions tests/main_test.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include <sstream>

#include <gtest/gtest.h> // include your library here
#include "lib/mylib/MyClass.h"

std::vector<std::string> SplitString(const std::string& str) {
std::istringstream iss(str);

return {std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()};
}

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!");
}

0 comments on commit ef6c9d2

Please sign in to comment.