Files
NetNewsWire/Modules/RSParser/Tests/RSParserTests/Feeds/XML/OPMLTests.swift
T

161 lines
5.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// OPMLTests.swift
// RSParser
//
// Created by Brent Simmons on 6/25/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Testing
import RSParser
@Suite struct OPMLTests {
let subsData = parserData("Subs", "opml", "http://example.org/")
@Test func notOPML() {
let d = parserData("DaringFireball", "rss", "http://daringfireball.net/")
#expect(throws: (any Error).self) {
try OPMLParser.parseOPML(with: d)
}
}
@Test func subsStructure() throws {
let opmlDocument = try OPMLParser.parseOPML(with: subsData)
#expect(opmlDocument.title == "Subs")
#expect(opmlDocument.url == "http://example.org/")
Self.recursivelyCheckOPMLStructure(opmlDocument)
}
@Test func findingTitles() throws {
// https://github.com/brentsimmons/NetNewsWire/issues/527
// Fix a bug where titles aren’t found when there’s no title attribute in the OPML,
// which appears to be true with OPML generated by The Old Reader.
let d = parserData("SubsNoTitleAttributes", "opml", "http://example.org/")
let opmlDocument = try OPMLParser.parseOPML(with: d)
Self.recursivelyCheckOPMLStructure(opmlDocument)
}
// MARK: - Helpers
private static func recursivelyCheckOPMLStructure(_ item: OPMLItem, sourceLocation: SourceLocation = #_sourceLocation) {
let feedSpecifier = item.feedSpecifier
if !(item is OPMLDocument) {
#expect(item.attributes?.opmlText != nil, sourceLocation: sourceLocation)
}
// If it has no children, it should have a feed specifier. The converse is also true.
var isFolder = !(item.children?.isEmpty ?? true)
if !isFolder && item.attributes?.opmlTitle == "Skip" {
isFolder = true
}
if !isFolder {
let spec = feedSpecifier
#expect(spec != nil, sourceLocation: sourceLocation)
#expect(spec?.title != nil, sourceLocation: sourceLocation)
#expect(spec?.feedURL.isEmpty == false, sourceLocation: sourceLocation)
} else {
#expect(feedSpecifier == nil, sourceLocation: sourceLocation)
}
if let children = item.children, !children.isEmpty {
for oneItem in children {
recursivelyCheckOPMLStructure(oneItem, sourceLocation: sourceLocation)
}
}
}
// MARK: - Inline fixture tests
@Test func deeplyNestedFolders() throws {
// 3-level nesting: root → L1 → L2 → leaf feed. The parser must preserve
// the hierarchy rather than flatten it. Subs.opml has 2-level nesting;
// this pins down that 3+ works.
let opml = """
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head><title>Nested</title></head>
<body>
<outline text="L1">
<outline text="L2">
<outline text="L3">
<outline text="feed" type="rss" xmlUrl="http://example.com/feed.xml"/>
</outline>
</outline>
</outline>
</body>
</opml>
"""
let d = ParserData(url: "http://example.com/", data: Data(opml.utf8))
let doc = try OPMLParser.parseOPML(with: d)
let l1 = try #require(doc.children?.first)
#expect(l1.attributes?.opmlText == "L1")
let l2 = try #require(l1.children?.first)
#expect(l2.attributes?.opmlText == "L2")
let l3 = try #require(l2.children?.first)
#expect(l3.attributes?.opmlText == "L3")
let leaf = try #require(l3.children?.first)
#expect(leaf.feedSpecifier?.feedURL == "http://example.com/feed.xml")
}
@Test func xmlUrlAttributeIsCaseInsensitive() throws {
// Real-world OPML exporters vary: `xmlUrl`, `xmlurl`, `XMLURL`, and
// even `XmlUrl` have all been seen. The parser must accept all
// casings so imports don't silently drop feeds.
let opml = """
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head><title>Mixed-case attributes</title></head>
<body>
<outline text="camelCase" type="rss" xmlUrl="http://example.com/a.xml"/>
<outline text="lowercase" type="rss" xmlurl="http://example.com/b.xml"/>
<outline text="UPPERCASE" type="rss" XMLURL="http://example.com/c.xml"/>
<outline text="MixedCase" type="rss" XmlUrl="http://example.com/d.xml"/>
</body>
</opml>
"""
let d = ParserData(url: "http://example.com/", data: Data(opml.utf8))
let doc = try OPMLParser.parseOPML(with: d)
let children = try #require(doc.children)
let feedURLs = children.compactMap { $0.feedSpecifier?.feedURL }
#expect(feedURLs.contains("http://example.com/a.xml"))
#expect(feedURLs.contains("http://example.com/b.xml"))
#expect(feedURLs.contains("http://example.com/c.xml"))
#expect(feedURLs.contains("http://example.com/d.xml"))
}
@Test func nonFeedOutlinesParseAsFolders() throws {
// Outlines without an xmlUrl (e.g. bare text notes, folder headers)
// have nil feed-specifier. The parser must still include them so the
// caller can walk the hierarchy.
let opml = """
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head><title>Mixed</title></head>
<body>
<outline text="Just a note"/>
<outline text="A folder">
<outline text="Real feed" type="rss" xmlUrl="http://example.com/feed.xml"/>
</outline>
<outline text="Another note"/>
</body>
</opml>
"""
let d = ParserData(url: "http://example.com/", data: Data(opml.utf8))
let doc = try OPMLParser.parseOPML(with: d)
let children = try #require(doc.children)
#expect(children.count == 3)
// Non-feed outlines: no feed specifier.
#expect(children[0].feedSpecifier == nil)
#expect(children[2].feedSpecifier == nil)
// Middle one is a folder containing the real feed.
let folder = children[1]
#expect(folder.feedSpecifier == nil)
let feed = try #require(folder.children?.first)
#expect(feed.feedSpecifier?.feedURL == "http://example.com/feed.xml")
}
}