Skip to content

Commit

Permalink
add book module (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Jul 14, 2023
1 parent cf08a18 commit cc04052
Show file tree
Hide file tree
Showing 10 changed files with 694 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(SOURCES
src/Commerce.cpp
src/Word.cpp
src/Helper.cpp
src/Book.cpp
)

set(UT_SOURCES
Expand All @@ -44,6 +45,7 @@ set(UT_SOURCES
src/ColorTest.cpp
src/CommerceTest.cpp
src/WordTest.cpp
src/BookTest.cpp
)

add_library(${LIBRARY_NAME} ${SOURCES})
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ target_link_libraries(main faker-cxx)
- 🔢 Number - Generate random integers, floating point numbers.
- ✍ Word - Generate sample words, nouns, verbs etc.
- 🎨 Color - Generate color names, rgb, hex.
- 📖 Book - Generate book title, genre, author, publisher, ISBN.
- ℹ️ Datatype - Generate booleans.
- 📚 Lorem - Generate lorem text.
- 🔢 String - Generate uuids, alphanumeric, numeric, hexadecimal.
Expand Down
10 changes: 5 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@

### 📖 Book:

- [ ] title
- [ ] author
- [ ] isbn
- [ ] published year
- [ ] category/genre
- [x] title
- [x] author
- [x] publisher
- [x] isbn
- [x] genre

### 🏢 Company:

Expand Down
65 changes: 65 additions & 0 deletions include/faker-cxx/Book.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <string>

namespace faker
{
class Book
{
public:
/**
* @brief Returns a random book title.
*
* @returns Book title.
*
* @code
* Book::title() // "Romeo and Juliet"
* @endcode
*/
static std::string title();

/**
* @brief Returns a random book genre.
*
* @returns Book genre.
*
* @code
* Book::genre() // "Fantasy"
* @endcode
*/
static std::string genre();

/**
* @brief Returns a random book author.
*
* @returns Book author.
*
* @code
* Book::author() // "Shakespeare, William"
* @endcode
*/
static std::string author();

/**
* @brief Returns a random book publisher.
*
* @returns Book publisher.
*
* @code
* Book::publisher() // "Addison-Wesley"
* @endcode
*/
static std::string publisher();

/**
* @brief Returns a random book ISBN.
*
* @returns Book ISBN.
*
* @code
* Book::isbn() // "978-83-01-00000-1"
* @endcode
*/
static std::string isbn();
};
}
37 changes: 37 additions & 0 deletions src/Book.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Book.h"

#include "data/book/Authors.h"
#include "data/book/Genres.h"
#include "data/book/Publishers.h"
#include "data/book/Titles.h"
#include "Helper.h"
#include "String.h"

namespace faker
{
std::string Book::title()
{
return Helper::arrayElement<std::string>(titles);
}

std::string Book::genre()
{
return Helper::arrayElement<std::string>(genres);
}

std::string Book::author()
{
return Helper::arrayElement<std::string>(authors);
}

std::string Book::publisher()
{
return Helper::arrayElement<std::string>(publishers);
}

std::string Book::isbn()
{
return std::format("{}-{}-{}-{}-{}", String::numeric(3, false), String::numeric(2), String::numeric(2),
String::numeric(5), String::numeric(1));
}
}
63 changes: 63 additions & 0 deletions src/BookTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Book.h"

#include "gtest/gtest.h"

#include "data/book/Authors.h"
#include "data/book/Genres.h"
#include "data/book/Publishers.h"
#include "data/book/Titles.h"
#include "StringHelper.h"

using namespace ::testing;
using namespace faker;

class BookTest : public Test
{
public:
};

TEST_F(BookTest, shouldGenerateTitle)
{
const auto bookTitle = Book::title();

ASSERT_TRUE(std::any_of(titles.begin(), titles.end(),
[bookTitle](const std::string& title) { return title == bookTitle; }));
}

TEST_F(BookTest, shouldGenerateGenre)
{
const auto bookGenre = Book::genre();

ASSERT_TRUE(std::any_of(genres.begin(), genres.end(),
[bookGenre](const std::string& genre) { return genre == bookGenre; }));
}

TEST_F(BookTest, shouldGenerateAuthor)
{
const auto bookAuthor = Book::author();

ASSERT_TRUE(std::any_of(authors.begin(), authors.end(),
[bookAuthor](const std::string& author) { return author == bookAuthor; }));
}

TEST_F(BookTest, shouldGeneratePublisher)
{
const auto bookPublisher = Book::publisher();

ASSERT_TRUE(std::any_of(publishers.begin(), publishers.end(),
[bookPublisher](const std::string& publisher) { return publisher == bookPublisher; }));
}

TEST_F(BookTest, shouldGenerateIsbn)
{
const auto bookIsbn = Book::isbn();

const auto isbnNumbersGroups = StringHelper::split(bookIsbn, "-");

ASSERT_EQ(bookIsbn.size(), 17);
ASSERT_EQ(isbnNumbersGroups[0].size(), 3);
ASSERT_EQ(isbnNumbersGroups[1].size(), 2);
ASSERT_EQ(isbnNumbersGroups[2].size(), 2);
ASSERT_EQ(isbnNumbersGroups[3].size(), 5);
ASSERT_EQ(isbnNumbersGroups[4].size(), 1);
}
108 changes: 108 additions & 0 deletions src/data/book/Authors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> authors = {"Shakespeare, William",
"Smollett, T. (Tobias)",
"Dumas, Alexandre",
"Montgomery, L. M. (Lucy Maud)",
"Melville, Herman",
"Alcott, Louisa May",
"Eliot, George",
"Forster, E. M. (Edward Morgan)",
"Austen, Jane",
"Merrill, Frank T.",
"Dickens, Charles",
"Gaskell, Elizabeth Cleghorn",
"Von Arnim, Elizabeth",
"Brock, C. E. (Charles Edmund)",
"Fielding, Henry",
"Wagner, Richard",
"Twain, Mark",
"Doyle, Arthur Conan",
"Dostoyevsky, Fyodor",
"Packard, Vance",
"Adams, Clifford R. (Clifford Rose)",
"Wilde, Oscar",
"Garnett, Constance",
"Carroll, Lewis",
"Nietzsche, Friedrich Wilhelm",
"Tolstoy, Leo",
"Stevenson, Robert Louis",
"Wells, H. G. (Herbert George)",
"Shelley, Mary Wollstonecraft",
"Howard, Robert E. (Robert Ervin)",
"Baum, L. Frank (Lyman Frank)",
"Chesterton, G. K. (Gilbert Keith)",
"Homer",
"Plato",
"Rizal, José",
"Christie, Agatha",
"Joyce, James",
"Jowett, Benjamin",
"Poe, Edgar Allan",
"Verne, Jules",
"Thoreau, Henry David",
"Kafka, Franz",
"Stoker, Bram",
"Kipling, Rudyard",
"Doré, Gustave",
"Widger, David",
"Fitzgerald, F. Scott (Francis Scott)",
"Russell, Bertrand",
"Swift, Jonathan",
"Dante Alighieri",
"Wyllie, David",
"Hugo, Victor",
"Lang, Andrew",
"Maude, Aylmer",
"Burton, Richard Francis, Sir",
"Maude, Louise",
"Hawthorne, Nathaniel",
"Conrad, Joseph",
"London, Jack",
"Goethe, Johann Wolfgang von",
"James, Henry",
"Scott, Walter",
"Chekhov, Anton Pavlovich",
"Pope, Alexander",
"Ibsen, Henrik",
"Cervantes Saavedra, Miguel de",
"Balzac, Honoré de",
"Grimm, Jacob",
"Grimm, Wilhelm",
"Lovecraft, H. P. (Howard Phillips)",
"Burroughs, Edgar Rice",
"Shaw, Bernard",
"Gilman, Charlotte Perkins",
"Wodehouse, P. G. (Pelham Grenville)",
"Hotten, John Camden",
"Morley, Henry",
"Machiavelli, Niccolò",
"Derbyshire, Charles E.",
"Barrie, J. M. (James Matthew)",
"Brontë, Charlotte",
"Defoe, Daniel",
"Ward, Grady",
"Levy, Oscar",
"Burnett, Frances Hodgson",
"Schopenhauer, Arthur",
"Buckley, Theodore Alois",
"Milne, A. A. (Alan Alexander)",
"Marriott, W. K. (William Kenaz)",
"Vatsyayana",
"Potter, Beatrix",
"Ormsby, John",
"Bhide, Shivaram Parashuram",
"Butler, Samuel",
"Indrajit, Bhagavanlal",
"Maupassant, Guy de",
"Hapgood, Isabel Florence",
"Chambers, Robert W. (Robert William)",
"Marx, Karl",
"Eliot, T. S. (Thomas Stearns)",
"Hardy, Thomas"};
}
33 changes: 33 additions & 0 deletions src/data/book/Genres.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> genres = {"Adventure stories",
"Classics",
"Crime",
"Fairy tales, fables, and folk tales",
"Fantasy",
"Historical fiction",
"Horror",
"Humour and satire",
"Literary fiction",
"Mystery",
"Poetry",
"Plays",
"Romance",
"Science fiction",
"Short stories",
"Thrillers",
"War",
"Women’s fiction",
"Young adult",
"Non-fiction",
"Autobiography and memoir",
"Biography",
"Essays",
"Non-fiction novel",
"Self-help"};
}
Loading

0 comments on commit cc04052

Please sign in to comment.