From f46a31dc83ee3ff9bcc56b1c503afde4a816beb3 Mon Sep 17 00:00:00 2001 From: "Colin T.A. Gray" Date: Tue, 14 Jun 2016 10:33:52 -0600 Subject: [PATCH] Exposes the 'native constraints', with specs --- Source/Constraint.swift | 13 +++++++++++++ Tests/Tests.swift | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index 4f291e6..c923ad2 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -57,6 +57,8 @@ public class Constraint { public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") } + + public func nativeConstraints() -> [LayoutConstraint] { fatalError("Must be implemented by Concrete subclass.") } internal var makerFile: String = "Unknown" internal var makerLine: UInt = 0 @@ -197,6 +199,17 @@ internal class ConcreteConstraint: Constraint { private var installInfo: ConcreteConstraintInstallInfo? = nil + internal override func nativeConstraints() -> [LayoutConstraint] { + if installInfo == nil { + install() + } + + guard let installInfo = installInfo else { + return [] + } + return installInfo.layoutConstraints.allObjects as! [LayoutConstraint] + } + internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float, label: String? = nil) { self.fromItem = fromItem self.toItem = toItem diff --git a/Tests/Tests.swift b/Tests/Tests.swift index 8d96495..debf790 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -291,4 +291,27 @@ class SnapKitTests: XCTestCase { XCTAssertEqual(constraints[0].constant, 10, "Should be 10") XCTAssertEqual(constraints[1].constant, -10, "Should be 10") } + + func testNativeConstraints() { + let view = View() + + container.addSubview(view) + + var topNativeConstraints: [LayoutConstraint]! + var topNativeConstraint: LayoutConstraint? + var sizeNativeConstraints: [LayoutConstraint]! + view.snp_makeConstraints { (make) -> Void in + let topConstraint = make.top.equalToSuperview().inset(10).constraint + topNativeConstraints = topConstraint.nativeConstraints() + topNativeConstraint = topConstraint.nativeConstraints().first + let sizeConstraints = make.size.equalTo(50).constraint + sizeNativeConstraints = sizeConstraints.nativeConstraints() + } + + XCTAssertEqual(topNativeConstraints.count, 1, "make.top should creates one native constraint") + XCTAssertEqual(topNativeConstraint?.constant, 10, "topNativeConstraint.constant is set to 10") + XCTAssertEqual(sizeNativeConstraints.count, 2, "make.tosize should create two native constraint") + XCTAssertEqual(sizeNativeConstraints[0].constant, 50, "sizeNativeConstraints should set size[0] to 50") + XCTAssertEqual(sizeNativeConstraints[1].constant, 50, "sizeNativeConstraints should set size[1] to 50") + } }