From 48a1f41ae50168e4012640b89281c71c39b8d055 Mon Sep 17 00:00:00 2001 From: Eric Floyd Date: Sun, 21 Jan 2024 00:06:05 -0500 Subject: [PATCH 1/2] Added new functions to book module Added following functions - series - page - translator - releaseYear - format --- include/faker-cxx/Book.h | 63 +++++++++++++++++++++++++++++ src/modules/book/Book.cpp | 29 +++++++++++++ src/modules/book/BookTest.cpp | 38 +++++++++++++++++ src/modules/book/data/Series.h | 28 +++++++++++++ src/modules/book/data/Titles.h | 3 +- src/modules/book/data/Translators.h | 24 +++++++++++ 6 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 src/modules/book/data/Series.h create mode 100644 src/modules/book/data/Translators.h diff --git a/include/faker-cxx/Book.h b/include/faker-cxx/Book.h index 73b8be7ad..b4b462908 100644 --- a/include/faker-cxx/Book.h +++ b/include/faker-cxx/Book.h @@ -61,5 +61,68 @@ class Book * @endcode */ static std::string isbn(); + + /** + * @brief Returns a random release year + * + * @returns int year + * + * @code + * Book::releaseYear() // "2016-06-27" + * @endcode + */ + static std::string releaseYear(); + + /** + * @brief Returns the full name of a translator + * + * @returns std::string full name + * + * @code + * Book::translator() // "Eric Floyd" + * @endcode + */ + static std::string translator(); + + enum class BookFormat { + Paperback, + Hardcover, + Kindle, + // Add more formats if needed + }; + + /** + * @brief Returns format of book + * + * @returns BookFormat format of book + * + * @code + * Book::format() // BookFormat::paperback + * @endcode + */ + static BookFormat 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(); }; } + diff --git a/src/modules/book/Book.cpp b/src/modules/book/Book.cpp index be36c0884..fafc362af 100644 --- a/src/modules/book/Book.cpp +++ b/src/modules/book/Book.cpp @@ -5,8 +5,12 @@ #include "data/Genres.h" #include "data/Publishers.h" #include "data/Titles.h" +#include "data/Translators.h" +#include "data/Series.h" #include "faker-cxx/Helper.h" #include "faker-cxx/String.h" +#include "faker-cxx/Date.h" +#include "faker-cxx/Number.h" namespace faker { @@ -35,4 +39,29 @@ std::string Book::isbn() return FormatHelper::format("{}-{}-{}-{}-{}", String::numeric(3, false), String::numeric(2), String::numeric(2), String::numeric(5), String::numeric(1)); } + +std::string Book::releaseYear() +{ + return Date::pastDate(100).substr(0, 10); +} + +std::string Book::translator() +{ + return Helper::arrayElement(translators); +} + +Book::BookFormat Book::format() +{ + return static_cast(Number::integer(0, 2)); +} + +int Book::page() +{ + return Number::integer(50, 999); +} + +std::string Book::series() +{ + return Helper::arrayElement(bookSeries); +} } diff --git a/src/modules/book/BookTest.cpp b/src/modules/book/BookTest.cpp index bd19bdf48..65b6d8c3d 100644 --- a/src/modules/book/BookTest.cpp +++ b/src/modules/book/BookTest.cpp @@ -9,6 +9,8 @@ #include "data/Genres.h" #include "data/Publishers.h" #include "data/Titles.h" +#include "data/Translators.h" +#include "data/Series.h" using namespace ::testing; using namespace faker; @@ -60,3 +62,39 @@ TEST_F(BookTest, shouldGenerateIsbn) ASSERT_EQ(isbnNumbersGroups[3].size(), 5); ASSERT_EQ(isbnNumbersGroups[4].size(), 1); } + +TEST_F(BookTest, shouldGenerateReleaseYear) { + int year = std::stoi(Book::releaseYear().substr(0,4)); + ASSERT_TRUE((year >= 1924) && (year <= 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(bookFormat == Book::BookFormat::Paperback || bookFormat == Book::BookFormat::Hardcover || + bookFormat == Book::BookFormat::Kindle); +} + +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; })); +} diff --git a/src/modules/book/data/Series.h b/src/modules/book/data/Series.h new file mode 100644 index 000000000..c1ed79c07 --- /dev/null +++ b/src/modules/book/data/Series.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +namespace faker { + const std::vector 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", + }; +} diff --git a/src/modules/book/data/Titles.h b/src/modules/book/data/Titles.h index 0ee3999b1..814481706 100644 --- a/src/modules/book/data/Titles.h +++ b/src/modules/book/data/Titles.h @@ -105,5 +105,6 @@ const std::vector 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)" +}; } diff --git a/src/modules/book/data/Translators.h b/src/modules/book/data/Translators.h new file mode 100644 index 000000000..21066c5ea --- /dev/null +++ b/src/modules/book/data/Translators.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace faker { + const std::vector 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", + }; +} From e0692dde01cf3bae69119eda01547154b244bd49 Mon Sep 17 00:00:00 2001 From: Eric Floyd Date: Sun, 21 Jan 2024 10:17:29 -0500 Subject: [PATCH 2/2] Changed releaseYear type and removed bookFormat struct changed releaseYear function return type from std::string to int removed bookFormat struct and changed format function data type to string --- include/faker-cxx/Book.h | 13 +++---------- src/modules/book/Book.cpp | 9 +++++---- src/modules/book/BookTest.cpp | 11 ++++++----- src/modules/book/data/BookFormat.h | 12 ++++++++++++ 4 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 src/modules/book/data/BookFormat.h diff --git a/include/faker-cxx/Book.h b/include/faker-cxx/Book.h index b4b462908..09cdd32fa 100644 --- a/include/faker-cxx/Book.h +++ b/include/faker-cxx/Book.h @@ -68,10 +68,10 @@ class Book * @returns int year * * @code - * Book::releaseYear() // "2016-06-27" + * Book::releaseYear() // 2016 * @endcode */ - static std::string releaseYear(); + static int releaseYear(); /** * @brief Returns the full name of a translator @@ -84,13 +84,6 @@ class Book */ static std::string translator(); - enum class BookFormat { - Paperback, - Hardcover, - Kindle, - // Add more formats if needed - }; - /** * @brief Returns format of book * @@ -100,7 +93,7 @@ class Book * Book::format() // BookFormat::paperback * @endcode */ - static BookFormat format(); + static std::string format(); /* * @brief returns a random page number (50-999) diff --git a/src/modules/book/Book.cpp b/src/modules/book/Book.cpp index fafc362af..81a96b61b 100644 --- a/src/modules/book/Book.cpp +++ b/src/modules/book/Book.cpp @@ -7,6 +7,7 @@ #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" @@ -40,9 +41,9 @@ std::string Book::isbn() String::numeric(5), String::numeric(1)); } -std::string Book::releaseYear() +int Book::releaseYear() { - return Date::pastDate(100).substr(0, 10); + return std::stoi(Date::pastDate(100).substr(0, 4)); } std::string Book::translator() @@ -50,9 +51,9 @@ std::string Book::translator() return Helper::arrayElement(translators); } -Book::BookFormat Book::format() +std::string Book::format() { - return static_cast(Number::integer(0, 2)); + return Helper::arrayElement(bookFormats); } int Book::page() diff --git a/src/modules/book/BookTest.cpp b/src/modules/book/BookTest.cpp index 65b6d8c3d..e47eb29f9 100644 --- a/src/modules/book/BookTest.cpp +++ b/src/modules/book/BookTest.cpp @@ -11,6 +11,7 @@ #include "data/Titles.h" #include "data/Translators.h" #include "data/Series.h" +#include "data/BookFormat.h" using namespace ::testing; using namespace faker; @@ -64,8 +65,8 @@ TEST_F(BookTest, shouldGenerateIsbn) } TEST_F(BookTest, shouldGenerateReleaseYear) { - int year = std::stoi(Book::releaseYear().substr(0,4)); - ASSERT_TRUE((year >= 1924) && (year <= 2024)); + int releaseYear = Book::releaseYear(); + ASSERT_TRUE((releaseYear >= 1924) && (releaseYear <= 2024)); } TEST_F(BookTest, shouldGenerateTranslator) @@ -79,9 +80,9 @@ TEST_F(BookTest, shouldGenerateTranslator) TEST_F(BookTest, shouldGenerateFormat) { const auto bookFormat = Book::format(); - - ASSERT_TRUE(bookFormat == Book::BookFormat::Paperback || bookFormat == Book::BookFormat::Hardcover || - bookFormat == Book::BookFormat::Kindle); + + ASSERT_TRUE(std::ranges::any_of(bookFormats, [bookFormat](const std::string& format) + { return format == bookFormat; })); } TEST_F(BookTest, shouldGeneratePage) diff --git a/src/modules/book/data/BookFormat.h b/src/modules/book/data/BookFormat.h new file mode 100644 index 000000000..628160337 --- /dev/null +++ b/src/modules/book/data/BookFormat.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace faker { + const std::vector bookFormats { + "Paperback", + "Hardcover", + "Kindle", + }; +}