Skip to content

Commit

Permalink
refactor: change book class to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Jun 18, 2024
1 parent d5f0c8a commit e0b6f3b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 81 deletions.
126 changes: 61 additions & 65 deletions include/faker-cxx/Book.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,71 @@

#include <string_view>

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();
}
14 changes: 7 additions & 7 deletions src/modules/book/Book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/book/BookData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <array>
#include <string_view>

namespace faker
namespace faker::book
{
const std::array<std::string_view, 125> authors = {
"A. A. Milne",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/book/BookData.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <array>
#include <string_view>

namespace faker
namespace faker::book
{
extern const std::array<std::string_view, 125> authors;
extern const std::array<std::string_view, 4> bookFormats;
Expand Down
14 changes: 7 additions & 7 deletions tests/modules/book/BookTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "book/BookData.h"

using namespace ::testing;
using namespace faker;
using namespace faker::book;

class BookTest : public Test
{
Expand All @@ -17,46 +17,46 @@ 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; }));
}

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; }));
}

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; }));
}

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; }));
}

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; }));
Expand Down

0 comments on commit e0b6f3b

Please sign in to comment.