Tests.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #if os(iOS) || os(tvOS)
  2. import UIKit
  3. typealias View = UIView
  4. extension View {
  5. var snp_constraints: [AnyObject] { return self.constraints }
  6. }
  7. #else
  8. import AppKit
  9. typealias View = NSView
  10. extension View {
  11. var snp_constraints: [AnyObject] { return self.constraints }
  12. }
  13. #endif
  14. import XCTest
  15. @testable import SnapKit
  16. class SnapKitTests: XCTestCase {
  17. let container = View()
  18. override func setUp() {
  19. super.setUp()
  20. // Put setup code here. This method is called before the invocation of each test method in the class.
  21. }
  22. override func tearDown() {
  23. // Put teardown code here. This method is called after the invocation of each test method in the class.
  24. super.tearDown()
  25. }
  26. func testLayoutGuideConstraints() {
  27. #if os(iOS) || os(tvOS)
  28. let vc = UIViewController()
  29. vc.view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
  30. vc.view.addSubview(self.container)
  31. self.container.snp.makeConstraints { (make) -> Void in
  32. make.top.equalTo(vc.snp.topLayoutGuideBottom)
  33. make.bottom.equalTo(vc.snp.bottomLayoutGuideTop)
  34. }
  35. XCTAssertEqual(vc.view.snp_constraints.count, 6, "Should have 6 constraints installed")
  36. #endif
  37. }
  38. func testMakeConstraints() {
  39. let v1 = View()
  40. let v2 = View()
  41. self.container.addSubview(v1)
  42. self.container.addSubview(v2)
  43. v1.snp.makeConstraints { (make) -> Void in
  44. make.top.equalTo(v2.snp.top).offset(50)
  45. make.left.equalTo(v2.snp.top).offset(50)
  46. return
  47. }
  48. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  49. v2.snp.makeConstraints { (make) -> Void in
  50. make.edges.equalTo(v1)
  51. return
  52. }
  53. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  54. }
  55. func testMakeImpliedSuperviewConstraints() {
  56. let v1 = View()
  57. let v2 = View()
  58. self.container.addSubview(v1)
  59. self.container.addSubview(v2)
  60. v1.snp.makeConstraints { (make) -> Void in
  61. make.top.equalTo(50.0)
  62. make.left.equalTo(50.0)
  63. return
  64. }
  65. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  66. v2.snp.makeConstraints { (make) -> Void in
  67. make.edges.equalTo(v1)
  68. return
  69. }
  70. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  71. }
  72. func testUpdateConstraints() {
  73. let v1 = View()
  74. let v2 = View()
  75. self.container.addSubview(v1)
  76. self.container.addSubview(v2)
  77. v1.snp.makeConstraints { (make) -> Void in
  78. make.top.equalTo(v2.snp.top).offset(50)
  79. make.left.equalTo(v2.snp.top).offset(50)
  80. return
  81. }
  82. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  83. v1.snp.updateConstraints { (make) -> Void in
  84. make.top.equalTo(v2.snp.top).offset(15)
  85. return
  86. }
  87. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  88. }
  89. func testRemakeConstraints() {
  90. let v1 = View()
  91. let v2 = View()
  92. self.container.addSubview(v1)
  93. self.container.addSubview(v2)
  94. v1.snp.makeConstraints { (make) -> Void in
  95. make.top.equalTo(v2.snp.top).offset(50)
  96. make.left.equalTo(v2.snp.top).offset(50)
  97. return
  98. }
  99. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  100. v1.snp.remakeConstraints { (make) -> Void in
  101. make.edges.equalTo(v2)
  102. return
  103. }
  104. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  105. }
  106. func testRemoveConstraints() {
  107. let v1 = View()
  108. let v2 = View()
  109. self.container.addSubview(v1)
  110. self.container.addSubview(v2)
  111. v1.snp.makeConstraints { (make) -> Void in
  112. make.top.equalTo(v2.snp.top).offset(50)
  113. make.left.equalTo(v2.snp.top).offset(50)
  114. return
  115. }
  116. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  117. v1.snp.removeConstraints()
  118. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  119. }
  120. func testPrepareConstraints() {
  121. let v1 = View()
  122. let v2 = View()
  123. self.container.addSubview(v1)
  124. self.container.addSubview(v2)
  125. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  126. make.edges.equalTo(v2)
  127. return
  128. }
  129. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  130. for constraint in constraints {
  131. constraint.activate()
  132. }
  133. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  134. for constraint in constraints {
  135. constraint.deactivate()
  136. }
  137. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  138. }
  139. func testReactivateConstraints() {
  140. let v1 = View()
  141. let v2 = View()
  142. self.container.addSubview(v1)
  143. self.container.addSubview(v2)
  144. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  145. make.edges.equalTo(v2)
  146. return
  147. }
  148. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  149. for constraint in constraints {
  150. constraint.activate()
  151. }
  152. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  153. for constraint in constraints {
  154. constraint.deactivate()
  155. }
  156. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  157. }
  158. func testActivateDeactivateConstraints() {
  159. let v1 = View()
  160. let v2 = View()
  161. self.container.addSubview(v1)
  162. self.container.addSubview(v2)
  163. var c1: Constraint? = nil
  164. var c2: Constraint? = nil
  165. v1.snp.prepareConstraints { (make) -> Void in
  166. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  167. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  168. return
  169. }
  170. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  171. c1?.activate()
  172. c2?.activate()
  173. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  174. c1?.deactivate()
  175. c2?.deactivate()
  176. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  177. c1?.activate()
  178. c2?.activate()
  179. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  180. }
  181. func testEdgeConstraints() {
  182. let view = View()
  183. self.container.addSubview(view)
  184. view.snp.makeConstraints { (make) -> Void in
  185. make.edges.equalTo(self.container).offset(50.0)
  186. }
  187. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
  188. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  189. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  190. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  191. XCTAssertEqual(constraints[2].constant, 50, "Should be 50")
  192. XCTAssertEqual(constraints[3].constant, 50, "Should be 50")
  193. }
  194. func testSizeConstraints() {
  195. let view = View()
  196. self.container.addSubview(view)
  197. view.snp.makeConstraints { (make) -> Void in
  198. make.size.equalTo(CGSize(width: 50, height: 50))
  199. make.left.top.equalTo(self.container)
  200. }
  201. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  202. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  203. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  204. XCTAssertEqual(constraints[0].firstAttribute.rawValue, NSLayoutAttribute.width.rawValue, "Should be width")
  205. XCTAssertEqual(constraints[1].firstAttribute.rawValue, NSLayoutAttribute.height.rawValue, "Should be height")
  206. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  207. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  208. }
  209. func testCenterConstraints() {
  210. let view = View()
  211. self.container.addSubview(view)
  212. view.snp.makeConstraints { (make) -> Void in
  213. make.center.equalTo(self.container).offset(50.0)
  214. }
  215. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  216. let constraints = self.container.snp_constraints as! [NSLayoutConstraint]
  217. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  218. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  219. }
  220. func testConstraintIdentifier() {
  221. let identifier = "Test-Identifier"
  222. let view = View()
  223. self.container.addSubview(view)
  224. view.snp.makeConstraints { (make) -> Void in
  225. make.top.equalTo(self.container.snp.top).labeled(identifier)
  226. }
  227. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  228. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  229. }
  230. //
  231. //// func testSuperviewConstraints() {
  232. //// let view = View()
  233. ////
  234. //// container.addSubview(view)
  235. ////
  236. //// view.snp.makeConstraints { (make) -> Void in
  237. //// make.top.equalToSuperview().inset(10)
  238. //// make.bottom.equalToSuperview().inset(10)
  239. //// }
  240. ////
  241. //// XCTAssertEqual(container.snp_constraints.count, 2, "Should have 2 constraints")
  242. ////
  243. //// let constraints = container.snp_constraints as! [NSLayoutConstraint]
  244. ////
  245. //// XCTAssertEqual(constraints[0].firstAttribute, NSLayoutAttribute.top, "Should be top")
  246. //// XCTAssertEqual(constraints[1].firstAttribute, NSLayoutAttribute.bottom, "Should be bottom")
  247. ////
  248. //// XCTAssertEqual(constraints[0].secondAttribute, NSLayoutAttribute.top, "Should be top")
  249. //// XCTAssertEqual(constraints[1].secondAttribute, NSLayoutAttribute.bottom, "Should be bottom")
  250. ////
  251. //// XCTAssertEqual(constraints[0].firstItem as? View, view, "Should be added subview")
  252. //// XCTAssertEqual(constraints[1].firstItem as? View, view, "Should be added subview")
  253. ////
  254. //// XCTAssertEqual(constraints[0].secondItem as? View, container, "Should be containerView")
  255. //// XCTAssertEqual(constraints[1].secondItem as? View, container, "Should be containerView")
  256. ////
  257. //// XCTAssertEqual(constraints[0].constant, 10, "Should be 10")
  258. //// XCTAssertEqual(constraints[1].constant, -10, "Should be 10")
  259. //// }
  260. //
  261. //// func testNativeConstraints() {
  262. //// let view = View()
  263. ////
  264. //// container.addSubview(view)
  265. ////
  266. //// var topNativeConstraints: [LayoutConstraint]!
  267. //// var topNativeConstraint: LayoutConstraint?
  268. //// var sizeNativeConstraints: [LayoutConstraint]!
  269. //// view.snp_makeConstraints { (make) -> Void in
  270. //// let topConstraint = make.top.equalToSuperview().inset(10).constraint
  271. //// topNativeConstraints = topConstraint.layoutConstraints
  272. //// topNativeConstraint = topConstraint.layoutConstraints.first
  273. //// let sizeConstraints = make.size.equalTo(50).constraint
  274. //// sizeNativeConstraints = sizeConstraints.layoutConstraints
  275. //// }
  276. ////
  277. //// XCTAssertEqual(topNativeConstraints.count, 1, "make.top should creates one native constraint")
  278. //// XCTAssertEqual(topNativeConstraint?.constant, 10, "topNativeConstraint.constant is set to 10")
  279. //// XCTAssertEqual(sizeNativeConstraints.count, 2, "make.tosize should create two native constraint")
  280. //// XCTAssertEqual(sizeNativeConstraints[0].constant, 50, "sizeNativeConstraints should set size[0] to 50")
  281. //// XCTAssertEqual(sizeNativeConstraints[1].constant, 50, "sizeNativeConstraints should set size[1] to 50")
  282. //// }
  283. }