Tests.swift 17 KB

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