2014-06-06 11:42:44 +08:00
|
|
|
//
|
2015-01-09 04:43:43 +08:00
|
|
|
// SnapTests.swift
|
|
|
|
// SnapTests
|
2014-06-06 11:42:44 +08:00
|
|
|
//
|
2014-09-23 12:28:00 +08:00
|
|
|
// Created by Robert Payne on 20/09/14.
|
2014-06-06 11:42:44 +08:00
|
|
|
// Copyright (c) 2014 Jonas Budelmann. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2014-07-25 12:24:17 +08:00
|
|
|
import UIKit
|
2014-06-06 11:42:44 +08:00
|
|
|
import XCTest
|
2015-02-14 18:37:27 +08:00
|
|
|
import Snap
|
2014-06-06 11:42:44 +08:00
|
|
|
|
2015-01-09 04:43:43 +08:00
|
|
|
class SnapTests: XCTestCase {
|
2014-06-06 11:42:44 +08:00
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
let container = UIView()
|
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
func testMakeConstraints() {
|
|
|
|
let v1 = UIView()
|
|
|
|
let v2 = UIView()
|
|
|
|
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
|
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 2, "Should have 2 constraints installed")
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v2.snp_makeConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:12:03 +08:00
|
|
|
XCTAssertEqual(self.container.constraints().count, 6, "Should have 6 constraints installed")
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdateConstraints() {
|
|
|
|
let v1 = UIView()
|
|
|
|
let v2 = UIView()
|
|
|
|
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
|
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 2, "Should have 2 constraints installed")
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v1.snp_updateConstraints { (make) -> Void in
|
|
|
|
make.top.equalTo(v2.snp_top).offset(15)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:12:03 +08:00
|
|
|
XCTAssertEqual(self.container.constraints().count, 2, "Should still have 2 constraints installed")
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testRemakeConstraints() {
|
|
|
|
let v1 = UIView()
|
|
|
|
let v2 = UIView()
|
|
|
|
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
|
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 2, "Should have 2 constraints installed")
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v1.snp_remakeConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:12:03 +08:00
|
|
|
XCTAssertEqual(self.container.constraints().count, 4, "Should have 4 constraints installed")
|
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
func testRemoveConstraints() {
|
|
|
|
let v1 = UIView()
|
|
|
|
let v2 = UIView()
|
|
|
|
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
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|
2015-02-17 06:12:03 +08:00
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 2, "Should have 2 constraints installed")
|
|
|
|
|
2015-02-14 18:37:27 +08:00
|
|
|
v1.snp_removeConstraints()
|
|
|
|
|
2015-02-17 06:12:03 +08:00
|
|
|
XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPrepareConstraints() {
|
|
|
|
let v1 = UIView()
|
|
|
|
let v2 = UIView()
|
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
let constraints = v1.snp_prepareConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
|
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.install()
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 4, "Should have 4 constraints installed")
|
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.uninstall()
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
|
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-17 08:41:45 +08:00
|
|
|
func testReinstallConstraints() {
|
|
|
|
let v1 = UIView()
|
|
|
|
let v2 = UIView()
|
|
|
|
self.container.addSubview(v1)
|
|
|
|
self.container.addSubview(v2)
|
|
|
|
|
|
|
|
let constraints = v1.snp_prepareConstraints { (make) -> Void in
|
|
|
|
make.edges.equalTo(v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
|
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.install()
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 4, "Should have 4 constraints installed")
|
|
|
|
|
|
|
|
for constraint in constraints {
|
|
|
|
constraint.install()
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(self.container.constraints().count, 4, "Should have 0 constraints installed")
|
|
|
|
}
|
|
|
|
|
2014-06-06 11:42:44 +08:00
|
|
|
}
|