Tests.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #if canImport(UIKit)
  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 testHorizontalVerticalEdges() {
  52. let v1 = View()
  53. self.container.addSubview(v1)
  54. v1.snp.makeConstraints { (make) -> Void in
  55. make.verticalEdges.equalToSuperview()
  56. make.horizontalEdges.equalToSuperview()
  57. return
  58. }
  59. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  60. XCTAssertTrue(container.constraints.count == 4)
  61. XCTAssertTrue(container.constraints.allSatisfy { $0.firstItem === v1 && $0.secondItem === v1.superview })
  62. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .left && $0.secondAttribute == .left })
  63. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .right && $0.secondAttribute == .right })
  64. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .top && $0.secondAttribute == .top })
  65. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .bottom && $0.secondAttribute == .bottom })
  66. }
  67. func testHorizontalVerticalDirectionalEdges() {
  68. let v1 = View()
  69. self.container.addSubview(v1)
  70. v1.snp.makeConstraints { (make) -> Void in
  71. make.directionalVerticalEdges.equalToSuperview()
  72. make.directionalHorizontalEdges.equalToSuperview()
  73. return
  74. }
  75. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  76. XCTAssertTrue(container.constraints.count == 4)
  77. XCTAssertTrue(container.constraints.allSatisfy { $0.firstItem === v1 && $0.secondItem === v1.superview })
  78. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .leading && $0.secondAttribute == .leading })
  79. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .trailing && $0.secondAttribute == .trailing })
  80. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .top && $0.secondAttribute == .top })
  81. XCTAssertNotNil(container.constraints.first { $0.firstAttribute == .bottom && $0.secondAttribute == .bottom })
  82. }
  83. func testGuideMakeConstraints() {
  84. guard #available(iOS 9.0, OSX 10.11, *) else { return }
  85. let v1 = View()
  86. let g1 = ConstraintLayoutGuide()
  87. self.container.addSubview(v1)
  88. self.container.addLayoutGuide(g1)
  89. v1.snp.makeConstraints { (make) -> Void in
  90. make.top.equalTo(g1).offset(50)
  91. make.left.equalTo(g1.snp.top).offset(50)
  92. return
  93. }
  94. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  95. g1.snp.makeConstraints { (make) -> Void in
  96. make.edges.equalTo(v1)
  97. return
  98. }
  99. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  100. }
  101. func testMakeImpliedSuperviewConstraints() {
  102. let v1 = View()
  103. let v2 = View()
  104. self.container.addSubview(v1)
  105. self.container.addSubview(v2)
  106. v1.snp.makeConstraints { (make) -> Void in
  107. make.top.equalTo(50.0)
  108. make.left.equalTo(50.0)
  109. return
  110. }
  111. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  112. v2.snp.makeConstraints { (make) -> Void in
  113. make.edges.equalTo(v1)
  114. return
  115. }
  116. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  117. }
  118. func testUpdateConstraints() {
  119. let v1 = View()
  120. let v2 = View()
  121. self.container.addSubview(v1)
  122. self.container.addSubview(v2)
  123. v1.snp.makeConstraints { (make) -> Void in
  124. make.top.equalTo(v2.snp.top).offset(50)
  125. make.left.equalTo(v2.snp.top).offset(50)
  126. return
  127. }
  128. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  129. v1.snp.updateConstraints { (make) -> Void in
  130. make.top.equalTo(v2.snp.top).offset(15)
  131. return
  132. }
  133. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  134. }
  135. func testRemakeConstraints() {
  136. let v1 = View()
  137. let v2 = View()
  138. self.container.addSubview(v1)
  139. self.container.addSubview(v2)
  140. v1.snp.makeConstraints { (make) -> Void in
  141. make.top.equalTo(v2.snp.top).offset(50)
  142. make.left.equalTo(v2.snp.top).offset(50)
  143. return
  144. }
  145. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  146. v1.snp.remakeConstraints { (make) -> Void in
  147. make.edges.equalTo(v2)
  148. return
  149. }
  150. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  151. }
  152. func testRemoveConstraints() {
  153. let v1 = View()
  154. let v2 = View()
  155. self.container.addSubview(v1)
  156. self.container.addSubview(v2)
  157. v1.snp.makeConstraints { (make) -> Void in
  158. make.top.equalTo(v2).offset(50)
  159. make.left.equalTo(v2).offset(50)
  160. return
  161. }
  162. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  163. print(self.container.snp_constraints)
  164. v1.snp.removeConstraints()
  165. print(self.container.snp_constraints)
  166. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  167. }
  168. func testPrepareConstraints() {
  169. let v1 = View()
  170. let v2 = View()
  171. self.container.addSubview(v1)
  172. self.container.addSubview(v2)
  173. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  174. make.edges.equalTo(v2)
  175. return
  176. }
  177. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  178. for constraint in constraints {
  179. constraint.activate()
  180. }
  181. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  182. for constraint in constraints {
  183. constraint.deactivate()
  184. }
  185. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  186. }
  187. func testReactivateConstraints() {
  188. let v1 = View()
  189. let v2 = View()
  190. self.container.addSubview(v1)
  191. self.container.addSubview(v2)
  192. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  193. make.edges.equalTo(v2)
  194. return
  195. }
  196. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  197. for constraint in constraints {
  198. constraint.activate()
  199. }
  200. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  201. for constraint in constraints {
  202. constraint.deactivate()
  203. }
  204. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  205. }
  206. func testActivateDeactivateConstraints() {
  207. let v1 = View()
  208. let v2 = View()
  209. self.container.addSubview(v1)
  210. self.container.addSubview(v2)
  211. var c1: Constraint? = nil
  212. var c2: Constraint? = nil
  213. v1.snp.prepareConstraints { (make) -> Void in
  214. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  215. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  216. return
  217. }
  218. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  219. c1?.activate()
  220. c2?.activate()
  221. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  222. c1?.deactivate()
  223. c2?.deactivate()
  224. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  225. c1?.activate()
  226. c2?.activate()
  227. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  228. }
  229. func testSetIsActivatedConstraints() {
  230. let v1 = View()
  231. let v2 = View()
  232. self.container.addSubview(v1)
  233. self.container.addSubview(v2)
  234. var c1: Constraint? = nil
  235. var c2: Constraint? = nil
  236. v1.snp.prepareConstraints { (make) -> Void in
  237. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  238. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  239. return
  240. }
  241. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  242. c1?.isActive = true
  243. c2?.isActive = false
  244. XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
  245. c1?.isActive = true
  246. c2?.isActive = true
  247. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  248. c1?.isActive = false
  249. c2?.isActive = false
  250. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  251. }
  252. func testEdgeConstraints() {
  253. let view = View()
  254. self.container.addSubview(view)
  255. view.snp.makeConstraints { (make) -> Void in
  256. make.edges.equalTo(self.container).offset(50.0)
  257. }
  258. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  259. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  260. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  261. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  262. XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
  263. XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
  264. }
  265. func testUpdateReferencedConstraints() {
  266. let v1 = View()
  267. let v2 = View()
  268. self.container.addSubview(v1)
  269. self.container.addSubview(v2)
  270. var c1: Constraint! = nil
  271. var c2: Constraint! = nil
  272. v1.snp.makeConstraints { (make) -> Void in
  273. c1 = make.top.equalTo(v2).offset(50).constraint
  274. c2 = make.bottom.equalTo(v2).offset(25).constraint
  275. return
  276. }
  277. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  278. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  279. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  280. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  281. c1.update(offset: 15)
  282. c2.update(offset: 20)
  283. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  284. XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
  285. XCTAssertEqual(constraints[1].constant, 20, "Should be 20")
  286. c1.update(inset: 15)
  287. c2.update(inset: 20)
  288. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  289. XCTAssertEqual(constraints[0].constant, 15, "Should be 15")
  290. XCTAssertEqual(constraints[1].constant, -20, "Should be -20")
  291. }
  292. func testInsetsAsConstraintsConstant() {
  293. let view = View()
  294. self.container.addSubview(view)
  295. view.snp.makeConstraints { (make) -> Void in
  296. make.edges.equalTo(self.container).inset(50.0)
  297. }
  298. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  299. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  300. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  301. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  302. XCTAssertEqual(constraints[2].constant, -50, "Should be -50")
  303. XCTAssertEqual(constraints[3].constant, -50, "Should be -50")
  304. }
  305. func testConstraintInsetsAsImpliedEqualToConstraints() {
  306. let view = View()
  307. self.container.addSubview(view)
  308. view.snp.makeConstraints { (make) -> Void in
  309. make.edges.equalTo(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
  310. }
  311. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  312. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  313. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  314. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  315. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  316. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  317. }
  318. func testConstraintInsetsAsConstraintsConstant() {
  319. let view = View()
  320. self.container.addSubview(view)
  321. view.snp.makeConstraints { (make) -> Void in
  322. make.edges.equalTo(self.container).inset(ConstraintInsets(top: 25, left: 25, bottom: 25, right: 25))
  323. }
  324. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  325. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.constant > $1.constant }
  326. XCTAssertEqual(constraints[0].constant, 25, "Should be 25")
  327. XCTAssertEqual(constraints[1].constant, 25, "Should be 25")
  328. XCTAssertEqual(constraints[2].constant, -25, "Should be -25")
  329. XCTAssertEqual(constraints[3].constant, -25, "Should be -25")
  330. }
  331. #if canImport(UIKit)
  332. @available(iOS 11.0, tvOS 11.0, *)
  333. func testConstraintDirectionalInsetsAsImpliedEqualToConstraints() {
  334. let view = View()
  335. self.container.addSubview(view)
  336. view.snp.makeConstraints { (make) -> Void in
  337. make.top.leading.bottom.trailing.equalTo(self.container).inset(ConstraintDirectionalInsets(top: 25, leading: 25, bottom: 25, trailing: 25))
  338. }
  339. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  340. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.firstAttribute.rawValue < $1.firstAttribute.rawValue }
  341. let verify: (NSLayoutConstraint, NSLayoutConstraint.Attribute, CGFloat) -> Void = { constraint, attribute, constant in
  342. XCTAssertEqual(constraint.firstAttribute, attribute, "First attribute \(constraint.firstAttribute.rawValue) is not \(attribute.rawValue)")
  343. XCTAssertEqual(constraint.secondAttribute, attribute, "Second attribute \(constraint.secondAttribute.rawValue) is not \(attribute.rawValue)")
  344. XCTAssertEqual(constraint.constant, constant, "Attribute \(attribute.rawValue) should have constant \(constant)")
  345. }
  346. verify(constraints[0], .top, 25)
  347. verify(constraints[1], .bottom, -25)
  348. verify(constraints[2], .leading, 25)
  349. verify(constraints[3], .trailing, -25)
  350. }
  351. #endif
  352. #if canImport(UIKit)
  353. @available(iOS 11.0, tvOS 11.0, *)
  354. func testConstraintDirectionalInsetsAsConstraintsConstant() {
  355. let view = View()
  356. self.container.addSubview(view)
  357. view.snp.makeConstraints { (make) -> Void in
  358. make.top.leading.bottom.trailing.equalTo(self.container).inset(ConstraintDirectionalInsets(top: 25, leading: 25, bottom: 25, trailing: 25))
  359. }
  360. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  361. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.firstAttribute.rawValue < $1.firstAttribute.rawValue }
  362. let verify: (NSLayoutConstraint, NSLayoutConstraint.Attribute, CGFloat) -> Void = { constraint, attribute, constant in
  363. XCTAssertEqual(constraint.firstAttribute, attribute, "First attribute \(constraint.firstAttribute.rawValue) is not \(attribute.rawValue)")
  364. XCTAssertEqual(constraint.secondAttribute, attribute, "Second attribute \(constraint.secondAttribute.rawValue) is not \(attribute.rawValue)")
  365. XCTAssertEqual(constraint.constant, constant, "Attribute \(attribute.rawValue) should have constant \(constant)")
  366. }
  367. verify(constraints[0], .top, 25)
  368. verify(constraints[1], .bottom, -25)
  369. verify(constraints[2], .leading, 25)
  370. verify(constraints[3], .trailing, -25)
  371. }
  372. #endif
  373. #if canImport(UIKit)
  374. @available(iOS 11.0, tvOS 11.0, *)
  375. func testConstraintDirectionalInsetsFallBackForNonDirectionalConstraints() {
  376. let view = View()
  377. self.container.addSubview(view)
  378. view.snp.makeConstraints { (make) -> Void in
  379. make.edges.equalTo(self.container).inset(ConstraintDirectionalInsets(top: 25, leading: 25, bottom: 25, trailing: 25))
  380. }
  381. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  382. let constraints = (self.container.snp_constraints as! [NSLayoutConstraint]).sorted { $0.firstAttribute.rawValue < $1.firstAttribute.rawValue }
  383. let verify: (NSLayoutConstraint, NSLayoutConstraint.Attribute, CGFloat) -> Void = { constraint, attribute, constant in
  384. XCTAssertEqual(constraint.firstAttribute, attribute, "First attribute \(constraint.firstAttribute.rawValue) is not \(attribute.rawValue)")
  385. XCTAssertEqual(constraint.secondAttribute, attribute, "Second attribute \(constraint.secondAttribute.rawValue) is not \(attribute.rawValue)")
  386. XCTAssertEqual(constraint.constant, constant, "Attribute \(attribute.rawValue) should have constant \(constant)")
  387. }
  388. verify(constraints[0], .left, 25)
  389. verify(constraints[1], .right, -25)
  390. verify(constraints[2], .top, 25)
  391. verify(constraints[3], .bottom, -25)
  392. }
  393. #endif
  394. func testSizeConstraints() {
  395. let view = View()
  396. self.container.addSubview(view)
  397. view.snp.makeConstraints { (make) -> Void in
  398. make.size.equalTo(CGSize(width: 50, height: 50))
  399. make.left.top.equalTo(self.container)
  400. }
  401. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  402. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  403. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  404. // no guarantee which order the constraints are in, but we should test their couple
  405. let widthHeight = (LayoutAttribute.width.rawValue, LayoutAttribute.height.rawValue)
  406. let heightWidth = (widthHeight.1, widthHeight.0)
  407. let firstSecond = (constraints[0].firstAttribute.rawValue, constraints[1].firstAttribute.rawValue)
  408. // constraint values are correct in either width, height or height, width order
  409. XCTAssertTrue(firstSecond == widthHeight || firstSecond == heightWidth, "2 contraint values should match")
  410. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  411. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  412. }
  413. func testCenterConstraints() {
  414. let view = View()
  415. self.container.addSubview(view)
  416. view.snp.makeConstraints { (make) -> Void in
  417. make.center.equalTo(self.container).offset(50.0)
  418. }
  419. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  420. if let constraints = self.container.snp_constraints as? [NSLayoutConstraint], constraints.count > 0 {
  421. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  422. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  423. }
  424. }
  425. func testConstraintIdentifier() {
  426. let identifier = "Test-Identifier"
  427. let view = View()
  428. self.container.addSubview(view)
  429. view.snp.makeConstraints { (make) -> Void in
  430. make.top.equalTo(self.container.snp.top).labeled(identifier)
  431. }
  432. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  433. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  434. }
  435. func testEdgesToEdges() {
  436. var fromAttributes = Set<LayoutAttribute>()
  437. var toAttributes = Set<LayoutAttribute>()
  438. let view = View()
  439. self.container.addSubview(view)
  440. view.snp.remakeConstraints { (make) -> Void in
  441. make.edges.equalTo(self.container.snp.edges)
  442. }
  443. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  444. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  445. fromAttributes.insert(constraint.firstAttribute)
  446. toAttributes.insert(constraint.secondAttribute)
  447. }
  448. XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
  449. XCTAssert(toAttributes == [.top, .left, .bottom, .right])
  450. }
  451. func testDirectionalEdgesToDirectionalEdges() {
  452. var fromAttributes = Set<LayoutAttribute>()
  453. var toAttributes = Set<LayoutAttribute>()
  454. let view = View()
  455. self.container.addSubview(view)
  456. view.snp.remakeConstraints { (make) -> Void in
  457. make.directionalEdges.equalTo(self.container.snp.directionalEdges)
  458. }
  459. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  460. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  461. fromAttributes.insert(constraint.firstAttribute)
  462. toAttributes.insert(constraint.secondAttribute)
  463. }
  464. XCTAssert(fromAttributes == [.top, .leading, .bottom, .trailing])
  465. XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing])
  466. }
  467. #if canImport(UIKit)
  468. func testEdgesToMargins() {
  469. var fromAttributes = Set<LayoutAttribute>()
  470. var toAttributes = Set<LayoutAttribute>()
  471. let view = View()
  472. self.container.addSubview(view)
  473. view.snp.remakeConstraints { (make) -> Void in
  474. make.edges.equalTo(self.container.snp.margins)
  475. }
  476. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  477. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  478. fromAttributes.insert(constraint.firstAttribute)
  479. toAttributes.insert(constraint.secondAttribute)
  480. }
  481. XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
  482. XCTAssert(toAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
  483. fromAttributes.removeAll()
  484. toAttributes.removeAll()
  485. view.snp.remakeConstraints { (make) -> Void in
  486. make.margins.equalTo(self.container.snp.edges)
  487. }
  488. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  489. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  490. fromAttributes.insert(constraint.firstAttribute)
  491. toAttributes.insert(constraint.secondAttribute)
  492. }
  493. XCTAssert(toAttributes == [.top, .left, .bottom, .right])
  494. XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
  495. }
  496. func testDirectionalEdgesToDirectionalMargins() {
  497. var fromAttributes = Set<LayoutAttribute>()
  498. var toAttributes = Set<LayoutAttribute>()
  499. let view = View()
  500. self.container.addSubview(view)
  501. view.snp.remakeConstraints { (make) -> Void in
  502. make.directionalEdges.equalTo(self.container.snp.directionalMargins)
  503. }
  504. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  505. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  506. fromAttributes.insert(constraint.firstAttribute)
  507. toAttributes.insert(constraint.secondAttribute)
  508. }
  509. XCTAssert(fromAttributes == [.top, .leading, .bottom, .trailing])
  510. XCTAssert(toAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin])
  511. fromAttributes.removeAll()
  512. toAttributes.removeAll()
  513. view.snp.remakeConstraints { (make) -> Void in
  514. make.directionalMargins.equalTo(self.container.snp.directionalEdges)
  515. }
  516. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  517. for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
  518. fromAttributes.insert(constraint.firstAttribute)
  519. toAttributes.insert(constraint.secondAttribute)
  520. }
  521. XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing])
  522. XCTAssert(fromAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin])
  523. }
  524. func testLayoutGuideConstraints() {
  525. let vc = UIViewController()
  526. vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
  527. vc.view.addSubview(self.container)
  528. self.container.snp.makeConstraints { (make) -> Void in
  529. make.top.equalTo(vc.topLayoutGuide.snp.bottom)
  530. make.bottom.equalTo(vc.bottomLayoutGuide.snp.top)
  531. }
  532. XCTAssertEqual(vc.view.snp_constraints.count, 2, "Should have 2 constraints installed")
  533. }
  534. #endif
  535. func testCanSetLabel() {
  536. self.container.snp.setLabel("Hello World")
  537. XCTAssertEqual(self.container.snp.label(), "Hello World")
  538. }
  539. func testPriorityShortcuts() {
  540. let view = View()
  541. self.container.addSubview(view)
  542. view.snp.remakeConstraints { make in
  543. make.left.equalTo(1000.0).priority(.required)
  544. }
  545. XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
  546. XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.required.value)
  547. view.snp.remakeConstraints { make in
  548. make.left.equalTo(1000.0).priority(.low)
  549. }
  550. XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
  551. XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.low.value)
  552. view.snp.remakeConstraints { make in
  553. make.left.equalTo(1000.0).priority(ConstraintPriority.low.value + 1)
  554. }
  555. XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint")
  556. XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.low.value + 1)
  557. }
  558. func testPriorityStride() {
  559. let highPriority: ConstraintPriority = .high
  560. let higherPriority: ConstraintPriority = ConstraintPriority.high.advanced(by: 1)
  561. XCTAssertEqual(higherPriority.value, highPriority.value + 1)
  562. }
  563. }