Exposes the 'native constraints', with specs

This commit is contained in:
Colin T.A. Gray 2016-06-14 10:33:52 -06:00
parent 0ad0315fb3
commit f46a31dc83
2 changed files with 36 additions and 0 deletions

View File

@ -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

View File

@ -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")
}
}