Tests.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 testUpdateReferencedConstraints() {
  196. let v1 = View()
  197. let v2 = View()
  198. self.container.addSubview(v1)
  199. self.container.addSubview(v2)
  200. var c1: Constraint! = nil
  201. var c2: Constraint! = nil
  202. v1.snp.makeConstraints { (make) -> Void in
  203. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  204. c2 = make.left.equalTo(v2.snp.top).offset(25).constraint
  205. return
  206. }
  207. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  208. let before = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  209. XCTAssertEqual(before[0].constant, 50, "Should be 50")
  210. XCTAssertEqual(before[1].constant, 25, "Should be 25")
  211. c1.update(offset: 15)
  212. c2.update(offset: 20)
  213. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  214. XCTAssertEqual(before[0].constant, 15, "Should be 15")
  215. XCTAssertEqual(before[1].constant, 20, "Should be 20")
  216. }
  217. func testInsetsAsConstraintsConstant() {
  218. let view = View()
  219. self.container.addSubview(view)
  220. view.snp.makeConstraints { (make) -> Void in
  221. make.edges.equalTo(self.container).inset(50.0)
  222. }
  223. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  224. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  225. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  226. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  227. XCTAssertEqual(constraints[2].constant, -50, "Should be -50")
  228. XCTAssertEqual(constraints[3].constant, -50, "Should be -50")
  229. }
  230. func testUIEdgeInsetsAsImpliedEqualToConstraints() {
  231. let view = View()
  232. self.container.addSubview(view)
  233. view.snp.makeConstraints { (make) -> Void in
  234. make.edges.equalTo(UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25))
  235. }
  236. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  237. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  238. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  239. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  240. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  241. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  242. }
  243. func testUIEdgeInsetsAsConstraintsConstant() {
  244. let view = View()
  245. self.container.addSubview(view)
  246. view.snp.makeConstraints { (make) -> Void in
  247. make.edges.equalTo(self.container).inset(UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25))
  248. }
  249. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  250. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  251. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  252. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  253. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  254. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  255. }
  256. func testSizeConstraints() {
  257. let view = View()
  258. self.container.addSubview(view)
  259. view.snp.makeConstraints { (make) -> Void in
  260. make.size.equalTo(CGSize(width: 50, height: 50))
  261. make.left.top.equalTo(self.container)
  262. }
  263. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  264. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  265. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  266. // no guarantee which order the constraints are in, but we should test their couple
  267. let widthHeight = (NSLayoutAttribute.width.rawValue, NSLayoutAttribute.height.rawValue)
  268. let heightWidth = (widthHeight.1, widthHeight.0)
  269. let firstSecond = (constraints[0].firstAttribute.rawValue, constraints[1].firstAttribute.rawValue)
  270. // constraint values are correct in either width, height or height, width order
  271. XCTAssertTrue(firstSecond == widthHeight || firstSecond == heightWidth, "2 contraint values should match")
  272. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  273. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  274. }
  275. func testCenterConstraints() {
  276. let view = View()
  277. self.container.addSubview(view)
  278. view.snp.makeConstraints { (make) -> Void in
  279. make.center.equalTo(self.container).offset(50.0)
  280. }
  281. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  282. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  283. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  284. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  285. }
  286. func testConstraintIdentifier() {
  287. let identifier = "Test-Identifier"
  288. let view = View()
  289. self.container.addSubview(view)
  290. view.snp.makeConstraints { (make) -> Void in
  291. make.top.equalTo(self.container.snp.top).labeled(identifier)
  292. }
  293. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  294. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  295. }
  296. }