Tests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 testUpdateConstraints() {
  56. let v1 = View()
  57. let v2 = View()
  58. self.container.addSubview(v1)
  59. self.container.addSubview(v2)
  60. print(v1)
  61. v1.snp.makeConstraints { (make) -> Void in
  62. make.top.equalTo(v2.snp.top).offset(50)
  63. make.left.equalTo(v2.snp.top).offset(50)
  64. return
  65. }
  66. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  67. v1.snp.updateConstraints { (make) -> Void in
  68. make.top.equalTo(v2.snp.top).offset(15)
  69. return
  70. }
  71. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  72. }
  73. func testRemakeConstraints() {
  74. let v1 = View()
  75. let v2 = View()
  76. self.container.addSubview(v1)
  77. self.container.addSubview(v2)
  78. v1.snp.makeConstraints { (make) -> Void in
  79. make.top.equalTo(v2.snp.top).offset(50)
  80. make.left.equalTo(v2.snp.top).offset(50)
  81. return
  82. }
  83. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  84. v1.snp.remakeConstraints { (make) -> Void in
  85. make.edges.equalTo(v2)
  86. return
  87. }
  88. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  89. }
  90. func testRemoveConstraints() {
  91. let v1 = View()
  92. let v2 = View()
  93. self.container.addSubview(v1)
  94. self.container.addSubview(v2)
  95. v1.snp.makeConstraints { (make) -> Void in
  96. make.top.equalTo(v2.snp.top).offset(50)
  97. make.left.equalTo(v2.snp.top).offset(50)
  98. return
  99. }
  100. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  101. v1.snp.removeConstraints()
  102. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  103. }
  104. func testPrepareConstraints() {
  105. let v1 = View()
  106. let v2 = View()
  107. self.container.addSubview(v1)
  108. self.container.addSubview(v2)
  109. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  110. make.edges.equalTo(v2)
  111. return
  112. }
  113. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  114. for constraint in constraints {
  115. constraint.activate()
  116. }
  117. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  118. for constraint in constraints {
  119. constraint.deactivate()
  120. }
  121. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  122. }
  123. func testReactivateConstraints() {
  124. let v1 = View()
  125. let v2 = View()
  126. self.container.addSubview(v1)
  127. self.container.addSubview(v2)
  128. let constraints = v1.snp.prepareConstraints { (make) -> Void in
  129. make.edges.equalTo(v2)
  130. return
  131. }
  132. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  133. for constraint in constraints {
  134. constraint.activate()
  135. }
  136. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  137. for constraint in constraints {
  138. constraint.deactivate()
  139. }
  140. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  141. }
  142. func testActivateDeactivateConstraints() {
  143. let v1 = View()
  144. let v2 = View()
  145. self.container.addSubview(v1)
  146. self.container.addSubview(v2)
  147. var c1: Constraint? = nil
  148. var c2: Constraint? = nil
  149. v1.snp.prepareConstraints { (make) -> Void in
  150. c1 = make.top.equalTo(v2.snp.top).offset(50).constraint
  151. c2 = make.left.equalTo(v2.snp.top).offset(50).constraint
  152. return
  153. }
  154. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  155. c1?.activate()
  156. c2?.activate()
  157. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  158. c1?.deactivate()
  159. c2?.deactivate()
  160. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  161. c1?.activate()
  162. c2?.activate()
  163. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  164. }
  165. func testSizeConstraints() {
  166. let view = View()
  167. self.container.addSubview(view)
  168. view.snp.makeConstraints { (make) -> Void in
  169. make.size.equalTo(CGSize(width: 50, height: 50))
  170. make.left.top.equalTo(self.container)
  171. }
  172. XCTAssertEqual(view.snp_constraints.count, 2, "Should have 2 constraints")
  173. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  174. let constraints = view.snp_constraints as! [NSLayoutConstraint]
  175. XCTAssertEqual(constraints[0].firstAttribute.rawValue, NSLayoutAttribute.width.rawValue, "Should be width")
  176. XCTAssertEqual(constraints[1].firstAttribute.rawValue, NSLayoutAttribute.height.rawValue, "Should be height")
  177. XCTAssertEqual(constraints[0].constant, 50, "Should be 50")
  178. XCTAssertEqual(constraints[1].constant, 50, "Should be 50")
  179. }
  180. func testConstraintIdentifier() {
  181. let identifier = "Test-Identifier"
  182. let view = View()
  183. self.container.addSubview(view)
  184. view.snp.makeConstraints { (make) -> Void in
  185. make.top.equalTo(self.container.snp.top).labeled(identifier)
  186. }
  187. let constraints = container.snp_constraints as! [NSLayoutConstraint]
  188. XCTAssertEqual(constraints[0].identifier, identifier, "Identifier should be 'Test'")
  189. }
  190. //
  191. //// func testSuperviewConstraints() {
  192. //// let view = View()
  193. ////
  194. //// container.addSubview(view)
  195. ////
  196. //// view.snp.makeConstraints { (make) -> Void in
  197. //// make.top.equalToSuperview().inset(10)
  198. //// make.bottom.equalToSuperview().inset(10)
  199. //// }
  200. ////
  201. //// XCTAssertEqual(container.snp_constraints.count, 2, "Should have 2 constraints")
  202. ////
  203. //// let constraints = container.snp_constraints as! [NSLayoutConstraint]
  204. ////
  205. //// XCTAssertEqual(constraints[0].firstAttribute, NSLayoutAttribute.top, "Should be top")
  206. //// XCTAssertEqual(constraints[1].firstAttribute, NSLayoutAttribute.bottom, "Should be bottom")
  207. ////
  208. //// XCTAssertEqual(constraints[0].secondAttribute, NSLayoutAttribute.top, "Should be top")
  209. //// XCTAssertEqual(constraints[1].secondAttribute, NSLayoutAttribute.bottom, "Should be bottom")
  210. ////
  211. //// XCTAssertEqual(constraints[0].firstItem as? View, view, "Should be added subview")
  212. //// XCTAssertEqual(constraints[1].firstItem as? View, view, "Should be added subview")
  213. ////
  214. //// XCTAssertEqual(constraints[0].secondItem as? View, container, "Should be containerView")
  215. //// XCTAssertEqual(constraints[1].secondItem as? View, container, "Should be containerView")
  216. ////
  217. //// XCTAssertEqual(constraints[0].constant, 10, "Should be 10")
  218. //// XCTAssertEqual(constraints[1].constant, -10, "Should be 10")
  219. //// }
  220. //
  221. //// func testNativeConstraints() {
  222. //// let view = View()
  223. ////
  224. //// container.addSubview(view)
  225. ////
  226. //// var topNativeConstraints: [LayoutConstraint]!
  227. //// var topNativeConstraint: LayoutConstraint?
  228. //// var sizeNativeConstraints: [LayoutConstraint]!
  229. //// view.snp_makeConstraints { (make) -> Void in
  230. //// let topConstraint = make.top.equalToSuperview().inset(10).constraint
  231. //// topNativeConstraints = topConstraint.layoutConstraints
  232. //// topNativeConstraint = topConstraint.layoutConstraints.first
  233. //// let sizeConstraints = make.size.equalTo(50).constraint
  234. //// sizeNativeConstraints = sizeConstraints.layoutConstraints
  235. //// }
  236. ////
  237. //// XCTAssertEqual(topNativeConstraints.count, 1, "make.top should creates one native constraint")
  238. //// XCTAssertEqual(topNativeConstraint?.constant, 10, "topNativeConstraint.constant is set to 10")
  239. //// XCTAssertEqual(sizeNativeConstraints.count, 2, "make.tosize should create two native constraint")
  240. //// XCTAssertEqual(sizeNativeConstraints[0].constant, 50, "sizeNativeConstraints should set size[0] to 50")
  241. //// XCTAssertEqual(sizeNativeConstraints[1].constant, 50, "sizeNativeConstraints should set size[1] to 50")
  242. //// }
  243. }