Tests.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 testMakeConstraints() {
  27. let v1 = View()
  28. let v2 = View()
  29. self.container.addSubview(v1)
  30. self.container.addSubview(v2)
  31. v1.snp.makeConstraints { (make) -> Void in
  32. make.top.equalTo(v2.snp.top).offset(50)
  33. make.left.equalTo(v2.snp.top).offset(50)
  34. return
  35. }
  36. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  37. v2.snp.makeConstraints { (make) -> Void in
  38. make.edges.equalTo(v1)
  39. return
  40. }
  41. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  42. }
  43. func testMakeImpliedSuperviewConstraints() {
  44. let v1 = View()
  45. let v2 = View()
  46. self.container.addSubview(v1)
  47. self.container.addSubview(v2)
  48. v1.snp.makeConstraints { (make) -> Void in
  49. make.top.equalTo(50.0)
  50. make.left.equalTo(50.0)
  51. return
  52. }
  53. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  54. v2.snp.makeConstraints { (make) -> Void in
  55. make.edges.equalTo(v1)
  56. return
  57. }
  58. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  59. }
  60. func testUpdateConstraints() {
  61. let v1 = View()
  62. let v2 = View()
  63. self.container.addSubview(v1)
  64. self.container.addSubview(v2)
  65. v1.snp.makeConstraints { (make) -> Void in
  66. make.top.equalTo(v2.snp.top).offset(50)
  67. make.left.equalTo(v2.snp.top).offset(50)
  68. return
  69. }
  70. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  71. v1.snp.updateConstraints { (make) -> Void in
  72. make.top.equalTo(v2.snp.top).offset(15)
  73. return
  74. }
  75. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  76. }
  77. func testRemakeConstraints() {
  78. let v1 = View()
  79. let v2 = View()
  80. self.container.addSubview(v1)
  81. self.container.addSubview(v2)
  82. v1.snp.makeConstraints { (make) -> Void in
  83. make.top.equalTo(v2.snp.top).offset(50)
  84. make.left.equalTo(v2.snp.top).offset(50)
  85. return
  86. }
  87. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  88. v1.snp.remakeConstraints { (make) -> Void in
  89. make.edges.equalTo(v2)
  90. return
  91. }
  92. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  93. }
  94. func testRemoveConstraints() {
  95. let v1 = View()
  96. let v2 = View()
  97. self.container.addSubview(v1)
  98. self.container.addSubview(v2)
  99. v1.snp.makeConstraints { (make) -> Void in
  100. make.top.equalTo(v2.snp.top).offset(50)
  101. make.left.equalTo(v2.snp.top).offset(50)
  102. return
  103. }
  104. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  105. v1.snp.removeConstraints()
  106. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  107. }
  108. func testPrepareConstraints() {
  109. let v1 = View()
  110. let v2 = View()
  111. self.container.addSubview(v1)
  112. self.container.addSubview(v2)
  113. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  114. make.edges.equalTo(v2)
  115. return
  116. }
  117. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  118. for constraint in constraints {
  119. constraint.activate()
  120. }
  121. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  122. for constraint in constraints {
  123. constraint.deactivate()
  124. }
  125. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  126. }
  127. func testReactivateConstraints() {
  128. let v1 = View()
  129. let v2 = View()
  130. self.container.addSubview(v1)
  131. self.container.addSubview(v2)
  132. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  133. make.edges.equalTo(v2)
  134. return
  135. }
  136. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  137. for constraint in constraints {
  138. constraint.activate()
  139. }
  140. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  141. for constraint in constraints {
  142. constraint.deactivate()
  143. }
  144. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  145. }
  146. func testActivateDeactivateConstraints() {
  147. let v1 = View()
  148. let v2 = View()
  149. self.container.addSubview(v1)
  150. self.container.addSubview(v2)
  151. var c1: Constraint? = nil
  152. var c2: Constraint? = nil
  153. v1.snp.prepareConstraints { (make) -> Void in
  154. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  155. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  156. return
  157. }
  158. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  159. c1?.activate()
  160. c2?.activate()
  161. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  162. c1?.deactivate()
  163. c2?.deactivate()
  164. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  165. c1?.activate()
  166. c2?.activate()
  167. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  168. }
  169. func testEdgeConstraints() {
  170. let view = View()
  171. self.container.addSubview(view)
  172. view.snp.makeConstraints { (make) -> Void in
  173. make.edges.equalTo(self.container).offset(50.0)
  174. }
  175. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  176. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  177. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  178. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  179. XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
  180. XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
  181. }
  182. func testUpdateReferencedConstraints() {
  183. let v1 = View()
  184. let v2 = View()
  185. self.container.addSubview(v1)
  186. self.container.addSubview(v2)
  187. var c1: Constraint! = nil
  188. var c2: Constraint! = nil
  189. v1.snp.makeConstraints { (make) -> Void in
  190. c1 = make.top.equalTo(v2).offset(50).constraint
  191. c2 = make.bottom.equalTo(v2).offset(25).constraint
  192. return
  193. }
  194. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  195. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  196. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  197. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  198. c1.update(offset: 15)
  199. c2.update(offset: 20)
  200. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  201. XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
  202. XCTAssertEqual(constraints[1].constant, 20, "Should be 20")
  203. c1.update(inset: 15)
  204. c2.update(inset: 20)
  205. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  206. XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
  207. XCTAssertEqual(constraints[1].constant, -20, "Should be -20")
  208. }
  209. func testInsetsAsConstraintsConstant() {
  210. let view = View()
  211. self.container.addSubview(view)
  212. view.snp.makeConstraints { (make) -> Void in
  213. make.edges.equalTo(self.container).inset(50.0)
  214. }
  215. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  216. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  217. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  218. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  219. XCTAssertEqual(constraints[2].constant, -50, "Should be -50")
  220. XCTAssertEqual(constraints[3].constant, -50, "Should be -50")
  221. }
  222. func testConstraintInsetsAsImpliedEqualToConstraints() {
  223. let view = View()
  224. self.container.addSubview(view)
  225. view.snp.makeConstraints { (make) -> Void in
  226. make.edges.equalTo(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
  227. }
  228. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  229. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  230. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  231. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  232. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  233. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  234. }
  235. func testConstraintInsetsAsConstraintsConstant() {
  236. let view = View()
  237. self.container.addSubview(view)
  238. view.snp.makeConstraints { (make) -> Void in
  239. make.edges.equalTo(self.container).inset(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
  240. }
  241. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  242. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  243. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  244. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  245. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  246. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  247. }
  248. func testSizeConstraints() {
  249. let view = View()
  250. self.container.addSubview(view)
  251. view.snp.makeConstraints { (make) -> Void in
  252. make.size.equalTo(CGSize(width: 50, height: 50))
  253. make.left.top.equalTo(self.container)
  254. }
  255. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  256. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  257. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  258. // no guarantee which order the constraints are in, but we should test their couple
  259. let widthHeight = (NSLayoutAttribute.width.rawValue, NSLayoutAttribute.height.rawValue)
  260. let heightWidth = (widthHeight.1, widthHeight.0)
  261. let firstSecond = (constraints[0].firstAttribute.rawValue, constraints[1].firstAttribute.rawValue)
  262. // constraint values are correct in either width, height or height, width order
  263. XCTAssertTrue(firstSecond == widthHeight || firstSecond == heightWidth, "2 contraint values should match")
  264. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  265. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  266. }
  267. func testCenterConstraints() {
  268. let view = View()
  269. self.container.addSubview(view)
  270. view.snp.makeConstraints { (make) -> Void in
  271. make.center.equalTo(self.container).offset(50.0)
  272. }
  273. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  274. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  275. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  276. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  277. }
  278. func testConstraintIdentifier() {
  279. let identifier = "Test-Identifier"
  280. let view = View()
  281. self.container.addSubview(view)
  282. view.snp.makeConstraints { (make) -> Void in
  283. make.top.equalTo(self.container.snp.top).labeled(identifier)
  284. }
  285. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  286. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  287. }
  288. #if os(iOS) || os(tvOS)
  289. func testEdgesToMargins() {
  290. var fromAttributes = Set<NSLayoutAttribute>()
  291. var toAttributes = Set<NSLayoutAttribute>()
  292. let view = View()
  293. self.container.addSubview(view)
  294. view.snp.remakeConstraints { (make) -> Void in
  295. make.edges.equalTo(self.container.snp.margins)
  296. }
  297. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  298. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  299. fromAttributes.insert(constraint.firstAttribute)
  300. toAttributes.insert(constraint.secondAttribute)
  301. }
  302. XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
  303. XCTAssert(toAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
  304. fromAttributes.removeAll()
  305. toAttributes.removeAll()
  306. view.snp.remakeConstraints { (make) -> Void in
  307. make.margins.equalTo(self.container.snp.edges)
  308. }
  309. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  310. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  311. fromAttributes.insert(constraint.firstAttribute)
  312. toAttributes.insert(constraint.secondAttribute)
  313. }
  314. XCTAssert(toAttributes == [.top, .left, .bottom, .right])
  315. XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
  316. }
  317. func testLayoutGuideConstraints() {
  318. let vc = UIViewController()
  319. vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
  320. vc.view.addSubview(self.container)
  321. self.container.snp.makeConstraints { (make) -> Void in
  322. make.top.equalTo(vc.topLayoutGuide.snp.bottom)
  323. make.bottom.equalTo(vc.bottomLayoutGuide.snp.top)
  324. }
  325. print(vc.view.snp_constraints)
  326. XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed")
  327. }
  328. #endif
  329. }