Skip to content

Commit

Permalink
adding unit tests for message/channel/user url generations
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-ilesik committed Sep 14, 2023
1 parent 8c92ef1 commit 6abb869
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
5 changes: 5 additions & 0 deletions include/dpp/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ namespace dpp {
*/
inline const std::string cdn_host = "https://cdn.discordapp.com";

/**
* @brief The base URL for message/user/channel links.
*/
inline const std::string url_host = "https://discord.com";

/**
* @brief Callback for the results of a command executed via dpp::utility::exec
*/
Expand Down
6 changes: 3 additions & 3 deletions src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,19 +551,19 @@ namespace dpp {
}

std::string message_url(const snowflake& guild_id, const snowflake& channel_id, const snowflake& message_id){
return "https://discord.com/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id) + "/" + std::to_string(message_id);
return url_host + "/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id) + "/" + std::to_string(message_id);
}

std::string channel_url(const snowflake& guild_id, const snowflake& channel_id){
return "https://discord.com/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id);
return url_host + "/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id);
}

std::string thread_url(const snowflake& guild_id, const snowflake& thread_id){
return channel_url(guild_id, thread_id);
};

std::string user_url(const snowflake& user_id){
return "https://discord.com/users/" + std::to_string(user_id);
return url_host + "/users/" + std::to_string(user_id);
};

template <typename T>
Expand Down
33 changes: 33 additions & 0 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b

set_test(USER_GET_CREATION_TIME, false);
set_test(USER_GET_CREATION_TIME, (uint64_t)user1.get_creation_time() == 1465312605);

set_test(USER_GET_URL, false);
set_test(USER_GET_URL, user1.get_url() == dpp::utility::url_host + "/users/189759562910400512");
}

{ // avatar size function
Expand Down Expand Up @@ -422,6 +425,16 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
set_test(EMOJI_GET_URL, emoji.get_url() == dpp::utility::cdn_host + "/emojis/825407338755653641.png");
}

{ // message methods
dpp::message m;
m.guild_id = 825407338755653642;
m.channel_id = 956230231277072415;
m.id = 1151617986541666386;

set_test(MESSAGE_GET_URL, false);
set_test(MESSAGE_GET_URL, m.get_url() == dpp::utility::url_host + "/channels/825407338755653642/956230231277072415/1151617986541666386");
}

{ // channel methods
set_test(CHANNEL_SET_TYPE, false);
dpp::channel c;
Expand All @@ -435,6 +448,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
set_test(CHANNEL_GET_MENTION, false);
c.id = 825411707521728511;
set_test(CHANNEL_GET_MENTION, c.get_mention() == "<#825411707521728511>");

set_test(CHANNEL_GET_URL, false);
c.guild_id = 825407338755653642;
set_test(CHANNEL_GET_URL, c.get_url() == dpp::utility::url_host + "/channels/825407338755653642/825411707521728511");
}

{ // utility methods
Expand Down Expand Up @@ -507,6 +524,22 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
emoji_mention3 == ":white_check_mark:" &&
emoji_mention4 == ":white_check_mark:"
);

set_test(UTILITY_USER_URL, false);
auto user_url = dpp::utility::user_url(123);
set_test(UTILITY_USER_URL, user_url == dpp::utility::url_host + "/users/123");

set_test(UTILITY_MESSAGE_URL, false);
auto message_url = dpp::utility::message_url(1,2,3);
set_test(UTILITY_MESSAGE_URL, message_url == dpp::utility::url_host+ "/channels/1/2/3");

set_test(UTILITY_CHANNEL_URL, false);
auto channel_url = dpp::utility::thread_url(1,2);
set_test(UTILITY_CHANNEL_URL, channel_url == dpp::utility::url_host+ "/channels/1/2");

set_test(UTILITY_THREAD_URL, false);
auto thread_url = dpp::utility::thread_url(1,2);
set_test(UTILITY_THREAD_URL, thread_url == dpp::utility::url_host+ "/channels/1/2");
}

#ifndef _WIN32
Expand Down
7 changes: 7 additions & 0 deletions src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ DPP_TEST(USER_GET, "cluster::user_get", tf_online);
DPP_TEST(USER_GET_FLAGS, "cluster::user_get flag parsing", tf_online);
DPP_TEST(MEMBER_GET, "cluster::guild_get_member", tf_online);
DPP_TEST(USER_GET_MENTION, "user::get_mention", tf_offline);
DPP_TEST(USER_GET_URL, "user::get_url", tf_offline);
DPP_TEST(USER_FORMAT_USERNAME, "user::format_username", tf_offline);
DPP_TEST(USER_GET_CREATION_TIME, "user::get_creation_time", tf_offline);
DPP_TEST(USER_GET_AVATAR_URL, "user::get_avatar_url", tf_offline);
DPP_TEST(CHANNEL_SET_TYPE, "channel::set_type", tf_offline);
DPP_TEST(CHANNEL_GET_MENTION, "channel::get_mention", tf_offline);
DPP_TEST(CHANNEL_GET_URL, "channel::get_url", tf_offline);
DPP_TEST(MESSAGE_GET_URL,"message::get_url",tf_offline);
DPP_TEST(UTILITY_GUILD_NAVIGATION, "utility::guild_navigation", tf_offline);
DPP_TEST(UTILITY_ICONHASH, "utility::iconhash", tf_offline);
DPP_TEST(UTILITY_MAKE_URL_PARAMETERS, "utility::make_url_parameters", tf_offline);
Expand All @@ -185,6 +188,10 @@ DPP_TEST(UTILITY_CHANNEL_MENTION, "utility::channel_mention", tf_offline);
DPP_TEST(UTILITY_USER_MENTION, "utility::user_mention", tf_offline);
DPP_TEST(UTILITY_ROLE_MENTION, "utility::role_mention", tf_offline);
DPP_TEST(UTILITY_EMOJI_MENTION, "utility::emoji_mention", tf_offline);
DPP_TEST(UTILITY_USER_URL, "utility::user_url", tf_offline);
DPP_TEST(UTILITY_MESSAGE_URL, "utility::message_url", tf_offline);
DPP_TEST(UTILITY_CHANNEL_URL, "utility::channel_url", tf_offline);
DPP_TEST(UTILITY_THREAD_URL, "utility::thread_url", tf_offline);
DPP_TEST(UTILITY_AVATAR_SIZE, "utility::avatar_size", tf_offline);
DPP_TEST(UTILITY_CDN_ENDPOINT_URL_HASH, "utility::cdn_endpoint_url_hash", tf_offline);
DPP_TEST(STICKER_GET_URL, "sticker::get_url aka utility::cdn_endpoint_url_sticker", tf_offline);
Expand Down

0 comments on commit 6abb869

Please sign in to comment.