Skip to content

Commit

Permalink
Rewrite tests to new swift-testing framework
Browse files Browse the repository at this point in the history
Fixes actor isolation issues with XCTestCase.
  • Loading branch information
rechsteiner committed Aug 22, 2024
1 parent 5d7ed95 commit 2e5981d
Show file tree
Hide file tree
Showing 14 changed files with 609 additions and 638 deletions.
180 changes: 90 additions & 90 deletions ParchmentTests/PageViewManagerTests.swift

Large diffs are not rendered by default.

263 changes: 127 additions & 136 deletions ParchmentTests/PagingCollectionViewLayoutTests.swift

Large diffs are not rendered by default.

275 changes: 125 additions & 150 deletions ParchmentTests/PagingControllerTests.swift

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions ParchmentTests/PagingDataStructureTests.swift
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import Foundation
import Testing
@testable import Parchment
import XCTest

final class PagingDataTests: XCTestCase {
var visibleItems: PagingItems!
struct PagingDataTests {
private let visibleItems: PagingItems

override func setUp() {
init() {
visibleItems = PagingItems(items: [
Item(index: 0),
Item(index: 1),
Item(index: 2),
])
}

func testIndexPathForPagingItemFound() {
@Test func indexPathForPagingItemFound() {
let indexPath = visibleItems.indexPath(for: Item(index: 0))!
XCTAssertEqual(indexPath.item, 0)
#expect(indexPath.item == 0)
}

func testIndexPathForPagingItemMissing() {
@Test func indexPathForPagingItemMissing() {
let indexPath = visibleItems.indexPath(for: Item(index: -1))
XCTAssertNil(indexPath)
#expect(indexPath == nil)
}

func testPagingItemForIndexPath() {
@Test func pagingItemForIndexPath() {
let indexPath = IndexPath(item: 0, section: 0)
let pagingItem = visibleItems.pagingItem(for: indexPath) as! Item
XCTAssertEqual(pagingItem, Item(index: 0))
#expect(pagingItem == Item(index: 0))
}

func testDirectionForIndexPathForward() {
@Test func directionForIndexPathForward() {
let currentPagingItem = Item(index: 0)
let upcomingPagingItem = Item(index: 1)
let direction = visibleItems.direction(from: currentPagingItem, to: upcomingPagingItem)
XCTAssertEqual(direction, PagingDirection.forward(sibling: true))
#expect(direction == PagingDirection.forward(sibling: true))
}

func testDirectionForIndexPathReverse() {
@Test func directionForIndexPathReverse() {
let currentPagingItem = Item(index: 1)
let upcomingPagingItem = Item(index: 0)
let direction = visibleItems.direction(from: currentPagingItem, to: upcomingPagingItem)
XCTAssertEqual(direction, PagingDirection.reverse(sibling: true))
#expect(direction == PagingDirection.reverse(sibling: true))
}
}
72 changes: 36 additions & 36 deletions ParchmentTests/PagingDiffTests.swift
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
import Foundation
import Testing
@testable import Parchment
import XCTest

final class PagingDiffTests: XCTestCase {
func testDetectsAddedItemsBeforeCenter() {
struct PagingDiffTests {
@Test func detectsAddedItemsBeforeCenter() {
let from = PagingItems(items: [Item(index: 1)])
let to = PagingItems(items: [Item(index: 0), Item(index: 1)])
let diff = PagingDiff(from: from, to: to)
let added = diff.added()
let removed = diff.removed()

XCTAssertEqual(added.count, 1)
XCTAssertEqual(added[0], IndexPath(item: 0, section: 0))
XCTAssertEqual(removed, [])
#expect(added.count == 1)
#expect(added[0] == IndexPath(item: 0, section: 0))
#expect(removed == [])
}

func testIgnoresAddedItemsAfterCenter() {
@Test func ignoresAddedItemsAfterCenter() {
let from = PagingItems(items: [Item(index: 0)])
let to = PagingItems(items: [Item(index: 0), Item(index: 1)])
let diff = PagingDiff(from: from, to: to)

XCTAssertEqual(diff.added(), [])
XCTAssertEqual(diff.removed(), [])
#expect(diff.added() == [])
#expect(diff.removed() == [])
}

func testIgnoresRemovedItemsAfterCenter() {
@Test func ignoresRemovedItemsAfterCenter() {
let from = PagingItems(items: [Item(index: 0), Item(index: 1)])
let to = PagingItems(items: [Item(index: 0)])
let diff = PagingDiff(from: from, to: to)

XCTAssertEqual(diff.removed(), [])
XCTAssertEqual(diff.added(), [])
#expect(diff.removed() == [])
#expect(diff.added() == [])
}

func testDetectsRemovedItemsBeforeCenter() {
@Test func detectsRemovedItemsBeforeCenter() {
let from = PagingItems(items: [Item(index: 0), Item(index: 1)])
let to = PagingItems(items: [Item(index: 1)])
let diff = PagingDiff(from: from, to: to)
let removed = diff.removed()
let added = diff.added()

XCTAssertEqual(added, [])
XCTAssertEqual(removed.count, 1)
XCTAssertEqual(removed[0], IndexPath(item: 0, section: 0))
#expect(added == [])
#expect(removed.count == 1)
#expect(removed[0] == IndexPath(item: 0, section: 0))
}

// TODO: Reduce these tests to a minimal test case and update
// the descriptions.
func testScenario1() {
@Test func scenario1() {
let from = PagingItems(items: [
Item(index: 16),
Item(index: 17),
Expand Down Expand Up @@ -88,18 +88,18 @@ final class PagingDiffTests: XCTestCase {
let added = diff.added()
let removed = diff.removed()

XCTAssertEqual(removed, [])
XCTAssertEqual(added.count, 7)
XCTAssertEqual(added[0], IndexPath(item: 0, section: 0))
XCTAssertEqual(added[1], IndexPath(item: 1, section: 0))
XCTAssertEqual(added[2], IndexPath(item: 2, section: 0))
XCTAssertEqual(added[3], IndexPath(item: 3, section: 0))
XCTAssertEqual(added[4], IndexPath(item: 4, section: 0))
XCTAssertEqual(added[5], IndexPath(item: 5, section: 0))
XCTAssertEqual(added[6], IndexPath(item: 6, section: 0))
#expect(removed == [])
#expect(added.count == 7)
#expect(added[0] == IndexPath(item: 0, section: 0))
#expect(added[1] == IndexPath(item: 1, section: 0))
#expect(added[2] == IndexPath(item: 2, section: 0))
#expect(added[3] == IndexPath(item: 3, section: 0))
#expect(added[4] == IndexPath(item: 4, section: 0))
#expect(added[5] == IndexPath(item: 5, section: 0))
#expect(added[6] == IndexPath(item: 6, section: 0))
}

func testScenario2() {
@Test func scenario2() {
let from = PagingItems(items: [
Item(index: 0),
Item(index: 1),
Expand Down Expand Up @@ -127,14 +127,14 @@ final class PagingDiffTests: XCTestCase {
let diff = PagingDiff(from: from, to: to)
let removed = diff.removed()

XCTAssertEqual(removed.count, 4)
XCTAssertEqual(removed[0], IndexPath(item: 0, section: 0))
XCTAssertEqual(removed[1], IndexPath(item: 1, section: 0))
XCTAssertEqual(removed[2], IndexPath(item: 2, section: 0))
XCTAssertEqual(removed[3], IndexPath(item: 3, section: 0))
#expect(removed.count == 4)
#expect(removed[0] == IndexPath(item: 0, section: 0))
#expect(removed[1] == IndexPath(item: 1, section: 0))
#expect(removed[2] == IndexPath(item: 2, section: 0))
#expect(removed[3] == IndexPath(item: 3, section: 0))
}

func testScenario3() {
@Test func scenario3() {
let from = PagingItems(items: [
Item(index: 1),
Item(index: 2),
Expand All @@ -151,8 +151,8 @@ final class PagingDiffTests: XCTestCase {
let added = diff.added()
let removed = diff.removed()

XCTAssertEqual(added, [])
XCTAssertEqual(removed.count, 1)
XCTAssertEqual(removed[0], IndexPath(item: 0, section: 0))
#expect(added == [])
#expect(removed.count == 1)
#expect(removed[0] == IndexPath(item: 0, section: 0))
}
}
Loading

0 comments on commit 2e5981d

Please sign in to comment.