From f2cf42427a4557b09e8ef3cddd089b5d18fd83a2 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 8 May 2022 20:39:31 +0400 Subject: [PATCH] New unit-test TaskbarlessServerTest.searchResults This is a preliminary implementation checking only the following cases: - no search results - all search results fitting on a single page The second test-case fails because of a bug in search renderer (leading to the pagination footer being pointlessly enabled). Will fix it in the next commit. --- test/server.cpp | 236 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) diff --git a/test/server.cpp b/test/server.cpp index 08a640297..2ed02a6ec 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -46,6 +46,11 @@ Headers invariantHeaders(Headers headers) return headers; } +std::string replace(std::string s, std::string pattern, std::string replacement) +{ + return std::regex_replace(s, std::regex(pattern), replacement); +} + // Output generated via mustache templates sometimes contains end-of-line // whitespace. This complicates representing the expected output of a unit-test // as C++ raw strings in editors that are configured to delete EOL whitespace. @@ -975,6 +980,237 @@ TEST_F(ServerTest, 500) EXPECT_EQ(r->body, expectedBody); } +std::string makeSearchResultsHtml(const std::string& pattern, + const std::string& header, + const std::string& results) +{ + const char SEARCHRESULTS_HTML_TEMPLATE[] = R"HTML( + + + + + //EOLWHITESPACEMARKER + + Search: %PATTERN% + + +
+ %HEADER% +
+ +
+ +
+ + + + +)HTML"; + + std::string html = removeEOLWhitespaceMarkers(SEARCHRESULTS_HTML_TEMPLATE); + html = replace(html, "%PATTERN%", pattern); + html = replace(html, "%HEADER%", header); + html = replace(html, "%RESULTS%", results); + html = replace(html, "%FOOTER%", ""); + return html; +} + +TEST_F(TaskbarlessServerTest, searchResults) +{ + struct TestData + { + std::string pattern; + size_t totalResultCount; + size_t firstResultIndex; + std::vector results; + + std::string url() const + { + return "/ROOT/search?content=zimfile&pattern=" + pattern; + } + + std::string expectedHeader() const + { + if ( totalResultCount == 0 ) { + return "\n No results were found for \"" + pattern + "\""; + } + + std::string header = R"( Results + + FIRSTRESULT-LASTRESULT + of + RESULTCOUNT + for + "PATTERN" + + )"; + + const size_t lastResultIndex = firstResultIndex + results.size() - 1; + header = replace(header, "FIRSTRESULT", to_string(firstResultIndex)); + header = replace(header, "LASTRESULT", to_string(lastResultIndex)); + header = replace(header, "RESULTCOUNT", to_string(totalResultCount)); + header = replace(header, "PATTERN", pattern); + return header; + } + + std::string expectedResultsString() const + { + if ( results.empty() ) { + return "\n "; + } + + std::string s; + for ( const auto& r : results ) { + s += "\n
  • "; + s += r; + s += "
  • "; + } + return s; + } + + std::string expectedHtml() const + { + return makeSearchResultsHtml( + pattern, + expectedHeader(), + expectedResultsString() + ); + } + + TestContext testContext() const + { + return TestContext{ { "url", url() } }; + } + }; + + const TestData testData[] = { + { + /* pattern */ "velomanyunkan", + /* totalResultCount */ 0, + /* firstResultIndex */ 0, + /* results */ {}, + }, + + { + /* pattern */ "razaf", + /* totalResultCount */ 1, + /* firstResultIndex */ 1, + /* results */ { +R"SEARCHRESULT( + + We Gonna Move to the Outskirts of Town + + ...to the Outskirts of Town "We Gonna Move to the Outskirts of Town" is a country blues song recorded September 3, 1936 by Casey Bill Weldon (voice and guitar). The song has been covered by many other musicians, most often under the title "I'm Gonna Move to the Outskirts of Town", and sometimes simply Outskirts of Town. All recordings seem to credit Weldon as songwriter, often as Weldon or as Will Weldon or as William Weldon. Some cover versions give credit also to Andy Razaf and/or to Roy Jacobs.... +
    from Ray Charles
    +
    93 words
    +)SEARCHRESULT" + }, + }, + }; + + for ( const auto& t : testData ) { + const auto r = zfs1_->GET(t.url().c_str()); + EXPECT_EQ(r->status, 200); + EXPECT_EQ(r->body, t.expectedHtml()) << t.testContext(); + } +} + TEST_F(ServerTest, UserLanguageControl) { struct TestData