Tests.swift 18 KB

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