Skip to content

Commit

Permalink
feat: Added localization for music-module (#1008)
Browse files Browse the repository at this point in the history
* Added module constructor to both header (defination and the declaration

* Reverting the changes as they are directly pushed into main

* -- Added localization for the music module
-- Updated the music_data header files
-- Updated the global header file for music module
-- Update the unit test cases to incorporate parameters
-- Used clang-format to format files according to style guidelines

* -- Fixed the formatting for movie.cpp

* -- Added doxygen comments to music.h for the newly added parameters.
  • Loading branch information
SgtApone117 authored Dec 3, 2024
1 parent d5f5f31 commit dbed564
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
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);

/**
* @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);});

0 comments on commit dbed564

Please sign in to comment.