Skip to content

Instantly share code, notes, and snippets.

@uilianries
Last active July 4, 2024 06:40
Show Gist options
  • Save uilianries/1ec377525eed5474d29c687adf49e622 to your computer and use it in GitHub Desktop.
Save uilianries/1ec377525eed5474d29c687adf49e622 to your computer and use it in GitHub Desktop.
Draft for dev.to post about faker-cxx
Error in user YAML: (<unknown>): mapping values are not allowed in this context at line 1 column 17
---
title: Faker C++: Generate Realistic Fake Data for Testing and Development
published: false
description: Presentation of Faker C++ project and how to use it minimally
tags: cpp,fakedata,datageneration
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iajnxpk85g82p8lx7fk3.png
# Use a ratio of 100:42 for best results.
# published_at: 2024-07-02 17:33 +0000
---

Hey there, C++ developers! πŸ‘‹ Are you tired of creating mock data for your tests and prototypes? Say hello to Faker C++, a powerful library designed to generate realistic fake data for C++ applications. Let's dive into what makes this library special and how it can supercharge your development workflow.

What is Faker C++?

Faker C++ is a C++ library inspired by the popular Faker.js, aimed at providing developers with a robust tool for generating fake (but realistic) data. Whether you're building test suites, populating databases, or creating demos, Faker C++ has got you covered. It's available on Github, as totally free, open source and under MIT license.

Key Features

  • πŸ“š Realistic Data Generation: Generate various types of data including names, addresses, emails, dates, and more.
  • πŸ› οΈ Modular Design: Choose from a wide range of modules like Internet, Location, String, Date, and more to generate specific types of data.
  • πŸ”— Easy Integration: Seamlessly integrate with CMake, and it supports major compilers like MSVC, GCC, Clang, and Apple Clang.

Quick Example

Here's a taste of what you can do with Faker C++:

// main.cpp
#include <cstdlib>
#include <iostream>

#include "faker-cxx/Internet.h"
#include "faker-cxx/Location.h"
#include "faker-cxx/String.h"

int main()
{
    const auto id = faker::String::uuid();
    const auto email = faker::Internet::email();
    const auto password = faker::Internet::password();
    const auto city = faker::Location::city();
    const auto streetAddress = faker::Location::streetAddress();

    std::cout << id << std::endl;
    std::cout << email << std::endl;
    std::cout << password << std::endl;
    std::cout << city << std::endl;
    std::cout << streetAddress << std::endl;

    return EXIT_SUCCESS;
}

Consuming and using Faker C++

Let's see how to install and link to faker library.

Installing with Conan 2.x

Faker C++ can be easily installed and integrated into your projects using Conan. Here's how you can do it:

1) First, make sure you have Conan 2.x installed. If not, you can install it using pip:

pip install conan

2) To install faker-cxx, run the following command:

conan install -r conancenter --requires="faker-cxx/[*]" --build=missing

This command will fetch the latest version of faker-cxx and build it if necessary.

If you want to use faker-cxx in your CMake project, you can create a conanfile.txt in your project root with the following content:

[requires]
faker-cxx/[*]

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout

Using this conanfile.txt, Conan will provide faker-cxx CMake files under the folder build/Release/generators/

Building Faker C++ from scratch

However, in case you don't want to use Conan, it's still possible to build Faker C++ from source, and consume its dependencies using git sub modules. Here is how to do that:

1) First, we clone the project and git submodules at same shot:

git clone --recurse-submodules  https://github.com/cieslarmichal/faker-cxx.git   

2) Now we configure the build setup using CMake:

cd faker-cxx/
cmake -S . -B build -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX=/opt/faker-cxx

This command will produce the build files needed to generate Faker C++ library. Plus, it disables unit tests (they are enabled by default), and configure to install all artifacts, like headers, library and CMake modules in the path /opt/faker.

3) Then, we build the project:

cmake --build build

4) Finally, we install all produced artifacts:

cmake --build build --target install

All installed files should be available under the configured path, in this example we are using /opt/faker-cxx, but could be any other path.

Consuming Faker C++

Once you've set up Faker C++ and compiled your code, you can run your example to generate and display fake data.

First, let's configure the CMakeLists.txt, in order to link:

cmake_minimum_required(VERSION 3.15)
project(example)

find_package(faker-cxx REQUIRED)
add_executable(example main.cpp)
target_link_libraries(example faker-cxx::faker-cxx)
target_compile_features(example cxx_std_20)

Now, we configure and build our example using Faker:

1a) In case using Conan, we should consume a CMake toolchain file produced by Conan, in order to find the CMake modules:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build build

1b) However, in case building without Conan, we need to configure the path where those CMake module files were installed in the previous step.

cmake -S . -B build -DCMAKE_PREFIX_PATH=/opt/faker-cxx/lib/cmake
cmake --build build

Note that when not passing the build type, CMake will be configured in debug mode.

2) Now we can run our example using fake data:

build/example

11e9c8cc-04f8-49b2-ae28-afd6c5724e6d
[email protected]
w(*Ba.u3!N@;Pz8
Shawnee
341 Meredith Hill

This output showcases a UUID, email address, password, city, and street address, all generated randomly by Faker C++. You can use similar functions to generate various other types of fake data as per your project’s requirements.

Supported Modules

Faker C++ comes with a plethora of modules, each designed to generate specific types of data:

  • 🌐 Internet: emails, usernames, passwords, IP addresses.
  • πŸ“ Location: countries, cities, zip codes, street addresses.
  • 🧡 String: UUIDs, alphanumeric, numeric, hexadecimal strings.
  • πŸ“† Date: past and future dates.
  • 🐼 Animal: bears, birds, cats, dogs.

And many more including modules for Commerce, Company, Finance, Food, Music, Sport, Vehicle, etc.

Supported Platforms and Compilers

Faker C++ is designed to be cross-platform, ensuring that developers can use it regardless of their preferred operating system or compiler. The library is continuously tested on multiple platforms through robust CI/CD pipelines, guaranteeing reliability across different environments. Supported Operating Systems:

  • πŸͺŸ Windows
  • 🍎 macOS
  • 🐧 Linux

Also, Faker C++ is compatible with major C++ compilers, including:

  • MSVC (Microsoft Visual C++) version 143 or newer
  • Apple Clang version 14 or newer
  • GCC (GNU Compiler Collection) version 12 or newer
  • Clang version 16 or newer

It's important to note that Faker C++ requires C++20 support from your compiler. This requirement is primarily due to the use of std::format, a powerful string formatting library introduced in C++20.

For compilers that don't yet fully support std::format, Faker C++ falls back to using the fmt library, ensuring broad compatibility while still leveraging modern C++ features.

Community and Contributions

Faker C++ is an open-source project and welcomes contributions! Whether you're fixing bugs, adding new features, or improving documentation, your help is appreciated. Check out our Contributing Guide for more details. Join our Discord server to connect with other contributors.

Conclusion

Faker C++ aims to be the go-to library for generating fake data in C++ projects. It's designed to make your testing and development process smoother and more efficient. Give it a try in your next project and let us know what you think! Check out the full documentation to explore all the capabilities of Faker C++. Happy coding! πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment