Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fmt #395

Merged
merged 13 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,24 @@ set(FAKER_UT_SOURCES
add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

target_include_directories(
${LIBRARY_NAME}
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include"
PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include" ${FMT_INCLUDE_DIR})

add_subdirectory(externals/fmt)

set(FMT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/fmt/include")

target_link_libraries(${LIBRARY_NAME} PRIVATE fmt)
${LIBRARY_NAME}
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include"
PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include"
)

if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_subdirectory(externals/fmt)
set(FMT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/fmt/include")
target_link_libraries(${LIBRARY_NAME} PRIVATE fmt)
endif()

if(BUILD_FAKER_TESTS)
add_subdirectory(externals/googletest)

set(GTEST_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")

enable_testing()

Expand All @@ -138,17 +139,24 @@ if(BUILD_FAKER_TESTS)
add_executable(${LIBRARY_NAME}-UT ${FAKER_UT_SOURCES})

target_link_libraries(${LIBRARY_NAME}-UT PRIVATE gtest_main gmock_main
faker-cxx)
faker-cxx)
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR})

target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
else()
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR})
endif()

add_test(
NAME ${LIBRARY_NAME}-UT
COMMAND ${LIBRARY_NAME}-UT
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
NAME ${LIBRARY_NAME}-UT
COMMAND ${LIBRARY_NAME}-UT
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

target_code_coverage(${LIBRARY_NAME}-UT ALL)
endif()
endif()
2 changes: 1 addition & 1 deletion include/faker-cxx/types/RandomGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace faker
{
template <typename T>
requires std::uniform_random_bit_generator<T>
requires std::uniform_random_bit_generator<T>
class RandomGenerator
{
public:
Expand Down
4 changes: 2 additions & 2 deletions src/common/FormatHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "FormatHelper.h"

#include "errors/TokenGeneratorNotFoundError.h"
#include "fmt/format.h"

namespace faker
{
Expand All @@ -27,7 +26,8 @@ std::string FormatHelper::fillTokenValues(const std::string& format,

if (foundTokenGenerator == tokenValueGenerators.end())
{
throw errors::TokenGeneratorNotFoundError{fmt::format("Generator not found for token {}.", token)};
throw errors::TokenGeneratorNotFoundError{
FormatHelper::format("Generator not found for token {}.", token)};
}

filledFormat += foundTokenGenerator->second();
Expand Down
21 changes: 21 additions & 0 deletions src/common/FormatHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@
#include <string>
#include <vector>

#if defined(__APPLE__) || defined(__MINGW32__)
#include <fmt/format.h>
#else
#include <format>
#endif


namespace faker
{
class FormatHelper
{
public:
#if defined(__APPLE__) || defined(__MINGW32__)
template <typename... Args>
static std::string format(fmt::format_string<Args...> fmt, Args&&... args)
{
return fmt::format(fmt, std::forward<Args>(args)...);
}
#else
template <typename... Args>
static std::string format(std::format_string<Args...> fmt, Args&&... args)
{
return std::format(fmt, std::forward<Args>(args)...);
}
#endif

static std::string fillTokenValues(const std::string& format,
std::map<std::string, std::function<std::string()>> tokenValueGenerators);
};
Expand Down
7 changes: 7 additions & 0 deletions src/common/FormatHelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ TEST_F(FormatHelperTest, givenTokensWithNotDefinedGenerator_shouldThrow)

ASSERT_THROW(FormatHelper::fillTokenValues(format, dataGeneratorsMapping), errors::TokenGeneratorNotFoundError);
}


TEST_F(FormatHelperTest, shouldFormat) {
EXPECT_EQ(FormatHelper::format("{}", 1), "1");
EXPECT_EQ(FormatHelper::format("{} {}", "Hello", "World"), "Hello World");
EXPECT_EQ(FormatHelper::format("{0} {1}", "Hello", "World"), "Hello World");
}
3 changes: 0 additions & 3 deletions src/common/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <algorithm>
#include <cctype>
#include <codecvt>
#include <locale>
#include <sstream>

namespace faker
Expand Down Expand Up @@ -80,5 +78,4 @@ std::string StringHelper::removePunctuation(const std::string& word)

return result;
}

}
6 changes: 3 additions & 3 deletions src/modules/book/Book.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "faker-cxx/Book.h"

#include "../../common/FormatHelper.h"
#include "data/Authors.h"
#include "data/Genres.h"
#include "data/Publishers.h"
#include "data/Titles.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/String.h"
#include "fmt/format.h"

namespace faker
{
Expand All @@ -32,7 +32,7 @@ std::string Book::publisher()

std::string Book::isbn()
{
return fmt::format("{}-{}-{}-{}-{}", String::numeric(3, false), String::numeric(2), String::numeric(2),
String::numeric(5), String::numeric(1));
return FormatHelper::format("{}-{}-{}-{}-{}", String::numeric(3, false), String::numeric(2), String::numeric(2),
String::numeric(5), String::numeric(1));
}
}
17 changes: 8 additions & 9 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

#include <sstream>

#include "../../common/FormatHelper.h"
#include "data/Colors.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/String.h"
#include "fmt/format.h"

namespace faker
{
std::string Color::name()
Expand All @@ -23,7 +22,7 @@ std::string Color::rgb(bool includeAlpha)

if (!includeAlpha)
{
return fmt::format("rgb({}, {}, {})", red, green, blue);
return FormatHelper::format("rgb({}, {}, {})", red, green, blue);
}

const std::floating_point auto alpha = Number::decimal<double>(1);
Expand All @@ -34,7 +33,7 @@ std::string Color::rgb(bool includeAlpha)
ss << alpha;
const auto formattedAlpha = ss.str();

return fmt::format("rgba({}, {}, {}, {})", red, green, blue, formattedAlpha);
return FormatHelper::format("rgba({}, {}, {}, {})", red, green, blue, formattedAlpha);
}

std::string Color::hex(HexCasing casing, HexPrefix prefix, bool includeAlpha)
Expand All @@ -54,7 +53,7 @@ std::string Color::hsl(bool includeAlpha)

if (!includeAlpha)
{
return fmt::format("hsl({}, {}, {})", hue, saturation, lightness);
return FormatHelper::format("hsl({}, {}, {})", hue, saturation, lightness);
}

const std::floating_point auto alpha = Number::decimal<double>(1);
Expand All @@ -65,7 +64,7 @@ std::string Color::hsl(bool includeAlpha)
ss << alpha;
const auto formattedAlpha = ss.str();

return fmt::format("hsla({}, {}, {}, {})", hue, saturation, lightness, formattedAlpha);
return FormatHelper::format("hsla({}, {}, {}, {})", hue, saturation, lightness, formattedAlpha);
}
std::string Color::lch(bool includeAlpha)
{
Expand All @@ -75,7 +74,7 @@ std::string Color::lch(bool includeAlpha)

if (!includeAlpha)
{
return fmt::format("lch({}, {}, {})", luminance, chroma, hue);
return FormatHelper::format("lch({}, {}, {})", luminance, chroma, hue);
}

const std::floating_point auto alpha = Number::decimal<double>(1);
Expand All @@ -86,7 +85,7 @@ std::string Color::lch(bool includeAlpha)
ss << alpha;
const auto formattedAlpha = ss.str();

return fmt::format("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha);
return FormatHelper::format("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha);
}

std::string Color::cmyk()
Expand All @@ -96,7 +95,7 @@ std::string Color::cmyk()
const std::floating_point auto yellow = Number::decimal<double>(1.);
const std::floating_point auto key = Number::decimal<double>(1.);

return fmt::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
}

}
1 change: 1 addition & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "gtest/gtest.h"

#include "../../common/FormatHelper.h"
#include "../../common/StringHelper.h"
#include "../string/data/Characters.h"
#include "data/Colors.h"
Expand Down
5 changes: 2 additions & 3 deletions src/modules/commerce/Commerce.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "faker-cxx/Commerce.h"

#include "../../common/FormatHelper.h"
#include "data/Commerce.h"
#include "faker-cxx/Finance.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/String.h"
#include "fmt/format.h"

namespace faker
{
std::string Commerce::department()
Expand Down Expand Up @@ -40,7 +39,7 @@ std::string Commerce::productName()

std::string Commerce::productFullName()
{
return fmt::format("{} {} {}", productAdjective(), productMaterial(), productName());
return FormatHelper::format("{} {} {}", productAdjective(), productMaterial(), productName());
}

std::string Commerce::EAN13()
Expand Down
19 changes: 10 additions & 9 deletions src/modules/company/Company.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "faker-cxx/Company.h"

#include "../../common/FormatHelper.h"
#include "data/BuzzAdjectives.h"
#include "data/BuzzNouns.h"
#include "data/BuzzVerbs.h"
Expand All @@ -11,8 +12,6 @@
#include "data/Suffixes.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"
#include "fmt/format.h"

namespace faker
{
// TODO: add internalization
Expand All @@ -24,17 +23,19 @@ std::string Company::name()
switch (Number::integer<int>(3))
{
case 0:
companyName = fmt::format("{} {}", Person::lastName(), Helper::arrayElement<std::string>(companySuffixes));
companyName =
FormatHelper::format("{} {}", Person::lastName(), Helper::arrayElement<std::string>(companySuffixes));
break;
case 1:
companyName = fmt::format("{} {} {}", Person::firstName(), Person::lastName(), Person::jobArea());
companyName = FormatHelper::format("{} {} {}", Person::firstName(), Person::lastName(), Person::jobArea());
break;
case 2:
companyName = fmt::format("{} {} {} Services", Person::firstName(), Person::lastName(), Person::jobArea());
companyName =
FormatHelper::format("{} {} {} Services", Person::firstName(), Person::lastName(), Person::jobArea());
break;
case 3:
companyName = fmt::format("{} {} {} {}", Person::firstName(), Person::lastName(), Person::jobArea(),
Helper::arrayElement<std::string>(companySuffixes));
companyName = FormatHelper::format("{} {} {} {}", Person::firstName(), Person::lastName(), Person::jobArea(),
Helper::arrayElement<std::string>(companySuffixes));
break;
}

Expand All @@ -53,7 +54,7 @@ std::string Company::industry()

std::string Company::buzzPhrase()
{
return fmt::format("{} {} {}", buzzVerb(), buzzAdjective(), buzzNoun());
return FormatHelper::format("{} {} {}", buzzVerb(), buzzAdjective(), buzzNoun());
}

std::string Company::buzzAdjective()
Expand All @@ -73,7 +74,7 @@ std::string Company::buzzVerb()

std::string Company::catchPhrase()
{
return fmt::format("{} {} {}", catchPhraseAdjective(), catchPhraseDescriptor(), catchPhraseNoun());
return FormatHelper::format("{} {} {}", catchPhraseAdjective(), catchPhraseDescriptor(), catchPhraseNoun());
}

std::string Company::catchPhraseAdjective()
Expand Down
1 change: 1 addition & 0 deletions src/modules/company/CompanyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "gtest/gtest.h"

#include "../../common/FormatHelper.h"
#include "../../common/StringHelper.h"
#include "../person/data/england/EnglishFirstNames.h"
#include "../person/data/england/EnglishLastNames.h"
Expand Down
8 changes: 4 additions & 4 deletions src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#include <ctime>
#include <iomanip>

#include "../../common/FormatHelper.h"
#include "data/MonthNames.h"
#include "data/WeekdayNames.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "fmt/format.h"

namespace faker
{
namespace
Expand Down Expand Up @@ -39,8 +38,9 @@ std::string betweenDate(const auto& from, const auto& to, Date::DateFormat dateF
{
if (from > to)
{
throw std::runtime_error{fmt::format("Start date is greater than end date. {{from: {}, to: {}}}",
serializeTimePoint(from, dateFormat), serializeTimePoint(to, dateFormat))};
throw std::runtime_error{FormatHelper::format("Start date is greater than end date. {{from: {}, to: {}}}",
serializeTimePoint(from, dateFormat),
serializeTimePoint(to, dateFormat))};
}

const auto size = std::chrono::duration_cast<std::chrono::seconds>(to - from).count();
Expand Down
Loading