Convert to Swift testing.

This commit is contained in:
Brent Simmons committed 2026-12-03 17:31:03 -08:00
1 parent d808c01ee9
commit c9605ee0d9
5 files changed
+73 -74

No files matched your search

@@ -6,23 +6,23 @@
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Testing
@testable import Account
@MainActor final class FeedlyCollectionParserTests: XCTestCase {
@MainActor struct FeedlyCollectionParserTests {
func testParsing() {
@Test func parsing() {
let collection = FeedlyCollection(feeds: [], label: "Test Collection", id: "test/collection/1")
let parser = FeedlyCollectionParser(collection: collection)
XCTAssertEqual(parser.folderName, collection.label)
XCTAssertEqual(parser.externalID, collection.id)
#expect(parser.folderName == collection.label)
#expect(parser.externalID == collection.id)
}
func testSanitization() {
@Test func sanitization() {
let name = "Test Collection"
let collection = FeedlyCollection(feeds: [], label: "<div style=\"direction:rtl;text-align:right\">\(name)</div>", id: "test/collection/1")
let parser = FeedlyCollectionParser(collection: collection)
XCTAssertEqual(parser.folderName, name)
XCTAssertEqual(parser.externalID, collection.id)
#expect(parser.folderName == name)
#expect(parser.externalID == collection.id)
}
}
@@ -6,12 +6,13 @@
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Foundation
import Testing
@testable import Account
@MainActor final class FeedlyEntryParserTests: XCTestCase {
@MainActor struct FeedlyEntryParserTests {
func testParsing() {
@Test func parsing() throws {
let content = FeedlyEntry.Content(content: "Test Content", direction: .leftToRight)
let summary = FeedlyEntry.Content(content: "Test Summary", direction: .leftToRight)
let origin = FeedlyOrigin(title: "Test Feed", streamID: "tests://feeds/1", htmlURL: nil)
@@ -39,42 +40,39 @@ import XCTest
let parser = FeedlyEntryParser(entry: entry)
XCTAssertEqual(parser.id, entry.id)
XCTAssertEqual(parser.feedURL, origin.streamID)
XCTAssertEqual(parser.externalURL, canonicalLink.href)
XCTAssertEqual(parser.title, entry.title)
XCTAssertEqual(parser.contentHTML, content.content)
XCTAssertEqual(parser.summary, summary.content)
XCTAssertEqual(parser.datePublished, .distantPast)
XCTAssertEqual(parser.dateModified, Date(timeIntervalSinceReferenceDate: 0))
#expect(parser.id == entry.id)
#expect(parser.feedURL == origin.streamID)
#expect(parser.externalURL == canonicalLink.href)
#expect(parser.title == entry.title)
#expect(parser.contentHTML == content.content)
#expect(parser.summary == summary.content)
#expect(parser.datePublished == .distantPast)
#expect(parser.dateModified == Date(timeIntervalSinceReferenceDate: 0))
guard let item = parser.parsedItemRepresentation else {
XCTFail("Expected a parsed item representation.")
return
}
let item = try #require(parser.parsedItemRepresentation)
XCTAssertEqual(item.syncServiceID, entry.id)
XCTAssertEqual(item.uniqueID, entry.id)
#expect(item.syncServiceID == entry.id)
#expect(item.uniqueID == entry.id)
// The following is not an error.
// The feedURL must match the feedID for the article to be connected to its matching feed.
XCTAssertEqual(item.feedURL, origin.streamID)
XCTAssertEqual(item.title, entry.title)
XCTAssertEqual(item.contentHTML, content.content)
XCTAssertEqual(item.contentText, nil, "Is it now free of HTML characters?")
XCTAssertEqual(item.summary, summary.content)
XCTAssertEqual(item.datePublished, entry.crawled)
XCTAssertEqual(item.dateModified, entry.recrawled)
#expect(item.feedURL == origin.streamID)
#expect(item.title == entry.title)
#expect(item.contentHTML == content.content)
#expect(item.contentText == nil, "Is it now free of HTML characters?")
#expect(item.summary == summary.content)
#expect(item.datePublished == entry.crawled)
#expect(item.dateModified == entry.recrawled)
let expectedTags = Set(tags.compactMap { $0.label })
XCTAssertEqual(item.tags, expectedTags)
#expect(item.tags == expectedTags)
let expectedAuthors = Set([entry.author])
let calculatedAuthors = Set(item.authors?.compactMap { $0.name } ?? [])
XCTAssertEqual(calculatedAuthors, expectedAuthors)
#expect(calculatedAuthors == expectedAuthors)
}
func testSanitization() {
@Test func sanitization() {
let content = FeedlyEntry.Content(content: "<div style=\"direction:rtl;text-align:right\">Test Content</div>", direction: .rightToLeft)
let summaryContent = "Test Summary"
let summary = FeedlyEntry.Content(content: "<div style=\"direction:rtl;text-align:right\">\(summaryContent)</div>", direction: .rightToLeft)
@@ -100,14 +98,14 @@ import XCTest
let parser = FeedlyEntryParser(entry: entry)
// These should be sanitized
XCTAssertEqual(parser.title, title)
XCTAssertEqual(parser.summary, summaryContent)
#expect(parser.title == title)
#expect(parser.summary == summaryContent)
// These should not be sanitized because it is supposed to be HTML content.
XCTAssertEqual(parser.contentHTML, content.content)
#expect(parser.contentHTML == content.content)
}
func testLocatesCanonicalExternalURL() {
@Test func locatesCanonicalExternalURL() {
let canonicalLink = FeedlyLink(href: "tests://feeds/1/entries/1", type: "text/html")
let alternateLink = FeedlyLink(href: "tests://feeds/1/entries/alternate/1", type: "text/html")
let entry = FeedlyEntry(id: "tests/feeds/1/entries/1",
@@ -129,10 +127,10 @@ import XCTest
let parser = FeedlyEntryParser(entry: entry)
XCTAssertEqual(parser.externalURL, canonicalLink.href)
#expect(parser.externalURL == canonicalLink.href)
}
func testLocatesAlternateExternalURL() {
@Test func locatesAlternateExternalURL() {
let canonicalLink = FeedlyLink(href: "tests://feeds/1/entries/1", type: "text/json")
let alternateLink = FeedlyLink(href: "tests://feeds/1/entries/alternate/1", type: nil)
let entry = FeedlyEntry(id: "tests/feeds/1/entries/1",
@@ -154,10 +152,10 @@ import XCTest
let parser = FeedlyEntryParser(entry: entry)
XCTAssertEqual(parser.externalURL, alternateLink.href)
#expect(parser.externalURL == alternateLink.href)
}
func testContentPreferredToSummary() {
@Test func contentPreferredToSummary() {
let content = FeedlyEntry.Content(content: "Test Content", direction: .leftToRight)
let summary = FeedlyEntry.Content(content: "Test Summary", direction: .leftToRight)
let entry = FeedlyEntry(id: "tests/feeds/1/entries/1",
@@ -179,26 +177,26 @@ import XCTest
let parser = FeedlyEntryParser(entry: entry)
XCTAssertEqual(parser.contentHTML, content.content)
#expect(parser.contentHTML == content.content)
}
func testPublishedDateUsedWhenNotAfterCrawlDate() {
@Test func publishedDateUsedWhenNotAfterCrawlDate() {
let crawled = Date(timeIntervalSinceReferenceDate: 1_000_000)
let published = Date(timeIntervalSinceReferenceDate: 500_000)
let entry = makeEntry(crawled: crawled, published: published)
XCTAssertEqual(FeedlyEntryParser(entry: entry).datePublished, published)
#expect(FeedlyEntryParser(entry: entry).datePublished == published)
}
func testFutureDatedPublishedDateFallsBackToCrawlDate() {
@Test func futureDatedPublishedDateFallsBackToCrawlDate() {
let crawled = Date(timeIntervalSinceReferenceDate: 1_000_000)
let published = Date(timeIntervalSinceReferenceDate: 2_000_000)
let entry = makeEntry(crawled: crawled, published: published)
XCTAssertEqual(FeedlyEntryParser(entry: entry).datePublished, crawled)
#expect(FeedlyEntryParser(entry: entry).datePublished == crawled)
}
func testSummaryUsedAsContentWhenContentMissing() {
@Test func summaryUsedAsContentWhenContentMissing() {
let summary = FeedlyEntry.Content(content: "Test Summary", direction: .leftToRight)
let entry = FeedlyEntry(id: "tests/feeds/1/entries/1",
title: "Test Entry 1",
@@ -219,7 +217,7 @@ import XCTest
let parser = FeedlyEntryParser(entry: entry)
XCTAssertEqual(parser.contentHTML, summary.content)
#expect(parser.contentHTML == summary.content)
}
private func makeEntry(crawled: Date, published: Date?) -> FeedlyEntry {
@@ -6,12 +6,13 @@
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Foundation
import Testing
@testable import Account
@MainActor final class FeedlyFeedParserTests: XCTestCase {
@MainActor struct FeedlyFeedParserTests {
func testParsing() {
@Test func parsing() {
let name = "Test Feed"
let website = "tests://nnw/feed/1"
let url = "tests://nnw/feed.xml"
@@ -19,13 +20,13 @@ import XCTest
let updated = Date.distantPast
let feed = FeedlyFeed(id: id, title: name, updated: updated, website: website)
let parser = FeedlyFeedParser(feed: feed)
XCTAssertEqual(parser.title, name)
XCTAssertEqual(parser.homePageURL, website)
XCTAssertEqual(parser.url, url)
XCTAssertEqual(parser.feedID, id)
#expect(parser.title == name)
#expect(parser.homePageURL == website)
#expect(parser.url == url)
#expect(parser.feedID == id)
}
func testSanitization() {
@Test func sanitization() {
let name = "Test Feed"
let website = "tests://nnw/feed/1"
let url = "tests://nnw/feed.xml"
@@ -33,9 +34,9 @@ import XCTest
let updated = Date.distantPast
let feed = FeedlyFeed(id: id, title: "<div style=\"direction:rtl;text-align:right\">\(name)</div>", updated: updated, website: website)
let parser = FeedlyFeedParser(feed: feed)
XCTAssertEqual(parser.title, name)
XCTAssertEqual(parser.homePageURL, website)
XCTAssertEqual(parser.url, url)
XCTAssertEqual(parser.feedID, id)
#expect(parser.title == name)
#expect(parser.homePageURL == website)
#expect(parser.url == url)
#expect(parser.feedID == id)
}
}
@@ -6,12 +6,12 @@
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Testing
@testable import Account
@MainActor final class FeedlyResourceIDTests: XCTestCase {
@MainActor struct FeedlyResourceIDTests {
func testFeedResourceID() {
@Test func feedResourceID() {
let expectedURL = "http://ranchero.com/blog/atom.xml"
let feedResource = FeedlyFeedResourceID(id: "feed/\(expectedURL)")
@@ -19,9 +19,9 @@ import XCTest
let otherResource = FeedlyFeedResourceID(id: "whiskey/\(expectedURL)")
let invalidResource = FeedlyFeedResourceID(id: "")
XCTAssertEqual(feedResource.url, expectedURL)
XCTAssertEqual(urlResource.url, expectedURL)
XCTAssertEqual(otherResource.url, otherResource.id)
XCTAssertEqual(invalidResource.url, invalidResource.id)
#expect(feedResource.url == expectedURL)
#expect(urlResource.url == expectedURL)
#expect(otherResource.url == otherResource.id)
#expect(invalidResource.url == invalidResource.id)
}
}
@@ -6,12 +6,12 @@
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Testing
@testable import Account
final class FeedlyTextSanitizationTests: XCTestCase {
struct FeedlyTextSanitizationTests {
func testRTLSanitization() {
@Test func rtlSanitization() {
let targetsAndExpectations: [(target: String?, expectation: String?)] = [
(nil, nil),
@@ -25,14 +25,14 @@ final class FeedlyTextSanitizationTests: XCTestCase {
("<div style=\"direction:rtl;text-align:right\"></div>", ""),
("<DIV style=\"direction:rtl;text-align:right\"></div>", "<DIV style=\"direction:rtl;text-align:right\"></div>"),
("<div style=\"direction:rtl;text-align:right\"></DIV>", "<div style=\"direction:rtl;text-align:right\"></DIV>"),
("<div style=\"direction:rtl;text-align:right\">text</div>", "text"),
("<div style=\"direction:rtl;text-align:right\">text</div>", "text")
]
let sanitizer = FeedlyRTLTextSanitizer()
for (target, expectation) in targetsAndExpectations {
let calculated = sanitizer.sanitize(target)
XCTAssertEqual(expectation, calculated)
#expect(calculated == expectation)
}
}
}