-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor generating full name (#260)
- Loading branch information
1 parent
e5f876e
commit b623ce4
Showing
7 changed files
with
131 additions
and
55 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
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,45 @@ | ||
#include "FormatHelper.h" | ||
|
||
#include "errors/TokenGeneratorNotFoundError.h" | ||
#include "fmt/format.h" | ||
|
||
namespace faker | ||
{ | ||
|
||
std::string FormatHelper::fillTokenValues(const std::string& format, | ||
std::map<std::string, std::function<std::string()>> tokenValueGenerators) | ||
{ | ||
std::string filledFormat; | ||
|
||
int tokenStart = -1; | ||
|
||
for (auto i = 0u; i < format.size(); i++) | ||
{ | ||
if (format[i] == '{') | ||
{ | ||
tokenStart = static_cast<int>(i) + 1; | ||
} | ||
else if (format[i] == '}' && tokenStart != -1 && static_cast<unsigned>(tokenStart) < i) | ||
{ | ||
const auto token = format.substr(static_cast<unsigned>(tokenStart), i - static_cast<unsigned>(tokenStart)); | ||
|
||
const auto foundTokenGenerator = tokenValueGenerators.find(token); | ||
|
||
if (foundTokenGenerator == tokenValueGenerators.end()) | ||
{ | ||
throw errors::TokenGeneratorNotFoundError{fmt::format("Generator not found for token {}.", token)}; | ||
} | ||
|
||
filledFormat += foundTokenGenerator->second(); | ||
|
||
tokenStart = -1; | ||
} | ||
else if (tokenStart == -1) | ||
{ | ||
filledFormat += format[i]; | ||
} | ||
} | ||
|
||
return filledFormat; | ||
} | ||
} |
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,16 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace faker | ||
{ | ||
class FormatHelper | ||
{ | ||
public: | ||
static std::string fillTokenValues(const std::string& format, | ||
std::map<std::string, std::function<std::string()>> tokenValueGenerators); | ||
}; | ||
} |
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 @@ | ||
#include "FormatHelper.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "src/common/errors/TokenGeneratorNotFoundError.h" | ||
|
||
using namespace ::testing; | ||
using namespace faker; | ||
|
||
class FormatHelperTest : public Test | ||
{ | ||
public: | ||
}; | ||
|
||
TEST_F(FormatHelperTest, fillFormatTokensData) | ||
{ | ||
const auto format = "{hello} {faker}-{cxx} {library}"; | ||
|
||
const auto dataGeneratorsMapping = | ||
std::map<std::string, std::function<std::string()>>{{"hello", []() { return "library"; }}, | ||
{"faker", []() { return "cxx"; }}, | ||
{"cxx", []() { return "faker"; }}, | ||
{"library", []() { return "hello"; }}}; | ||
|
||
const auto result = FormatHelper::fillTokenValues(format, dataGeneratorsMapping); | ||
|
||
const auto expectedResult = "library cxx-faker hello"; | ||
|
||
EXPECT_EQ(result, expectedResult); | ||
} | ||
|
||
TEST_F(FormatHelperTest, givenTokensWithNotDefinedGenerator_shouldThrow) | ||
{ | ||
const auto format = "{hello} {faker}-{cxx} {library}"; | ||
|
||
const auto dataGeneratorsMapping = std::map<std::string, std::function<std::string()>>{ | ||
{"hello", []() { return "library"; }}, {"faker", []() { return "cxx"; }}, {"cxx", []() { return "faker"; }}}; | ||
|
||
ASSERT_THROW(FormatHelper::fillTokenValues(format, dataGeneratorsMapping), errors::TokenGeneratorNotFoundError); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <stdexcept> | ||
|
||
namespace faker::errors | ||
{ | ||
struct TokenGeneratorNotFoundError : std::runtime_error | ||
{ | ||
using std::runtime_error::runtime_error; | ||
}; | ||
} |
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