Skip to content

Commit

Permalink
Merge pull request #13 from felixschurk/9-config-file-reader
Browse files Browse the repository at this point in the history
9 config file reader
  • Loading branch information
ImProta authored Dec 20, 2023
2 parents 3abd81f + 118f721 commit 4e24844
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 24 deletions.
28 changes: 17 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
cmake_minimum_required(VERSION 3.16)
SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

PROJECT(sisc-lab
LANGUAGES CXX
project(sisc-lab
LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)

ADD_SUBDIRECTORY(src)
add_subdirectory(src)

FetchContent_Declare(
googletest
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
googletest
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

FetchContent_Declare(
abseil
GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
GIT_TAG 8028a87c96df0fff5ab58daeec30c43ce6fb0d20 # 2023-11-28
abseil
GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
GIT_TAG 8028a87c96df0fff5ab58daeec30c43ce6fb0d20 # 2023-11-28
)
set(ABSL_PROPAGATE_CXX_STD ON)

FetchContent_MakeAvailable(googletest abseil)
FetchContent_Declare(
yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
GIT_TAG 0.8.0 # 2023-08-10
)

FetchContent_MakeAvailable(googletest abseil yaml-cpp)

add_executable(main src/main.cpp)
target_link_libraries(main PRIVATE libutilities)
29 changes: 20 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
enable_testing()
include(GoogleTest)

add_library(libutilities
utilities.cpp
utilities.hpp
utilities.cpp
utilities.hpp
ConfigFile.hpp
SimpleConfigFile.hpp
)

target_link_libraries(libutilities
absl::strings
absl::algorithm
absl::strings
absl::algorithm
yaml-cpp::yaml-cpp
)


add_executable(utilities.test
utilities.test.cpp
)

target_link_libraries(utilities.test
libutilities
GTest::gtest_main
libutilities
GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(utilities.test)

add_executable(SimpleConfigFile.test
SimpleConfigFile.test.cpp)

target_link_libraries(SimpleConfigFile.test
libutilities
GTest::gtest_main
)
gtest_discover_tests(SimpleConfigFile.test)

40 changes: 40 additions & 0 deletions src/ConfigFile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CONFIG_FILE_HPP
#define CONFIG_FILE_HPP

#include <string>

struct CallSignature{
std::string _call_signature{};
std::string _active{};
std::string _mode{};
std::string _driver_type{};

CallSignature( std::string_view call_signature,
std::string_view active,
std::string_view mode,
std::string_view driver_type)
: _call_signature(call_signature),
_active(active),
_mode(mode),
_driver_type(driver_type) {}

std::string getCallSignature() const { return _call_signature; }
std::string getActive() const { return _active; }
std::string getMode() const { return _mode; }
std::string getDriverType() const { return _driver_type; }
};

class ConfigFile {
std::string _language{};
std::vector<CallSignature> _functions{};

public:
virtual ~ConfigFile() = default;

virtual bool validateInput() { return false; }
virtual std::string getLanguage() const { return _language; }
virtual std::vector<CallSignature> getCallSignatureVector() const { return _functions; }
virtual void readYamlFile(std::string const &file_path) {};
};

#endif //CONFIG_FILE_HPP
50 changes: 50 additions & 0 deletions src/SimpleConfigFile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef SIMPLECONFIGFILE_HPP
#define SIMPLECONFIGFILE_HPP

#include "absl/strings/match.h"
#include "yaml-cpp/yaml.h"

#include "ConfigFile.hpp"


class SimpleConfigFile : public ConfigFile
{
std::string _language{};
std::vector<CallSignature> _functions{};

public:
SimpleConfigFile() { }

SimpleConfigFile(std::string_view language,
std::string_view call_signature,
std::string_view active,
std::string_view mode,
std::string_view driver_type)
: _language(language){
_functions = { CallSignature(call_signature, active, mode, driver_type)};
}

bool validateInput() override
{
return absl::StrContains(_functions[0].getCallSignature(), _functions[0].getActive());
}

std::string getLanguage() const override { return _language; }

std::vector<CallSignature> getCallSignatureVector() const override { return _functions; }
CallSignature getCallSignature() const { return _functions[0]; }

void readYamlFile(std::string const &file_path) final
{
YAML::Node yamlFile = YAML::Load(file_path);
_language = yamlFile["language"].as<std::string>();
_functions = { CallSignature(yamlFile["functions"]["call_signature"].as<std::string>(),
yamlFile["functions"]["active_variable"].as<std::string>(),
yamlFile["functions"]["mode"].as<std::string>(),
yamlFile["functions"]["driver_type"].as<std::string>()) };
}

~SimpleConfigFile() override = default;
};

#endif //SIMPLECONFIGFILE_HPP
59 changes: 59 additions & 0 deletions src/SimpleConfigFile.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "gtest/gtest.h"
#include "SimpleConfigFile.hpp"

TEST (SimpleConfigFile, ValidateInput_active_in_call_signature)
{
auto config_file = std::make_unique<SimpleConfigFile>("cpp",
"void f(double &x)",
"x",
"tangent",
"something");
EXPECT_TRUE(config_file->validateInput());
}

TEST (SimpleConfigFile, ValidateInput_active_not_in_call_signature)
{
auto config_file = std::make_unique<SimpleConfigFile>("cpp",
"void f(double &x)",
"active_not_in_call_signature",
"tangent",
"something");
EXPECT_FALSE(config_file->validateInput());
}

TEST(SimpleConfigFile, Return_Call_Signature){
auto config_file = std::make_unique<SimpleConfigFile>("cpp",
"void f(double &x)",
"x",
"tangent",
"something");
EXPECT_EQ("cpp", config_file->getLanguage());
EXPECT_EQ("x", config_file->getCallSignature().getActive());
EXPECT_EQ("something", config_file->getCallSignature().getDriverType());
EXPECT_EQ("tangent", config_file->getCallSignature().getMode());
EXPECT_EQ("void f(double &x)", config_file->getCallSignature().getCallSignature());
}

TEST(SimpleConfigFile, Creation_of_yaml_reader){
std::string yamlContent = R"(
language: cpp
functions:
call_signature: void f(double &x)
active_variable: x
mode: tangent
driver_type: something
)";

// Setup
auto config_file = std::make_unique<SimpleConfigFile>();
config_file->readYamlFile(yamlContent);

EXPECT_EQ("cpp", config_file->getLanguage());
EXPECT_EQ("x", config_file->getCallSignature().getActive());
EXPECT_EQ("void f(double &x)", config_file->getCallSignature().getCallSignature());
EXPECT_EQ("something", config_file->getCallSignature().getDriverType());
EXPECT_EQ("tangent", config_file->getCallSignature().getMode());

}


3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <memory>

int main() {
std::cout << "nothing to see here" << std::endl;
std::cout << "Nothing there yet :/" << std::endl;
return 0;
}
8 changes: 8 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ std::string setSeedValue_old(const std::string& variable, const std::string& typ
return result;
}

/**
* Retrieves the type of a variable from the given function call signature.
*
* @param callSignature the function call signature
* @param variableName the name of the variable
*
* @return the type of the variable
*/
std::string getTypeOfVariable(const std::string &callSignature, const std::string &variableName)
{
std::vector<std::string> callSignatureSplitted = absl::StrSplit(callSignature, absl::ByAnyChar(" ,("), absl::SkipEmpty());
Expand Down
6 changes: 3 additions & 3 deletions src/utilities.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <vector>
#include <string>

#ifndef SISC_LAB_UTILITIES_HPP
#define SISC_LAB_UTILITIES_HPP

#include <vector>
#include <string>

std::string initializeSeedValue(const std::string& variable,
const std::string& type_of_variable,
const std::string& mode,
Expand Down

0 comments on commit 4e24844

Please sign in to comment.