diff --git a/Packages/Env/Tests/RouterTests.swift b/Packages/Env/Tests/RouterTests.swift index d14fd627..d1522beb 100644 --- a/Packages/Env/Tests/RouterTests.swift +++ b/Packages/Env/Tests/RouterTests.swift @@ -2,49 +2,57 @@ import Network import SwiftUI import XCTest +import Testing +@Test @MainActor -final class RouterTests: XCTestCase { - func testRouterThreadsURL() { - let router = RouterPath() - let url = URL(string: "https://www.threads.net/@dimillian")! - _ = router.handle(url: url) - XCTAssertTrue(router.path.isEmpty) - } - - func testRouterTagsURL() { - let router = RouterPath() - let url = URL(string: "https://mastodon.social/tags/test")! - _ = router.handle(url: url) - XCTAssertTrue(router.path.first == .hashTag(tag: "test", account: nil)) - } - - func testRouterLocalStatusURL() { - let router = RouterPath() - let client = Client(server: "mastodon.social", - oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) - client.addConnections(["mastodon.social"]) - router.client = client - let url = URL(string: "https://mastodon.social/status/1010384")! - _ = router.handle(url: url) - XCTAssertTrue(router.path.first == .statusDetail(id: "1010384")) - } - - func testRouterRemoteStatusURL() { - let router = RouterPath() - let client = Client(server: "mastodon.social", - oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) - client.addConnections(["mastodon.social", "mastodon.online"]) - router.client = client - let url = URL(string: "https://mastodon.online/status/1010384")! - _ = router.handle(url: url) - XCTAssertTrue(router.path.first == .remoteStatusDetail(url: url)) - } - - func testRouteRandomURL() { - let router = RouterPath() - let url = URL(string: "https://theweb.com/test/test/one")! - _ = router.handle(url: url) - XCTAssertTrue(router.path.isEmpty) - } +func testRouterThreadsURL() { + let router = RouterPath() + let url = URL(string: "https://www.threads.net/@dimillian")! + _ = router.handle(url: url) + #expect(router.path.isEmpty) +} + +@Test +@MainActor +func testRouterTagsURL() { + let router = RouterPath() + let url = URL(string: "https://mastodon.social/tags/test")! + _ = router.handle(url: url) + #expect(router.path.first == .hashTag(tag: "test", account: nil)) +} + +@Test +@MainActor +func testRouterLocalStatusURL() { + let router = RouterPath() + let client = Client(server: "mastodon.social", + oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) + client.addConnections(["mastodon.social"]) + router.client = client + let url = URL(string: "https://mastodon.social/status/1010384")! + _ = router.handle(url: url) + #expect(router.path.first == .statusDetail(id: "1010384")) +} + +@Test +@MainActor +func testRouterRemoteStatusURL() { + let router = RouterPath() + let client = Client(server: "mastodon.social", + oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) + client.addConnections(["mastodon.social", "mastodon.online"]) + router.client = client + let url = URL(string: "https://mastodon.online/status/1010384")! + _ = router.handle(url: url) + #expect(router.path.first == .remoteStatusDetail(url: url)) +} + +@Test +@MainActor +func testRouteRandomURL() { + let router = RouterPath() + let url = URL(string: "https://theweb.com/test/test/one")! + _ = router.handle(url: url) + #expect(router.path.isEmpty) } diff --git a/Packages/Models/Tests/ModelsTests/HTMLStringTests.swift b/Packages/Models/Tests/ModelsTests/HTMLStringTests.swift index 1f674cd3..1bf3ab5b 100644 --- a/Packages/Models/Tests/ModelsTests/HTMLStringTests.swift +++ b/Packages/Models/Tests/ModelsTests/HTMLStringTests.swift @@ -1,88 +1,88 @@ @testable import Models -import XCTest +import Testing +import Foundation -final class HTMLStringTests: XCTestCase { - func testURLInit() throws { - XCTAssertNil(URL(string: "", encodePath: true)) +@Test +func testURLInit() throws { + let simpleUrl = URL(string: "https://www.google.com", encodePath: true) + #expect("https://www.google.com" == simpleUrl?.absoluteString) - let simpleUrl = URL(string: "https://www.google.com", encodePath: true) - XCTAssertEqual("https://www.google.com", simpleUrl?.absoluteString) + let urlWithTrailingSlash = URL(string: "https://www.google.com/", encodePath: true) + #expect("https://www.google.com/" == urlWithTrailingSlash?.absoluteString) - let urlWithTrailingSlash = URL(string: "https://www.google.com/", encodePath: true) - XCTAssertEqual("https://www.google.com/", urlWithTrailingSlash?.absoluteString) + let extendedCharPath = URL(string: "https://en.wikipedia.org/wiki/Elbbrücken_station", encodePath: true) + #expect("https://en.wikipedia.org/wiki/Elbbr%C3%BCcken_station" == extendedCharPath?.absoluteString) - let extendedCharPath = URL(string: "https://en.wikipedia.org/wiki/Elbbrücken_station", encodePath: true) - XCTAssertEqual("https://en.wikipedia.org/wiki/Elbbr%C3%BCcken_station", extendedCharPath?.absoluteString) + let extendedCharQuery = URL(string: "http://test.com/blah/city?name=京都市", encodePath: true) + #expect("http://test.com/blah/city?name=%E4%BA%AC%E9%83%BD%E5%B8%82" == extendedCharQuery?.absoluteString) - let extendedCharQuery = URL(string: "http://test.com/blah/city?name=京都市", encodePath: true) - XCTAssertEqual("http://test.com/blah/city?name=%E4%BA%AC%E9%83%BD%E5%B8%82", extendedCharQuery?.absoluteString) - - // Double encoding will happen if you ask to encodePath on an already encoded string - let alreadyEncodedPath = URL(string: "https://en.wikipedia.org/wiki/Elbbr%C3%BCcken_station", encodePath: true) - XCTAssertEqual("https://en.wikipedia.org/wiki/Elbbr%25C3%25BCcken_station", alreadyEncodedPath?.absoluteString) - } - - func testHTMLStringInit() throws { - let decoder = JSONDecoder() - - let basicContent = "\"
This is a test
\"" - var htmlString = try decoder.decode(HTMLString.self, from: Data(basicContent.utf8)) - XCTAssertEqual("This is a test", htmlString.asRawText) - XCTAssertEqual("This is a test
", htmlString.htmlValue) - XCTAssertEqual("This is a test", htmlString.asMarkdown) - XCTAssertEqual(0, htmlString.statusesURLs.count) - XCTAssertEqual(0, htmlString.links.count) - - let basicLink = "\"This is a test
\"" - htmlString = try decoder.decode(HTMLString.self, from: Data(basicLink.utf8)) - XCTAssertEqual("This is a test", htmlString.asRawText) - XCTAssertEqual("This is a test
", htmlString.htmlValue) - XCTAssertEqual("This is a [test](https://test.com)", htmlString.asMarkdown) - XCTAssertEqual(0, htmlString.statusesURLs.count) - XCTAssertEqual(1, htmlString.links.count) - XCTAssertEqual("https://test.com", htmlString.links[0].url.absoluteString) - XCTAssertEqual("test", htmlString.links[0].displayString) - - let extendedCharLink = "\"This is a test
\"" - htmlString = try decoder.decode(HTMLString.self, from: Data(extendedCharLink.utf8)) - XCTAssertEqual("This is a test", htmlString.asRawText) - XCTAssertEqual("This is a test
", htmlString.htmlValue) - XCTAssertEqual("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)", htmlString.asMarkdown) - XCTAssertEqual(0, htmlString.statusesURLs.count) - XCTAssertEqual(1, htmlString.links.count) - XCTAssertEqual("https://test.com/go%C3%9F%C3%AB%C3%B1a", htmlString.links[0].url.absoluteString) - XCTAssertEqual("test", htmlString.links[0].displayString) - - let alreadyEncodedLink = "\"This is a test
\"" - htmlString = try decoder.decode(HTMLString.self, from: Data(alreadyEncodedLink.utf8)) - XCTAssertEqual("This is a test", htmlString.asRawText) - XCTAssertEqual("This is a test
", htmlString.htmlValue) - XCTAssertEqual("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)", htmlString.asMarkdown) - XCTAssertEqual(0, htmlString.statusesURLs.count) - XCTAssertEqual(1, htmlString.links.count) - XCTAssertEqual("https://test.com/go%C3%9F%C3%AB%C3%B1a", htmlString.links[0].url.absoluteString) - XCTAssertEqual("test", htmlString.links[0].displayString) - } - - func testHTMLStringInit_markdownEscaping() throws { - let decoder = JSONDecoder() - - let stdMarkdownContent = "\"This [*is*] `a`\\n**test**
\"" - var htmlString = try decoder.decode(HTMLString.self, from: Data(stdMarkdownContent.utf8)) - XCTAssertEqual("This [*is*] `a`\n**test**", htmlString.asRawText) - XCTAssertEqual("This [*is*] `a`\n**test**
", htmlString.htmlValue) - XCTAssertEqual("This \\[\\*is\\*] \\`a\\` \\*\\*test\\*\\*", htmlString.asMarkdown) - - let underscoreContent = "\"This _is_ an :emoji_maybe:
\"" - htmlString = try decoder.decode(HTMLString.self, from: Data(underscoreContent.utf8)) - XCTAssertEqual("This _is_ an :emoji_maybe:", htmlString.asRawText) - XCTAssertEqual("This _is_ an :emoji_maybe:
", htmlString.htmlValue) - XCTAssertEqual("This \\_is\\_ an :emoji_maybe:", htmlString.asMarkdown) - - let strikeContent = "\"This ~is~ a\\n`test`
\"" - htmlString = try decoder.decode(HTMLString.self, from: Data(strikeContent.utf8)) - XCTAssertEqual("This ~is~ a\n`test`", htmlString.asRawText) - XCTAssertEqual("This ~is~ a\n`test`
", htmlString.htmlValue) - XCTAssertEqual("This \\~is\\~ a \\`test\\`", htmlString.asMarkdown) - } + // Double encoding will happen if you ask to encodePath on an already encoded string + let alreadyEncodedPath = URL(string: "https://en.wikipedia.org/wiki/Elbbr%C3%BCcken_station", encodePath: true) + #expect("https://en.wikipedia.org/wiki/Elbbr%25C3%25BCcken_station" == alreadyEncodedPath?.absoluteString) +} + +@Test +func testHTMLStringInit() throws { + let decoder = JSONDecoder() + + let basicContent = "\"This is a test
\"" + var htmlString = try decoder.decode(HTMLString.self, from: Data(basicContent.utf8)) + #expect("This is a test" == htmlString.asRawText) + #expect("This is a test
" == htmlString.htmlValue) + #expect("This is a test" == htmlString.asMarkdown) + #expect(0 == htmlString.statusesURLs.count) + #expect(0 == htmlString.links.count) + + let basicLink = "\"This is a test
\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(basicLink.utf8)) + #expect("This is a test" == htmlString.asRawText) + #expect("This is a test
" == htmlString.htmlValue) + #expect("This is a [test](https://test.com)" == htmlString.asMarkdown) + #expect(0 == htmlString.statusesURLs.count) + #expect(1 == htmlString.links.count) + #expect("https://test.com" == htmlString.links[0].url.absoluteString) + #expect("test" == htmlString.links[0].displayString) + + let extendedCharLink = "\"This is a test
\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(extendedCharLink.utf8)) + #expect("This is a test" == htmlString.asRawText) + #expect("This is a test
" == htmlString.htmlValue) + #expect("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)" == htmlString.asMarkdown) + #expect(0 == htmlString.statusesURLs.count) + #expect(1 == htmlString.links.count) + #expect("https://test.com/go%C3%9F%C3%AB%C3%B1a" == htmlString.links[0].url.absoluteString) + #expect("test" == htmlString.links[0].displayString) + + let alreadyEncodedLink = "\"This is a test
\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(alreadyEncodedLink.utf8)) + #expect("This is a test" == htmlString.asRawText) + #expect("This is a test
" == htmlString.htmlValue) + #expect("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)" == htmlString.asMarkdown) + #expect(0 == htmlString.statusesURLs.count) + #expect(1 == htmlString.links.count) + #expect("https://test.com/go%C3%9F%C3%AB%C3%B1a" == htmlString.links[0].url.absoluteString) + #expect("test" == htmlString.links[0].displayString) +} + +@Test +func testHTMLStringInit_markdownEscaping() throws { + let decoder = JSONDecoder() + + let stdMarkdownContent = "\"This [*is*] `a`\\n**test**
\"" + var htmlString = try decoder.decode(HTMLString.self, from: Data(stdMarkdownContent.utf8)) + #expect("This [*is*] `a`\n**test**" == htmlString.asRawText) + #expect("This [*is*] `a`\n**test**
" == htmlString.htmlValue) + #expect("This \\[\\*is\\*] \\`a\\` \\*\\*test\\*\\*" == htmlString.asMarkdown) + + let underscoreContent = "\"This _is_ an :emoji_maybe:
\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(underscoreContent.utf8)) + #expect("This _is_ an :emoji_maybe:" == htmlString.asRawText) + #expect("This _is_ an :emoji_maybe:
" == htmlString.htmlValue) + #expect("This \\_is\\_ an :emoji_maybe:" == htmlString.asMarkdown) + + let strikeContent = "\"This ~is~ a\\n`test`
\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(strikeContent.utf8)) + #expect("This ~is~ a\n`test`" == htmlString.asRawText) + #expect("This ~is~ a\n`test`
" == htmlString.htmlValue) + #expect("This \\~is\\~ a \\`test\\`" == htmlString.asMarkdown) } diff --git a/Packages/Timeline/Tests/TimelineTests/TimelineFilterTests.swift b/Packages/Timeline/Tests/TimelineTests/TimelineFilterTests.swift index 31d544b9..490945a0 100644 --- a/Packages/Timeline/Tests/TimelineTests/TimelineFilterTests.swift +++ b/Packages/Timeline/Tests/TimelineTests/TimelineFilterTests.swift @@ -1,25 +1,28 @@ import Models import Network +import Testing +import Foundation @testable import Timeline -import XCTest -final class TimelineFilterTests: XCTestCase { - func testCodableHome() throws { - XCTAssertTrue(try testCodableOn(filter: .home)) - XCTAssertTrue(try testCodableOn(filter: .local)) - XCTAssertTrue(try testCodableOn(filter: .federated)) - XCTAssertTrue(try testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local))) - XCTAssertTrue(try testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil))) - XCTAssertTrue(try testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test"))) - XCTAssertTrue(try testCodableOn(filter: .hashtag(tag: "test", accountId: nil))) - XCTAssertTrue(try testCodableOn(filter: .list(list: .init(id: "test", title: "test")))) - } - - private func testCodableOn(filter: TimelineFilter) throws -> Bool { - let encoder = JSONEncoder() - let decoder = JSONDecoder() - let data = try encoder.encode(filter) - let newFilter = try decoder.decode(TimelineFilter.self, from: data) - return newFilter == filter - } +@Test +func testTimelineCodableHome() { + #expect(testCodableOn(filter: .home)) + #expect(testCodableOn(filter: .local)) + #expect(testCodableOn(filter: .federated)) + #expect(testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local))) + #expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil))) + #expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test"))) + #expect(testCodableOn(filter: .hashtag(tag: "test", accountId: nil))) + #expect(testCodableOn(filter: .list(list: .init(id: "test", title: "test")))) +} + +fileprivate func testCodableOn(filter: TimelineFilter) -> Bool { + let encoder = JSONEncoder() + let decoder = JSONDecoder() + guard let data = try? encoder.encode(filter) else { + return false + } + let newFilter = try? decoder.decode(TimelineFilter.self, from: data) + return newFilter == filter + }