Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added localization for music-module #1008

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions include/faker-cxx/music.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,46 @@
#include <string_view>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"

namespace faker::music
{
/**
* @brief Returns a random artist.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Artist.
*
* @code
* faker::music::artist() // "Nirvana"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view artist();
FAKER_CXX_EXPORT std::string_view artist(Locale locale = Locale::en_US);
cieslarmichal marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Returns a random music genre.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Music genre.
*
* @code
* faker::music::genre() // "Rock"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view genre();
FAKER_CXX_EXPORT std::string_view genre(Locale locale = Locale::en_US);

/**
* @brief Returns a random song name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Song name.
*
* @code
* faker::music::songName() // "Light My Fire"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view songName();
FAKER_CXX_EXPORT std::string_view songName(Locale locale = Locale::en_US);
}
1 change: 1 addition & 0 deletions src/modules/movie_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -1317,4 +1317,5 @@ const MovieDefinition enUSmoviesDefinitions = {
.movies = enUSmovies,
.tvShows = enUStvShows,
};

}
27 changes: 21 additions & 6 deletions src/modules/music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@

namespace faker::music
{
std::string_view artist()
namespace
{
return helper::randomElement(artists);
const struct MusicDefinition& getMusicDefinition(Locale locale)
{
switch (locale)
{
default:
return enUSMusicDefinition;
}
}
}

std::string_view artist(Locale locale)
{
const auto& musicDefinition = getMusicDefinition(locale);
return helper::randomElement(musicDefinition.artists);
}

std::string_view genre()
std::string_view genre(Locale locale)
{
return helper::randomElement(musicGenres);
const auto& musicDefinition = getMusicDefinition(locale);
return helper::randomElement(musicDefinition.musicGenres);
}

std::string_view songName()
std::string_view songName(Locale locale)
{
return helper::randomElement(songNames);
const auto& musicDefinition = getMusicDefinition(locale);
return helper::randomElement(musicDefinition.songNames);
}
}
20 changes: 17 additions & 3 deletions src/modules/music_data.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#pragma once

#include <array>
#include <span>
#include <string_view>

namespace faker::music
{
const auto artists = std::to_array<std::string_view>({
struct MusicDefinition
{
std::span<const std::string_view> artists;
std::span<const std::string_view> musicGenres;
std::span<const std::string_view> songNames;
};

const auto enUSartists = std::to_array<std::string_view>({
"2 Pac",
"AC/DC",
"Abba",
Expand Down Expand Up @@ -306,7 +314,7 @@ const auto artists = std::to_array<std::string_view>({
"Yes",
});

const auto musicGenres = std::to_array<std::string_view>({
const auto enUSmusicGenres = std::to_array<std::string_view>({
"Blues",
"Classical",
"Country",
Expand All @@ -326,7 +334,7 @@ const auto musicGenres = std::to_array<std::string_view>({
"World",
});

const auto songNames = std::to_array<std::string_view>({
const auto enUSsongNames = std::to_array<std::string_view>({
"(Everything I Do) I Do it For You",
"(Ghost) Riders in the Sky",
"(I've Got a Gal In) Kalamazoo",
Expand Down Expand Up @@ -1300,4 +1308,10 @@ const auto songNames = std::to_array<std::string_view>({
"Your Song",
});

const MusicDefinition enUSMusicDefinition = {
.artists = enUSartists,
.musicGenres = enUSmusicGenres,
.songNames = enUSsongNames,
};

}
36 changes: 29 additions & 7 deletions tests/modules/music_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,53 @@ using namespace ::testing;
using namespace faker;
using namespace faker::music;

class MusicTest : public Test
namespace
{
const struct MusicDefinition& getMusicDefinition(Locale locale)
{
switch(locale)
{
default:
return enUSMusicDefinition;
}
}
}


class MusicTest : public TestWithParam<Locale>
{
public:
};

TEST_F(MusicTest, shouldGenerateArtist)
TEST_P(MusicTest, shouldGenerateArtist)
{
const auto locale = GetParam();
const auto& musicDefinition = getMusicDefinition(locale);
const auto generatedArtist = artist();

ASSERT_TRUE(std::ranges::any_of(artists, [generatedArtist](const std::string_view& artist)
ASSERT_TRUE(std::ranges::any_of(musicDefinition.artists, [generatedArtist](const std::string_view& artist)
{ return generatedArtist == artist; }));
}

TEST_F(MusicTest, shouldGenerateGenre)
TEST_P(MusicTest, shouldGenerateGenre)
{
const auto locale = GetParam();
const auto& musicDefinition = getMusicDefinition(locale);
const auto generatedGenre = genre();

ASSERT_TRUE(std::ranges::any_of(musicGenres, [generatedGenre](const std::string_view& genre)
ASSERT_TRUE(std::ranges::any_of(musicDefinition.musicGenres, [generatedGenre](const std::string_view& genre)
{ return generatedGenre == genre; }));
}

TEST_F(MusicTest, shouldGenerateSongName)
TEST_P(MusicTest, shouldGenerateSongName)
{
const auto locale = GetParam();
const auto& musicDefinition = getMusicDefinition(locale);
const auto generatedSongName = songName();

ASSERT_TRUE(std::ranges::any_of(songNames, [generatedSongName](const std::string_view& songName)
ASSERT_TRUE(std::ranges::any_of(musicDefinition.songNames, [generatedSongName](const std::string_view& songName)
{ return generatedSongName == songName; }));
}

INSTANTIATE_TEST_SUITE_P(TestMusicByLocale, MusicTest, ValuesIn(locales),
[](const TestParamInfo<Locale>& paramInfo){return toString(paramInfo.param);});
Loading