Skip to content
This repository was archived by the owner on Sep 15, 2019. It is now read-only.

Commit db2fe5b

Browse files
author
Satinder Singh
committed
[FEATURE] Test Coverage For Snap Manager Chaining
- Improved Overall Test Coverage - Removed Boilerplate Test Setup into Base class - Found bug where SnapManager would not sync with subsequent SnapManager - Bug crushed thanks to Unit Tests :)
1 parent 54aec76 commit db2fe5b

File tree

7 files changed

+287
-133
lines changed

7 files changed

+287
-133
lines changed

Example/SnapLayout.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0543392C1EA1FB1100044728 /* BaseTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0543392B1EA1FB1100044728 /* BaseTestCase.swift */; };
1011
05D8B3621E8E1F7F00E5819A /* SnapConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05D8B3611E8E1F7F00E5819A /* SnapConfigTests.swift */; };
1112
05D8B3641E8E1FAE00E5819A /* SnapManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05D8B3631E8E1FAE00E5819A /* SnapManagerTests.swift */; };
1213
05FA916D1E919EE90033BC64 /* SnapLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05FA916C1E919EE90033BC64 /* SnapLayoutTests.swift */; };
@@ -29,6 +30,7 @@
2930
/* End PBXContainerItemProxy section */
3031

3132
/* Begin PBXFileReference section */
33+
0543392B1EA1FB1100044728 /* BaseTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestCase.swift; sourceTree = "<group>"; };
3234
05D8B3571E8E1D7C00E5819A /* SnapLayout_ExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SnapLayout_ExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3335
05D8B35B1E8E1D7C00E5819A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3436
05D8B3611E8E1F7F00E5819A /* SnapConfigTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapConfigTests.swift; sourceTree = "<group>"; };
@@ -78,6 +80,7 @@
7880
05D8B3631E8E1FAE00E5819A /* SnapManagerTests.swift */,
7981
05FA916C1E919EE90033BC64 /* SnapLayoutTests.swift */,
8082
05D8B35B1E8E1D7C00E5819A /* Info.plist */,
83+
0543392B1EA1FB1100044728 /* BaseTestCase.swift */,
8184
);
8285
path = SnapLayout_ExampleTests;
8386
sourceTree = "<group>";
@@ -356,6 +359,7 @@
356359
isa = PBXSourcesBuildPhase;
357360
buildActionMask = 2147483647;
358361
files = (
362+
0543392C1EA1FB1100044728 /* BaseTestCase.swift in Sources */,
359363
05D8B3641E8E1FAE00E5819A /* SnapManagerTests.swift in Sources */,
360364
05D8B3621E8E1F7F00E5819A /* SnapConfigTests.swift in Sources */,
361365
05FA916D1E919EE90033BC64 /* SnapLayoutTests.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// BaseTestCase.swift
3+
// SnapLayout
4+
//
5+
// Created by Satinder Singh on 4/14/17.
6+
// Copyright © 2017 CocoaPods. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import XCTest
11+
12+
/// Holds boilerplate properies subclasses may use for testing purposes.
13+
/// Manages view hierarchy of childView and parentView relationship throughout test cases
14+
class BaseTestCase: XCTestCase {
15+
16+
/// Child View of parent View
17+
let childView = UIView()
18+
19+
/// Second child view of parent View
20+
let childView2 = UIView()
21+
22+
/// Parent View of Child View
23+
let parentView = UIView()
24+
25+
/// View that is neither a parent view nor child view
26+
let view = UIView()
27+
28+
override func setUp() {
29+
super.setUp()
30+
parentView.addSubview(childView)
31+
parentView.addSubview(childView2)
32+
}
33+
34+
override func tearDown() {
35+
childView.removeFromSuperview()
36+
childView2.removeFromSuperview()
37+
}
38+
39+
}

Example/SnapLayout_ExampleTests/SnapConfigTests.swift

+1-10
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,9 @@
99
import XCTest
1010
import SnapLayout
1111

12+
/// Tests SnapConfig
1213
class SnapConfigTests: XCTestCase {
1314

14-
override func setUp() {
15-
super.setUp()
16-
// Put setup code here. This method is called before the invocation of each test method in the class.
17-
}
18-
19-
override func tearDown() {
20-
// Put teardown code here. This method is called after the invocation of each test method in the class.
21-
super.tearDown()
22-
}
23-
2415
/// Test Snap Config Constructor
2516
func testSnapConfigConstructor() {
2617
let topConstant = CGFloat(0)

Example/SnapLayout_ExampleTests/SnapLayoutTests.swift

+22-71
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import UIKit
1010
import XCTest
1111
import SnapLayout
1212

13-
class SnapLayoutTests: XCTestCase {
13+
/// Tests SnapLayout
14+
class SnapLayoutTests: BaseTestCase {
1415

1516
/// Tests Snap Layout where no constraints were applied.
1617
func testSnapInActiveError() {
17-
let childView = UIView()
18-
let superview = UIView()
19-
superview.addSubview(childView)
2018
let snapManager = childView.snap()
2119
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
2220
XCTAssertNil(snapManager.top)
@@ -31,58 +29,31 @@ class SnapLayoutTests: XCTestCase {
3129

3230
/// Tests Snap Width method
3331
func testSnapWidth() {
34-
let view1 = UIView()
35-
let view2 = UIView()
36-
let containerView = UIView()
37-
containerView.addSubview(view1)
38-
containerView.addSubview(view2)
39-
let snapManager = view1.snapWidth(to: view2, multiplier: 0.5)
40-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
41-
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
32+
let snapManager = childView.snapWidth(to: childView2, multiplier: 0.5)
33+
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
34+
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
4235
XCTAssertNotNil(snapManager.width)
4336
XCTAssertEqual(snapManager.width!.isActive, true)
4437
XCTAssertEqual(snapManager.width!.constant, 0)
4538
XCTAssertEqual(snapManager.width!.multiplier, 0.5)
4639
}
4740

48-
/// Tests Snap Width where constraint was not applied
49-
func testsSnapWidthError() {
50-
let view1 = UIView()
51-
let snapManager = view1.snapWidth(multiplier: 0.5)
52-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, true)
53-
XCTAssertNil(snapManager.width)
54-
}
55-
5641
/// Tets Snap Height method
5742
func testSnapHeight() {
58-
let view1 = UIView()
59-
let view2 = UIView()
60-
let containerView = UIView()
61-
containerView.addSubview(view1)
62-
containerView.addSubview(view2)
63-
let snapManager = view1.snapHeight(to: view2, multiplier: 0.5)
64-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
65-
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
43+
let snapManager = childView.snapHeight(to: childView2, multiplier: 0.5)
44+
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
45+
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
6646
XCTAssertNotNil(snapManager.height)
6747
XCTAssertEqual(snapManager.height!.isActive, true)
6848
XCTAssertEqual(snapManager.height!.constant, 0)
6949
XCTAssertEqual(snapManager.height!.multiplier, 0.5)
7050
}
7151

72-
/// Tests Snap Height where constraint was not applied
73-
func testsSnapHeightError() {
74-
let view1 = UIView()
75-
let snapManager = view1.snapHeight(multiplier: 0.5)
76-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, true)
77-
XCTAssertNil(snapManager.height)
78-
}
79-
8052
/// Tests Snap Size method
8153
func testSnapSize() {
8254
let size = CGSize(width: 30, height: 40)
83-
let view1 = UIView()
84-
let snapManager = view1.snapSize(size: size)
85-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
55+
let snapManager = view.snapSize(size: size)
56+
XCTAssertEqual(view.translatesAutoresizingMaskIntoConstraints, false)
8657
XCTAssertNotNil(snapManager.width)
8758
XCTAssertNotNil(snapManager.height)
8859
XCTAssertEqual(snapManager.width!.isActive, true)
@@ -96,14 +67,9 @@ class SnapLayoutTests: XCTestCase {
9667
/// Tests Snap Trailing View
9768
func testSnapTrailingView() {
9869
let trailingConstant = CGFloat(8)
99-
let view1 = UIView()
100-
let view2 = UIView()
101-
let containerView = UIView()
102-
containerView.addSubview(view1)
103-
containerView.addSubview(view2)
104-
let snapManager = view1.snap(trailingView: view2, constant: trailingConstant)
105-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
106-
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
70+
let snapManager = childView.snap(trailingView: childView2, constant: trailingConstant)
71+
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
72+
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
10773
XCTAssertNotNil(snapManager.trailing)
10874
XCTAssertEqual(snapManager.trailing!.isActive, true)
10975
XCTAssertEqual(snapManager.trailing!.constant, trailingConstant)
@@ -113,14 +79,9 @@ class SnapLayoutTests: XCTestCase {
11379
/// Tests Snap Leading View
11480
func testSnapLeadingView() {
11581
let leadingConstant = CGFloat(8)
116-
let view1 = UIView()
117-
let view2 = UIView()
118-
let containerView = UIView()
119-
containerView.addSubview(view1)
120-
containerView.addSubview(view2)
121-
let snapManager = view1.snap(leadingView: view2, constant: leadingConstant)
122-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
123-
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
82+
let snapManager = childView.snap(leadingView: childView2, constant: leadingConstant)
83+
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
84+
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
12485
XCTAssertNotNil(snapManager.leading)
12586
XCTAssertEqual(snapManager.leading!.isActive, true)
12687
XCTAssertEqual(snapManager.leading!.constant, leadingConstant)
@@ -130,14 +91,9 @@ class SnapLayoutTests: XCTestCase {
13091
/// Tests Snap Bottom View
13192
func testSnapBottomView() {
13293
let bottomConstant = CGFloat(8)
133-
let view1 = UIView()
134-
let view2 = UIView()
135-
let containerView = UIView()
136-
containerView.addSubview(view1)
137-
containerView.addSubview(view2)
138-
let snapManager = view1.snap(bottomView: view2, constant: bottomConstant)
139-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
140-
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
94+
let snapManager = childView.snap(bottomView: childView2, constant: bottomConstant)
95+
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
96+
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
14197
XCTAssertNotNil(snapManager.bottom)
14298
XCTAssertEqual(snapManager.bottom!.isActive, true)
14399
XCTAssertEqual(snapManager.bottom!.constant, bottomConstant)
@@ -147,14 +103,9 @@ class SnapLayoutTests: XCTestCase {
147103
/// Tests Snap Trailing View
148104
func testSnapTopView() {
149105
let topConstant = CGFloat(8)
150-
let view1 = UIView()
151-
let view2 = UIView()
152-
let containerView = UIView()
153-
containerView.addSubview(view1)
154-
containerView.addSubview(view2)
155-
let snapManager = view1.snap(topView: view2, constant: topConstant)
156-
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
157-
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
106+
let snapManager = childView.snap(topView: childView2, constant: topConstant)
107+
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
108+
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
158109
XCTAssertNotNil(snapManager.top)
159110
XCTAssertEqual(snapManager.top!.isActive, true)
160111
XCTAssertEqual(snapManager.top!.constant, topConstant)

0 commit comments

Comments
 (0)