#include "charencoding.h" #include #include "3rd-party/catch.hpp" using namespace newsboat; TEST_CASE("charset_from_bom", "[charencoding]") { const std::map> test_cases { { "", nonstd::nullopt }, { "text without BOM", nonstd::nullopt }, { "\xEF\xBB\xBFtext with BOM", "UTF-8" }, { "\xFE\xFFtext with BOM", "UTF-16BE" }, { "\xFF\xFEtext with BOM", "UTF-16LE" }, }; for (const auto& test_case : test_cases) { std::vector input(test_case.first.begin(), test_case.first.end()); const auto actual = charencoding::charset_from_bom(input); const auto expected = test_case.second; INFO("actual: " << (actual.has_value() ? actual.value().c_str() : "")); INFO("expected: " << (expected.has_value() ? expected.value().c_str() : "")); REQUIRE(actual == expected); } } TEST_CASE("charset_from_xml_declaration", "[charencoding]") { const std::map> test_cases { { "", nonstd::nullopt }, { "not a declaration", nonstd::nullopt }, { R"(No encoding specified)", nonstd::nullopt }, { R"(Encoding specified)", "UTF-8" }, { R"(Encoding specified)", "utf-8" }, { R"(Encoding specified)", "fake.encoding" }, }; for (const auto& test_case : test_cases) { std::vector input(test_case.first.begin(), test_case.first.end()); const auto actual = charencoding::charset_from_xml_declaration(input); const auto expected = test_case.second; INFO("actual: " << (actual.has_value() ? actual.value().c_str() : "")); INFO("expected: " << (expected.has_value() ? expected.value().c_str() : "")); REQUIRE(actual == expected); } } TEST_CASE("charset_from_content_type_header", "[charencoding]") { const std::map> test_cases { { "", nonstd::nullopt }, { "application/xml", nonstd::nullopt }, { "multipart/form-data; boundary=something", nonstd::nullopt }, { "application/xml; charset=utf-8", "utf-8" }, }; for (const auto& test_case : test_cases) { std::vector input(test_case.first.begin(), test_case.first.end()); const auto actual = charencoding::charset_from_content_type_header(input); const auto expected = test_case.second; INFO("input: " << test_case.first); INFO("actual: " << (actual.has_value() ? actual.value().c_str() : "")); INFO("expected: " << (expected.has_value() ? expected.value().c_str() : "")); REQUIRE(actual == expected); } }