Skip to content

Commit

Permalink
Improve constraint convenience methods (#249)
Browse files Browse the repository at this point in the history
* Improve constraint convenience methods

* Improve test name
  • Loading branch information
tinder-cfuller authored Dec 7, 2023
1 parent 0b2b53a commit bf4a772
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
16 changes: 10 additions & 6 deletions Sources/Layout/Swift/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,35 @@ extension Array where Element == NSLayoutConstraint {
@preconcurrency
@MainActor
@discardableResult
public func activate() -> [NSLayoutConstraint] {
public func activate() -> Self {
NSLayoutConstraint.activate(self)
return self
}

@preconcurrency
@MainActor
@discardableResult
public func deactivate() -> [NSLayoutConstraint] {
public func deactivate() -> Self {
NSLayoutConstraint.deactivate(self)
return self
}

@preconcurrency
@MainActor
public func withPriority(
_ priority: UILayoutPriority
) -> [NSLayoutConstraint] {
public func require() -> Self {
withPriority(.required)
}

@preconcurrency
@MainActor
public func withPriority(_ priority: UILayoutPriority) -> Self {
prioritize(priority)
return self
}

@preconcurrency
@MainActor
public func prioritize(_ priority: UILayoutPriority) {
forEach { $0.priority = priority }
forEach { $0.prioritize(priority) }
}
}
11 changes: 5 additions & 6 deletions Sources/Layout/UIKit/NSLayoutConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ extension NSLayoutConstraint {
return self
}

public func withConstant(_ constant: CGFloat) -> NSLayoutConstraint {
self.constant = constant
return self
public func require() -> NSLayoutConstraint {
withPriority(.required)
}

public func withPriority(_ priority: UILayoutPriority) -> NSLayoutConstraint {
self.priority = priority
prioritize(priority)
return self
}

public func require() -> NSLayoutConstraint {
withPriority(.required)
public func prioritize(_ priority: UILayoutPriority) {
self.priority = priority
}
}
38 changes: 38 additions & 0 deletions Tests/LayoutTests/Swift/ArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,44 @@ final class ArrayTests: XCTestCase {
expect(deactivatedConstraints) == constraints
}

func testRequire() {

// GIVEN

let view: UIView = .init()
let constraint1: NSLayoutConstraint = .init(
item: view,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 0
)
let constraint2: NSLayoutConstraint = .init(
item: view,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 0
)
constraint1.priority = .high
constraint2.priority = .high
let constraints: [NSLayoutConstraint] = [constraint1, constraint2]

// WHEN

let requiredConstraints: [NSLayoutConstraint] = constraints.require()

// THEN

expect(constraint1.priority) == .required
expect(constraint2.priority) == .required
expect(requiredConstraints) == constraints
}

func testWithPriority() {

// GIVEN
Expand Down
18 changes: 9 additions & 9 deletions Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,24 @@ final class NSLayoutConstraintTests: XCTestCase {
}
}

func testWithConstant() {
func testRequire() {

// GIVEN

let constraint: NSLayoutConstraint = .init()
constraint.priority = .high

// THEN

expect(constraint.constant) == 0
expect(constraint.priority) == .high

// WHEN

_ = constraint.withConstant(5)
_ = constraint.require()

// THEN

expect(constraint.constant) == 5
expect(constraint.priority) == .required
}

func testWithPriority() {
Expand All @@ -146,23 +147,22 @@ final class NSLayoutConstraintTests: XCTestCase {
expect(constraint.priority) == .high
}

func testRequiredPriority() {
func testPrioritize() {

// GIVEN

let constraint: NSLayoutConstraint = .init()
constraint.priority = .high

// THEN

expect(constraint.priority) == .high
expect(constraint.priority) == .required

// WHEN

_ = constraint.require()
constraint.prioritize(.high)

// THEN

expect(constraint.priority) == .required
expect(constraint.priority) == .high
}
}

0 comments on commit bf4a772

Please sign in to comment.