Skip to content

Commit

Permalink
refactor: change datatype class to namespace (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Jun 24, 2024
1 parent 5de4244 commit da55f85
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 56 deletions.
64 changes: 30 additions & 34 deletions include/faker-cxx/Datatype.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
#pragma once

namespace faker
namespace faker::datatype
{
class Datatype
{
public:
/**
* @brief Returns a random boolean.
*
* @returns Boolean.
*
* @code
* Datatype::boolean() // "false"
* @endcode
*/
static bool boolean();
/**
* @brief Returns a random boolean.
*
* @returns Boolean.
*
* @code
* datatype::boolean() // "false"
* @endcode
*/
bool boolean();

/**
* @brief Returns a random boolean.
* **Note:**
* A probability of `0.75` results in `true` being returned `75%` of the calls; likewise `0.3` => `30%`.
* If the probability is `<= 0.0`, it will always return `false`.
* If the probability is `>= 1.0`, it will always return `true`.
* The probability is limited to two decimal places.
*
* @param probability The probability (`[0.00, 1.00]`) of returning `true`.
*
* @returns Boolean.
*
* @code
* Datatype::boolean() // "false"
* Datatype::boolean(0.9) // "true"
* Datatype::boolean(0.1) // "false"
* @endcode
*/
static bool boolean(double probability);
};
/**
* @brief Returns a random boolean.
* **Note:**
* A probability of `0.75` results in `true` being returned `75%` of the calls; likewise `0.3` => `30%`.
* If the probability is `<= 0.0`, it will always return `false`.
* If the probability is `>= 1.0`, it will always return `true`.
* The probability is limited to two decimal places.
*
* @param probability The probability (`[0.00, 1.00]`) of returning `true`.
*
* @returns Boolean.
*
* @code
* datatype::boolean() // "false"
* datatype::boolean(0.9) // "true"
* datatype::boolean(0.1) // "false"
* @endcode
*/
bool boolean(double probability);
}
4 changes: 2 additions & 2 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Helper
template <typename It>
static auto arrayElement(It start, It end) -> decltype(*::std::declval<It>())
{
size_t size = static_cast<size_t>(end - start);
auto size = static_cast<size_t>(end - start);

if (size == 0)
{
Expand Down Expand Up @@ -282,7 +282,7 @@ class Helper
template <typename TResult>
static TResult maybe(std::function<TResult()> callback, double probability = 0.5)
{
if (Datatype::boolean(probability))
if (datatype::boolean(probability))
{
return callback();
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/datatype/Datatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#include "faker-cxx/Number.h"

namespace faker
namespace faker::datatype
{
bool Datatype::boolean()
bool boolean()
{
return Number::decimal<float>(0.f, 1.f) > 0.5f;
}

bool Datatype::boolean(double probability)
bool boolean(double probability)
{
if (probability != nan(""))
{
Expand Down
21 changes: 11 additions & 10 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ std::string filePath()

std::string semver()
{
int major = Number::integer(9);
int minor = Number::integer(9);
int patch = Number::integer(9);
const int major = Number::integer(9);
const int minor = Number::integer(9);
const int patch = Number::integer(9);

return FormatHelper::format("{}.{}.{}", major, minor, patch);
}
Expand Down Expand Up @@ -253,14 +253,15 @@ std::string cron(const CronOptions& options)
years = {std::to_string(Number::integer(1970, 2099)), "*"};
}

auto minute = Helper::arrayElement(minutes);
auto hour = Helper::arrayElement(hours);
auto day = Helper::arrayElement(days);
auto month = Helper::arrayElement(months);
auto dayOfWeek = Helper::arrayElement(daysOfWeek);
auto year = Helper::arrayElement(years);
const auto minute = Helper::arrayElement(minutes);
const auto hour = Helper::arrayElement(hours);
const auto day = Helper::arrayElement(days);
const auto month = Helper::arrayElement(months);
const auto dayOfWeek = Helper::arrayElement(daysOfWeek);
const auto year = Helper::arrayElement(years);

std::string standardExpression = minute + " " + hour + " " + day + " " + month + " " + dayOfWeek;

if (includeYear)
{
standardExpression += " " + year;
Expand All @@ -269,7 +270,7 @@ std::string cron(const CronOptions& options)
std::vector<std::string> nonStandardExpressions = {"@annually", "@daily", "@hourly", "@monthly",
"@reboot", "@weekly", "@yearly"};

return (!includeNonStandard || Datatype::boolean(0)) ? standardExpression :
return (!includeNonStandard || datatype::boolean(0)) ? standardExpression :
Helper::arrayElement<std::string>(nonStandardExpressions);
}
}
15 changes: 8 additions & 7 deletions tests/modules/datatype/DatatypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "gtest/gtest.h"

using namespace ::testing;
using namespace faker;
using namespace faker::datatype;

class DatatypeTest : public Test
{
Expand All @@ -17,22 +17,23 @@ TEST_F(DatatypeTest, shouldGenerateBoolean)
{
std::vector<bool> booleanValues{false, true};

const auto boolean = Datatype::boolean();
const auto generatedBoolean = boolean();

ASSERT_TRUE(std::ranges::any_of(booleanValues, [boolean](bool booleanValue) { return boolean == booleanValue; }));
ASSERT_TRUE(std::ranges::any_of(booleanValues, [generatedBoolean](bool booleanValue)
{ return generatedBoolean == booleanValue; }));
}

TEST_F(DatatypeTest, BooleanWithProbTest)
{
const auto result2 = Datatype::boolean(0.3);
const auto result2 = boolean(0.3);
EXPECT_TRUE(result2 || !result2);

const auto result3 = Datatype::boolean(0.8);
const auto result3 = boolean(0.8);
EXPECT_TRUE(result3 || !result3);

const auto result4 = Datatype::boolean(0.0);
const auto result4 = boolean(0.0);
EXPECT_FALSE(result4);

const auto result5 = Datatype::boolean(1.0);
const auto result5 = boolean(1.0);
EXPECT_TRUE(result5);
}

0 comments on commit da55f85

Please sign in to comment.