Skip to content

Commit

Permalink
Feature/system (#73)
Browse files Browse the repository at this point in the history
* Add file name, file extension method and tests for them

* Updated functionality

* Add boolean method with probability definition

* Add objectKey and maybe methods

* Add hexadecimal generation function

* Add mac generation function

* Add directory path, file path, semantic version and network interface generation

* Add cron expression generation

* Made methods static, add more base for generation

* Clean up

* Bug fix

* System test fix

* System test fix

* Moved structs into include/types, SystemTest bug fix

* Refactor
  • Loading branch information
shekeraoleksandr committed Jul 29, 2023
1 parent 78e2a5b commit ab2ca46
Show file tree
Hide file tree
Showing 23 changed files with 1,136 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ set(FAKER_SOURCES
src/modules/phone/Phone.cpp
src/common/LuhnCheck.cpp
src/common/mappers/PrecisionMapper.cpp
)
src/modules/system/System.cpp)

set(FAKER_UT_SOURCES
src/modules/book/BookTest.cpp
Expand All @@ -58,7 +58,7 @@ set(FAKER_UT_SOURCES
src/modules/helper/HelperTest.cpp
src/common/LuhnCheckTest.cpp
src/common/mappers/PrecisionMapperTest.cpp
)
src/modules/system/SystemTest.cpp)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

Expand Down
21 changes: 21 additions & 0 deletions include/faker-cxx/Datatype.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <optional>

namespace faker
{
Expand All @@ -15,5 +16,25 @@ class Datatype
* @endcode
*/
static 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);
};
}
62 changes: 62 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include <span>
#include <string_view>
#include <vector>
#include <unordered_map>
#include <functional>

#include "../../src/common/LuhnCheck.h"
#include "../src/common/StringHelper.h"
#include "Number.h"
#include "Datatype.h"

namespace faker
{
Expand Down Expand Up @@ -118,6 +121,65 @@ class Helper
*/
static std::string regexpStyleStringParse(const std::string& input);

/**
* @brief Returns a random key from given object.
*
* @tparam T The type of the object to select from.
*
* @param object The object to be used.
*
* @throws If the given object is empty
*
* @return A random key from given object.
*
* @code
* std::unordered_map<int, std::string> testMap = {
* {1, "one"},
* {2, "two"},
* {3, "three"}
* };
* Helper::objectKey(testMap) // "2"
* @endcode
*/
template <typename T>
static typename T::key_type objectKey(const T& object)
{
if (object.empty()) {
throw std::runtime_error("Object is empty.");
}

std::vector<typename T::key_type> keys;
for (const auto& entry : object) {
keys.push_back(entry.first);
}

return arrayElement<typename T::key_type>(keys);
}

/**
* @brief Returns the result of the callback if the probability check was successful, otherwise empty string.
*
*
* @tparam TResult The type of result of the given callback.
*
* @param callback The callback to that will be invoked if the probability check was successful.
* @param options.probability The probability (`[0.00, 1.00]`) of the callback being invoked. Defaults to `0.5`.
*
* @return The result of the callback if the probability check was successful, otherwise empty string.
*
* @code
* Helper::maybe<std::string>([]() { return "Hello World!"; }) // ""
* Helper::maybe<int>([]() { return 42; }, 0.9) // "42"
* @endcode
*/
template <typename TResult>
static TResult maybe(std::function<TResult()> callback, double probability = 0.5) {
if (Datatype::boolean(probability)) {
return callback();
}
return TResult();
}

private:
static std::random_device randomDevice;
static std::mt19937 pseudoRandomGenerator;
Expand Down
14 changes: 14 additions & 0 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "types/EmojiType.h"
#include "types/Ipv4Address.h"
#include "types/Ipv4Class.h"
#include "faker-cxx/String.h"

namespace faker
{
Expand Down Expand Up @@ -198,5 +199,18 @@ class Internet
* @endcode
*/
static std::string ipv4(const IPv4Address& baseIpv4Address, const IPv4Address& generationMask);

/**
* @brief Returns a generated random mac address.
*
* @param sep Separator to use. Defaults to ":". Also can be "-" or "".
*
* @return A generated random mac address.
*
* @code
* Internet::mac() // "2d:10:34:2f:ac:ac"
* @endcode
*/
static std::string mac(const std::string& sep = ":");
};
}
20 changes: 20 additions & 0 deletions include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string>
#include <type_traits>
#include <utility>
#include <optional>
#include <sstream>

namespace faker
{
Expand Down Expand Up @@ -357,8 +359,26 @@ class Number
return distribution(pseudoRandomGenerator);
}

/**
* @brief Returns a lowercase hexadecimal number.
*
* @param min Optional parameter for lower bound of generated number.
* @param max Optional parameter for upper bound of generated number.
*
* @return A lowercase hexadecimal number.
*
* @example
* Number::hex() // "b"
* Number::hex(0, 255) // "9d"
*
*/
static std::string hex(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);


private:
static std::random_device randomDevice;
static std::mt19937 pseudoRandomGenerator;
static std::string convertToHex(int number);

};
}

0 comments on commit ab2ca46

Please sign in to comment.