Tests.swift 27 KB

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