Fix tests

This commit is contained in:
Robert Payne 2016-09-20 15:12:30 +12:00
parent 5c7ede844a
commit 505ed036a0
1 changed files with 41 additions and 29 deletions

View File

@ -2,13 +2,21 @@
import UIKit import UIKit
typealias View = UIView typealias View = UIView
extension View { extension View {
var snp_constraints: [AnyObject] { return self.constraints } var snp_constraints: [AnyObject] {
return self.constraints
.filter { $0 is LayoutConstraint }
.filter { $0.isActive }
}
} }
#else #else
import AppKit import AppKit
typealias View = NSView typealias View = NSView
extension View { extension View {
var snp_constraints: [AnyObject] { return self.constraints } var snp_constraints: [AnyObject] {
return self.constraints
.filter { $0 is LayoutConstraint }
.filter { $0.isActive }
}
} }
#endif #endif
@ -128,15 +136,19 @@ class SnapKitTests: XCTestCase {
self.container.addSubview(v2) self.container.addSubview(v2)
v1.snp.makeConstraints { (make) -> Void in v1.snp.makeConstraints { (make) -> Void in
make.top.equalTo(v2.snp.top).offset(50) make.top.equalTo(v2).offset(50)
make.left.equalTo(v2.snp.top).offset(50) make.left.equalTo(v2).offset(50)
return return
} }
XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed") XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
print(self.container.snp_constraints)
v1.snp.removeConstraints() v1.snp.removeConstraints()
print(self.container.snp_constraints)
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed") XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
} }
@ -400,6 +412,28 @@ class SnapKitTests: XCTestCase {
XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'") XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
} }
func testEdgesToEdges() {
var fromAttributes = Set<NSLayoutAttribute>()
var toAttributes = Set<NSLayoutAttribute>()
let view = View()
self.container.addSubview(view)
view.snp.remakeConstraints { (make) -> Void in
make.edges.equalTo(self.container.snp.edges)
}
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
fromAttributes.insert(constraint.firstAttribute)
toAttributes.insert(constraint.secondAttribute)
}
XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
XCTAssert(toAttributes == [.top, .left, .bottom, .right])
}
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
func testEdgesToMargins() { func testEdgesToMargins() {
var fromAttributes = Set<NSLayoutAttribute>() var fromAttributes = Set<NSLayoutAttribute>()
@ -441,28 +475,6 @@ class SnapKitTests: XCTestCase {
} }
func testEdgesToEdges() {
var fromAttributes = Set<NSLayoutAttribute>()
var toAttributes = Set<NSLayoutAttribute>()
let view = View()
self.container.addSubview(view)
view.snp.remakeConstraints { (make) -> Void in
make.edges.equalTo(self.container.snp.edges)
}
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
fromAttributes.insert(constraint.firstAttribute)
toAttributes.insert(constraint.secondAttribute)
}
XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
XCTAssert(toAttributes == [.top, .left, .bottom, .right])
}
func testLayoutGuideConstraints() { func testLayoutGuideConstraints() {
let vc = UIViewController() let vc = UIViewController()
vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
@ -470,11 +482,11 @@ class SnapKitTests: XCTestCase {
vc.view.addSubview(self.container) vc.view.addSubview(self.container)
self.container.snp.makeConstraints { (make) -> Void in self.container.snp.makeConstraints { (make) -> Void in
make.top.equalTo(vc.topLayoutGuide.snp.bottom) make.top.equalTo(vc.topLayoutGuide.snp.bottom)
make.bottom.equalTo(vc.bottomLayoutGuide.snp.top) make.bottom.equalTo(vc.bottomLayoutGuide.snp.top)
} }
XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed") XCTAssertEqual(vc.view.snp_constraints.count, 2, "Should have 2 constraints installed")
} }
#endif #endif