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

Commit

Permalink
[FEATURE] Test Coverage For Snap Manager Chaining
Browse files Browse the repository at this point in the history
- 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 :)
  • Loading branch information
Satinder Singh committed Apr 15, 2017
1 parent 54aec76 commit db2fe5b
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 133 deletions.
4 changes: 4 additions & 0 deletions Example/SnapLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

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

/* Begin PBXFileReference section */
0543392B1EA1FB1100044728 /* BaseTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestCase.swift; sourceTree = "<group>"; };
05D8B3571E8E1D7C00E5819A /* SnapLayout_ExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SnapLayout_ExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
05D8B35B1E8E1D7C00E5819A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
05D8B3611E8E1F7F00E5819A /* SnapConfigTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapConfigTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -78,6 +80,7 @@
05D8B3631E8E1FAE00E5819A /* SnapManagerTests.swift */,
05FA916C1E919EE90033BC64 /* SnapLayoutTests.swift */,
05D8B35B1E8E1D7C00E5819A /* Info.plist */,
0543392B1EA1FB1100044728 /* BaseTestCase.swift */,
);
path = SnapLayout_ExampleTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -356,6 +359,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0543392C1EA1FB1100044728 /* BaseTestCase.swift in Sources */,
05D8B3641E8E1FAE00E5819A /* SnapManagerTests.swift in Sources */,
05D8B3621E8E1F7F00E5819A /* SnapConfigTests.swift in Sources */,
05FA916D1E919EE90033BC64 /* SnapLayoutTests.swift in Sources */,
Expand Down
39 changes: 39 additions & 0 deletions Example/SnapLayout_ExampleTests/BaseTestCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// BaseTestCase.swift
// SnapLayout
//
// Created by Satinder Singh on 4/14/17.
// Copyright © 2017 CocoaPods. All rights reserved.
//

import UIKit
import XCTest

/// Holds boilerplate properies subclasses may use for testing purposes.
/// Manages view hierarchy of childView and parentView relationship throughout test cases
class BaseTestCase: XCTestCase {

/// Child View of parent View
let childView = UIView()

/// Second child view of parent View
let childView2 = UIView()

/// Parent View of Child View
let parentView = UIView()

/// View that is neither a parent view nor child view
let view = UIView()

override func setUp() {
super.setUp()
parentView.addSubview(childView)
parentView.addSubview(childView2)
}

override func tearDown() {
childView.removeFromSuperview()
childView2.removeFromSuperview()
}

}
11 changes: 1 addition & 10 deletions Example/SnapLayout_ExampleTests/SnapConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@
import XCTest
import SnapLayout

/// Tests SnapConfig
class SnapConfigTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

/// Test Snap Config Constructor
func testSnapConfigConstructor() {
let topConstant = CGFloat(0)
Expand Down
93 changes: 22 additions & 71 deletions Example/SnapLayout_ExampleTests/SnapLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import UIKit
import XCTest
import SnapLayout

class SnapLayoutTests: XCTestCase {
/// Tests SnapLayout
class SnapLayoutTests: BaseTestCase {

/// Tests Snap Layout where no constraints were applied.
func testSnapInActiveError() {
let childView = UIView()
let superview = UIView()
superview.addSubview(childView)
let snapManager = childView.snap()
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertNil(snapManager.top)
Expand All @@ -31,58 +29,31 @@ class SnapLayoutTests: XCTestCase {

/// Tests Snap Width method
func testSnapWidth() {
let view1 = UIView()
let view2 = UIView()
let containerView = UIView()
containerView.addSubview(view1)
containerView.addSubview(view2)
let snapManager = view1.snapWidth(to: view2, multiplier: 0.5)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
let snapManager = childView.snapWidth(to: childView2, multiplier: 0.5)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNotNil(snapManager.width)
XCTAssertEqual(snapManager.width!.isActive, true)
XCTAssertEqual(snapManager.width!.constant, 0)
XCTAssertEqual(snapManager.width!.multiplier, 0.5)
}

/// Tests Snap Width where constraint was not applied
func testsSnapWidthError() {
let view1 = UIView()
let snapManager = view1.snapWidth(multiplier: 0.5)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNil(snapManager.width)
}

/// Tets Snap Height method
func testSnapHeight() {
let view1 = UIView()
let view2 = UIView()
let containerView = UIView()
containerView.addSubview(view1)
containerView.addSubview(view2)
let snapManager = view1.snapHeight(to: view2, multiplier: 0.5)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
let snapManager = childView.snapHeight(to: childView2, multiplier: 0.5)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNotNil(snapManager.height)
XCTAssertEqual(snapManager.height!.isActive, true)
XCTAssertEqual(snapManager.height!.constant, 0)
XCTAssertEqual(snapManager.height!.multiplier, 0.5)
}

/// Tests Snap Height where constraint was not applied
func testsSnapHeightError() {
let view1 = UIView()
let snapManager = view1.snapHeight(multiplier: 0.5)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNil(snapManager.height)
}

/// Tests Snap Size method
func testSnapSize() {
let size = CGSize(width: 30, height: 40)
let view1 = UIView()
let snapManager = view1.snapSize(size: size)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
let snapManager = view.snapSize(size: size)
XCTAssertEqual(view.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertNotNil(snapManager.width)
XCTAssertNotNil(snapManager.height)
XCTAssertEqual(snapManager.width!.isActive, true)
Expand All @@ -96,14 +67,9 @@ class SnapLayoutTests: XCTestCase {
/// Tests Snap Trailing View
func testSnapTrailingView() {
let trailingConstant = CGFloat(8)
let view1 = UIView()
let view2 = UIView()
let containerView = UIView()
containerView.addSubview(view1)
containerView.addSubview(view2)
let snapManager = view1.snap(trailingView: view2, constant: trailingConstant)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
let snapManager = childView.snap(trailingView: childView2, constant: trailingConstant)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNotNil(snapManager.trailing)
XCTAssertEqual(snapManager.trailing!.isActive, true)
XCTAssertEqual(snapManager.trailing!.constant, trailingConstant)
Expand All @@ -113,14 +79,9 @@ class SnapLayoutTests: XCTestCase {
/// Tests Snap Leading View
func testSnapLeadingView() {
let leadingConstant = CGFloat(8)
let view1 = UIView()
let view2 = UIView()
let containerView = UIView()
containerView.addSubview(view1)
containerView.addSubview(view2)
let snapManager = view1.snap(leadingView: view2, constant: leadingConstant)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
let snapManager = childView.snap(leadingView: childView2, constant: leadingConstant)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNotNil(snapManager.leading)
XCTAssertEqual(snapManager.leading!.isActive, true)
XCTAssertEqual(snapManager.leading!.constant, leadingConstant)
Expand All @@ -130,14 +91,9 @@ class SnapLayoutTests: XCTestCase {
/// Tests Snap Bottom View
func testSnapBottomView() {
let bottomConstant = CGFloat(8)
let view1 = UIView()
let view2 = UIView()
let containerView = UIView()
containerView.addSubview(view1)
containerView.addSubview(view2)
let snapManager = view1.snap(bottomView: view2, constant: bottomConstant)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
let snapManager = childView.snap(bottomView: childView2, constant: bottomConstant)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNotNil(snapManager.bottom)
XCTAssertEqual(snapManager.bottom!.isActive, true)
XCTAssertEqual(snapManager.bottom!.constant, bottomConstant)
Expand All @@ -147,14 +103,9 @@ class SnapLayoutTests: XCTestCase {
/// Tests Snap Trailing View
func testSnapTopView() {
let topConstant = CGFloat(8)
let view1 = UIView()
let view2 = UIView()
let containerView = UIView()
containerView.addSubview(view1)
containerView.addSubview(view2)
let snapManager = view1.snap(topView: view2, constant: topConstant)
XCTAssertEqual(view1.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(view2.translatesAutoresizingMaskIntoConstraints, true)
let snapManager = childView.snap(topView: childView2, constant: topConstant)
XCTAssertEqual(childView.translatesAutoresizingMaskIntoConstraints, false)
XCTAssertEqual(childView2.translatesAutoresizingMaskIntoConstraints, true)
XCTAssertNotNil(snapManager.top)
XCTAssertEqual(snapManager.top!.isActive, true)
XCTAssertEqual(snapManager.top!.constant, topConstant)
Expand Down
Loading

0 comments on commit db2fe5b

Please sign in to comment.