better tests

This commit is contained in:
Thomas Ricouard 2024-06-12 07:57:46 +02:00
parent 0032c1dbf5
commit f82b2b4578
2 changed files with 55 additions and 43 deletions

View file

@ -4,19 +4,20 @@ import Testing
import Foundation import Foundation
@testable import Timeline @testable import Timeline
@Test @Suite("Timeline Filter Tests")
func testTimelineCodableHome() { struct TimelineFilterTests {
#expect(testCodableOn(filter: .home)) @Test("All timeline filter can be decoded and encoded",
#expect(testCodableOn(filter: .local)) arguments: [TimelineFilter.home, TimelineFilter.local, TimelineFilter.federated,
#expect(testCodableOn(filter: .federated)) TimelineFilter.remoteLocal(server: "me.dm", filter: .local),
#expect(testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local))) TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: nil),
#expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil))) TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: "test"),
#expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test"))) TimelineFilter.hashtag(tag: "test", accountId: nil),
#expect(testCodableOn(filter: .hashtag(tag: "test", accountId: nil))) TimelineFilter.list(list: .init(id: "test", title: "test"))])
#expect(testCodableOn(filter: .list(list: .init(id: "test", title: "test")))) func timelineCanEncodeAndDecode(filter: TimelineFilter) {
#expect(testCodableOn(filter: filter))
} }
fileprivate func testCodableOn(filter: TimelineFilter) -> Bool { func testCodableOn(filter: TimelineFilter) -> Bool {
let encoder = JSONEncoder() let encoder = JSONEncoder()
let decoder = JSONDecoder() let decoder = JSONDecoder()
guard let data = try? encoder.encode(filter) else { guard let data = try? encoder.encode(filter) else {
@ -26,3 +27,5 @@ fileprivate func testCodableOn(filter: TimelineFilter) -> Bool {
return newFilter == filter return newFilter == filter
} }
}

View file

@ -2,62 +2,71 @@ import Models
import Network import Network
@testable import Timeline @testable import Timeline
import XCTest import XCTest
import Testing
@MainActor @MainActor
final class TimelineViewModelTests: XCTestCase { @Suite("Timeline View Model tests")
var subject = TimelineViewModel() struct Tests {
func makeSubject() -> TimelineViewModel {
override func setUp() async throws { let subject = TimelineViewModel()
subject = TimelineViewModel()
let client = Client(server: "localhost") let client = Client(server: "localhost")
subject.client = client subject.client = client
subject.timeline = .home subject.timeline = .home
subject.isTimelineVisible = true subject.isTimelineVisible = true
subject.timelineTask?.cancel() subject.timelineTask?.cancel()
return subject
} }
func testStreamEventInsertNewStatus() async throws { @Test
func streamEventInsertNewStatus() async throws {
let subject = makeSubject()
let isEmpty = await subject.datasource.isEmpty let isEmpty = await subject.datasource.isEmpty
XCTAssertTrue(isEmpty) #expect(isEmpty)
await subject.datasource.append(.placeholder()) await subject.datasource.append(.placeholder())
var count = await subject.datasource.count() var count = await subject.datasource.count()
XCTAssertTrue(count == 1) #expect(count == 1)
await subject.handleEvent(event: StreamEventUpdate(status: .placeholder())) await subject.handleEvent(event: StreamEventUpdate(status: .placeholder()))
count = await subject.datasource.count() count = await subject.datasource.count()
XCTAssertTrue(count == 2) #expect(count == 2)
} }
func testStreamEventInsertDuplicateStatus() async throws { @Test
func streamEventInsertDuplicateStatus() async throws {
let subject = makeSubject()
let isEmpty = await subject.datasource.isEmpty let isEmpty = await subject.datasource.isEmpty
XCTAssertTrue(isEmpty) #expect(isEmpty)
let status = Status.placeholder() let status = Status.placeholder()
await subject.datasource.append(status) await subject.datasource.append(status)
var count = await subject.datasource.count() var count = await subject.datasource.count()
XCTAssertTrue(count == 1) #expect(count == 1)
await subject.handleEvent(event: StreamEventUpdate(status: status)) await subject.handleEvent(event: StreamEventUpdate(status: status))
count = await subject.datasource.count() count = await subject.datasource.count()
XCTAssertTrue(count == 1) #expect(count == 1)
} }
func testStreamEventRemove() async throws { @Test
func streamEventRemove() async throws {
let subject = makeSubject()
let isEmpty = await subject.datasource.isEmpty let isEmpty = await subject.datasource.isEmpty
XCTAssertTrue(isEmpty) #expect(isEmpty)
let status = Status.placeholder() let status = Status.placeholder()
await subject.datasource.append(status) await subject.datasource.append(status)
var count = await subject.datasource.count() var count = await subject.datasource.count()
XCTAssertTrue(count == 1) #expect(count == 1)
await subject.handleEvent(event: StreamEventDelete(status: status.id)) await subject.handleEvent(event: StreamEventDelete(status: status.id))
count = await subject.datasource.count() count = await subject.datasource.count()
XCTAssertTrue(count == 0) #expect(count == 0)
} }
func testStreamEventUpdateStatus() async throws { @Test
func streamEventUpdateStatus() async throws {
let subject = makeSubject()
var status = Status.placeholder() var status = Status.placeholder()
let isEmpty = await subject.datasource.isEmpty let isEmpty = await subject.datasource.isEmpty
XCTAssertTrue(isEmpty) #expect(isEmpty)
await subject.datasource.append(status) await subject.datasource.append(status)
var count = await subject.datasource.count() var count = await subject.datasource.count()
XCTAssertTrue(count == 1) #expect(count == 1)
status = .init(id: status.id, status = .init(id: status.id,
content: .init(stringValue: "test"), content: .init(stringValue: "test"),
account: status.account, account: status.account,
@ -88,7 +97,7 @@ final class TimelineViewModelTests: XCTestCase {
await subject.handleEvent(event: StreamEventStatusUpdate(status: status)) await subject.handleEvent(event: StreamEventStatusUpdate(status: status))
let statuses = await subject.datasource.get() let statuses = await subject.datasource.get()
count = await subject.datasource.count() count = await subject.datasource.count()
XCTAssertTrue(count == 1) #expect(count == 1)
XCTAssertTrue(statuses.first?.content.asRawText == "test") #expect(statuses.first?.content.asRawText == "test")
} }
} }