From c17a1e5147994bbf7d95dbac3b54b2dd1ad1ac90 Mon Sep 17 00:00:00 2001 From: Satinder Singh Date: Sun, 2 Apr 2017 15:12:33 -0700 Subject: [PATCH] [CHORE] Added More Unit Tests to SnapLayout and SnapManager - Found bugs and fixed due to unit testing :) --- Example/SnapLayout.xcodeproj/project.pbxproj | 34 ++-- .../SnapLayoutTests.swift | 164 ++++++++++++++++++ .../SnapManagerTests.swift | 21 ++- SnapLayout/Classes/SnapLayout.swift | 7 +- 4 files changed, 194 insertions(+), 32 deletions(-) create mode 100644 Example/SnapLayout_ExampleTests/SnapLayoutTests.swift diff --git a/Example/SnapLayout.xcodeproj/project.pbxproj b/Example/SnapLayout.xcodeproj/project.pbxproj index 3236ef4..71a2190 100644 --- a/Example/SnapLayout.xcodeproj/project.pbxproj +++ b/Example/SnapLayout.xcodeproj/project.pbxproj @@ -9,12 +9,13 @@ /* Begin PBXBuildFile section */ 05D8B3621E8E1F7F00E5819A /* SnapConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05D8B3611E8E1F7F00E5819A /* SnapConfigTests.swift */; }; 05D8B3641E8E1FAE00E5819A /* SnapManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05D8B3631E8E1FAE00E5819A /* SnapManagerTests.swift */; }; - 1903337A84AAE47EB8FAD934 /* Pods_SnapLayout_ExampleTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AF111A53420B9650C1DF7D5 /* Pods_SnapLayout_ExampleTests.framework */; }; - 5ABBFC5A61D891256E312103 /* Pods_SnapLayout_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C4411399DDCCAE9D04770720 /* Pods_SnapLayout_Example.framework */; }; + 05FA916D1E919EE90033BC64 /* SnapLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05FA916C1E919EE90033BC64 /* SnapLayoutTests.swift */; }; + 16620983C7FD6063E77C3842 /* Pods_SnapLayout_ExampleTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1C8E67D8280A8CA43B58D9B0 /* Pods_SnapLayout_ExampleTests.framework */; }; 607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD51AFB9204008FA782 /* AppDelegate.swift */; }; 607FACD81AFB9204008FA782 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD71AFB9204008FA782 /* ViewController.swift */; }; 607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; }; 607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; }; + C5916DD918499AC040716CAE /* Pods_SnapLayout_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6E8455DE81A33BC7EDDE58E6 /* Pods_SnapLayout_Example.framework */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -28,32 +29,26 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 05D8B3431E8E1B4000E5819A /* SnapLayout.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SnapLayout.framework; path = "Pods/../build/Debug-iphoneos/SnapLayout/SnapLayout.framework"; sourceTree = ""; }; 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 = ""; }; 05D8B3611E8E1F7F00E5819A /* SnapConfigTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapConfigTests.swift; sourceTree = ""; }; 05D8B3631E8E1FAE00E5819A /* SnapManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapManagerTests.swift; sourceTree = ""; }; + 05FA916C1E919EE90033BC64 /* SnapLayoutTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapLayoutTests.swift; sourceTree = ""; }; + 1C8E67D8280A8CA43B58D9B0 /* Pods_SnapLayout_ExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SnapLayout_ExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 286255A2AE6D51660F5C7B63 /* Pods-SnapLayout_ExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_ExampleTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_ExampleTests/Pods-SnapLayout_ExampleTests.release.xcconfig"; sourceTree = ""; }; 2C9EFFE12017367D906EA542 /* Pods-SnapLayout_ExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_ExampleTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_ExampleTests/Pods-SnapLayout_ExampleTests.debug.xcconfig"; sourceTree = ""; }; - 39BF416F85D73A58E0759C5A /* Pods-SnapLayout_Example-SnapLayout_ExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_Example-SnapLayout_ExampleTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_Example-SnapLayout_ExampleTests/Pods-SnapLayout_Example-SnapLayout_ExampleTests.debug.xcconfig"; sourceTree = ""; }; - 3AF111A53420B9650C1DF7D5 /* Pods_SnapLayout_ExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SnapLayout_ExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 4C9147F394BD9B0020A19460 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; lineEnding = 0; name = LICENSE; path = ../LICENSE; sourceTree = ""; xcLanguageSpecificationIdentifier = ""; }; - 5D24B25AC0181DDD97F376C0 /* Pods_SnapLayout_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SnapLayout_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 607FACD01AFB9204008FA782 /* SnapLayout_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SnapLayout_Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 607FACD41AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 607FACD51AFB9204008FA782 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 607FACD71AFB9204008FA782 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 607FACDC1AFB9204008FA782 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 607FACDF1AFB9204008FA782 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; - 630EA847B7E2C05C27DF4671 /* Pods-SnapLayout_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_Tests/Pods-SnapLayout_Tests.debug.xcconfig"; sourceTree = ""; }; + 6E8455DE81A33BC7EDDE58E6 /* Pods_SnapLayout_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SnapLayout_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7CC5E256D0B6B8AB75218628 /* SnapLayout.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; lineEnding = 0; name = SnapLayout.podspec; path = ../SnapLayout.podspec; sourceTree = ""; xcLanguageSpecificationIdentifier = ""; }; 81249413C533FA3C633F8B09 /* Pods-SnapLayout_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_Example/Pods-SnapLayout_Example.release.xcconfig"; sourceTree = ""; }; 83DD46CF64EC537739968A0D /* Pods-SnapLayout_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_Example/Pods-SnapLayout_Example.debug.xcconfig"; sourceTree = ""; }; - 9FE2FE590678BB0D7C3AE9D5 /* Pods_SnapLayout_Example_SnapLayout_ExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SnapLayout_Example_SnapLayout_ExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - C4411399DDCCAE9D04770720 /* Pods_SnapLayout_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SnapLayout_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; }; CF6C903DE4EA30D1C820E7C5 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; lineEnding = 0; name = README.md; path = ../README.md; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.markdown; }; - D26389D625F6CC2DE5BE5F5B /* Pods-SnapLayout_Example-SnapLayout_ExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_Example-SnapLayout_ExampleTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_Example-SnapLayout_ExampleTests/Pods-SnapLayout_Example-SnapLayout_ExampleTests.release.xcconfig"; sourceTree = ""; }; - FF5E1BCF564DE8261F019A1C /* Pods-SnapLayout_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapLayout_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-SnapLayout_Tests/Pods-SnapLayout_Tests.release.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -61,7 +56,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 1903337A84AAE47EB8FAD934 /* Pods_SnapLayout_ExampleTests.framework in Frameworks */, + 16620983C7FD6063E77C3842 /* Pods_SnapLayout_ExampleTests.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -69,7 +64,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 5ABBFC5A61D891256E312103 /* Pods_SnapLayout_Example.framework in Frameworks */, + C5916DD918499AC040716CAE /* Pods_SnapLayout_Example.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -81,6 +76,7 @@ children = ( 05D8B3611E8E1F7F00E5819A /* SnapConfigTests.swift */, 05D8B3631E8E1FAE00E5819A /* SnapManagerTests.swift */, + 05FA916C1E919EE90033BC64 /* SnapLayoutTests.swift */, 05D8B35B1E8E1D7C00E5819A /* Info.plist */, ); path = SnapLayout_ExampleTests; @@ -91,10 +87,6 @@ children = ( 83DD46CF64EC537739968A0D /* Pods-SnapLayout_Example.debug.xcconfig */, 81249413C533FA3C633F8B09 /* Pods-SnapLayout_Example.release.xcconfig */, - 630EA847B7E2C05C27DF4671 /* Pods-SnapLayout_Tests.debug.xcconfig */, - FF5E1BCF564DE8261F019A1C /* Pods-SnapLayout_Tests.release.xcconfig */, - 39BF416F85D73A58E0759C5A /* Pods-SnapLayout_Example-SnapLayout_ExampleTests.debug.xcconfig */, - D26389D625F6CC2DE5BE5F5B /* Pods-SnapLayout_Example-SnapLayout_ExampleTests.release.xcconfig */, 2C9EFFE12017367D906EA542 /* Pods-SnapLayout_ExampleTests.debug.xcconfig */, 286255A2AE6D51660F5C7B63 /* Pods-SnapLayout_ExampleTests.release.xcconfig */, ); @@ -156,11 +148,8 @@ B26703EFE7205943E8D7FD25 /* Frameworks */ = { isa = PBXGroup; children = ( - 05D8B3431E8E1B4000E5819A /* SnapLayout.framework */, - C4411399DDCCAE9D04770720 /* Pods_SnapLayout_Example.framework */, - 5D24B25AC0181DDD97F376C0 /* Pods_SnapLayout_Tests.framework */, - 9FE2FE590678BB0D7C3AE9D5 /* Pods_SnapLayout_Example_SnapLayout_ExampleTests.framework */, - 3AF111A53420B9650C1DF7D5 /* Pods_SnapLayout_ExampleTests.framework */, + 6E8455DE81A33BC7EDDE58E6 /* Pods_SnapLayout_Example.framework */, + 1C8E67D8280A8CA43B58D9B0 /* Pods_SnapLayout_ExampleTests.framework */, ); name = Frameworks; sourceTree = ""; @@ -369,6 +358,7 @@ files = ( 05D8B3641E8E1FAE00E5819A /* SnapManagerTests.swift in Sources */, 05D8B3621E8E1F7F00E5819A /* SnapConfigTests.swift in Sources */, + 05FA916D1E919EE90033BC64 /* SnapLayoutTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Example/SnapLayout_ExampleTests/SnapLayoutTests.swift b/Example/SnapLayout_ExampleTests/SnapLayoutTests.swift new file mode 100644 index 0000000..b9ef2df --- /dev/null +++ b/Example/SnapLayout_ExampleTests/SnapLayoutTests.swift @@ -0,0 +1,164 @@ +// +// SnapLayoutTests.swift +// SnapLayout +// +// Created by Satinder Singh on 4/2/17. +// Copyright © 2017 CocoaPods. All rights reserved. +// + +import UIKit +import XCTest +import SnapLayout + +class SnapLayoutTests: XCTestCase { + + /// 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) + XCTAssertNil(snapManager.leading) + XCTAssertNil(snapManager.bottom) + XCTAssertNil(snapManager.trailing) + XCTAssertNil(snapManager.width) + XCTAssertNil(snapManager.height) + XCTAssertNil(snapManager.centerX) + XCTAssertNil(snapManager.centerY) + } + + /// 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) + 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) + 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) + XCTAssertNotNil(snapManager.width) + XCTAssertNotNil(snapManager.height) + XCTAssertEqual(snapManager.width!.isActive, true) + XCTAssertEqual(snapManager.height!.isActive, true) + XCTAssertEqual(snapManager.width!.constant, size.width) + XCTAssertEqual(snapManager.height!.constant, size.height) + XCTAssertEqual(snapManager.width!.multiplier, 1.0) + XCTAssertEqual(snapManager.height!.multiplier, 1.0) + } + + /// 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) + XCTAssertNotNil(snapManager.trailing) + XCTAssertEqual(snapManager.trailing!.isActive, true) + XCTAssertEqual(snapManager.trailing!.constant, trailingConstant) + XCTAssertEqual(snapManager.trailing!.multiplier, 1.0) + } + + /// 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) + XCTAssertNotNil(snapManager.leading) + XCTAssertEqual(snapManager.leading!.isActive, true) + XCTAssertEqual(snapManager.leading!.constant, leadingConstant) + XCTAssertEqual(snapManager.leading!.multiplier, 1.0) + } + + /// 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) + XCTAssertNotNil(snapManager.bottom) + XCTAssertEqual(snapManager.bottom!.isActive, true) + XCTAssertEqual(snapManager.bottom!.constant, bottomConstant) + XCTAssertEqual(snapManager.bottom!.multiplier, 1.0) + } + + /// 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) + XCTAssertNotNil(snapManager.top) + XCTAssertEqual(snapManager.top!.isActive, true) + XCTAssertEqual(snapManager.top!.constant, topConstant) + XCTAssertEqual(snapManager.top!.multiplier, 1.0) + } + +} diff --git a/Example/SnapLayout_ExampleTests/SnapManagerTests.swift b/Example/SnapLayout_ExampleTests/SnapManagerTests.swift index afe2852..160c7a5 100644 --- a/Example/SnapLayout_ExampleTests/SnapManagerTests.swift +++ b/Example/SnapLayout_ExampleTests/SnapManagerTests.swift @@ -12,6 +12,7 @@ import SnapLayout class SnapManagerTests: XCTestCase { + /// Test Snap Manager Constructor func testSnapManagerConstructor() { let topConstant = CGFloat(0) let leadingConstant = CGFloat(8) @@ -51,6 +52,7 @@ class SnapManagerTests: XCTestCase { XCTAssertEqual(snapManager.centerY!.constant, 0) } + /// Test Snap Manager Constructor with a view who does not have a superview func testSnapManagerConstructorWithoutSuperView() { let widthConstant = CGFloat(32) let heightConstant = CGFloat(64) @@ -70,6 +72,7 @@ class SnapManagerTests: XCTestCase { XCTAssertNil(snapManager.centerY) } + /// Test Snap Manager Constructor where a error would occur through logging func testSnapManagerConstructorError() { let view = UIView() let snapManager = view.snap(trailing: 0) @@ -84,21 +87,30 @@ class SnapManagerTests: XCTestCase { XCTAssertNil(snapManager.centerY) } + /// Test Snap Manager Constructor with view argument func testSnapManagerConstructorWithSnapConfigErrorForNotSharingSameViewHeirarchy() { let view = UIView() let otherView = UIView() + let containerView = UIView() + containerView.addSubview(view) + containerView.addSubview(otherView) let snapManager = view.snap(to: otherView, constants: .zero) XCTAssertEqual(view.translatesAutoresizingMaskIntoConstraints, false) - XCTAssertNil(snapManager.top) - XCTAssertNil(snapManager.leading) - XCTAssertNil(snapManager.bottom) - XCTAssertNil(snapManager.trailing) + XCTAssertNotNil(snapManager.top) + XCTAssertEqual(snapManager.top!.constant, 0) + XCTAssertNotNil(snapManager.leading) + XCTAssertEqual(snapManager.leading!.constant, 0) + XCTAssertNotNil(snapManager.bottom) + XCTAssertEqual(snapManager.bottom!.constant, 0) + XCTAssertNotNil(snapManager.trailing) + XCTAssertEqual(snapManager.trailing!.constant, 0) XCTAssertNil(snapManager.width) XCTAssertNil(snapManager.height) XCTAssertNil(snapManager.centerX) XCTAssertNil(snapManager.centerY) } + /// Test Snap Manager Constructor with a config parameter func testSnapManagerConstructorWithSnapConfig() { let parentView = UIView() let childView = UIView() @@ -139,5 +151,4 @@ class SnapManagerTests: XCTestCase { XCTAssertEqual(snapManager.centerY!.constant, 0) } - } diff --git a/SnapLayout/Classes/SnapLayout.swift b/SnapLayout/Classes/SnapLayout.swift index 1779316..0f45089 100644 --- a/SnapLayout/Classes/SnapLayout.swift +++ b/SnapLayout/Classes/SnapLayout.swift @@ -43,11 +43,6 @@ public extension UIView { } return snapManager } - if view.superview == nil && superview == nil { - print("SnapLayout Error - following views do not share the same view hierarchy: \(String(describing: self))" + - "and \(String(describing: view))") - return snapManager - } if let top = top { snapManager.top = topAnchor.constraint(equalTo: view.topAnchor, constant: top) snapManager.top?.isActive = true @@ -186,6 +181,7 @@ public extension UIView { /// - Returns: SnapManager holding all the values associated with constraints @discardableResult func snap(bottomView: UIView, constant: CGFloat = 0) -> SnapManager { + translatesAutoresizingMaskIntoConstraints = false let snapManager = SnapManager(view: self) snapManager.bottom = bottomAnchor.constraint(equalTo: bottomView.topAnchor, constant: constant) snapManager.bottom?.isActive = true @@ -200,6 +196,7 @@ public extension UIView { /// - Returns: SnapManager holding all the values associated with constraints @discardableResult func snap(topView: UIView, constant: CGFloat = 0) -> SnapManager { + translatesAutoresizingMaskIntoConstraints = false let snapManager = SnapManager(view: self) snapManager.top = topAnchor.constraint(equalTo: topView.bottomAnchor, constant: constant) snapManager.top?.isActive = true