From 80f779606bbcad7a450087f8be0a4f302fed40a7 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Thu, 3 Oct 2024 08:25:39 +0200 Subject: [PATCH] refactor: add locale (#933) * add locale to video game module Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/video_game.h | 17 ++++++--- src/modules/video_game.cpp | 35 ++++++++++++++----- src/modules/video_game_data.h | 25 ++++++++++--- tests/modules/video_game_test.cpp | 58 ++++++++++++++++++++++++------- 4 files changed, 106 insertions(+), 29 deletions(-) diff --git a/include/faker-cxx/video_game.h b/include/faker-cxx/video_game.h index 7ad6d1d80..9d8e05dd8 100644 --- a/include/faker-cxx/video_game.h +++ b/include/faker-cxx/video_game.h @@ -3,11 +3,14 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" namespace faker::videogame { /** * @brief Returns a random video game name. + * + * @param locale The locale. Defaults to `Locale::en_US`. * * @returns Video game name. * @@ -15,10 +18,12 @@ namespace faker::videogame * faker::videogame::gameTitle() // "Rayman Arena" * @endcode */ -FAKER_CXX_EXPORT std::string_view gameTitle(); +FAKER_CXX_EXPORT std::string_view gameTitle(Locale locale = Locale::en_US); /** * @brief Returns a random video game genre. + * + * @param locale The locale. Defaults to `Locale::en_US`. * * @returns Video game genre. * @@ -26,10 +31,12 @@ FAKER_CXX_EXPORT std::string_view gameTitle(); * faker::videogame::genre() // "Platformer" * @endcode */ -FAKER_CXX_EXPORT std::string_view genre(); +FAKER_CXX_EXPORT std::string_view genre(Locale locale = Locale::en_US); /** * @brief Returns a random video game platform. + * + * @param locale The locale. Defaults to `Locale::en_US`. * * @returns Platform. * @@ -37,10 +44,12 @@ FAKER_CXX_EXPORT std::string_view genre(); * faker::videogame::platform() // "Playstation 5" * @endcode */ -FAKER_CXX_EXPORT std::string_view platform(); +FAKER_CXX_EXPORT std::string_view platform(Locale locale = Locale::en_US); /** * @brief Returns a random video game studio name. + * + * @param locale The locale. Defaults to `Locale::en_US`. * * @returns Studio name. * @@ -48,5 +57,5 @@ FAKER_CXX_EXPORT std::string_view platform(); * faker::videogame::studioName() // "Activision Blizzard" * @endcode */ -FAKER_CXX_EXPORT std::string_view studioName(); +FAKER_CXX_EXPORT std::string_view studioName(Locale locale = Locale::en_US); } diff --git a/src/modules/video_game.cpp b/src/modules/video_game.cpp index e2674c4c5..06c87461c 100644 --- a/src/modules/video_game.cpp +++ b/src/modules/video_game.cpp @@ -7,23 +7,42 @@ namespace faker::videogame { -std::string_view gameTitle() +namespace { - return helper::randomElement(videoGameNames); +const struct VideoGames& getVideoGame(Locale locale) +{ + switch (locale) + { + default: + return enUSVideoGames; + } +} +} +std::string_view gameTitle(Locale locale) +{ + const auto& videoGame = getVideoGame(locale); + + return helper::randomElement(videoGame.videoGameNames); } -std::string_view genre() +std::string_view genre(Locale locale) { - return helper::randomElement(videoGameGenres); + const auto& videoGame = getVideoGame(locale); + + return helper::randomElement(videoGame.videoGameGenres); } -std::string_view platform() +std::string_view platform(Locale locale) { - return helper::randomElement(platforms); + const auto& videoGame = getVideoGame(locale); + + return helper::randomElement(videoGame.platforms); } -std::string_view studioName() +std::string_view studioName(Locale locale) { - return helper::randomElement(studioNames); + const auto& videoGame = getVideoGame(locale); + + return helper::randomElement(videoGame.studioNames); } } diff --git a/src/modules/video_game_data.h b/src/modules/video_game_data.h index bfb20aa70..99999b3fa 100644 --- a/src/modules/video_game_data.h +++ b/src/modules/video_game_data.h @@ -1,11 +1,21 @@ #pragma once #include +#include #include namespace faker::videogame { -const auto videoGameNames = std::to_array({ + +struct VideoGames +{ + std::span videoGameNames; + std::span videoGameGenres; + std::span platforms; + std::span studioNames; +}; + +const auto enUSVideoGameNames = std::to_array({ "#killallzombies", ".hack//G.U. Last Recode", "007: Agent Under Fire", @@ -3160,7 +3170,7 @@ const auto videoGameNames = std::to_array({ "Æon Flux", }); -const auto videoGameGenres = std::to_array({ +const auto enUSVideoGameGenres = std::to_array({ "Action", "Adventure", "Battle royale", @@ -3191,7 +3201,7 @@ const auto videoGameGenres = std::to_array({ "Tower defense", }); -const auto platforms = std::to_array({ +const auto enUSPlatforms = std::to_array({ "Android", "Linux", "Nintendo Switch", @@ -3203,7 +3213,7 @@ const auto platforms = std::to_array({ "iOS", }); -const auto studioNames = std::to_array({ +const auto enUSStudioNames = std::to_array({ "0verflow", "1st Playable Productions", "2K Czech", @@ -3598,4 +3608,11 @@ const auto studioNames = std::to_array({ "n-Space", }); +const VideoGames enUSVideoGames = { + .videoGameNames = enUSVideoGameNames, + .videoGameGenres = enUSVideoGameGenres, + .platforms = enUSPlatforms, + .studioNames = enUSStudioNames, +}; + } diff --git a/tests/modules/video_game_test.cpp b/tests/modules/video_game_test.cpp index 77e3b4ac2..c354e07a1 100644 --- a/tests/modules/video_game_test.cpp +++ b/tests/modules/video_game_test.cpp @@ -10,39 +10,71 @@ using namespace ::testing; using namespace faker; using namespace faker::videogame; -class VideoGameTest : public Test +namespace +{ +const struct VideoGames& getVideoGame(Locale locale) +{ + switch (locale) + { + default: + return enUSVideoGames; + } +} +} + +class VideoGameTest : public TestWithParam { public: }; -TEST_F(VideoGameTest, shouldGenerateGameTitle) +TEST_P(VideoGameTest, shouldGenerateGameTitle) { - const auto generatedGameTitle = gameTitle(); + const auto locale = GetParam(); + + const auto& videoGame = getVideoGame(locale); - ASSERT_TRUE(std::ranges::any_of(videoGameNames, [generatedGameTitle](const std::string_view& gameTitle) + const auto generatedGameTitle = gameTitle(locale); + + ASSERT_TRUE(std::ranges::any_of(videoGame.videoGameNames, [generatedGameTitle](const std::string_view& gameTitle) { return generatedGameTitle == gameTitle; })); } -TEST_F(VideoGameTest, shouldGenerateGenre) +TEST_P(VideoGameTest, shouldGenerateGenre) { - const auto generatedGenre = genre(); + const auto locale = GetParam(); + + const auto& videoGame = getVideoGame(locale); - ASSERT_TRUE(std::ranges::any_of(videoGameGenres, [generatedGenre](const std::string_view& genre) + const auto generatedGenre = genre(locale); + + ASSERT_TRUE(std::ranges::any_of(videoGame.videoGameGenres, [generatedGenre](const std::string_view& genre) { return generatedGenre == genre; })); } -TEST_F(VideoGameTest, shouldGeneratePlatform) +TEST_P(VideoGameTest, shouldGeneratePlatform) { - const auto generatedPlatform = platform(); + const auto locale = GetParam(); + + const auto& videoGame = getVideoGame(locale); - ASSERT_TRUE(std::ranges::any_of(platforms, [generatedPlatform](const std::string_view& platform) + const auto generatedPlatform = platform(locale); + + ASSERT_TRUE(std::ranges::any_of(videoGame.platforms, [generatedPlatform](const std::string_view& platform) { return generatedPlatform == platform; })); } -TEST_F(VideoGameTest, shouldGenerateStudioName) +TEST_P(VideoGameTest, shouldGenerateStudioName) { - const auto generatedStudioName = studioName(); + const auto locale = GetParam(); + + const auto& videoGame = getVideoGame(locale); - ASSERT_TRUE(std::ranges::any_of(studioNames, [generatedStudioName](const std::string_view& studioName) + const auto generatedStudioName = studioName(locale); + + ASSERT_TRUE(std::ranges::any_of(videoGame.studioNames, [generatedStudioName](const std::string_view& studioName) { return generatedStudioName == studioName; })); } + + +INSTANTIATE_TEST_SUITE_P(TestVideoGameByLocale, VideoGameTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); });