-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from felixschurk/9-config-file-reader
9 config file reader
- Loading branch information
Showing
8 changed files
with
199 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters