Răsfoiți Sursa

Exposes the 'native constraints', with specs

Colin T.A. Gray 9 ani în urmă
părinte
comite
f46a31dc83
2 a modificat fișierele cu 36 adăugiri și 0 ștergeri
  1. 13 0
      Source/Constraint.swift
  2. 23 0
      Tests/Tests.swift

+ 13 - 0
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

+ 23 - 0
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")
+    }
 }