Skip to content

Commit 595035f

Browse files
committed
Imoproved
1 parent f781082 commit 595035f

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

app/configs/include/configs/StartupConfig.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,19 @@
55
#ifndef DESIGN_PATTERNS_STARTUPCONFIG_H
66
#define DESIGN_PATTERNS_STARTUPCONFIG_H
77

8+
9+
#include <cstdint>
10+
#include <optional>
11+
12+
#include "types/ReportFormat.h"
13+
14+
struct StartupConfig
15+
{
16+
const std::uint16_t port;
17+
const types::ReportFormat format;
18+
};
19+
20+
std::optional<const StartupConfig> optionsToStartupConfig(int arg, char *argv[]);
21+
22+
823
#endif //DESIGN_PATTERNS_STARTUPCONFIG_H

app/configs/src/StartupConfig.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,31 @@
22
// Created by sajith on 6/23/22.
33
//
44

5+
#include "configs/StartupConfig.h"
6+
7+
#include <iostream>
8+
9+
#include <boost/program_options.hpp>
10+
11+
12+
namespace po = boost::program_options;
13+
14+
std::optional<const StartupConfig> optionsToStartupConfig(int argc, char *argv[])
15+
{
16+
po::options_description desc{"Allowed options"};
17+
desc.add_options()
18+
("help", "Produce help message")
19+
("port", po::value<std::uint16_t>(), "Set server port")
20+
("format", po::value<types::ReportFormat>(), "Set tax report format (json or xml)");
21+
22+
po::variables_map vm;
23+
po::store(po::parse_command_line(argc, argv, desc), vm);
24+
po::notify(vm);
25+
26+
if (vm.count("help"))
27+
{
28+
std::cout << desc << '\n';
29+
return std::nullopt;
30+
}
31+
return StartupConfig{vm["port"].as<std::uint16_t>(), vm["format"].as<types::ReportFormat>()};
32+
}

app/configs/test/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
project(ConfigsTests)
2+
3+
find_package(GTest REQUIRED)
4+
5+
include_directories(${GTEST_INCLUDE_DIRS})
6+
7+
set(SOURCES StartupConfig_tests.cpp)
8+
9+
add_executable(${PROJECT_NAME} ${SOURCES})
10+
11+
target_link_libraries(${PROJECT_NAME} PRIVATE
12+
GTest::GTest
13+
GTest::Main
14+
Configs
15+
Common
16+
)
17+
18+
add_test(${PROJECT_NAME} ${PROJECT_NAME})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by sajith on 6/23/22.
3+
//
4+
5+
#include "configs/StartupConfig.h"
6+
7+
#include <gtest/gtest.h>
8+
#include <string>
9+
10+
#include "types/ReportFormat.h"
11+
12+
char arg0[] = "Program name";
13+
char arg1[] = "--port";
14+
char arg2[] = "8000";
15+
char arg3[] = "--format";
16+
char arg4[] = "xml";
17+
18+
TEST(StartupConfigTests, validOptions_returnsStartupConfig)
19+
{
20+
char *argv[] = {&arg0[0], &arg1[0], &arg2[0], &arg3[0], &arg4[0], nullptr};
21+
const auto config = optionsToStartupConfig(5, argv);
22+
ASSERT_TRUE(config);
23+
ASSERT_EQ(config->port, 8000);
24+
ASSERT_EQ(config->format, types::ReportFormat::Xml);
25+
}
26+
27+
TEST(StartupConfigTests, helpOption_returnsEmptyStartupConfig)
28+
{
29+
char help[] = "--help";
30+
char *argv[] = {&arg0[0], &help[0], NULL};
31+
const auto config = optionsToStartupConfig(2, argv);
32+
ASSERT_FALSE(config);
33+
}
34+
35+
TEST(StartupConfigTests, notAllOptions_throws)
36+
{
37+
char* argv[] = { &arg0[0], &arg1[0], &arg2[0], NULL };
38+
ASSERT_ANY_THROW(optionsToStartupConfig(3, argv));
39+
}

0 commit comments

Comments
 (0)