Fixed Swift 1.2 regressions and added some simple tests

Conflicts:
	Snap/ConstraintMaker.swift
	Snap/View+Snap.swift
This commit is contained in:
Robert Payne 2015-02-14 23:37:27 +13:00
parent fed8ab9921
commit f97ec8d1d2
4 changed files with 85 additions and 21 deletions

View File

@ -7,6 +7,8 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
EEAED5481A8F56A500777EF9 /* Snap.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EEBCC9D819CC627D0083B827 /* Snap.framework */; };
EEAED5491A8F56BF00777EF9 /* SnapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE91728C19CB304E007888CF /* SnapTests.swift */; };
EEBCC9F019CC64F80083B827 /* EdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9EF19CC64F70083B827 /* EdgeInsets.swift */; }; EEBCC9F019CC64F80083B827 /* EdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9EF19CC64F70083B827 /* EdgeInsets.swift */; };
EEBCC9F219CC65050083B827 /* View+Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9F119CC65040083B827 /* View+Snap.swift */; }; EEBCC9F219CC65050083B827 /* View+Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9F119CC65040083B827 /* View+Snap.swift */; };
EEBCC9F419CC65110083B827 /* ConstraintAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9F319CC65110083B827 /* ConstraintAttributes.swift */; }; EEBCC9F419CC65110083B827 /* ConstraintAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9F319CC65110083B827 /* ConstraintAttributes.swift */; };
@ -46,6 +48,7 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
EEAED5481A8F56A500777EF9 /* Snap.framework in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -233,6 +236,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
EEAED5491A8F56BF00777EF9 /* SnapTests.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -61,7 +61,7 @@ public class ConstraintMaker {
return constraint return constraint
} }
internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> ()) { internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> Void) {
#if os(iOS) #if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false) view.setTranslatesAutoresizingMaskIntoConstraints(false)
#else #else
@ -70,7 +70,7 @@ public class ConstraintMaker {
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
var layoutConstraints = view.snp_installedLayoutConstraints var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for constraint in maker.constraints { for constraint in maker.constraints {
layoutConstraints += constraint.install() layoutConstraints += constraint.install()
} }
@ -78,7 +78,7 @@ public class ConstraintMaker {
view.snp_installedLayoutConstraints = layoutConstraints view.snp_installedLayoutConstraints = layoutConstraints
} }
internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> ()) { internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> Void) {
#if os(iOS) #if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false) view.setTranslatesAutoresizingMaskIntoConstraints(false)
#else #else
@ -87,7 +87,7 @@ public class ConstraintMaker {
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
var layoutConstraints: Array<LayoutConstraint> = view.snp_installedLayoutConstraints var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for existingLayoutConstraint in layoutConstraints { for existingLayoutConstraint in layoutConstraints {
existingLayoutConstraint.constraint?.uninstall() existingLayoutConstraint.constraint?.uninstall()
} }
@ -100,7 +100,7 @@ public class ConstraintMaker {
view.snp_installedLayoutConstraints = layoutConstraints view.snp_installedLayoutConstraints = layoutConstraints
} }
internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> ()) { internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> Void) {
#if os(iOS) #if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false) view.setTranslatesAutoresizingMaskIntoConstraints(false)
#else #else
@ -109,7 +109,7 @@ public class ConstraintMaker {
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
var layoutConstraints = view.snp_installedLayoutConstraints var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for constraint in maker.constraints { for constraint in maker.constraints {
layoutConstraints += constraint.installOnView(updateExisting: true) layoutConstraints += constraint.installOnView(updateExisting: true)
} }
@ -118,7 +118,8 @@ public class ConstraintMaker {
} }
internal class func removeConstraints(view: View) { internal class func removeConstraints(view: View) {
for existingLayoutConstraint in view.snp_installedLayoutConstraints { let eixsitingLayoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for existingLayoutConstraint in eixsitingLayoutConstraints {
existingLayoutConstraint.constraint?.uninstall() existingLayoutConstraint.constraint?.uninstall()
} }

View File

@ -68,15 +68,15 @@ public extension View {
public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) } public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
#endif #endif
public func snp_makeConstraints(block: (make: ConstraintMaker) -> ()) { public func snp_makeConstraints(block: (make: ConstraintMaker) -> Void) {
ConstraintMaker.makeConstraints(self, block: block) ConstraintMaker.makeConstraints(self, block: block)
} }
public func snp_updateConstraints(block: (make: ConstraintMaker) -> ()) { public func snp_updateConstraints(block: (make: ConstraintMaker) -> Void) {
ConstraintMaker.updateConstraints(self, block: block) ConstraintMaker.updateConstraints(self, block: block)
} }
public func snp_remakeConstraints(block: (make: ConstraintMaker) -> ()) { public func snp_remakeConstraints(block: (make: ConstraintMaker) -> Void) {
ConstraintMaker.remakeConstraints(self, block: block) ConstraintMaker.remakeConstraints(self, block: block)
} }
@ -88,11 +88,10 @@ public extension View {
internal var snp_installedLayoutConstraints: Array<LayoutConstraint> { internal var snp_installedLayoutConstraints: Array<LayoutConstraint> {
get { get {
var constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? Array<LayoutConstraint> if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? Array<LayoutConstraint> {
if constraints != nil { return constraints
return constraints!
} }
return [] return Array<LayoutConstraint>()
} }
set { set {
objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))

View File

@ -8,9 +8,12 @@
import UIKit import UIKit
import XCTest import XCTest
import Snap
class SnapTests: XCTestCase { class SnapTests: XCTestCase {
let container = UIView()
override func setUp() { override func setUp() {
super.setUp() super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class. // Put setup code here. This method is called before the invocation of each test method in the class.
@ -21,16 +24,73 @@ class SnapTests: XCTestCase {
super.tearDown() super.tearDown()
} }
func testExample() { func testMakeConstraints() {
// This is an example of a functional test case. let v1 = UIView()
XCTAssert(true, "Pass") let v2 = UIView()
self.container.addSubview(v1)
self.container.addSubview(v2)
v1.snp_makeConstraints { (make) -> Void in
make.top.equalTo(v2.snp_top).offset(50)
make.left.equalTo(v2.snp_top).offset(50)
return
}
v2.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(v1)
return
}
} }
func testPerformanceExample() { func testUpdateConstraints() {
// This is an example of a performance test case. let v1 = UIView()
self.measureBlock() { let v2 = UIView()
// Put the code you want to measure the time of here. self.container.addSubview(v1)
self.container.addSubview(v2)
v1.snp_makeConstraints { (make) -> Void in
make.top.equalTo(v2.snp_top).offset(50)
make.left.equalTo(v2.snp_top).offset(50)
return
} }
v1.snp_updateConstraints { (make) -> Void in
make.top.equalTo(v2.snp_top).offset(15)
return
}
}
func testRemakeConstraints() {
let v1 = UIView()
let v2 = UIView()
self.container.addSubview(v1)
self.container.addSubview(v2)
v1.snp_makeConstraints { (make) -> Void in
make.top.equalTo(v2.snp_top).offset(50)
make.left.equalTo(v2.snp_top).offset(50)
return
}
v1.snp_remakeConstraints { (make) -> Void in
make.edges.equalTo(v2)
return
}
}
func testRemoveConstraints() {
let v1 = UIView()
let v2 = UIView()
self.container.addSubview(v1)
self.container.addSubview(v2)
v1.snp_makeConstraints { (make) -> Void in
make.top.equalTo(v2.snp_top).offset(50)
make.left.equalTo(v2.snp_top).offset(50)
return
}
v1.snp_removeConstraints()
} }
} }