From e0b6f3b17ae8869d5d89ba436e0c4c43365e360a Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Tue, 18 Jun 2024 19:44:44 +0200 Subject: [PATCH] refactor: change book class to namespace --- include/faker-cxx/Book.h | 126 ++++++++++++++++---------------- src/modules/book/Book.cpp | 14 ++-- src/modules/book/BookData.cpp | 2 +- src/modules/book/BookData.h | 2 +- tests/modules/book/BookTest.cpp | 14 ++-- 5 files changed, 77 insertions(+), 81 deletions(-) diff --git a/include/faker-cxx/Book.h b/include/faker-cxx/Book.h index 0bf7eab8f..8a047bc0d 100644 --- a/include/faker-cxx/Book.h +++ b/include/faker-cxx/Book.h @@ -2,75 +2,71 @@ #include -namespace faker +namespace faker::book { -class Book -{ -public: - /** - * @brief Returns a random book title. - * - * @returns Book title. - * - * @code - * Book::title() // "Romeo and Juliet" - * @endcode - */ - static std::string_view title(); +/** + * @brief Returns a random book title. + * + * @returns Book title. + * + * @code + * book::title() // "Romeo and Juliet" + * @endcode + */ +std::string_view title(); - /** - * @brief Returns a random book genre. - * - * @returns Book genre. - * - * @code - * Book::genre() // "Fantasy" - * @endcode - */ - static std::string_view genre(); +/** + * @brief Returns a random book genre. + * + * @returns Book genre. + * + * @code + * book::genre() // "Fantasy" + * @endcode + */ +std::string_view genre(); - /** - * @brief Returns a random book author. - * - * @returns Book author. - * - * @code - * Book::author() // "Shakespeare, William" - * @endcode - */ - static std::string_view author(); +/** + * @brief Returns a random book author. + * + * @returns Book author. + * + * @code + * book::author() // "William Shakespeare" + * @endcode + */ +std::string_view author(); - /** - * @brief Returns a random book publisher. - * - * @returns Book publisher. - * - * @code - * Book::publisher() // "Addison-Wesley" - * @endcode - */ - static std::string_view publisher(); +/** + * @brief Returns a random book publisher. + * + * @returns Book publisher. + * + * @code + * book::publisher() // "Addison-Wesley" + * @endcode + */ +std::string_view publisher(); - /** - * @brief Returns format of book - * - * @returns BookFormat format of book - * - * @code - * Book::format() // BookFormat::paperback - * @endcode - */ - static std::string_view format(); +/** + * @brief Returns format of book + * + * @returns BookFormat format of book + * + * @code + * book::format() // "Paperback" + * @endcode + */ +std::string_view format(); - /** - * @brief returns a random book series - * - * @returns std::string_view book series - * - * @code - * Book::series() // "Harry Potter" - * @endcode - */ - static std::string_view series(); -}; +/** + * @brief returns a random book series + * + * @returns std::string_view book series + * + * @code + * book::series() // "Harry Potter" + * @endcode + */ +std::string_view series(); } diff --git a/src/modules/book/Book.cpp b/src/modules/book/Book.cpp index 470dac808..4a2b6aafd 100644 --- a/src/modules/book/Book.cpp +++ b/src/modules/book/Book.cpp @@ -5,34 +5,34 @@ #include "BookData.h" #include "faker-cxx/Helper.h" -namespace faker +namespace faker::book { -std::string_view Book::title() +std::string_view title() { return Helper::arrayElement(titles); } -std::string_view Book::genre() +std::string_view genre() { return Helper::arrayElement(bookGenres); } -std::string_view Book::author() +std::string_view author() { return Helper::arrayElement(authors); } -std::string_view Book::publisher() +std::string_view publisher() { return Helper::arrayElement(publishers); } -std::string_view Book::format() +std::string_view format() { return Helper::arrayElement(bookFormats); } -std::string_view Book::series() +std::string_view series() { return Helper::arrayElement(bookSeries); } diff --git a/src/modules/book/BookData.cpp b/src/modules/book/BookData.cpp index f9c9032de..c52aeef56 100644 --- a/src/modules/book/BookData.cpp +++ b/src/modules/book/BookData.cpp @@ -3,7 +3,7 @@ #include #include -namespace faker +namespace faker::book { const std::array authors = { "A. A. Milne", diff --git a/src/modules/book/BookData.h b/src/modules/book/BookData.h index 7aff3c8b6..fa44d3141 100644 --- a/src/modules/book/BookData.h +++ b/src/modules/book/BookData.h @@ -3,7 +3,7 @@ #include #include -namespace faker +namespace faker::book { extern const std::array authors; extern const std::array bookFormats; diff --git a/tests/modules/book/BookTest.cpp b/tests/modules/book/BookTest.cpp index 489922b45..f72aa8ce9 100644 --- a/tests/modules/book/BookTest.cpp +++ b/tests/modules/book/BookTest.cpp @@ -8,7 +8,7 @@ #include "book/BookData.h" using namespace ::testing; -using namespace faker; +using namespace faker::book; class BookTest : public Test { @@ -17,14 +17,14 @@ class BookTest : public Test TEST_F(BookTest, shouldGenerateTitle) { - const auto bookTitle = Book::title(); + const auto bookTitle = title(); ASSERT_TRUE(std::ranges::any_of(titles, [bookTitle](const std::string_view& title) { return title == bookTitle; })); } TEST_F(BookTest, shouldGenerateGenre) { - const auto bookGenre = Book::genre(); + const auto bookGenre = genre(); ASSERT_TRUE( std::ranges::any_of(bookGenres, [bookGenre](const std::string_view& genre) { return genre == bookGenre; })); @@ -32,7 +32,7 @@ TEST_F(BookTest, shouldGenerateGenre) TEST_F(BookTest, shouldGenerateAuthor) { - const auto bookAuthor = Book::author(); + const auto bookAuthor = author(); ASSERT_TRUE( std::ranges::any_of(authors, [bookAuthor](const std::string_view& author) { return author == bookAuthor; })); @@ -40,7 +40,7 @@ TEST_F(BookTest, shouldGenerateAuthor) TEST_F(BookTest, shouldGeneratePublisher) { - const auto bookPublisher = Book::publisher(); + const auto bookPublisher = publisher(); ASSERT_TRUE(std::ranges::any_of(publishers, [bookPublisher](const std::string_view& publisher) { return publisher == bookPublisher; })); @@ -48,7 +48,7 @@ TEST_F(BookTest, shouldGeneratePublisher) TEST_F(BookTest, shouldGenerateFormat) { - const auto bookFormat = Book::format(); + const auto bookFormat = format(); ASSERT_TRUE(std::ranges::any_of(bookFormats, [bookFormat](const std::string_view& format) { return format == bookFormat; })); @@ -56,7 +56,7 @@ TEST_F(BookTest, shouldGenerateFormat) TEST_F(BookTest, shouldGenerateSeries) { - const auto randomSeries = Book::series(); + const auto randomSeries = series(); ASSERT_TRUE(std::ranges::any_of(bookSeries, [randomSeries](const std::string_view& series) { return series == randomSeries; }));