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

72 lines
2.2 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
import RSParserObjC
@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 RSOPMLParser.parseOPML(with: d)
}
}
@Test func subsStructure() throws {
let opmlDocument = try RSOPMLParser.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 RSOPMLParser.parseOPML(with: d)
Self.recursivelyCheckOPMLStructure(opmlDocument)
}
// MARK: - Helpers
private static func recursivelyCheckOPMLStructure(_ item: RSOPMLItem, sourceLocation: SourceLocation = #_sourceLocation) {
let feedSpecifier = item.feedSpecifier
if !(item is RSOPMLDocument) {
#expect((item.attributes! as NSDictionary).opml_text != nil, sourceLocation: sourceLocation)
}
// If it has no children, it should have a feed specifier. The converse is also true.
var isFolder = item.children != nil && item.children!.count > 0
if !isFolder && (item.attributes! as NSDictionary).opml_title == "Skip" {
isFolder = true
}
if !isFolder {
let title: String? = feedSpecifier!.title
let feedURL: String? = feedSpecifier!.feedURL
#expect(title != nil, sourceLocation: sourceLocation)
#expect(feedURL != nil, sourceLocation: sourceLocation)
} else {
#expect(feedSpecifier == nil, sourceLocation: sourceLocation)
}
if let children = item.children, children.count > 0 {
for oneItem in children {
recursivelyCheckOPMLStructure(oneItem, sourceLocation: sourceLocation)
}
}
}
}