Skip to content

Commit

Permalink
Created proper test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bialger committed Jan 22, 2024
1 parent 74b5c1f commit 4b6ee69
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 28 deletions.
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ enable_testing()
add_executable(
${PROJECT_NAME}_tests
main_test.cpp
unit_tests.cpp
test_functions.cpp
test_functions.hpp
ProjectIntegrationTestSuite.cpp
ProjectIntegrationTestSuite.hpp
)

target_link_libraries(
Expand Down
9 changes: 9 additions & 0 deletions tests/ProjectIntegrationTestSuite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "ProjectIntegrationTestSuite.hpp"

void ProjectIntegrationTestSuite::SetUp() {
std::filesystem::create_directories(dirname);
}

void ProjectIntegrationTestSuite::TearDown() {
std::filesystem::remove_all(dirname);
}
17 changes: 17 additions & 0 deletions tests/ProjectIntegrationTestSuite.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef TEMPORARYDIRECTORYTESTSUITE_HPP_
#define TEMPORARYDIRECTORYTESTSUITE_HPP_

#include <filesystem>
#include <string>

#include <gtest/gtest.h>

struct ProjectIntegrationTestSuite : public testing::Test { // special test structure
const std::string dirname = "./gtest_tmp";

void SetUp() override; // method that is called at the beginning of every test

void TearDown() override; // method that is called at the end of every test
};

#endif //TEMPORARYDIRECTORYTESTSUITE_HPP_
32 changes: 4 additions & 28 deletions tests/main_test.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
#include <sstream>
#include <filesystem>

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

struct TemporaryDirectoryTestSuite : public testing::Test { // special test structure
const std::string dirname = "./gtest_tmp";

void SetUp() override { // method that is called at the beginning of every test
std::filesystem::create_directories(dirname);
}

void TearDown() override { // method that is called at the end of every test
std::filesystem::remove_all(dirname);
}
};

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_F(TemporaryDirectoryTestSuite, InitTest) {
TEST_F(ProjectIntegrationTestSuite, InitTest) {
ASSERT_TRUE(std::filesystem::is_directory(dirname));
}

TEST(MyLibUnitTestSuite, BasicTest1) {
std::ostringstream out;
MyClass printer(out);
printer.Print("Hello, World!");
ASSERT_EQ(out.str(), "Hello, World!");
}
10 changes: 10 additions & 0 deletions tests/test_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iterator>
#include <sstream>

#include "test_functions.hpp"

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>()};
}
10 changes: 10 additions & 0 deletions tests/test_functions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef TESTFUNCTIONS_HPP_
#define TESTFUNCTIONS_HPP_

#include <iostream>
#include <vector>
#include <string>

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

#endif //TESTFUNCTIONS_HPP_
15 changes: 15 additions & 0 deletions tests/unit_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <sstream>

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

TEST(MyLibUnitTestSuite, BasicTest1) {
std::ostringstream out;
MyClass printer(out);
printer.Print("Hello, World!");
std::vector<std::string> out_by_words = SplitString(out.str());
ASSERT_EQ(out_by_words.size(), 2);
ASSERT_EQ(out_by_words[0], "Hello,");
ASSERT_EQ(out_by_words[1], "World!");
}

0 comments on commit 4b6ee69

Please sign in to comment.