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,25 +4,28 @@ import Testing
import Foundation
@testable import Timeline
@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
@Suite("Timeline Filter Tests")
struct TimelineFilterTests {
@Test("All timeline filter can be decoded and encoded",
arguments: [TimelineFilter.home, TimelineFilter.local, TimelineFilter.federated,
TimelineFilter.remoteLocal(server: "me.dm", filter: .local),
TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: nil),
TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: "test"),
TimelineFilter.hashtag(tag: "test", accountId: nil),
TimelineFilter.list(list: .init(id: "test", title: "test"))])
func timelineCanEncodeAndDecode(filter: TimelineFilter) {
#expect(testCodableOn(filter: filter))
}
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
}
let newFilter = try? decoder.decode(TimelineFilter.self, from: data)
return newFilter == filter
}

View file

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