Tests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 testMakeConstraints() {
  27. let v1 = View()
  28. let v2 = View()
  29. self.container.addSubview(v1)
  30. self.container.addSubview(v2)
  31. v1.snp_makeConstraints { (make) -> Void in
  32. make.top.equalTo(v2.snp_top).offset(50)
  33. make.left.equalTo(v2.snp_top).offset(50)
  34. return
  35. }
  36. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  37. v2.snp_makeConstraints { (make) -> Void in
  38. make.edges.equalTo(v1)
  39. return
  40. }
  41. XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
  42. }
  43. func testUpdateConstraints() {
  44. let v1 = View()
  45. let v2 = View()
  46. self.container.addSubview(v1)
  47. self.container.addSubview(v2)
  48. v1.snp_makeConstraints { (make) -> Void in
  49. make.top.equalTo(v2.snp_top).offset(50)
  50. make.left.equalTo(v2.snp_top).offset(50)
  51. return
  52. }
  53. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  54. v1.snp_updateConstraints { (make) -> Void in
  55. make.top.equalTo(v2.snp_top).offset(15)
  56. return
  57. }
  58. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should still have 2 constraints installed")
  59. }
  60. func testRemakeConstraints() {
  61. let v1 = View()
  62. let v2 = View()
  63. self.container.addSubview(v1)
  64. self.container.addSubview(v2)
  65. v1.snp_makeConstraints { (make) -> Void in
  66. make.top.equalTo(v2.snp_top).offset(50)
  67. make.left.equalTo(v2.snp_top).offset(50)
  68. return
  69. }
  70. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  71. v1.snp_remakeConstraints { (make) -> Void in
  72. make.edges.equalTo(v2)
  73. return
  74. }
  75. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  76. }
  77. func testRemoveConstraints() {
  78. let v1 = View()
  79. let v2 = View()
  80. self.container.addSubview(v1)
  81. self.container.addSubview(v2)
  82. v1.snp_makeConstraints { (make) -> Void in
  83. make.top.equalTo(v2.snp_top).offset(50)
  84. make.left.equalTo(v2.snp_top).offset(50)
  85. return
  86. }
  87. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
  88. v1.snp_removeConstraints()
  89. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  90. }
  91. func testPrepareConstraints() {
  92. let v1 = View()
  93. let v2 = View()
  94. self.container.addSubview(v1)
  95. self.container.addSubview(v2)
  96. let constraints = v1.snp_prepareConstraints { (make) -> Void in
  97. make.edges.equalTo(v2)
  98. return
  99. }
  100. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  101. for constraint in constraints {
  102. constraint.install()
  103. }
  104. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  105. for constraint in constraints {
  106. constraint.uninstall()
  107. }
  108. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  109. }
  110. func testReinstallConstraints() {
  111. let v1 = View()
  112. let v2 = View()
  113. self.container.addSubview(v1)
  114. self.container.addSubview(v2)
  115. let constraints = v1.snp_prepareConstraints { (make) -> Void in
  116. make.edges.equalTo(v2)
  117. return
  118. }
  119. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
  120. for constraint in constraints {
  121. constraint.install()
  122. }
  123. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints installed")
  124. for constraint in constraints {
  125. constraint.install()
  126. }
  127. XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 0 constraints installed")
  128. }
  129. func testActivateDeactivateConstraints() {
  130. let v1 = View()
  131. let v2 = View()
  132. self.container.addSubview(v1)
  133. self.container.addSubview(v2)
  134. var c1: Constraint? = nil
  135. var c2: Constraint? = nil
  136. v1.snp_prepareConstraints { (make) -> Void in
  137. c1 = make.top.equalTo(v2.snp_top).offset(50).constraint
  138. c2 = make.left.equalTo(v2.snp_top).offset(50).constraint
  139. return
  140. }
  141. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  142. c1?.activate()
  143. c2?.activate()
  144. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  145. c1?.deactivate()
  146. c2?.deactivate()
  147. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  148. c1?.uninstall()
  149. c2?.uninstall()
  150. XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints")
  151. c1?.activate()
  152. c2?.activate()
  153. XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints")
  154. }
  155. }