From 750f2505456f6caa8aae215ef3df87ad47089379 Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 30 Sep 2022 21:41:21 -0400 Subject: [PATCH] Tests: Added Series Tests This will be the last set of tests added for now. Seasons don't have easily testable functions. --- src/episode.cpp | 13 +++++++++- src/episode.h | 9 +++++++ src/series.cpp | 18 +++----------- src/series.h | 2 +- tests/CMakeLists.txt | 4 +-- tests/episode_tests.cpp | 54 ++++++++++++++++++++--------------------- tests/episode_tests.h | 2 +- tests/test.cpp | 29 ++++++++++++++++++++-- 8 files changed, 81 insertions(+), 50 deletions(-) diff --git a/src/episode.cpp b/src/episode.cpp index 59d84ae..9c53d66 100644 --- a/src/episode.cpp +++ b/src/episode.cpp @@ -27,7 +27,18 @@ namespace dropout_dl { } } - #if defined(__WIN32__) + + std::string remove_leading_and_following_whitespace(const std::string& str) { + int start = 0; + int end = str.length() - 1; + + for (; str[start] == ' ' || str[start] == '\t' || str[start] == '\n'; start++); + for (; str[end] == ' ' || str[end] == '\t' || str[end] == '\n'; end--); + + return str.substr(start, end - start + 1); + } + +#if defined(__WIN32__) #include msec_t time_ms(void) { diff --git a/src/episode.h b/src/episode.h index 34f99c2..9f749d8 100644 --- a/src/episode.h +++ b/src/episode.h @@ -162,6 +162,15 @@ namespace dropout_dl { */ void replace_all(std::string& str, const std::string& from, const std::string& to); + /** + * + * @param str - A string + * @return str with any leading or following whitespace + * + * Removes leading and following whitespace from a string and returns the modified string + */ + std::string remove_leading_and_following_whitespace(const std::string& str); + #if defined(__WIN32__) #include msec_t time_ms(void); diff --git a/src/series.cpp b/src/series.cpp index 83036c2..6276bdc 100644 --- a/src/series.cpp +++ b/src/series.cpp @@ -8,36 +8,24 @@ namespace dropout_dl { std::string series::get_series_name(const std::string& html_data) { std::string collection_title("collection-title"); - std::string open_a_tag(""); std::string close_a(""); - int series_name_start = -1; - for (int i = 0; i < html_data.size(); i++) { if (substr_is(html_data, i, collection_title)) { for (int j = i + collection_title.size(); j < html_data.size(); j++) { if (html_data[j] == '\n' || html_data[j] == ' ' || html_data[j] == '\t') continue; if (substr_is(html_data, j, close_tag)) { for (int l = 0; l < html_data.size() - j; l++) { - char c = html_data[j + l]; - if (series_name_start == -1) { - if (html_data[j + l + 1] == '\n' || html_data[j + l + 1] == ' ' || - html_data[j + l + 1] == '\t') { - continue; - } else { - series_name_start = j + l + 1; - } - } - if (substr_is(html_data, j + l, close_a) || (series_name_start != -1 && html_data[j + l] == '\n')) { - return html_data.substr(series_name_start, l - (series_name_start - j)); + if (substr_is(html_data, j + l, close_a)) { + return remove_leading_and_following_whitespace(html_data.substr(j + 1, l - 1)); } } } } } } - return "-1"; + return "ERROR"; } std::vector series::get_seasons(const std::string &html_data, const std::vector& cookies) { diff --git a/src/series.h b/src/series.h index 3fe392c..08fb94a 100644 --- a/src/series.h +++ b/src/series.h @@ -76,7 +76,7 @@ namespace dropout_dl { this->url = url; this->page_data = get_generic_page(url); this->name = get_series_name(page_data); - if (name == "-1") { + if (name == "ERROR") { std::cerr << "SERIES PARSE ERROR: Could not parse series name\n"; exit(10); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4a32e92..c22c877 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,10 +3,8 @@ project(dropout-dl-tests) set(CMAKE_CXX_STANDARD 17) -add_executable(test test.cpp episode_tests.cpp ../src/episode.cpp) +add_executable(test test.cpp episode_tests.cpp ../src/episode.cpp ../src/season.cpp series_tests.cpp ../src/series.cpp) target_link_libraries(test curl sqlite3 gcrypt) message(STATUS "Building Tests") - -include_directories(episode ../src) diff --git a/tests/episode_tests.cpp b/tests/episode_tests.cpp index dd8f264..30f2a7c 100644 --- a/tests/episode_tests.cpp +++ b/tests/episode_tests.cpp @@ -5,7 +5,7 @@ namespace dropout_dl { tests test_episode_name_parsing() { - bool success; + std::vector> out; std::string (*test_function)(const std::string&) = episode::get_episode_name; @@ -14,7 +14,7 @@ namespace dropout_dl { " " + base_test_solution + "\n" " "; - test base("Basic Episode Name Parsing", test_function, base_test, base_test_solution); + out.emplace_back("Basic Episode Name Parsing", test_function, base_test, base_test_solution); std::string multiple_header_test_solution = "Multi Header Test Title"; @@ -35,7 +35,7 @@ namespace dropout_dl { ""; - test multiple_header("Multiple Header Episode Name Parsing", test_function, multiple_header_test, multiple_header_test_solution); + out.emplace_back("Multiple Header Episode Name Parsing", test_function, multiple_header_test, multiple_header_test_solution); std::string no_valid_header_test_solution = "ERROR"; std::string no_valid_header_test = "

\n" @@ -48,7 +48,7 @@ namespace dropout_dl { "Header with strong" "

\n"; - test no_valid_header("No Valid Header Episode Name Parsing", test_function, no_valid_header_test, no_valid_header_test_solution); + out.emplace_back("No Valid Header Episode Name Parsing", test_function, no_valid_header_test, no_valid_header_test_solution); std::string html_character_test_solution = "'&;"; @@ -56,14 +56,14 @@ namespace dropout_dl { " '&;\n" ""; - test html_character_code("Html Character Code Episode Name Parsing", test_function, html_character_test, html_character_test_solution); + out.emplace_back("Html Character Code Episode Name Parsing", test_function, html_character_test, html_character_test_solution); - return tests("Episode Name Parsing", {base, multiple_header, no_valid_header, html_character_code}); + return {"Episode Name Parsing", out}; } tests test_episode_number_parsing() { - bool success; + std::vector> out; std::string (*test_function)(const std::string&) = episode::get_episode_number; @@ -72,7 +72,7 @@ namespace dropout_dl { " Season 1, Episode 1\n" ""; - test base("Basic Episode Number Parsing", test_function, base_test, base_test_solution); + out.emplace_back("Basic Episode Number Parsing", test_function, base_test, base_test_solution); std::string multiple_link_test_solution = "1"; @@ -86,7 +86,7 @@ namespace dropout_dl { " Season 1, Episode 2\n"; - test multiple_link("Multiple Link Episode Number Parsing", test_function, multiple_link_test, multiple_link_test_solution); + out.emplace_back("Multiple Link Episode Number Parsing", test_function, multiple_link_test, multiple_link_test_solution); std::string no_valid_number_test_solution = "-1"; std::string no_valid_number_test = "\n" @@ -99,7 +99,7 @@ namespace dropout_dl { "Episode" "\n"; - test no_valid_number("No Valid Episode Number Parsing", test_function, no_valid_number_test, no_valid_number_test_solution); + out.emplace_back("No Valid Episode Number Parsing", test_function, no_valid_number_test, no_valid_number_test_solution); std::string earlier_episode_text_test_solution = "15"; @@ -110,13 +110,13 @@ namespace dropout_dl { " Season 1, Episode 15\n" ""; - test earlier_episode_text("Earlier Episode Text Number Parsing", test_function, earlier_episode_text_test, earlier_episode_text_test_solution); + out.emplace_back("Earlier Episode Text Number Parsing", test_function, earlier_episode_text_test, earlier_episode_text_test_solution); - return tests("Episode Name Parsing", {base, multiple_link, no_valid_number, earlier_episode_text}); + return {"Episode Name Parsing", out}; } tests test_episode_series_name_parsing() { - bool success; + std::vector> out; std::string (*test_function)(const std::string&) = episode::get_series_name; @@ -127,7 +127,7 @@ namespace dropout_dl { " \n" ""; - test base("Basic Episode Series Name Parsing", test_function, base_test, base_test_solution); + out.emplace_back("Basic Episode Series Name Parsing", test_function, base_test, base_test_solution); std::string multiple_header_test_solution = "Multi Header Test Title"; @@ -148,7 +148,7 @@ namespace dropout_dl { ""; - test multiple_header("Multiple Header Episode Series Name Parsing", test_function, multiple_header_test, multiple_header_test_solution); + out.emplace_back("Multiple Header Episode Series Name Parsing", test_function, multiple_header_test, multiple_header_test_solution); std::string no_valid_header_test_solution = "ERROR"; std::string no_valid_header_test = "

\n" @@ -161,7 +161,7 @@ namespace dropout_dl { "Header with strong" "

\n"; - test no_valid_header("No Valid Header Episode Series Name Parsing", test_function, no_valid_header_test, no_valid_header_test_solution); + out.emplace_back("No Valid Header Episode Series Name Parsing", test_function, no_valid_header_test, no_valid_header_test_solution); std::string html_character_test_solution = "'&;"; @@ -169,13 +169,13 @@ namespace dropout_dl { " '&;\n" ""; - test html_character("Html Character Code Episode Series Name Parsing", test_function, html_character_test, html_character_test_solution); + out.emplace_back("Html Character Code Episode Series Name Parsing", test_function, html_character_test, html_character_test_solution); - return tests("Episode Name Parsing", {base, multiple_header, no_valid_header, html_character}); + return {"Episode Name Parsing", out}; } tests test_episode_embedded_url_parsing() { - bool success; + std::vector> out; std::string (*test_function)(const std::string&) = episode::get_embed_url; @@ -184,7 +184,7 @@ namespace dropout_dl { " embed_url: \"" + base_test_solution + "\"\n" " };"; - test base("Basic Episode Embedded URL Parsing", test_function, base_test, base_test_solution); + out.emplace_back("Basic Episode Embedded URL Parsing", test_function, base_test, base_test_solution); std::string multiple_script_test_solution = "Multi Header Test Title"; @@ -247,7 +247,7 @@ namespace dropout_dl { " "; - test multiple_script("Multiple Script Embedded URL Parsing", test_function, multiple_script_test, multiple_script_test_solution); + out.emplace_back("Multiple Script Embedded URL Parsing", test_function, multiple_script_test, multiple_script_test_solution); std::string no_valid_URL_test_solution = ""; std::string no_valid_URL_test = " "; - test no_valid_URL("No Valid Embedded URL Parsing", test_function, no_valid_URL_test, no_valid_URL_test_solution); + out.emplace_back("No Valid Embedded URL Parsing", test_function, no_valid_URL_test, no_valid_URL_test_solution); - return tests("Episode Name Parsing", {base, multiple_script, no_valid_URL}); + return {"Episode Name Parsing", out}; } tests test_episode_config_url_parsing() { - bool success; + std::vector> out; std::string (*test_function)(const std::string&) = episode::get_config_url; std::string base_test_solution = "Base Test URL"; std::string base_test = R"(window.OTTData = {"config_url":")" + base_test_solution + "\"};"; - test base("Basic Episode Config URL Parsing", test_function, base_test, base_test_solution); + out.emplace_back("Basic Episode Config URL Parsing", test_function, base_test, base_test_solution); std::string no_valid_URL_test_solution = ""; std::string no_valid_URL_test = R"(window.OTTData = {"api_data":{"api_host":"","api_token":"","user_auth_token":{"auth_user_token":"","embed_referrer_host":""}},"buy_button":{"label":null,"url":null},"collection":{"id":null},"product":{"id":null},"":{"label":"","url":""},"site":{"id":null,"subdomain":"","twitter_name":""},"video":{"duration":null,"id":null,"is_trailer":"","title":"","is_live_video":false,"live_event_id":null},"google_cast_app_id":"","hide_chrome":null,"initial_time":null,"js_api_enabled":null,"locale":"","show_share_actions":null,"user":{"id":,"email":""},"analytics_url":""})"; - test no_valid_URL("No Valid Config URL Parsing", test_function, no_valid_URL_test, no_valid_URL_test_solution); + out.emplace_back("No Valid Config URL Parsing", test_function, no_valid_URL_test, no_valid_URL_test_solution); - return tests("Episode Name Parsing", {base, no_valid_URL}); + return {"Episode Name Parsing", out}; } } diff --git a/tests/episode_tests.h b/tests/episode_tests.h index 8bd4e2b..79ceff9 100644 --- a/tests/episode_tests.h +++ b/tests/episode_tests.h @@ -4,7 +4,7 @@ #pragma once -#include "episode.h" +#include "../src/episode.h" #include "test.h" std::vector test_episode(); \ No newline at end of file diff --git a/tests/test.cpp b/tests/test.cpp index 3429895..4732690 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -2,6 +2,7 @@ // Created by moss on 9/30/22. // #include "test.h" +#include "series_tests.h" #include "episode_tests.h" template @@ -28,6 +29,7 @@ void dropout_dl::tests::display() { int main() { + std::string episode_tests_name = "Episode Tests"; std::vector episode_tests = test_episode(); bool episode_tests_success = true; @@ -36,13 +38,36 @@ int main() { } if (episode_tests_success) { - std::cout << TESTNAME << BOLDGREEN << "Episode Tests" << std::endl; + std::cout << TESTNAME << BOLDGREEN << episode_tests_name << std::endl; } else { - std::cout << TESTNAME << BOLDRED << "Episode Tests" << std::endl; + std::cout << TESTNAME << BOLDRED << episode_tests_name << std::endl; } for (auto& test : episode_tests) { test.display(); } + + + std::cout << "\n\n"; + + + std::string series_tests_name = "Series Tests"; + std::vector series_tests = test_series(); + + bool series_tests_success = true; + for (const auto& test : episode_tests) { + series_tests_success &= test.success; + } + + if (series_tests_success) { + std::cout << TESTNAME << BOLDGREEN << series_tests_name << std::endl; + } + else { + std::cout << TESTNAME << BOLDRED << series_tests_name << std::endl; + } + + for (auto& test : series_tests) { + test.display(); + } } \ No newline at end of file