Tests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #if os(iOS) || os(tvOS)
  2. import UIKit
  3. typealias View = UIView
  4. extension View {
  5. var snp_constraints: [AnyObject] { return self.constraints }
  6. }
  7. #else
  8. import AppKit
  9. typealias View = NSView
  10. extension View {
  11. var snp_constraints: [AnyObject] { return self.constraints }
  12. }
  13. #endif
  14. import XCTest
  15. @testable import SnapKit
  16. class SnapKitTests: XCTestCase {
  17. let container = View()
  18. override func setUp() {
  19. super.setUp()
  20. // Put setup code here. This method is called before the invocation of each test method in the class.
  21. }
  22. override func tearDown() {
  23. // Put teardown code here. This method is called after the invocation of each test method in the class.
  24. super.tearDown()
  25. }
  26. func testLayoutGuideConstraints() {
  27. #if os(iOS) || os(tvOS)
  28. let vc = UIViewController()
  29. vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
  30. vc.view.addSubview(self.container)
  31. self.container.snp.makeConstraints { (make) -> Void in
  32. make.top.equalTo(vc.topLayoutGuide.snp.bottom)
  33. make.bottom.equalTo(vc.bottomLayoutGuide.snp.top)
  34. }
  35. print(vc.view.snp_constraints)
  36. XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed")
  37. #endif
  38. }
  39. func testMakeConstraints() {
  40. let v1 = View()
  41. let v2 = View()
  42. self.container.addSubview(v1)
  43. self.container.addSubview(v2)
  44. v1.snp.makeConstraints { (make) -> Void in
  45. make.top.equalTo(v2.snp.top).offset(50)
  46. make.left.equalTo(v2.snp.top).offset(50)
  47. return
  48. }
  49. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  50. v2.snp.makeConstraints { (make) -> Void in
  51. make.edges.equalTo(v1)
  52. return
  53. }
  54. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  55. }
  56. func testMakeImpliedSuperviewConstraints() {
  57. let v1 = View()
  58. let v2 = View()
  59. self.container.addSubview(v1)
  60. self.container.addSubview(v2)
  61. v1.snp.makeConstraints { (make) -> Void in
  62. make.top.equalTo(50.0)
  63. make.left.equalTo(50.0)
  64. return
  65. }
  66. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  67. v2.snp.makeConstraints { (make) -> Void in
  68. make.edges.equalTo(v1)
  69. return
  70. }
  71. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  72. }
  73. func testUpdateConstraints() {
  74. let v1 = View()
  75. let v2 = View()
  76. self.container.addSubview(v1)
  77. self.container.addSubview(v2)
  78. v1.snp.makeConstraints { (make) -> Void in
  79. make.top.equalTo(v2.snp.top).offset(50)
  80. make.left.equalTo(v2.snp.top).offset(50)
  81. return
  82. }
  83. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  84. v1.snp.updateConstraints { (make) -> Void in
  85. make.top.equalTo(v2.snp.top).offset(15)
  86. return
  87. }
  88. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  89. }
  90. func testRemakeConstraints() {
  91. let v1 = View()
  92. let v2 = View()
  93. self.container.addSubview(v1)
  94. self.container.addSubview(v2)
  95. v1.snp.makeConstraints { (make) -> Void in
  96. make.top.equalTo(v2.snp.top).offset(50)
  97. make.left.equalTo(v2.snp.top).offset(50)
  98. return
  99. }
  100. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  101. v1.snp.remakeConstraints { (make) -> Void in
  102. make.edges.equalTo(v2)
  103. return
  104. }
  105. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  106. }
  107. func testRemoveConstraints() {
  108. let v1 = View()
  109. let v2 = View()
  110. self.container.addSubview(v1)
  111. self.container.addSubview(v2)
  112. v1.snp.makeConstraints { (make) -> Void in
  113. make.top.equalTo(v2.snp.top).offset(50)
  114. make.left.equalTo(v2.snp.top).offset(50)
  115. return
  116. }
  117. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  118. v1.snp.removeConstraints()
  119. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  120. }
  121. func testPrepareConstraints() {
  122. let v1 = View()
  123. let v2 = View()
  124. self.container.addSubview(v1)
  125. self.container.addSubview(v2)
  126. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  127. make.edges.equalTo(v2)
  128. return
  129. }
  130. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  131. for constraint in constraints {
  132. constraint.activate()
  133. }
  134. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  135. for constraint in constraints {
  136. constraint.deactivate()
  137. }
  138. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  139. }
  140. func testReactivateConstraints() {
  141. let v1 = View()
  142. let v2 = View()
  143. self.container.addSubview(v1)
  144. self.container.addSubview(v2)
  145. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  146. make.edges.equalTo(v2)
  147. return
  148. }
  149. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  150. for constraint in constraints {
  151. constraint.activate()
  152. }
  153. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  154. for constraint in constraints {
  155. constraint.deactivate()
  156. }
  157. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  158. }
  159. func testActivateDeactivateConstraints() {
  160. let v1 = View()
  161. let v2 = View()
  162. self.container.addSubview(v1)
  163. self.container.addSubview(v2)
  164. var c1: Constraint? = nil
  165. var c2: Constraint? = nil
  166. v1.snp.prepareConstraints { (make) -> Void in
  167. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  168. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  169. return
  170. }
  171. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  172. c1?.activate()
  173. c2?.activate()
  174. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  175. c1?.deactivate()
  176. c2?.deactivate()
  177. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  178. c1?.activate()
  179. c2?.activate()
  180. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  181. }
  182. func testEdgeConstraints() {
  183. let view = View()
  184. self.container.addSubview(view)
  185. view.snp.makeConstraints { (make) -> Void in
  186. make.edges.equalTo(self.container).offset(50.0)
  187. }
  188. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  189. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  190. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  191. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  192. XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
  193. XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
  194. }
  195. func testSizeConstraints() {
  196. let view = View()
  197. self.container.addSubview(view)
  198. view.snp.makeConstraints { (make) -> Void in
  199. make.size.equalTo(CGSize(width: 50, height: 50))
  200. make.left.top.equalTo(self.container)
  201. }
  202. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  203. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  204. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  205. // no guarantee which order the constraints are in, but we should test their couple
  206. let widthHeight = (NSLayoutAttribute.width.rawValue, NSLayoutAttribute.height.rawValue)
  207. let heightWidth = (widthHeight.1, widthHeight.0)
  208. let firstSecond = (constraints[0].firstAttribute.rawValue, constraints[1].firstAttribute.rawValue)
  209. // constraint values are correct in either width, height or height, width order
  210. XCTAssertTrue(firstSecond == widthHeight || firstSecond == heightWidth, "2 contraint values should match")
  211. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  212. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  213. }
  214. func testCenterConstraints() {
  215. let view = View()
  216. self.container.addSubview(view)
  217. view.snp.makeConstraints { (make) -> Void in
  218. make.center.equalTo(self.container).offset(50.0)
  219. }
  220. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  221. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  222. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  223. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  224. }
  225. func testConstraintIdentifier() {
  226. let identifier = "Test-Identifier"
  227. let view = View()
  228. self.container.addSubview(view)
  229. view.snp.makeConstraints { (make) -> Void in
  230. make.top.equalTo(self.container.snp.top).labeled(identifier)
  231. }
  232. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  233. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  234. }
  235. }