Migrate to Swift Tests

This commit is contained in:
Thomas Ricouard 2024-06-12 07:42:02 +02:00
parent 177bc21fce
commit 0032c1dbf5
3 changed files with 155 additions and 144 deletions

View file

@ -2,24 +2,29 @@
import Network import Network
import SwiftUI import SwiftUI
import XCTest import XCTest
import Testing
@Test
@MainActor @MainActor
final class RouterTests: XCTestCase { func testRouterThreadsURL() {
func testRouterThreadsURL() {
let router = RouterPath() let router = RouterPath()
let url = URL(string: "https://www.threads.net/@dimillian")! let url = URL(string: "https://www.threads.net/@dimillian")!
_ = router.handle(url: url) _ = router.handle(url: url)
XCTAssertTrue(router.path.isEmpty) #expect(router.path.isEmpty)
} }
func testRouterTagsURL() { @Test
@MainActor
func testRouterTagsURL() {
let router = RouterPath() let router = RouterPath()
let url = URL(string: "https://mastodon.social/tags/test")! let url = URL(string: "https://mastodon.social/tags/test")!
_ = router.handle(url: url) _ = router.handle(url: url)
XCTAssertTrue(router.path.first == .hashTag(tag: "test", account: nil)) #expect(router.path.first == .hashTag(tag: "test", account: nil))
} }
func testRouterLocalStatusURL() { @Test
@MainActor
func testRouterLocalStatusURL() {
let router = RouterPath() let router = RouterPath()
let client = Client(server: "mastodon.social", let client = Client(server: "mastodon.social",
oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0))
@ -27,10 +32,12 @@ final class RouterTests: XCTestCase {
router.client = client router.client = client
let url = URL(string: "https://mastodon.social/status/1010384")! let url = URL(string: "https://mastodon.social/status/1010384")!
_ = router.handle(url: url) _ = router.handle(url: url)
XCTAssertTrue(router.path.first == .statusDetail(id: "1010384")) #expect(router.path.first == .statusDetail(id: "1010384"))
} }
func testRouterRemoteStatusURL() { @Test
@MainActor
func testRouterRemoteStatusURL() {
let router = RouterPath() let router = RouterPath()
let client = Client(server: "mastodon.social", let client = Client(server: "mastodon.social",
oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0))
@ -38,13 +45,14 @@ final class RouterTests: XCTestCase {
router.client = client router.client = client
let url = URL(string: "https://mastodon.online/status/1010384")! let url = URL(string: "https://mastodon.online/status/1010384")!
_ = router.handle(url: url) _ = router.handle(url: url)
XCTAssertTrue(router.path.first == .remoteStatusDetail(url: url)) #expect(router.path.first == .remoteStatusDetail(url: url))
} }
func testRouteRandomURL() { @Test
@MainActor
func testRouteRandomURL() {
let router = RouterPath() let router = RouterPath()
let url = URL(string: "https://theweb.com/test/test/one")! let url = URL(string: "https://theweb.com/test/test/one")!
_ = router.handle(url: url) _ = router.handle(url: url)
XCTAssertTrue(router.path.isEmpty) #expect(router.path.isEmpty)
}
} }

View file

@ -1,88 +1,88 @@
@testable import Models @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) let simpleUrl = URL(string: "https://www.google.com", encodePath: true)
XCTAssertEqual("https://www.google.com", simpleUrl?.absoluteString) #expect("https://www.google.com" == simpleUrl?.absoluteString)
let urlWithTrailingSlash = URL(string: "https://www.google.com/", encodePath: true) let urlWithTrailingSlash = URL(string: "https://www.google.com/", encodePath: true)
XCTAssertEqual("https://www.google.com/", urlWithTrailingSlash?.absoluteString) #expect("https://www.google.com/" == urlWithTrailingSlash?.absoluteString)
let extendedCharPath = URL(string: "https://en.wikipedia.org/wiki/Elbbrücken_station", encodePath: true) 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) #expect("https://en.wikipedia.org/wiki/Elbbr%C3%BCcken_station" == extendedCharPath?.absoluteString)
let extendedCharQuery = URL(string: "http://test.com/blah/city?name=京都市", encodePath: true) 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) #expect("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 // 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) 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) #expect("https://en.wikipedia.org/wiki/Elbbr%25C3%25BCcken_station" == alreadyEncodedPath?.absoluteString)
} }
func testHTMLStringInit() throws { @Test
func testHTMLStringInit() throws {
let decoder = JSONDecoder() let decoder = JSONDecoder()
let basicContent = "\"<p>This is a test</p>\"" let basicContent = "\"<p>This is a test</p>\""
var htmlString = try decoder.decode(HTMLString.self, from: Data(basicContent.utf8)) var htmlString = try decoder.decode(HTMLString.self, from: Data(basicContent.utf8))
XCTAssertEqual("This is a test", htmlString.asRawText) #expect("This is a test" == htmlString.asRawText)
XCTAssertEqual("<p>This is a test</p>", htmlString.htmlValue) #expect("<p>This is a test</p>" == htmlString.htmlValue)
XCTAssertEqual("This is a test", htmlString.asMarkdown) #expect("This is a test" == htmlString.asMarkdown)
XCTAssertEqual(0, htmlString.statusesURLs.count) #expect(0 == htmlString.statusesURLs.count)
XCTAssertEqual(0, htmlString.links.count) #expect(0 == htmlString.links.count)
let basicLink = "\"<p>This is a <a href=\\\"https://test.com\\\">test</a></p>\"" let basicLink = "\"<p>This is a <a href=\\\"https://test.com\\\">test</a></p>\""
htmlString = try decoder.decode(HTMLString.self, from: Data(basicLink.utf8)) htmlString = try decoder.decode(HTMLString.self, from: Data(basicLink.utf8))
XCTAssertEqual("This is a test", htmlString.asRawText) #expect("This is a test" == htmlString.asRawText)
XCTAssertEqual("<p>This is a <a href=\"https://test.com\">test</a></p>", htmlString.htmlValue) #expect("<p>This is a <a href=\"https://test.com\">test</a></p>" == htmlString.htmlValue)
XCTAssertEqual("This is a [test](https://test.com)", htmlString.asMarkdown) #expect("This is a [test](https://test.com)" == htmlString.asMarkdown)
XCTAssertEqual(0, htmlString.statusesURLs.count) #expect(0 == htmlString.statusesURLs.count)
XCTAssertEqual(1, htmlString.links.count) #expect(1 == htmlString.links.count)
XCTAssertEqual("https://test.com", htmlString.links[0].url.absoluteString) #expect("https://test.com" == htmlString.links[0].url.absoluteString)
XCTAssertEqual("test", htmlString.links[0].displayString) #expect("test" == htmlString.links[0].displayString)
let extendedCharLink = "\"<p>This is a <a href=\\\"https://test.com/goßëña\\\">test</a></p>\"" let extendedCharLink = "\"<p>This is a <a href=\\\"https://test.com/goßëña\\\">test</a></p>\""
htmlString = try decoder.decode(HTMLString.self, from: Data(extendedCharLink.utf8)) htmlString = try decoder.decode(HTMLString.self, from: Data(extendedCharLink.utf8))
XCTAssertEqual("This is a test", htmlString.asRawText) #expect("This is a test" == htmlString.asRawText)
XCTAssertEqual("<p>This is a <a href=\"https://test.com/goßëña\">test</a></p>", htmlString.htmlValue) #expect("<p>This is a <a href=\"https://test.com/goßëña\">test</a></p>" == htmlString.htmlValue)
XCTAssertEqual("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)", htmlString.asMarkdown) #expect("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)" == htmlString.asMarkdown)
XCTAssertEqual(0, htmlString.statusesURLs.count) #expect(0 == htmlString.statusesURLs.count)
XCTAssertEqual(1, htmlString.links.count) #expect(1 == htmlString.links.count)
XCTAssertEqual("https://test.com/go%C3%9F%C3%AB%C3%B1a", htmlString.links[0].url.absoluteString) #expect("https://test.com/go%C3%9F%C3%AB%C3%B1a" == htmlString.links[0].url.absoluteString)
XCTAssertEqual("test", htmlString.links[0].displayString) #expect("test" == htmlString.links[0].displayString)
let alreadyEncodedLink = "\"<p>This is a <a href=\\\"https://test.com/go%C3%9F%C3%AB%C3%B1a\\\">test</a></p>\"" let alreadyEncodedLink = "\"<p>This is a <a href=\\\"https://test.com/go%C3%9F%C3%AB%C3%B1a\\\">test</a></p>\""
htmlString = try decoder.decode(HTMLString.self, from: Data(alreadyEncodedLink.utf8)) htmlString = try decoder.decode(HTMLString.self, from: Data(alreadyEncodedLink.utf8))
XCTAssertEqual("This is a test", htmlString.asRawText) #expect("This is a test" == htmlString.asRawText)
XCTAssertEqual("<p>This is a <a href=\"https://test.com/go%C3%9F%C3%AB%C3%B1a\">test</a></p>", htmlString.htmlValue) #expect("<p>This is a <a href=\"https://test.com/go%C3%9F%C3%AB%C3%B1a\">test</a></p>" == htmlString.htmlValue)
XCTAssertEqual("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)", htmlString.asMarkdown) #expect("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)" == htmlString.asMarkdown)
XCTAssertEqual(0, htmlString.statusesURLs.count) #expect(0 == htmlString.statusesURLs.count)
XCTAssertEqual(1, htmlString.links.count) #expect(1 == htmlString.links.count)
XCTAssertEqual("https://test.com/go%C3%9F%C3%AB%C3%B1a", htmlString.links[0].url.absoluteString) #expect("https://test.com/go%C3%9F%C3%AB%C3%B1a" == htmlString.links[0].url.absoluteString)
XCTAssertEqual("test", htmlString.links[0].displayString) #expect("test" == htmlString.links[0].displayString)
} }
func testHTMLStringInit_markdownEscaping() throws { @Test
func testHTMLStringInit_markdownEscaping() throws {
let decoder = JSONDecoder() let decoder = JSONDecoder()
let stdMarkdownContent = "\"<p>This [*is*] `a`\\n**test**</p>\"" let stdMarkdownContent = "\"<p>This [*is*] `a`\\n**test**</p>\""
var htmlString = try decoder.decode(HTMLString.self, from: Data(stdMarkdownContent.utf8)) var htmlString = try decoder.decode(HTMLString.self, from: Data(stdMarkdownContent.utf8))
XCTAssertEqual("This [*is*] `a`\n**test**", htmlString.asRawText) #expect("This [*is*] `a`\n**test**" == htmlString.asRawText)
XCTAssertEqual("<p>This [*is*] `a`\n**test**</p>", htmlString.htmlValue) #expect("<p>This [*is*] `a`\n**test**</p>" == htmlString.htmlValue)
XCTAssertEqual("This \\[\\*is\\*] \\`a\\` \\*\\*test\\*\\*", htmlString.asMarkdown) #expect("This \\[\\*is\\*] \\`a\\` \\*\\*test\\*\\*" == htmlString.asMarkdown)
let underscoreContent = "\"<p>This _is_ an :emoji_maybe:</p>\"" let underscoreContent = "\"<p>This _is_ an :emoji_maybe:</p>\""
htmlString = try decoder.decode(HTMLString.self, from: Data(underscoreContent.utf8)) htmlString = try decoder.decode(HTMLString.self, from: Data(underscoreContent.utf8))
XCTAssertEqual("This _is_ an :emoji_maybe:", htmlString.asRawText) #expect("This _is_ an :emoji_maybe:" == htmlString.asRawText)
XCTAssertEqual("<p>This _is_ an :emoji_maybe:</p>", htmlString.htmlValue) #expect("<p>This _is_ an :emoji_maybe:</p>" == htmlString.htmlValue)
XCTAssertEqual("This \\_is\\_ an :emoji_maybe:", htmlString.asMarkdown) #expect("This \\_is\\_ an :emoji_maybe:" == htmlString.asMarkdown)
let strikeContent = "\"<p>This ~is~ a\\n`test`</p>\"" let strikeContent = "\"<p>This ~is~ a\\n`test`</p>\""
htmlString = try decoder.decode(HTMLString.self, from: Data(strikeContent.utf8)) htmlString = try decoder.decode(HTMLString.self, from: Data(strikeContent.utf8))
XCTAssertEqual("This ~is~ a\n`test`", htmlString.asRawText) #expect("This ~is~ a\n`test`" == htmlString.asRawText)
XCTAssertEqual("<p>This ~is~ a\n`test`</p>", htmlString.htmlValue) #expect("<p>This ~is~ a\n`test`</p>" == htmlString.htmlValue)
XCTAssertEqual("This \\~is\\~ a \\`test\\`", htmlString.asMarkdown) #expect("This \\~is\\~ a \\`test\\`" == htmlString.asMarkdown)
}
} }

View file

@ -1,25 +1,28 @@
import Models import Models
import Network import Network
import Testing
import Foundation
@testable import Timeline @testable import Timeline
import XCTest
final class TimelineFilterTests: XCTestCase { @Test
func testCodableHome() throws { func testTimelineCodableHome() {
XCTAssertTrue(try testCodableOn(filter: .home)) #expect(testCodableOn(filter: .home))
XCTAssertTrue(try testCodableOn(filter: .local)) #expect(testCodableOn(filter: .local))
XCTAssertTrue(try testCodableOn(filter: .federated)) #expect(testCodableOn(filter: .federated))
XCTAssertTrue(try testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local))) #expect(testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local)))
XCTAssertTrue(try testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil))) #expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil)))
XCTAssertTrue(try testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test"))) #expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test")))
XCTAssertTrue(try testCodableOn(filter: .hashtag(tag: "test", accountId: nil))) #expect(testCodableOn(filter: .hashtag(tag: "test", accountId: nil)))
XCTAssertTrue(try testCodableOn(filter: .list(list: .init(id: "test", title: "test")))) #expect(testCodableOn(filter: .list(list: .init(id: "test", title: "test"))))
} }
private func testCodableOn(filter: TimelineFilter) throws -> Bool { fileprivate func testCodableOn(filter: TimelineFilter) -> Bool {
let encoder = JSONEncoder() let encoder = JSONEncoder()
let decoder = JSONDecoder() let decoder = JSONDecoder()
let data = try encoder.encode(filter) guard let data = try? encoder.encode(filter) else {
let newFilter = try decoder.decode(TimelineFilter.self, from: data) return false
return newFilter == filter
} }
let newFilter = try? decoder.decode(TimelineFilter.self, from: data)
return newFilter == filter
} }