Skip to content

Commit

Permalink
Fix concurrency warning in with XCTestCase and @mainactor
Browse files Browse the repository at this point in the history
Need to apply @mainactor to each individual test case instead:
https://forums.swift.org/t/swift-5-10-concurrency-and-xctest/69929
  • Loading branch information
rechsteiner committed May 24, 2024
1 parent ac733ce commit 6c96c13
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions ParchmentTests/PagingControllerTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation
@testable import Parchment
import XCTest
@testable import Parchment

@MainActor
final class PagingControllerTests: XCTestCase {
static let ItemSize: CGFloat = 50

Expand All @@ -14,6 +13,7 @@ final class PagingControllerTests: XCTestCase {
var sizeDelegate: MockPagingControllerSizeDelegate?
var pagingController: PagingController!

@MainActor
override func setUp() {
options = PagingOptions()
options.selectedScrollPosition = .left
Expand Down Expand Up @@ -50,6 +50,7 @@ final class PagingControllerTests: XCTestCase {

// MARK: - Content scrolled

@MainActor
func testContentScrolledFromSelectedProgressPositive() {
// Select the first item.
pagingController.select(pagingItem: Item(index: 3), animated: false)
Expand Down Expand Up @@ -81,10 +82,11 @@ final class PagingControllerTests: XCTestCase {
)),
.collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: false
)),
)),
])
}

@MainActor
func testContentScrolledFromSelectedProgressNegative() {
// Select the first item.
pagingController.select(pagingItem: Item(index: 3), animated: false)
Expand Down Expand Up @@ -116,10 +118,11 @@ final class PagingControllerTests: XCTestCase {
)),
.collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: false
)),
)),
])
}

@MainActor
func testContentOffsetFromSelectedProgressZero() {
// Select the first item.
pagingController.select(pagingItem: Item(index: 3), animated: false)
Expand All @@ -136,9 +139,10 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(collectionViewLayout.calls, [])
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 3)
))
))
}

@MainActor
func testContentScrolledNoUpcomingPagingItem() {
// Prevent the data source from returning an upcoming item.
dataSource.maxIndexAfter = 3
Expand All @@ -158,10 +162,11 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(actions, [
.collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: false
)),
)),
])
}

@MainActor
func testContentScrolledSizeDelegate() {
// Setup the size delegate.
sizeDelegate = MockPagingControllerSizeDelegate()
Expand All @@ -178,9 +183,10 @@ final class PagingControllerTests: XCTestCase {
let action = collectionViewLayout.calls.last?.action
XCTAssertEqual(action, .collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: true
)))
)))
}

@MainActor
func testContentScrolledNoUpcomingPagingItemAndSizeDelegate() {
// Prevent the data source from returning an upcoming item.
dataSource.maxIndexAfter = 3
Expand All @@ -205,10 +211,11 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(actions, [
.collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: false
)),
)),
])
}

@MainActor
func testContentScrolledUpcomingItemOutsideVisibleItems() {
// Select the first item, and scroll to the edge of the
// collection view a few times to make sure the selected
Expand Down Expand Up @@ -260,10 +267,11 @@ final class PagingControllerTests: XCTestCase {
)),
.collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: false
)),
)),
])
}

@MainActor
func testContentScrolledProgressChangedFromPositiveToNegative() {
// Select an item and enter the scrolling state.
pagingController.select(pagingItem: Item(index: 1), animated: false)
Expand All @@ -281,9 +289,10 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(collectionViewLayout.calls, [])
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 1)
))
))
}

@MainActor
func testContentScrolledProgressChangedFromNegativeToPositive() {
// Select an item and enter the scrolling state.
pagingController.select(pagingItem: Item(index: 1), animated: false)
Expand All @@ -301,9 +310,10 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(collectionViewLayout.calls, [])
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 1)
))
))
}

@MainActor
func testContentScrolledProgressChangedToZero() {
// Select an item and enter the scrolling state.
pagingController.select(pagingItem: Item(index: 1), animated: false)
Expand All @@ -321,9 +331,10 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(collectionViewLayout.calls, [])
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 1)
))
))
}

@MainActor
func testContentScrolledProgressChangedSameSign() {
// Select an item and enter the scrolling state.
pagingController.select(pagingItem: Item(index: 1), animated: false)
Expand Down Expand Up @@ -356,12 +367,13 @@ final class PagingControllerTests: XCTestCase {
)),
.collectionViewLayout(.invalidateLayoutWithContext(
invalidateSizes: false
)),
)),
])
}

// MARK: - Select item

@MainActor
func testSelectWhileEmpty() {
// Make sure there is no item before index 0.
dataSource.minIndexBefore = 0
Expand All @@ -376,7 +388,7 @@ final class PagingControllerTests: XCTestCase {
// Expect it to enter selected state.
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 0)
))
))

// Combine the method calls for the collection view,
// collection view layout and delegate to ensure that
Expand Down Expand Up @@ -406,6 +418,7 @@ final class PagingControllerTests: XCTestCase {
])
}

@MainActor
func testSelectWhileEmptyAndNoSuperview() {
// Remove the superview.
collectionView.superview = nil
Expand All @@ -419,9 +432,10 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(delegate.calls, [])
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 0)
))
))
}

@MainActor
func testSelectWhileEmptyAndNoWindow() {
// Remove the window and make sure we have a superview.
collectionView.superview = UIView(frame: .zero)
Expand All @@ -435,9 +449,10 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(delegate.calls, [])
XCTAssertEqual(pagingController.state, PagingState.selected(
pagingItem: Item(index: 0)
))
))
}

@MainActor
func testSelectItemWhileScrolling() {
// Select an item and enter the scrolling state.
pagingController.select(pagingItem: Item(index: 1), animated: false)
Expand All @@ -459,6 +474,7 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(pagingController.state, oldState)
}

@MainActor
func testSelectSameItem() {
// Select an item and enter the scrolling state.
pagingController.select(pagingItem: Item(index: 0), animated: false)
Expand All @@ -479,6 +495,7 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(pagingController.state, oldState)
}

@MainActor
func testSelectDifferentItem() {
// Make sure there is no item before index 0.
dataSource.minIndexBefore = 0
Expand All @@ -504,6 +521,7 @@ final class PagingControllerTests: XCTestCase {
))
}

@MainActor
func testSelectPreviousSibling() {
// Make sure there is no item before index 0.
dataSource.minIndexBefore = 0
Expand Down Expand Up @@ -531,6 +549,7 @@ final class PagingControllerTests: XCTestCase {
])
}

@MainActor
func testSelectNextSibling() {
// Make sure there is no item before index 0.
dataSource.minIndexBefore = 0
Expand Down Expand Up @@ -558,6 +577,7 @@ final class PagingControllerTests: XCTestCase {
])
}

@MainActor
func testSelectNotSibling() {
// Make sure there is no item before index 0.
dataSource.minIndexBefore = 0
Expand Down Expand Up @@ -585,6 +605,7 @@ final class PagingControllerTests: XCTestCase {
])
}

@MainActor
func testSelectItemOutsideVisibleItems() {
// Select the first item, and scroll to the edge of the
// collection view a few times to make sure the selected
Expand Down Expand Up @@ -647,6 +668,7 @@ final class PagingControllerTests: XCTestCase {

// MARK: - Content finished scrolling

@MainActor
func testContentFinishedScrollingWithUpcomingItem() {
// Select an item and enter the scrolling state.
dataSource.minIndexBefore = 0
Expand Down Expand Up @@ -689,6 +711,7 @@ final class PagingControllerTests: XCTestCase {
])
}

@MainActor
func testContentFinishedScrollingCollectionViewBeingDragged() {
// Select an item and enter the scrolling state.
dataSource.minIndexBefore = 0
Expand All @@ -710,6 +733,7 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(delegate.calls, [])
}

@MainActor
func testContentFinishedScrollingWihtoutUpcomingItem() {
// Select an item and enter the scrolling state.
dataSource.minIndexBefore = 0
Expand All @@ -730,6 +754,7 @@ final class PagingControllerTests: XCTestCase {

// MARK: - Transition size

@MainActor
func testTransitionSize() {
dataSource.minIndexBefore = 0
pagingController.select(pagingItem: Item(index: 0), animated: false)
Expand Down Expand Up @@ -767,6 +792,7 @@ final class PagingControllerTests: XCTestCase {
])
}

@MainActor
func testTransitionSizeWhenSelected() {
dataSource.minIndexBefore = 0
pagingController.select(pagingItem: Item(index: 0), animated: false)
Expand All @@ -783,6 +809,7 @@ final class PagingControllerTests: XCTestCase {
XCTAssertEqual(pagingController.state, .selected(pagingItem: Item(index: 0)))
}

@MainActor
func testTransitionSizeWhenScrolling() {
dataSource.minIndexBefore = 0
pagingController.select(pagingItem: Item(index: 0), animated: false)
Expand All @@ -804,6 +831,7 @@ final class PagingControllerTests: XCTestCase {

// MARK: - Reload data

@MainActor
func testReloadData() {
pagingController.reloadData(around: Item(index: 2))

Expand Down Expand Up @@ -847,6 +875,7 @@ final class PagingControllerTests: XCTestCase {

// MARK: - Reload menu

@MainActor
func testReloadMenu() {
pagingController.reloadMenu(around: Item(index: 2))

Expand Down

0 comments on commit 6c96c13

Please sign in to comment.