Tests.swift 18 KB

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