SnapTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // SnapTests.swift
  3. // SnapTests
  4. //
  5. // Created by Robert Payne on 20/09/14.
  6. // Copyright (c) 2014 Jonas Budelmann. All rights reserved.
  7. //
  8. import UIKit
  9. import XCTest
  10. import Snap
  11. class SnapTests: XCTestCase {
  12. let container = UIView()
  13. override func setUp() {
  14. super.setUp()
  15. // Put setup code here. This method is called before the invocation of each test method in the class.
  16. }
  17. override func tearDown() {
  18. // Put teardown code here. This method is called after the invocation of each test method in the class.
  19. super.tearDown()
  20. }
  21. func testMakeConstraints() {
  22. let v1 = UIView()
  23. let v2 = UIView()
  24. self.container.addSubview(v1)
  25. self.container.addSubview(v2)
  26. v1.snp_makeConstraints { (make) -> Void in
  27. make.top.equalTo(v2.snp_top).offset(50)
  28. make.left.equalTo(v2.snp_top).offset(50)
  29. return
  30. }
  31. XCTAssertEqual(self.container.constraints().count, 2, "Should have 2 constraints installed")
  32. v2.snp_makeConstraints { (make) -> Void in
  33. make.edges.equalTo(v1)
  34. return
  35. }
  36. XCTAssertEqual(self.container.constraints().count, 6, "Should have 6 constraints installed")
  37. }
  38. func testUpdateConstraints() {
  39. let v1 = UIView()
  40. let v2 = UIView()
  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.constraints().count, 2, "Should have 2 constraints installed")
  49. v1.snp_updateConstraints { (make) -> Void in
  50. make.top.equalTo(v2.snp_top).offset(15)
  51. return
  52. }
  53. XCTAssertEqual(self.container.constraints().count, 2, "Should still have 2 constraints installed")
  54. }
  55. func testRemakeConstraints() {
  56. let v1 = UIView()
  57. let v2 = UIView()
  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.constraints().count, 2, "Should have 2 constraints installed")
  66. v1.snp_remakeConstraints { (make) -> Void in
  67. make.edges.equalTo(v2)
  68. return
  69. }
  70. XCTAssertEqual(self.container.constraints().count, 4, "Should have 4 constraints installed")
  71. }
  72. func testRemoveConstraints() {
  73. let v1 = UIView()
  74. let v2 = UIView()
  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.constraints().count, 2, "Should have 2 constraints installed")
  83. v1.snp_removeConstraints()
  84. XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
  85. }
  86. func testPrepareConstraints() {
  87. let v1 = UIView()
  88. let v2 = UIView()
  89. self.container.addSubview(v1)
  90. self.container.addSubview(v2)
  91. let constraints = v1.snp_prepareConstraints { (make) -> Void in
  92. make.edges.equalTo(v2)
  93. return
  94. }
  95. XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
  96. for constraint in constraints {
  97. constraint.install()
  98. }
  99. XCTAssertEqual(self.container.constraints().count, 4, "Should have 4 constraints installed")
  100. for constraint in constraints {
  101. constraint.uninstall()
  102. }
  103. XCTAssertEqual(self.container.constraints().count, 0, "Should have 0 constraints installed")
  104. }
  105. }