Skip to content

Instantly share code, notes, and snippets.

@wingchi
Created May 5, 2018 22:13
Show Gist options
  • Select an option

  • Save wingchi/e88ad40c0e23850922182fc73916ec90 to your computer and use it in GitHub Desktop.

Select an option

Save wingchi/e88ad40c0e23850922182fc73916ec90 to your computer and use it in GitHub Desktop.
Unit Tests for Grid Cell sizing 6 - 10
class GridViewModelTests: XCTestCase {
let testFrame = CGRect(x: 0, y: 0, width: 72, height: 130)
let expectedFullWidth: CGFloat = 72 // 72
let expectedHalfWidth = (72 / 2) - (GridViewModel.itemMargin / 2) //32
let expectedHeight = (130 / 5) - 2 * GridViewModel.itemMargin //10
// ... tests one through five
func testSixItemCellSize() {
let expectedCellSizes = [
CGSize(width: expectedFullWidth, height: expectedHeight),
CGSize(width: expectedFullWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight)
]
let viewModel = GridViewModel()
viewModel.data = [1, 2, 3, 4, 5, 6]
for row in 0...(viewModel.data.count - 1) {
let testIndexPath = IndexPath(row: row, section: 0)
let cellSize = viewModel.cellSize(for: testFrame, at: testIndexPath)
XCTAssertEqual(cellSize, expectedCellSizes[row])
}
}
// ... tests seven through nine
func testTenItemCellSize() {
let expectedCellSizes = [
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight),
CGSize(width: expectedHalfWidth, height: expectedHeight)
]
let viewModel = GridViewModel()
viewModel.data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for row in 0...(viewModel.data.count - 1) {
let testIndexPath = IndexPath(row: row, section: 0)
let cellSize = viewModel.cellSize(for: testFrame, at: testIndexPath)
XCTAssertEqual(cellSize, expectedCellSizes[row])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment