Tests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #if os(iOS)
  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. 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)
  28. let vc = UIViewController()
  29. vc.view = UIView(frame: CGRectMake(0, 0, 300, 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. v1.snp_makeConstraints { (make) -> Void in
  61. make.top.equalTo(v2.snp_top).offset(50)
  62. make.left.equalTo(v2.snp_top).offset(50)
  63. return
  64. }
  65. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  66. v1.snp_updateConstraints { (make) -> Void in
  67. make.top.equalTo(v2.snp_top).offset(15)
  68. return
  69. }
  70. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  71. }
  72. func testRemakeConstraints() {
  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_remakeConstraints { (make) -> Void in
  84. make.edges.equalTo(v2)
  85. return
  86. }
  87. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  88. }
  89. func testRemoveConstraints() {
  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_removeConstraints()
  101. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  102. }
  103. func testPrepareConstraints() {
  104. let v1 = View()
  105. let v2 = View()
  106. self.container.addSubview(v1)
  107. self.container.addSubview(v2)
  108. let constraints = v1.snp_prepareConstraints { (make) -> Void in
  109. make.edges.equalTo(v2)
  110. return
  111. }
  112. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  113. for constraint in constraints {
  114. constraint.install()
  115. }
  116. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  117. for constraint in constraints {
  118. constraint.uninstall()
  119. }
  120. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  121. }
  122. func testReinstallConstraints() {
  123. let v1 = View()
  124. let v2 = View()
  125. self.container.addSubview(v1)
  126. self.container.addSubview(v2)
  127. let constraints = v1.snp_prepareConstraints { (make) -> Void in
  128. make.edges.equalTo(v2)
  129. return
  130. }
  131. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  132. for constraint in constraints {
  133. constraint.install()
  134. }
  135. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  136. for constraint in constraints {
  137. constraint.install()
  138. }
  139. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 0 constraints installed")
  140. }
  141. func testActivateDeactivateConstraints() {
  142. let v1 = View()
  143. let v2 = View()
  144. self.container.addSubview(v1)
  145. self.container.addSubview(v2)
  146. var c1: Constraint? = nil
  147. var c2: Constraint? = nil
  148. v1.snp_prepareConstraints { (make) -> Void in
  149. c1 = make.top.equalTo(v2.snp_top).offset(50).constraint
  150. c2 = make.left.equalTo(v2.snp_top).offset(50).constraint
  151. return
  152. }
  153. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  154. c1?.activate()
  155. c2?.activate()
  156. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  157. c1?.deactivate()
  158. c2?.deactivate()
  159. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  160. c1?.uninstall()
  161. c2?.uninstall()
  162. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  163. c1?.activate()
  164. c2?.activate()
  165. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  166. }
  167. }