epubgrep/tests/test_helpers.cpp

112 lines
3.0 KiB
C++

#include "fs-compat.hpp"
#include "helpers.hpp"
#include <catch.hpp>
#include <array>
#include <exception>
#include <string>
SCENARIO("Helpers work as intended")
{
bool exception{false};
bool result{false};
SECTION("is_whitespace() does what it should do")
{
for (const auto c : std::array{' ', '\n', '\r', '\t'})
{
WHEN(std::string("char is ") + c)
{
try
{
result = epubgrep::helpers::is_whitespace(c);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("Whitespace is detected")
{
REQUIRE_FALSE(exception);
REQUIRE(result);
}
}
}
}
SECTION("urldecode() doesn't fail and returns the decoded string")
{
GIVEN("The string test%20folder/%2Afile%5Btest%5D%2A")
{
std::string encoded_text{"test%20folder/%2Afile%5Btest%5D%2A"};
std::string decoded_text{};
try
{
decoded_text = epubgrep::helpers::urldecode(encoded_text);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the decoded text")
{
REQUIRE_FALSE(exception);
REQUIRE(decoded_text == "test folder/*file[test]*");
}
}
}
SECTION("unescape_html() doesn't fail and returns the decoded text")
{
GIVEN("A text with a named entity in it.")
{
std::string encoded_text{"Sleepy &amp; ready for bed"};
std::string decoded_text{};
try
{
decoded_text = epubgrep::helpers::unescape_html(encoded_text);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the unescaped text")
{
REQUIRE_FALSE(exception);
REQUIRE(decoded_text == "Sleepy & ready for bed");
}
}
GIVEN("A text with numbered entities in it.")
{
std::string encoded_text{"Sleepy &#x26; ready for&#32;bed"};
std::string decoded_text{};
try
{
decoded_text = epubgrep::helpers::unescape_html(encoded_text);
}
catch (const std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN("It returns the unescaped text")
{
REQUIRE_FALSE(exception);
REQUIRE(decoded_text == "Sleepy & ready for bed");
}
}
}
}