foxygit / Torlinkc Log in
commits tags

/tests/test_x1337.cpp · 1.29 KB

raw
#include <doctest/doctest.h>

#include <ctime>

#include "torlinkc/sources/x1337.hpp"

using namespace torlinkc;

TEST_CASE("parseUploadDate parses 1337x's 'Mon. Dth 'YY' detail-page format") {
  const std::string html = R"(<strong>Date uploaded</strong><span>Jun. 26th '26</span>)";
  auto secs = parseUploadDate(html);
  REQUIRE(secs.has_value());

  std::tm tm{};
  time_t t = static_cast<time_t>(*secs);
  gmtime_r(&t, &tm);
  CHECK(tm.tm_year + 1900 == 2026);
  CHECK(tm.tm_mon == 5);  // June, 0-indexed
  CHECK(tm.tm_mday == 26);
}

TEST_CASE("parseUploadDate accepts a month without a trailing period") {
  const std::string html = R"(<strong>Date uploaded</strong><span>Sep 3rd '24</span>)";
  auto secs = parseUploadDate(html);
  REQUIRE(secs.has_value());
  std::tm tm{};
  time_t t = static_cast<time_t>(*secs);
  gmtime_r(&t, &tm);
  CHECK(tm.tm_year + 1900 == 2024);
  CHECK(tm.tm_mon == 8);  // September
  CHECK(tm.tm_mday == 3);
}

TEST_CASE("parseUploadDate returns nullopt when the marker is absent") {
  CHECK_FALSE(parseUploadDate("<div>nothing here</div>").has_value());
}

TEST_CASE("parseUploadDate returns nullopt for an unrecognized month abbreviation") {
  const std::string html = R"(<strong>Date uploaded</strong><span>Xyz. 1st '24</span>)";
  CHECK_FALSE(parseUploadDate(html).has_value());
}