Skip to content

Commit

Permalink
Rewrite tests in Swift
Browse files Browse the repository at this point in the history
More concise, faster to add more tests, and also allows for natural
testing of interfacing with the library with Swift
  • Loading branch information
joelekstrom committed Nov 14, 2018
1 parent 1fa9374 commit 00098de
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 138 deletions.
21 changes: 16 additions & 5 deletions JEKScrollableSectionCollectionViewLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
5D256B1E219A0A510054A84A /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D256B1D219A0A510054A84A /* Tests.swift */; };
5D3B436A1F95F559006B8B8E /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D3B435F1F95F558006B8B8E /* AppDelegate.m */; };
5D3B436F1F95F559006B8B8E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D3B43681F95F559006B8B8E /* ViewController.m */; };
5D3B43781F95F614006B8B8E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5D3B43711F95F614006B8B8E /* Assets.xcassets */; };
Expand All @@ -15,7 +16,6 @@
5D3B43811F95F6B7006B8B8E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5D3B43741F95F614006B8B8E /* Main.storyboard */; };
5D99484C1F988FCC008DF84C /* ExampleCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D99484B1F988FCC008DF84C /* ExampleCell.m */; };
5DB90F9621007BB2007A137D /* JEKScrollableSectionCollectionViewLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DB90F9521007BB2007A137D /* JEKScrollableSectionCollectionViewLayout.m */; };
5DB9F3BC21125AAB00FB286C /* Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DB9F3BB21125AAB00FB286C /* Tests.m */; };
5DB9F3C421125ECC00FB286C /* JEKScrollableSectionCollectionViewLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DB90F9521007BB2007A137D /* JEKScrollableSectionCollectionViewLayout.m */; };
/* End PBXBuildFile section */

Expand All @@ -30,6 +30,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
5D256B1C219A0A510054A84A /* Tests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Bridging-Header.h"; sourceTree = "<group>"; };
5D256B1D219A0A510054A84A /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
5D3B435E1F95F558006B8B8E /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ViewController.h; path = "Example Project/ViewController.h"; sourceTree = "<group>"; };
5D3B435F1F95F558006B8B8E /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppDelegate.m; path = "Example Project/AppDelegate.m"; sourceTree = "<group>"; };
5D3B43621F95F558006B8B8E /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = "Example Project/AppDelegate.h"; sourceTree = "<group>"; };
Expand All @@ -45,7 +47,6 @@
5DB90F9421007BB2007A137D /* JEKScrollableSectionCollectionViewLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JEKScrollableSectionCollectionViewLayout.h; sourceTree = "<group>"; };
5DB90F9521007BB2007A137D /* JEKScrollableSectionCollectionViewLayout.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JEKScrollableSectionCollectionViewLayout.m; sourceTree = "<group>"; };
5DB9F3B921125AAA00FB286C /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5DB9F3BB21125AAB00FB286C /* Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Tests.m; sourceTree = "<group>"; };
5DB9F3BD21125AAB00FB286C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -117,8 +118,9 @@
5DB9F3BA21125AAB00FB286C /* Tests */ = {
isa = PBXGroup;
children = (
5DB9F3BB21125AAB00FB286C /* Tests.m */,
5DB9F3BD21125AAB00FB286C /* Info.plist */,
5D256B1D219A0A510054A84A /* Tests.swift */,
5D256B1C219A0A510054A84A /* Tests-Bridging-Header.h */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -167,7 +169,7 @@
5D8FEDC61F463D0300857A7E /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0940;
LastUpgradeCheck = 1010;
ORGANIZATIONNAME = "Joel Ekström";
TargetAttributes = {
5D8FEDCD1F463D0300857A7E = {
Expand All @@ -178,6 +180,7 @@
5DB9F3B821125AAA00FB286C = {
CreatedOnToolsVersion = 9.4.1;
DevelopmentTeam = 5RNJHF933P;
LastSwiftMigration = 1010;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -239,7 +242,7 @@
buildActionMask = 2147483647;
files = (
5DB9F3C421125ECC00FB286C /* JEKScrollableSectionCollectionViewLayout.m in Sources */,
5DB9F3BC21125AAB00FB286C /* Tests.m in Sources */,
5D256B1E219A0A510054A84A /* Tests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -377,6 +380,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand Down Expand Up @@ -412,6 +416,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "iPhone Developer";
Expand All @@ -423,6 +428,9 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.footballaddicts.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Tests/Tests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -431,6 +439,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "iPhone Developer";
Expand All @@ -442,6 +451,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.footballaddicts.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Tests/Tests-Bridging-Header.h";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0940"
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0940"
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 4 additions & 0 deletions Tests/Tests-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "JEKScrollableSectionCollectionViewLayout.h"
131 changes: 0 additions & 131 deletions Tests/Tests.m

This file was deleted.

112 changes: 112 additions & 0 deletions Tests/Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import Foundation
import XCTest

class LayoutMeasurements : NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var itemSize = CGSize(width: 10, height: 10)
var headerSize = CGSize.zero
var footerSize = CGSize.zero
var sectionInsets = UIEdgeInsets.zero
var interItemSpacing: CGFloat = 0
var numberOfSections: Int = 1
var numberOfItems: Int = 2
}

class Tests : XCTestCase {
var layout: JEKScrollableSectionCollectionViewLayout!
var collectionView: UICollectionView!
var measurements: LayoutMeasurements!

override func setUp() {
layout = JEKScrollableSectionCollectionViewLayout()
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 320, height: 100), collectionViewLayout: layout)
measurements = LayoutMeasurements()
collectionView.dataSource = self.measurements
collectionView.delegate = self.measurements
}

func testInterItemSpacing() {
measurements.interItemSpacing = 5
layout.prepare()
let expectedOrigin = CGPoint(x: measurements.itemSize.width + measurements.interItemSpacing, y: 0)
let expectedFrame = CGRect(origin: expectedOrigin, size: measurements.itemSize)
let attributes = layout.layoutAttributesForItem(at: IndexPath(item: 1, section: 0))
XCTAssertEqual(attributes?.frame, expectedFrame)
}

func testSectionInsets() {
measurements.sectionInsets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 50)
layout.prepare()
let expectedOrigin = CGPoint(x: measurements.sectionInsets.left + measurements.itemSize.width,
y: measurements.sectionInsets.top)
let expectedFrame = CGRect(origin: expectedOrigin, size: measurements.itemSize)
let attributes = layout.layoutAttributesForItem(at: IndexPath(row: 1, section: 0))
XCTAssertEqual(attributes?.frame, expectedFrame)

let expectedContentSize = CGSize(width: collectionView.frame.size.width,
height: measurements.sectionInsets.top + measurements.itemSize.height + measurements.sectionInsets.bottom)
XCTAssertEqual(layout.collectionViewContentSize, expectedContentSize)
}

func testHeaderViewLayout() {
measurements.sectionInsets = UIEdgeInsets(top: 5, left: 5, bottom: 0, right: 0)
measurements.headerSize = CGSize(width: 10, height: 25)
layout.prepare()
let headerAttributes = layout.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath(index: 0))
XCTAssertEqual(headerAttributes?.representedElementKind, UICollectionView.elementKindSectionHeader)

let expectedFrame = CGRect(x: 0, y: 0, width: collectionView.frame.size.width, height: measurements.headerSize.height)
XCTAssertEqual(headerAttributes?.frame, expectedFrame)

let itemAttributes = layout.layoutAttributesForItem(at: IndexPath(row: 0, section: 0))
XCTAssertEqual(itemAttributes?.frame.origin.y, measurements.headerSize.height + measurements.sectionInsets.top)
}

func testFooterViewLayout() {
measurements.sectionInsets = UIEdgeInsets(top: 5, left: 5, bottom: 25, right: 0)
measurements.footerSize = CGSize(width: 10, height: 25)
layout.prepare()
let footerAttributes = layout.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, at: IndexPath(index: 0))
XCTAssertEqual(footerAttributes?.representedElementKind, UICollectionView.elementKindSectionFooter)

let expectedFrame = CGRect(x: 0,
y: measurements.sectionInsets.top + measurements.itemSize.height + measurements.sectionInsets.bottom,
width: collectionView.frame.size.width,
height: measurements.footerSize.height)
XCTAssertEqual(footerAttributes?.frame, expectedFrame)
XCTAssertEqual(layout.collectionViewContentSize.height, measurements.sectionInsets.top + measurements.itemSize.height + measurements.sectionInsets.bottom + measurements.footerSize.height);
}
}

extension LayoutMeasurements {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return numberOfSections
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfItems
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return itemSize
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return interItemSpacing
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return headerSize
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return footerSize
}
}

0 comments on commit 00098de

Please sign in to comment.