Skip to content

Commit

Permalink
Added work with ranges and type conversion
Browse files Browse the repository at this point in the history
Since the library uses one of the most recent compilers, it is reasonable to use
-std::ranges from modern STL
-charconv to conversion
  • Loading branch information
UlrichVonRekkenin committed Sep 15, 2023
1 parent 497d9a6 commit caa1c99
Show file tree
Hide file tree
Showing 22 changed files with 579 additions and 710 deletions.
3 changes: 1 addition & 2 deletions src/common/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ std::string StringHelper::toLower(const std::string& data)
{
std::string lowerData{data};

std::transform(lowerData.begin(), lowerData.end(), lowerData.begin(),
[](unsigned char c) { return std::tolower(c); });
std::ranges::transform(lowerData, lowerData.begin(), [](unsigned char c) { return std::tolower(c); });

return lowerData;
}
Expand Down
13 changes: 5 additions & 8 deletions src/modules/book/BookTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,29 @@ TEST_F(BookTest, shouldGenerateTitle)
{
const auto bookTitle = Book::title();

ASSERT_TRUE(std::any_of(titles.begin(), titles.end(),
[bookTitle](const std::string& title) { return title == bookTitle; }));
ASSERT_TRUE(std::ranges::any_of(titles, [bookTitle](const std::string& title) { return title == bookTitle; }));
}

TEST_F(BookTest, shouldGenerateGenre)
{
const auto bookGenre = Book::genre();

ASSERT_TRUE(std::any_of(genres.begin(), genres.end(),
[bookGenre](const std::string& genre) { return genre == bookGenre; }));
ASSERT_TRUE(std::ranges::any_of(genres, [bookGenre](const std::string& genre) { return genre == bookGenre; }));
}

TEST_F(BookTest, shouldGenerateAuthor)
{
const auto bookAuthor = Book::author();

ASSERT_TRUE(std::any_of(authors.begin(), authors.end(),
[bookAuthor](const std::string& author) { return author == bookAuthor; }));
ASSERT_TRUE(std::ranges::any_of(authors, [bookAuthor](const std::string& author) { return author == bookAuthor; }));
}

TEST_F(BookTest, shouldGeneratePublisher)
{
const auto bookPublisher = Book::publisher();

ASSERT_TRUE(std::any_of(publishers.begin(), publishers.end(),
[bookPublisher](const std::string& publisher) { return publisher == bookPublisher; }));
ASSERT_TRUE(std::ranges::any_of(publishers, [bookPublisher](const std::string& publisher)
{ return publisher == bookPublisher; }));
}

TEST_F(BookTest, shouldGenerateIsbn)
Expand Down
32 changes: 16 additions & 16 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "faker-cxx/Color.h"

#include <algorithm>
#include <charconv>

#include "gtest/gtest.h"

Expand All @@ -20,9 +21,8 @@ TEST_F(ColorTest, shouldGenerateColorName)
{
const auto generatedColorName = Color::name();

ASSERT_TRUE(std::any_of(colors.begin(), colors.end(),
[generatedColorName](const std::string& colorName)
{ return colorName == generatedColorName; }));
ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string& colorName)
{ return colorName == generatedColorName; }));
}

TEST_F(ColorTest, shouldGenerateRgbColorWithoutAlpha)
Expand All @@ -31,9 +31,10 @@ TEST_F(ColorTest, shouldGenerateRgbColorWithoutAlpha)

const auto rgbNumbers = StringHelper::split(generatedRgbColor.substr(4, generatedRgbColor.size() - 1), " ");

const auto& red = std::stoi(rgbNumbers[0]);
const auto& green = std::stoi(rgbNumbers[1]);
const auto& blue = std::stoi(rgbNumbers[2]);
int red, green, blue;
std::from_chars(rgbNumbers[0].data(), rgbNumbers[0].data() + rgbNumbers[0].size(), red);
std::from_chars(rgbNumbers[1].data(), rgbNumbers[1].data() + rgbNumbers[1].size(), green);
std::from_chars(rgbNumbers[2].data(), rgbNumbers[2].data() + rgbNumbers[2].size(), blue);

ASSERT_TRUE(generatedRgbColor.starts_with("rgb("));
ASSERT_TRUE(generatedRgbColor.ends_with(")"));
Expand All @@ -48,10 +49,11 @@ TEST_F(ColorTest, shouldGenerateRgbColorWithAlpha)

const auto rgbaNumbers = StringHelper::split(generatedRgbaColor.substr(5, generatedRgbaColor.size() - 1), " ");

const auto& red = std::stoi(rgbaNumbers[0]);
const auto& green = std::stoi(rgbaNumbers[1]);
const auto& blue = std::stoi(rgbaNumbers[2]);
const auto& alpha = std::stod(rgbaNumbers[3]);
int red, green, blue, alpha;
std::from_chars(rgbaNumbers[0].data(), rgbaNumbers[0].data() + rgbaNumbers[0].size(), red);
std::from_chars(rgbaNumbers[1].data(), rgbaNumbers[1].data() + rgbaNumbers[1].size(), green);
std::from_chars(rgbaNumbers[2].data(), rgbaNumbers[2].data() + rgbaNumbers[2].size(), blue);
std::from_chars(rgbaNumbers[3].data(), rgbaNumbers[3].data() + rgbaNumbers[3].size(), alpha);

ASSERT_TRUE(generatedRgbaColor.starts_with("rgba("));
ASSERT_TRUE(generatedRgbaColor.ends_with(")"));
Expand All @@ -70,9 +72,8 @@ TEST_F(ColorTest, shouldGenerateHexColorWithoutAlpha)

ASSERT_EQ(hexadecimal.size(), 7);
ASSERT_EQ(prefix, "#");
ASSERT_TRUE(std::any_of(hexNumber.begin(), hexNumber.end(),
[hexNumber](char hexNumberCharacter)
{ return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
ASSERT_TRUE(std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(ColorTest, shouldGenerateHexColorWithAlpha)
Expand All @@ -84,7 +85,6 @@ TEST_F(ColorTest, shouldGenerateHexColorWithAlpha)

ASSERT_EQ(hexadecimal.size(), 10);
ASSERT_EQ(prefix, "0x");
ASSERT_TRUE(std::any_of(hexNumber.begin(), hexNumber.end(),
[hexNumber](char hexNumberCharacter)
{ return hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
ASSERT_TRUE(std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
}
67 changes: 31 additions & 36 deletions src/modules/commerce/CommerceTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "faker-cxx/Commerce.h"

#include <algorithm>
#include <charconv>

#include "gtest/gtest.h"

Expand All @@ -20,16 +21,16 @@ TEST_F(CommerceTest, shouldGenerateCommerceDepartment)
{
const auto generatedDepartment = Commerce::department();

ASSERT_TRUE(std::any_of(departments.begin(), departments.end(),
[generatedDepartment](const std::string& department)
{ return department == generatedDepartment; }));
ASSERT_TRUE(std::ranges::any_of(departments, [generatedDepartment](const std::string& department)
{ return department == generatedDepartment; }));
}

TEST_F(CommerceTest, shouldGeneratePrice)
{
const auto generatedPrice = Commerce::price(100, 10000);

const auto priceAsFloat = std::stof(generatedPrice);
auto priceAsFloat{0.0f};
std::from_chars(generatedPrice.data(), generatedPrice.data() + generatedPrice.size(), priceAsFloat);

const auto generatedPriceElements = StringHelper::split(generatedPrice, ".");

Expand All @@ -44,13 +45,13 @@ TEST_F(CommerceTest, shouldGenerateSku)
const auto sku = Commerce::sku();

ASSERT_EQ(sku.size(), 4);
ASSERT_TRUE(std::all_of(sku.begin(), sku.end(),
[](char skuCharacter)
{
return std::any_of(numericCharacters.begin(), numericCharacters.end(),
[skuCharacter](char numericCharacter)
{ return skuCharacter == numericCharacter; });
}));
ASSERT_TRUE(std::ranges::all_of(sku,
[](char skuCharacter)
{
return std::ranges::any_of(numericCharacters,
[skuCharacter](char numericCharacter)
{ return skuCharacter == numericCharacter; });
}));
}

TEST_F(CommerceTest, shouldGenerateSkuWithSpecifiedLength)
Expand All @@ -60,13 +61,13 @@ TEST_F(CommerceTest, shouldGenerateSkuWithSpecifiedLength)
const auto sku = Commerce::sku(skuLength);

ASSERT_EQ(sku.size(), skuLength);
ASSERT_TRUE(std::all_of(sku.begin(), sku.end(),
[](char skuCharacter)
{
return std::any_of(numericCharacters.begin(), numericCharacters.end(),
[skuCharacter](char numericCharacter)
{ return skuCharacter == numericCharacter; });
}));
ASSERT_TRUE(std::ranges::all_of(sku,
[](char skuCharacter)
{
return std::ranges::any_of(numericCharacters,
[skuCharacter](char numericCharacter)
{ return skuCharacter == numericCharacter; });
}));
}

TEST_F(CommerceTest, shouldGenerateProductFullName)
Expand All @@ -79,40 +80,34 @@ TEST_F(CommerceTest, shouldGenerateProductFullName)
const auto& generatedProductMaterial = productFullNameElements[1];
const auto& generatedProductName = productFullNameElements[2];

ASSERT_TRUE(std::any_of(productAdjectives.begin(), productAdjectives.end(),
[generatedProductAdjective](const std::string& adjective)
{ return adjective == generatedProductAdjective; }));
ASSERT_TRUE(std::any_of(productMaterials.begin(), productMaterials.end(),
[generatedProductMaterial](const std::string& material)
{ return material == generatedProductMaterial; }));
ASSERT_TRUE(std::any_of(productNames.begin(), productNames.end(),
[generatedProductName](const std::string& productName)
{ return productName == generatedProductName; }));
ASSERT_TRUE(std::ranges::any_of(productAdjectives, [generatedProductAdjective](const std::string& adjective)
{ return adjective == generatedProductAdjective; }));
ASSERT_TRUE(std::ranges::any_of(productMaterials, [generatedProductMaterial](const std::string& material)
{ return material == generatedProductMaterial; }));
ASSERT_TRUE(std::ranges::any_of(productNames, [generatedProductName](const std::string& productName)
{ return productName == generatedProductName; }));
}

TEST_F(CommerceTest, shouldGenerateProductAdjective)
{
const auto generatedProductAdjective = Commerce::productAdjective();

ASSERT_TRUE(std::any_of(productAdjectives.begin(), productAdjectives.end(),
[generatedProductAdjective](const std::string& adjective)
{ return adjective == generatedProductAdjective; }));
ASSERT_TRUE(std::ranges::any_of(productAdjectives, [generatedProductAdjective](const std::string& adjective)
{ return adjective == generatedProductAdjective; }));
}

TEST_F(CommerceTest, shouldGenerateProductMaterial)
{
const auto generatedProductMaterial = Commerce::productMaterial();

ASSERT_TRUE(std::any_of(productMaterials.begin(), productMaterials.end(),
[generatedProductMaterial](const std::string& material)
{ return material == generatedProductMaterial; }));
ASSERT_TRUE(std::ranges::any_of(productMaterials, [generatedProductMaterial](const std::string& material)
{ return material == generatedProductMaterial; }));
}

TEST_F(CommerceTest, shouldGenerateProductName)
{
const auto generatedProductName = Commerce::productName();

ASSERT_TRUE(std::any_of(productNames.begin(), productNames.end(),
[generatedProductName](const std::string& productName)
{ return productName == generatedProductName; }));
ASSERT_TRUE(std::ranges::any_of(productNames, [generatedProductName](const std::string& productName)
{ return productName == generatedProductName; }));
}
Loading

0 comments on commit caa1c99

Please sign in to comment.