Login
1 branch 0 tags
Ben (Desktop/Arch) Added a testsuite 7a0c574 28 days ago 84 Commits
moon / tests / test_podcast_entry.c
#include <stdio.h>
#include <string.h>

#include "podcast_xml.h"
#include "test_harness.h"
#include "test_helpers.h"

static bool test_sanitize_title_replaces_unsafe_chars(void) {
	char out[64];
	podcast_sanitize_title(" Hello, world!!! /? ", out, sizeof(out));
	ASSERT_EQ_STR("Hello_world", out);
	return true;
}

static bool test_write_and_parse_entry_roundtrip(void) {
	ASSERT_TRUE(test_prepare_storage());

	podcast_episode_t in;
	memset(&in, 0, sizeof(in));
	snprintf(in.title, sizeof(in.title), "Rock & Roll <Live> \"Special\"");
	snprintf(in.guid, sizeof(in.guid), "guid&1");
	snprintf(in.enclosure_url, sizeof(in.enclosure_url),
	         "https://cdn.example.com/audio?x=1&y=2");
	snprintf(in.pub_date, sizeof(in.pub_date), "Wed, 03 Jan 2024 10:00:00 GMT");

	ASSERT_TRUE(test_storage_mkdir_p(".tests/entry"));
	ASSERT_TRUE(podcast_write_entry("/.tests/entry/roundtrip.xml", &in));

	podcast_episode_t out;
	memset(&out, 0, sizeof(out));
	ASSERT_TRUE(podcast_parse_entry("/.tests/entry/roundtrip.xml", &out));

	ASSERT_EQ_STR(in.title, out.title);
	ASSERT_EQ_STR(in.guid, out.guid);
	ASSERT_EQ_STR(in.enclosure_url, out.enclosure_url);
	ASSERT_EQ_STR(in.pub_date, out.pub_date);
	return true;
}

static bool test_parse_entry_guid_falls_back_to_title(void) {
	ASSERT_TRUE(test_prepare_storage());
	ASSERT_TRUE(test_write_storage_text(
	    ".tests/entry/no_guid.xml",
	    "<item>\n"
	    "<title>Guid Fallback</title>\n"
	    "<enclosure url=\"https://cdn.example.com/fallback.mp3\" "
	    "type=\"audio/mpeg\"/>\n"
	    "</item>\n"));

	podcast_episode_t ep;
	memset(&ep, 0, sizeof(ep));
	ASSERT_TRUE(podcast_parse_entry("/.tests/entry/no_guid.xml", &ep));
	ASSERT_EQ_STR("Guid Fallback", ep.title);
	ASSERT_EQ_STR("Guid Fallback", ep.guid);
	ASSERT_EQ_STR("https://cdn.example.com/fallback.mp3", ep.enclosure_url);
	return true;
}

void register_podcast_entry_tests(void) {
	test_register("sanitize_title_replaces_unsafe_chars",
	              test_sanitize_title_replaces_unsafe_chars);
	test_register("write_and_parse_entry_roundtrip",
	              test_write_and_parse_entry_roundtrip);
	test_register("parse_entry_guid_falls_back_to_title",
	              test_parse_entry_guid_falls_back_to_title);
}