Skip to content

Commit 54a5b96

Browse files
Ondrej RafajOndrej Rafaj
authored andcommitted
Merge branch 'master' of github.com:manGoweb/SpecTools into swift4
2 parents 9f6b783 + b66b3aa commit 54a5b96

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Check if all UITableViewCells available through the UITableView data source eval
200200
```Swift
201201
// Create an enumerate closure
202202
let doesFitClosure: (UITableViewCell)->Bool = { (cell) -> Bool in
203-
guard let cell = cell as? TableViewCell else {
203+
guard let cell = cell as? CustomTableViewCell else {
204204
return false
205205
}
206206
if cell.customLabel.text?.characters.count == 0 {
@@ -218,6 +218,30 @@ You can also ask for an array of IndexPaths that don't fit the criteria
218218
let indexPaths: [IndexPath] = subject.tableView.spec.check.allCells(thatDontFit: doesFitClosure)
219219
```
220220

221+
#### Checking collection view cells
222+
223+
Check if all UICollectionViewCell available through the UICollectionView data source evaluate correctly
224+
```Swift
225+
// Create an enumerate closure
226+
let doesFitClosure: (UICollectionViewCell)->Bool = { (cell) -> Bool in
227+
guard let cell = cell as? CustomCollectionViewCell else {
228+
return false
229+
}
230+
if cell.customLabel.text?.characters.count == 0 {
231+
return false
232+
}
233+
return true
234+
}
235+
236+
// Test if all cells generated by the data source are ok using your closure
237+
let ok = subject.collectionView.spec.check.allCells(fit: doesFitClosure)
238+
```
239+
240+
You can also ask for an array of IndexPaths that don't fit the criteria
241+
```Swift
242+
let indexPaths: [IndexPath] = subject.collectionView.spec.check.allCells(thatDontFit: doesFitClosure)
243+
```
244+
221245
#### UIViewController checks
222246

223247
Look for a specific view controller in your navigation stack
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Checks+UICollectionView.swift
3+
// SpecTools
4+
//
5+
// Created by Ondrej Rafaj on 15/09/2017.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
12+
extension Check where T: UICollectionView {
13+
14+
// MARK: UICollectionView
15+
16+
/// Check if there are any cells that don't fit criteria specified in a closure
17+
/// - Parameter fit: Closure that needs to evaluate the cell which is passed onto it
18+
/// - Returns: Bool (true if no issue is found)
19+
public func allCells(fit evaluateClosure: (UICollectionViewCell)->Bool) -> Bool {
20+
return allCells(thatDontFit: evaluateClosure).count == 0
21+
}
22+
23+
/// Check if there are any cells that don't fit criteria specified in a closure
24+
/// - Parameter fit: Closure that needs to evaluate the cell which is passed onto it
25+
/// - Returns: [IndexPath] Index path of all cells that do not match the given criteria
26+
public func allCells(thatDontFit evaluateClosure: (UICollectionViewCell)->Bool) -> [IndexPath] {
27+
var indexPaths: [IndexPath] = []
28+
for sectionIndex in 0...element.spec.find.numberOfSections() {
29+
row: for rowIndex in 0...element.spec.find.number(ofRowsIn: sectionIndex) {
30+
let indexPath = IndexPath(item: rowIndex, section: sectionIndex)
31+
guard let cell = element.dataSource?.collectionView(element, cellForItemAt: indexPath) else {
32+
fatalError("Data source is not set")
33+
}
34+
35+
let ok = evaluateClosure(cell)
36+
if !ok {
37+
indexPaths.append(indexPath)
38+
}
39+
}
40+
}
41+
return indexPaths
42+
}
43+
44+
}
45+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Find+UICollectionView.swift
3+
// SpecTools
4+
//
5+
// Created by Ondrej Rafaj on 15/09/2017.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
12+
extension Find where T: UICollectionView {
13+
14+
// MARK: UICollectionView
15+
16+
/// Return number of sections on a collection view data source
17+
/// - Returns: Int
18+
public func numberOfSections() -> Int {
19+
return element.dataSource?.numberOfSections?(in: element) ?? 0
20+
}
21+
22+
/// Number of rows on a section in a collection view data source
23+
/// - Parameter ofRowsIn: Section index
24+
/// - Returns: Int
25+
public func number(ofRowsIn section: Int) -> Int {
26+
return element.dataSource?.collectionView(element, numberOfItemsInSection: section) ?? 0
27+
}
28+
29+
}

0 commit comments

Comments
 (0)