| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- #if canImport(UIKit)
- import UIKit
- typealias View = UIView
- extension View {
- var snp_constraints: [AnyObject] {
- return self.constraints
- .filter { $0 is LayoutConstraint }
- .filter { $0.isActive }
- }
- }
- #else
- import AppKit
- typealias View = NSView
- extension View {
- var snp_constraints: [AnyObject] {
- return self.constraints
- .filter { $0 is LayoutConstraint }
- .filter { $0.isActive }
- }
- }
- #endif
- import XCTest
- @testable import SnapKit
- class SnapKitTests: XCTestCase {
-
- let container = View()
-
- override func setUp() {
- super.setUp()
- // Put setup code here. This method is called before the invocation of each test method in the class.
- }
-
- override func tearDown() {
- // Put teardown code here. This method is called after the invocation of each test method in the class.
- super.tearDown()
- }
-
- func testMakeConstraints() {
- let v1 = View()
- let v2 = View()
- 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
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
-
- v2.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(v1)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
-
- }
- func testHorizontalVerticalEdges() {
- let v1 = View()
- self.container.addSubview(v1)
- v1.snp.makeConstraints { (make) -> Void in
- make.verticalEdges.equalToSuperview()
- make.horizontalEdges.equalToSuperview()
- return
- }
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
- XCTAssertTrue(container.constraints.count == 4)
- XCTAssertTrue(container.constraints.allSatisfy { $0.firstItem === v1 && $0.secondItem === v1.superview })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .left && $0.secondAttribute == .left })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .right && $0.secondAttribute == .right })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .top && $0.secondAttribute == .top })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .bottom && $0.secondAttribute == .bottom })
- }
- func testHorizontalVerticalDirectionalEdges() {
- let v1 = View()
- self.container.addSubview(v1)
- v1.snp.makeConstraints { (make) -> Void in
- make.directionalVerticalEdges.equalToSuperview()
- make.directionalHorizontalEdges.equalToSuperview()
- return
- }
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
- XCTAssertTrue(container.constraints.count == 4)
- XCTAssertTrue(container.constraints.allSatisfy { $0.firstItem === v1 && $0.secondItem === v1.superview })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .leading && $0.secondAttribute == .leading })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .trailing && $0.secondAttribute == .trailing })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .top && $0.secondAttribute == .top })
- XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .bottom && $0.secondAttribute == .bottom })
- }
-
- func testGuideMakeConstraints() {
- guard #available(iOS 9.0, OSX 10.11, *) else { return }
- let v1 = View()
- let g1 = ConstraintLayoutGuide()
- self.container.addSubview(v1)
- self.container.addLayoutGuide(g1)
-
- v1.snp.makeConstraints { (make) -> Void in
- make.top.equalTo(g1).offset(50)
- make.left.equalTo(g1.snp.top).offset(50)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
-
- g1.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(v1)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
- }
-
- func testMakeImpliedSuperviewConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- v1.snp.makeConstraints { (make) -> Void in
- make.top.equalTo(50.0)
- make.left.equalTo(50.0)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
-
- v2.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(v1)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
- }
-
- func testUpdateConstraints() {
- let v1 = View()
- let v2 = View()
- 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
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
-
- v1.snp.updateConstraints { (make) -> Void in
- make.top.equalTo(v2.snp.top).offset(15)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
-
- }
-
- func testRemakeConstraints() {
- let v1 = View()
- let v2 = View()
- 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
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
-
- v1.snp.remakeConstraints { (make) -> Void in
- make.edges.equalTo(v2)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
-
- }
-
- func testRemoveConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- v1.snp.makeConstraints { (make) -> Void in
- make.top.equalTo(v2).offset(50)
- make.left.equalTo(v2).offset(50)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
-
- print(self.container.snp_constraints)
-
- v1.snp.removeConstraints()
-
- print(self.container.snp_constraints)
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
-
- }
-
- func testPrepareConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- let constraints = v1.snp.prepareConstraints { (make) -> Void in
- make.edges.equalTo(v2)
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
-
- for constraint in constraints {
- constraint.activate()
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
-
- for constraint in constraints {
- constraint.deactivate()
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
-
- }
-
- func testReactivateConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- let constraints = v1.snp.prepareConstraints { (make) -> Void in
- make.edges.equalTo(v2)
- return
- }
-
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
-
- for constraint in constraints {
- constraint.activate()
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
-
- for constraint in constraints {
- constraint.deactivate()
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
- }
-
- func testActivateDeactivateConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- var c1: Constraint? = nil
- var c2: Constraint? = nil
-
- v1.snp.prepareConstraints { (make) -> Void in
- c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
- c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
-
- c1?.activate()
- c2?.activate()
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- c1?.deactivate()
- c2?.deactivate()
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
-
- c1?.activate()
- c2?.activate()
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- }
-
- func testSetIsActivatedConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- var c1: Constraint? = nil
- var c2: Constraint? = nil
-
- v1.snp.prepareConstraints { (make) -> Void in
- c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
- c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
-
- c1?.isActive = true
- c2?.isActive = false
-
- XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
-
- c1?.isActive = true
- c2?.isActive = true
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- c1?.isActive = false
- c2?.isActive = false
-
- XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
-
- }
-
- func testEdgeConstraints() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(self.container).offset(50.0)
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
-
-
- let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
-
- XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
- }
-
- func testUpdateReferencedConstraints() {
- let v1 = View()
- let v2 = View()
- self.container.addSubview(v1)
- self.container.addSubview(v2)
-
- var c1: Constraint! = nil
- var c2: Constraint! = nil
-
- v1.snp.makeConstraints { (make) -> Void in
- c1 = make.top.equalTo(v2).offset(50).constraint
- c2 = make.bottom.equalTo(v2).offset(25).constraint
- return
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
-
- XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
-
- c1.update(offset: 15)
- c2.update(offset: 20)
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
- XCTAssertEqual(constraints[1].constant, 20, "Should be 20")
-
- c1.update(inset: 15)
- c2.update(inset: 20)
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
- XCTAssertEqual(constraints[1].constant, -20, "Should be -20")
-
- }
-
- func testInsetsAsConstraintsConstant() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(self.container).inset(50.0)
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
-
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
-
- XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[2].constant, -50, "Should be -50")
- XCTAssertEqual(constraints[3].constant, -50, "Should be -50")
- }
-
- func testConstraintInsetsAsImpliedEqualToConstraints() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
-
-
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
-
- XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
- XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
- XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
- XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
- }
-
- func testConstraintInsetsAsConstraintsConstant() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(self.container).inset(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
-
-
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
-
- XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
- XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
- XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
- XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
- }
-
- #if canImport(UIKit)
- @available(iOS 11.0, tvOS 11.0, *)
- func testConstraintDirectionalInsetsAsImpliedEqualToConstraints() {
- let view = View()
- self.container.addSubview(view)
- view.snp.makeConstraints { (make) -> Void in
- make.top.leading.bottom.trailing.equalTo(self.container).inset(ConstraintDirectionalInsets(top: 25, leading: 25, bottom: 25, trailing: 25))
- }
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.firstAttribute.rawValue < $1.firstAttribute.rawValue }
- let verify: (NSLayoutConstraint, NSLayoutConstraint.Attribute, CGFloat) -> Void = { constraint, attribute, constant in
- XCTAssertEqual(constraint.firstAttribute, attribute, "First attribute \(constraint.firstAttribute.rawValue) is not \(attribute.rawValue)")
- XCTAssertEqual(constraint.secondAttribute, attribute, "Second attribute \(constraint.secondAttribute.rawValue) is not \(attribute.rawValue)")
- XCTAssertEqual(constraint.constant, constant, "Attribute \(attribute.rawValue) should have constant \(constant)")
- }
- verify(constraints[0], .top, 25)
- verify(constraints[1], .bottom, -25)
- verify(constraints[2], .leading, 25)
- verify(constraints[3], .trailing, -25)
- }
- #endif
- #if canImport(UIKit)
- @available(iOS 11.0, tvOS 11.0, *)
- func testConstraintDirectionalInsetsAsConstraintsConstant() {
- let view = View()
- self.container.addSubview(view)
- view.snp.makeConstraints { (make) -> Void in
- make.top.leading.bottom.trailing.equalTo(self.container).inset(ConstraintDirectionalInsets(top: 25, leading: 25, bottom: 25, trailing: 25))
- }
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.firstAttribute.rawValue < $1.firstAttribute.rawValue }
- let verify: (NSLayoutConstraint, NSLayoutConstraint.Attribute, CGFloat) -> Void = { constraint, attribute, constant in
- XCTAssertEqual(constraint.firstAttribute, attribute, "First attribute \(constraint.firstAttribute.rawValue) is not \(attribute.rawValue)")
- XCTAssertEqual(constraint.secondAttribute, attribute, "Second attribute \(constraint.secondAttribute.rawValue) is not \(attribute.rawValue)")
- XCTAssertEqual(constraint.constant, constant, "Attribute \(attribute.rawValue) should have constant \(constant)")
- }
- verify(constraints[0], .top, 25)
- verify(constraints[1], .bottom, -25)
- verify(constraints[2], .leading, 25)
- verify(constraints[3], .trailing, -25)
- }
- #endif
- #if canImport(UIKit)
- @available(iOS 11.0, tvOS 11.0, *)
- func testConstraintDirectionalInsetsFallBackForNonDirectionalConstraints() {
- let view = View()
- self.container.addSubview(view)
- view.snp.makeConstraints { (make) -> Void in
- make.edges.equalTo(self.container).inset(ConstraintDirectionalInsets(top: 25, leading: 25, bottom: 25, trailing: 25))
- }
- XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
- let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.firstAttribute.rawValue < $1.firstAttribute.rawValue }
- let verify: (NSLayoutConstraint, NSLayoutConstraint.Attribute, CGFloat) -> Void = { constraint, attribute, constant in
- XCTAssertEqual(constraint.firstAttribute, attribute, "First attribute \(constraint.firstAttribute.rawValue) is not \(attribute.rawValue)")
- XCTAssertEqual(constraint.secondAttribute, attribute, "Second attribute \(constraint.secondAttribute.rawValue) is not \(attribute.rawValue)")
- XCTAssertEqual(constraint.constant, constant, "Attribute \(attribute.rawValue) should have constant \(constant)")
- }
- verify(constraints[0], .left, 25)
- verify(constraints[1], .right, -25)
- verify(constraints[2], .top, 25)
- verify(constraints[3], .bottom, -25)
- }
- #endif
- func testSizeConstraints() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.size.equalTo(CGSize(width: 50, height: 50))
- make.left.top.equalTo(self.container)
- }
-
- XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
- let constraints = view.snp_constraints as! [NSLayoutConstraint]
- // no guarantee which order the constraints are in, but we should test their couple
- let widthHeight = (LayoutAttribute.width.rawValue, LayoutAttribute.height.rawValue)
- let heightWidth = (widthHeight.1, widthHeight.0)
- let firstSecond = (constraints[0].firstAttribute.rawValue, constraints[1].firstAttribute.rawValue)
- // constraint values are correct in either width, height or height, width order
- XCTAssertTrue(firstSecond == widthHeight || firstSecond == heightWidth, "2 contraint values should match")
- XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
- }
-
- func testCenterConstraints() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.center.equalTo(self.container).offset(50.0)
- }
-
- XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
-
-
- if let constraints = self.container.snp_constraints as? [NSLayoutConstraint], constraints.count > 0 {
-
- XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
- XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
- }
- }
-
- func testConstraintIdentifier() {
- let identifier = "Test-Identifier"
- let view = View()
- self.container.addSubview(view)
-
- view.snp.makeConstraints { (make) -> Void in
- make.top.equalTo(self.container.snp.top).labeled(identifier)
- }
-
- let constraints = container.snp_constraints as! [NSLayoutConstraint]
- XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
- }
-
- func testEdgesToEdges() {
- var fromAttributes = Set<LayoutAttribute>()
- var toAttributes = Set<LayoutAttribute>()
-
- 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 testDirectionalEdgesToDirectionalEdges() {
- var fromAttributes = Set<LayoutAttribute>()
- var toAttributes = Set<LayoutAttribute>()
-
- let view = View()
- self.container.addSubview(view)
-
- view.snp.remakeConstraints { (make) -> Void in
- make.directionalEdges.equalTo(self.container.snp.directionalEdges)
- }
-
- 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, .leading, .bottom, .trailing])
- XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing])
- }
-
- #if canImport(UIKit)
- func testEdgesToMargins() {
- var fromAttributes = Set<LayoutAttribute>()
- var toAttributes = Set<LayoutAttribute>()
-
- let view = View()
- self.container.addSubview(view)
-
- view.snp.remakeConstraints { (make) -> Void in
- make.edges.equalTo(self.container.snp.margins)
- }
-
- 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 == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
-
- fromAttributes.removeAll()
- toAttributes.removeAll()
-
- view.snp.remakeConstraints { (make) -> Void in
- make.margins.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(toAttributes == [.top, .left, .bottom, .right])
- XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
-
- }
- func testDirectionalEdgesToDirectionalMargins() {
- var fromAttributes = Set<LayoutAttribute>()
- var toAttributes = Set<LayoutAttribute>()
-
- let view = View()
- self.container.addSubview(view)
-
- view.snp.remakeConstraints { (make) -> Void in
- make.directionalEdges.equalTo(self.container.snp.directionalMargins)
- }
-
- 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, .leading, .bottom, .trailing])
- XCTAssert(toAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin])
-
- fromAttributes.removeAll()
- toAttributes.removeAll()
-
- view.snp.remakeConstraints { (make) -> Void in
- make.directionalMargins.equalTo(self.container.snp.directionalEdges)
- }
-
- 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(toAttributes == [.top, .leading, .bottom, .trailing])
- XCTAssert(fromAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin])
-
- }
-
- func testLayoutGuideConstraints() {
- let vc = UIViewController()
- vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
-
- vc.view.addSubview(self.container)
-
- self.container.snp.makeConstraints { (make) -> Void in
- make.top.equalTo(vc.topLayoutGuide.snp.bottom)
- make.bottom.equalTo(vc.bottomLayoutGuide.snp.top)
- }
-
- XCTAssertEqual(vc.view.snp_constraints.count, 2, "Should have 2 constraints installed")
- }
- #endif
-
- func testCanSetLabel() {
- self.container.snp.setLabel("Hello World")
- XCTAssertEqual(self.container.snp.label(), "Hello World")
- }
-
- func testPriorityShortcuts() {
- let view = View()
- self.container.addSubview(view)
-
- view.snp.remakeConstraints { make in
- make.left.equalTo(1000.0).priority(.required)
- }
- XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
- XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.required.value)
-
- view.snp.remakeConstraints { make in
- make.left.equalTo(1000.0).priority(.low)
- }
- XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
- XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.low.value)
-
- view.snp.remakeConstraints { make in
- make.left.equalTo(1000.0).priority(ConstraintPriority.low.value + 1)
- }
- XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
- XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.low.value + 1)
- }
- func testPriorityStride() {
- let highPriority: ConstraintPriority = .high
- let higherPriority: ConstraintPriority = ConstraintPriority.high.advanced(by: 1)
- XCTAssertEqual(higherPriority.value, highPriority.value + 1)
- }
- }
|