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

Added new functions to book module #486

Merged
merged 2 commits into from
Jan 21, 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
56 changes: 56 additions & 0 deletions include/faker-cxx/Book.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,61 @@ class Book
* @endcode
*/
static std::string isbn();

/**
* @brief Returns a random release year
*
* @returns int year
*
* @code
* Book::releaseYear() // 2016
* @endcode
*/
static int releaseYear();

/**
* @brief Returns the full name of a translator
*
* @returns std::string full name
*
* @code
* Book::translator() // "Eric Floyd"
* @endcode
*/
static std::string translator();

/**
* @brief Returns format of book
*
* @returns BookFormat format of book
*
* @code
* Book::format() // BookFormat::paperback
* @endcode
*/
static std::string format();

/*
* @brief returns a random page number (50-999)
*
* @returns int page number
*
* @code
* Book::page() // 314
* @endcode
*/
static int page();

/*
* @brief returns a random book series
*
* @returns std::string book series
*
* @code
* Book::series() // "Harry Potter"
* @endcode
*/
static std::string series();
};
}

30 changes: 30 additions & 0 deletions src/modules/book/Book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
#include "data/Genres.h"
#include "data/Publishers.h"
#include "data/Titles.h"
#include "data/Translators.h"
#include "data/Series.h"
#include "data/BookFormat.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/String.h"
#include "faker-cxx/Date.h"
#include "faker-cxx/Number.h"

namespace faker
{
Expand Down Expand Up @@ -35,4 +40,29 @@ std::string Book::isbn()
return FormatHelper::format("{}-{}-{}-{}-{}", String::numeric(3, false), String::numeric(2), String::numeric(2),
String::numeric(5), String::numeric(1));
}

int Book::releaseYear()
{
return std::stoi(Date::pastDate(100).substr(0, 4));
}

std::string Book::translator()
{
return Helper::arrayElement<std::string>(translators);
}

std::string Book::format()
{
return Helper::arrayElement<std::string>(bookFormats);
}

int Book::page()
{
return Number::integer(50, 999);
}

std::string Book::series()
{
return Helper::arrayElement<std::string>(bookSeries);
}
}
39 changes: 39 additions & 0 deletions src/modules/book/BookTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "data/Genres.h"
#include "data/Publishers.h"
#include "data/Titles.h"
#include "data/Translators.h"
#include "data/Series.h"
#include "data/BookFormat.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -60,3 +63,39 @@ TEST_F(BookTest, shouldGenerateIsbn)
ASSERT_EQ(isbnNumbersGroups[3].size(), 5);
ASSERT_EQ(isbnNumbersGroups[4].size(), 1);
}

TEST_F(BookTest, shouldGenerateReleaseYear) {
int releaseYear = Book::releaseYear();
ASSERT_TRUE((releaseYear >= 1924) && (releaseYear <= 2024));
}

TEST_F(BookTest, shouldGenerateTranslator)
{
const auto bookTranslator = Book::translator();

ASSERT_TRUE(std::ranges::any_of(translators, [bookTranslator](const std::string& translator)
{ return translator == bookTranslator; }));
}

TEST_F(BookTest, shouldGenerateFormat)
{
const auto bookFormat = Book::format();

ASSERT_TRUE(std::ranges::any_of(bookFormats, [bookFormat](const std::string& format)
{ return format == bookFormat; }));
}

TEST_F(BookTest, shouldGeneratePage)
{
const auto bookPage = Book::page();

ASSERT_TRUE(bookPage >= 50 && bookPage <= 999);
}

TEST_F(BookTest, shouldGenerateSeries)
{
const auto randomSeries = Book::series();

ASSERT_TRUE(std::ranges::any_of(bookSeries, [randomSeries](const std::string& series)
{ return series == randomSeries; }));
}
12 changes: 12 additions & 0 deletions src/modules/book/data/BookFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>
#include <vector>

namespace faker {
const std::vector<std::string> bookFormats {
"Paperback",
"Hardcover",
"Kindle",
};
}
28 changes: 28 additions & 0 deletions src/modules/book/data/Series.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <vector>
#include <string>

namespace faker {
const std::vector<std::string> bookSeries = {
"Harry Potter",
"The Lord of the Rings",
"Game of Thrones",
"Sherlock Holmes",
"Percy Jackson",
"The Hunger Games",
"The Chronicles of Narnia",
"Dune",
"The Maze Runner",
"The Wheel of Time",
"A Song of Ice and Fire",
"Discworld",
"The Dark Tower",
"The Hitchhiker's Guide to the Galaxy",
"The Foundation Series",
"His Dark Materials",
"Outlander",
"The Inheritance Cycle",
"The Dresden Files",
};
}
3 changes: 2 additions & 1 deletion src/modules/book/data/Titles.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ const std::vector<std::string> titles = {
"Josefine Mutzenbacher",
"Heart of Darkness",
"David Copperfield",
"Three Men in a Boat (To Say Nothing of the Dog)"};
"Three Men in a Boat (To Say Nothing of the Dog)"
};
}
24 changes: 24 additions & 0 deletions src/modules/book/data/Translators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <vector>
#include <string>

namespace faker {
const std::vector<std::string> translators = {
"Gregory Rabassa",
"Edith Grossman",
"Charlotte Mandell",
"David Bellos",
"Ann Goldstein",
"C.K. Scott Moncrieff",
"Lydia Davis",
"Richard Howard",
"Elliott Colla",
"Katherine Silver",
"Ros Schwartz",
"Jennifer Croft",
"Michael Hofmann",
"Maureen Freely",
"Natasha Wimmer",
};
}