Skip to content

Commit

Permalink
added check for 0 id's to get_url methods and imporved unit tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-ilesik committed Sep 15, 2023
1 parent 6abb869 commit 5c83eb8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
8 changes: 4 additions & 4 deletions include/dpp/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,30 +541,30 @@ namespace dpp {
* @param guild_id The ID of the guild where message is written.
* @param channel_id The ID of the channel where message is written.
* @param message_id The ID of the message.
* @return std::string The URL to message.
* @return std::string The URL to message or empty string if any of ids is 0.
*/
std::string DPP_EXPORT message_url(const snowflake& guild_id, const snowflake& channel_id, const snowflake& message_id);

/**
* @brief Create a URL for message.
* @param guild_id The ID of the guild where channel is located.
* @param channel_id The ID of the channel.
* @return std::string The URL to message.
* @return std::string The URL to message or empty string if any of ids is 0.
*/
std::string DPP_EXPORT channel_url(const snowflake& guild_id, const snowflake& channel_id);

/**
* @brief Create a URL for message.
* @param guild_id The ID of the guild where thread is located.
* @param thread_id The ID of the thread.
* @return std::string The URL to message.
* @return std::string The URL to message or empty string if any of ids is 0.
*/
std::string DPP_EXPORT thread_url(const snowflake& guild_id, const snowflake& thread_id);

/**
* @brief Create a URL for message.
* @param user_id The ID of the guild where thread is located.
* @return std::string The URL to message.
* @return std::string The URL to message or empty string if id is 0.
*/
std::string DPP_EXPORT user_url(const snowflake& user_id);

Expand Down
9 changes: 9 additions & 0 deletions src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,16 @@ namespace dpp {
}

std::string message_url(const snowflake& guild_id, const snowflake& channel_id, const snowflake& message_id){
if (guild_id == snowflake(0) || channel_id == snowflake(0) || message_id == snowflake(0)) {
return "";
}
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){
if (guild_id == snowflake(0) || channel_id == snowflake(0)) {
return "";
}
return url_host + "/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id);
}

Expand All @@ -563,6 +569,9 @@ namespace dpp {
};

std::string user_url(const snowflake& user_id){
if (user_id == snowflake(0)) {
return "";
}
return url_host + "/users/" + std::to_string(user_id);
};

Expand Down
89 changes: 81 additions & 8 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
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");

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

{ // avatar size function
Expand Down Expand Up @@ -431,8 +436,40 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
m.channel_id = 956230231277072415;
m.id = 1151617986541666386;

dpp::message m2;
m2.guild_id = 825407338755653642;
m2.channel_id = 956230231277072415;

dpp::message m3;
m3.guild_id = 825407338755653642;
m3.id = 1151617986541666386;

dpp::message m4;
m4.channel_id = 956230231277072415;
m4.id = 1151617986541666386;

dpp::message m5;
m5.guild_id = 825407338755653642;

dpp::message m6;
m6.channel_id = 956230231277072415;

dpp::message m7;
m7.id = 1151617986541666386;

dpp::message m8;

set_test(MESSAGE_GET_URL, false);
set_test(MESSAGE_GET_URL, m.get_url() == dpp::utility::url_host + "/channels/825407338755653642/956230231277072415/1151617986541666386");
set_test(MESSAGE_GET_URL,
m.get_url() == dpp::utility::url_host + "/channels/825407338755653642/956230231277072415/1151617986541666386" &&
m2.get_url() == "" &&
m3.get_url() == "" &&
m4.get_url() == "" &&
m5.get_url() == "" &&
m6.get_url() == "" &&
m7.get_url() == "" &&
m8.get_url() == ""
);
}

{ // channel methods
Expand All @@ -451,7 +488,21 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b

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");

dpp::channel c2;
c2.id = 825411707521728511;

dpp::channel c3;
c3.guild_id = 825407338755653642;

dpp::channel c4;

set_test(CHANNEL_GET_URL,
c.get_url() == dpp::utility::url_host + "/channels/825407338755653642/825411707521728511" &&
c2.get_url() == "" &&
c3.get_url() == "" &&
c4.get_url() == ""
);
}

{ // utility methods
Expand Down Expand Up @@ -527,19 +578,41 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b

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_USER_URL,
user_url == dpp::utility::url_host + "/users/123" &&
dpp::utility::user_url(0) == ""
);

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_MESSAGE_URL,
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
dpp::utility::message_url(0,2,3) == "" &&
dpp::utility::message_url(1,0,3) == "" &&
dpp::utility::message_url(1,2,0) == "" &&
dpp::utility::message_url(0,0,3) == "" &&
dpp::utility::message_url(0,2,0) == "" &&
dpp::utility::message_url(1,0,0) == "" &&
dpp::utility::message_url(0,0,0) == ""
);

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");
auto channel_url = dpp::utility::channel_url(1,2);
set_test(UTILITY_CHANNEL_URL,
channel_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::channel_url(0,2) == "" &&
dpp::utility::channel_url(1,0) == "" &&
dpp::utility::channel_url(0,0) == ""
);

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");
set_test(UTILITY_THREAD_URL,
thread_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::thread_url(0,2) == "" &&
dpp::utility::thread_url(1,0) == "" &&
dpp::utility::thread_url(0,0) == ""
);
}

#ifndef _WIN32
Expand Down

0 comments on commit 5c83eb8

Please sign in to comment.