import lib
fn contains(haystack: String, needle: String): Bool =
match String.indexOf(haystack, needle) {
Some(_) => true,
None => false,
}
fn test_create_item(): Unit with {Test} = {
let i = lib.item("My Post", "https://example.com/post", "A description", "2025-01-29")
Test.assertEqualMsg("My Post", lib.itemTitle(i), "item title")
Test.assertEqualMsg("https://example.com/post", lib.itemLink(i), "item link")
Test.assertEqualMsg("A description", lib.itemDescription(i), "item description")
Test.assertEqualMsg("2025-01-29", lib.itemPubDate(i), "item pubDate")
Test.assertEqualMsg("https://example.com/post", lib.itemGuid(i), "item guid defaults to link")
}
fn test_item_with_guid(): Unit with {Test} = {
let i = lib.itemWithGuid("Post", "https://example.com", "Desc", "2025-01-29", "custom-guid-123")
Test.assertEqualMsg("custom-guid-123", lib.itemGuid(i), "item custom guid")
}
fn test_date_conversion(): Unit with {Test} = {
Test.assertEqualMsg("29 Jan 2025 00:00:00 GMT", lib.isoToRfc822("2025-01-29"), "date jan")
Test.assertEqualMsg("31 Dec 2025 00:00:00 GMT", lib.isoToRfc822("2025-12-31"), "date dec")
Test.assertEqualMsg("15 Jun 2025 00:00:00 GMT", lib.isoToRfc822("2025-06-15"), "date jun")
Test.assertEqualMsg("bad", lib.isoToRfc822("bad"), "date short string passthrough")
}
fn test_empty_feed(): Unit with {Test} = {
let emptyFeed = lib.feed("My Blog", "https://example.com", "Blog about things", [])
let xml = lib.render(emptyFeed)
Test.assert(contains(xml, "My Blog"), "feed title")
Test.assert(contains(xml, "https://example.com"), "feed link")
Test.assert(contains(xml, "Blog about things"), "feed description")
Test.assert(contains(xml, "en"), "feed language")
}
fn test_feed_with_items(): Unit with {Test} = {
let items = [
lib.item("First Post", "https://example.com/first", "First!", "2025-01-29"),
lib.item("Second Post", "https://example.com/second", "Second!", "2025-02-01")
]
let fullFeed = lib.feed("Blog", "https://example.com", "A blog", items)
let xml = lib.render(fullFeed)
Test.assert(contains(xml, "- "), "has item tag")
Test.assert(contains(xml, "First Post"), "first item title")
Test.assert(contains(xml, "Second Post"), "second item title")
Test.assert(contains(xml, "29 Jan 2025 00:00:00 GMT"), "rfc822 date in feed")
Test.assert(contains(xml, ""), "guid with permalink")
}
fn test_feed_with_language(): Unit with {Test} = {
let frFeed = lib.feedWithLang("Mon Blog", "https://example.fr", "Un blog", "fr", [])
let xml = lib.render(frFeed)
Test.assert(contains(xml, "fr"), "custom language")
}
fn test_special_characters(): Unit with {Test} = {
let specialItem = lib.item("A & B ", "https://example.com", "\"quoted\" & ", "2025-01-01")
let specialFeed = lib.feed("Test", "https://example.com", "Test", [specialItem])
let xml = lib.render(specialFeed)
Test.assert(contains(xml, "&"), "ampersand escaped")
Test.assert(contains(xml, "<"), "angle bracket escaped")
}