Tests.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.snp.topLayoutGuideBottom)
  33. make.bottom.equalTo(vc.snp.bottomLayoutGuideTop)
  34. }
  35. XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed")
  36. #endif
  37. }
  38. func testMakeConstraints() {
  39. let v1 = View()
  40. let v2 = View()
  41. self.container.addSubview(v1)
  42. self.container.addSubview(v2)
  43. v1.snp.makeConstraints { (make) -> Void in
  44. make.top.equalTo(v2.snp.top).offset(50)
  45. make.left.equalTo(v2.snp.top).offset(50)
  46. return
  47. }
  48. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  49. v2.snp.makeConstraints { (make) -> Void in
  50. make.edges.equalTo(v1)
  51. return
  52. }
  53. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  54. }
  55. func testUpdateConstraints() {
  56. let v1 = View()
  57. let v2 = View()
  58. self.container.addSubview(v1)
  59. self.container.addSubview(v2)
  60. v1.snp.makeConstraints { (make) -> Void in
  61. make.top.equalTo(v2.snp.top).offset(50)
  62. make.left.equalTo(v2.snp.top).offset(50)
  63. return
  64. }
  65. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  66. v1.snp.updateConstraints { (make) -> Void in
  67. make.top.equalTo(v2.snp.top).offset(15)
  68. return
  69. }
  70. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  71. }
  72. func testRemakeConstraints() {
  73. let v1 = View()
  74. let v2 = View()
  75. self.container.addSubview(v1)
  76. self.container.addSubview(v2)
  77. v1.snp.makeConstraints { (make) -> Void in
  78. make.top.equalTo(v2.snp.top).offset(50)
  79. make.left.equalTo(v2.snp.top).offset(50)
  80. return
  81. }
  82. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  83. v1.snp.remakeConstraints { (make) -> Void in
  84. make.edges.equalTo(v2)
  85. return
  86. }
  87. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  88. }
  89. func testRemoveConstraints() {
  90. let v1 = View()
  91. let v2 = View()
  92. self.container.addSubview(v1)
  93. self.container.addSubview(v2)
  94. v1.snp.makeConstraints { (make) -> Void in
  95. make.top.equalTo(v2.snp.top).offset(50)
  96. make.left.equalTo(v2.snp.top).offset(50)
  97. return
  98. }
  99. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  100. v1.snp.removeConstraints()
  101. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  102. }
  103. func testPrepareConstraints() {
  104. let v1 = View()
  105. let v2 = View()
  106. self.container.addSubview(v1)
  107. self.container.addSubview(v2)
  108. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  109. make.edges.equalTo(v2)
  110. return
  111. }
  112. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  113. for constraint in constraints {
  114. constraint.activate()
  115. }
  116. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  117. for constraint in constraints {
  118. constraint.deactivate()
  119. }
  120. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  121. }
  122. func testReactivateConstraints() {
  123. let v1 = View()
  124. let v2 = View()
  125. self.container.addSubview(v1)
  126. self.container.addSubview(v2)
  127. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  128. make.edges.equalTo(v2)
  129. return
  130. }
  131. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  132. for constraint in constraints {
  133. constraint.activate()
  134. }
  135. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  136. for constraint in constraints {
  137. constraint.deactivate()
  138. }
  139. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  140. }
  141. func testActivateDeactivateConstraints() {
  142. let v1 = View()
  143. let v2 = View()
  144. self.container.addSubview(v1)
  145. self.container.addSubview(v2)
  146. var c1: Constraint? = nil
  147. var c2: Constraint? = nil
  148. v1.snp.prepareConstraints { (make) -> Void in
  149. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  150. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  151. return
  152. }
  153. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  154. c1?.activate()
  155. c2?.activate()
  156. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  157. c1?.deactivate()
  158. c2?.deactivate()
  159. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  160. c1?.activate()
  161. c2?.activate()
  162. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  163. }
  164. func testEdgeConstraints() {
  165. let view = View()
  166. self.container.addSubview(view)
  167. view.snp.makeConstraints { (make) -> Void in
  168. make.edges.equalTo(self.container).offset(50.0)
  169. }
  170. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  171. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  172. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  173. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  174. XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
  175. XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
  176. }
  177. func testSizeConstraints() {
  178. let view = View()
  179. self.container.addSubview(view)
  180. view.snp.makeConstraints { (make) -> Void in
  181. make.size.equalTo(CGSize(width: 50, height: 50))
  182. make.left.top.equalTo(self.container)
  183. }
  184. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  185. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  186. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  187. XCTAssertEqual(constraints[0].firstAttribute.rawValue, NSLayoutAttribute.width.rawValue, "Should be width")
  188. XCTAssertEqual(constraints[1].firstAttribute.rawValue, NSLayoutAttribute.height.rawValue, "Should be height")
  189. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  190. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  191. }
  192. func testCenterConstraints() {
  193. let view = View()
  194. self.container.addSubview(view)
  195. view.snp.makeConstraints { (make) -> Void in
  196. make.center.equalTo(self.container).offset(50.0)
  197. }
  198. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  199. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  200. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  201. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  202. }
  203. func testConstraintIdentifier() {
  204. let identifier = "Test-Identifier"
  205. let view = View()
  206. self.container.addSubview(view)
  207. view.snp.makeConstraints { (make) -> Void in
  208. make.top.equalTo(self.container.snp.top).labeled(identifier)
  209. }
  210. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  211. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  212. }
  213. //
  214. //// func testSuperviewConstraints() {
  215. //// let view = View()
  216. ////
  217. //// container.addSubview(view)
  218. ////
  219. //// view.snp.makeConstraints { (make) -> Void in
  220. //// make.top.equalToSuperview().inset(10)
  221. //// make.bottom.equalToSuperview().inset(10)
  222. //// }
  223. ////
  224. //// XCTAssertEqual(container.snp_constraints.count, 2, "Should have 2 constraints")
  225. ////
  226. //// let constraints = container.snp_constraints as! [NSLayoutConstraint]
  227. ////
  228. //// XCTAssertEqual(constraints[0].firstAttribute, NSLayoutAttribute.top, "Should be top")
  229. //// XCTAssertEqual(constraints[1].firstAttribute, NSLayoutAttribute.bottom, "Should be bottom")
  230. ////
  231. //// XCTAssertEqual(constraints[0].secondAttribute, NSLayoutAttribute.top, "Should be top")
  232. //// XCTAssertEqual(constraints[1].secondAttribute, NSLayoutAttribute.bottom, "Should be bottom")
  233. ////
  234. //// XCTAssertEqual(constraints[0].firstItem as? View, view, "Should be added subview")
  235. //// XCTAssertEqual(constraints[1].firstItem as? View, view, "Should be added subview")
  236. ////
  237. //// XCTAssertEqual(constraints[0].secondItem as? View, container, "Should be containerView")
  238. //// XCTAssertEqual(constraints[1].secondItem as? View, container, "Should be containerView")
  239. ////
  240. //// XCTAssertEqual(constraints[0].constant, 10, "Should be 10")
  241. //// XCTAssertEqual(constraints[1].constant, -10, "Should be 10")
  242. //// }
  243. //
  244. //// func testNativeConstraints() {
  245. //// let view = View()
  246. ////
  247. //// container.addSubview(view)
  248. ////
  249. //// var topNativeConstraints: [LayoutConstraint]!
  250. //// var topNativeConstraint: LayoutConstraint?
  251. //// var sizeNativeConstraints: [LayoutConstraint]!
  252. //// view.snp_makeConstraints { (make) -> Void in
  253. //// let topConstraint = make.top.equalToSuperview().inset(10).constraint
  254. //// topNativeConstraints = topConstraint.layoutConstraints
  255. //// topNativeConstraint = topConstraint.layoutConstraints.first
  256. //// let sizeConstraints = make.size.equalTo(50).constraint
  257. //// sizeNativeConstraints = sizeConstraints.layoutConstraints
  258. //// }
  259. ////
  260. //// XCTAssertEqual(topNativeConstraints.count, 1, "make.top should creates one native constraint")
  261. //// XCTAssertEqual(topNativeConstraint?.constant, 10, "topNativeConstraint.constant is set to 10")
  262. //// XCTAssertEqual(sizeNativeConstraints.count, 2, "make.tosize should create two native constraint")
  263. //// XCTAssertEqual(sizeNativeConstraints[0].constant, 50, "sizeNativeConstraints should set size[0] to 50")
  264. //// XCTAssertEqual(sizeNativeConstraints[1].constant, 50, "sizeNativeConstraints should set size[1] to 50")
  265. //// }
  266. }