Skip to content

Commit

Permalink
Add multiplier argument to NSLayoutDimension
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller committed Feb 21, 2024
1 parent 2a2d4c6 commit fb8fb79
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
9 changes: 5 additions & 4 deletions Sources/Layout/UIKit/NSLayoutDimension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ extension NSLayoutDimension {
public func constraint(
is relation: NSLayoutConstraint.Relation = .equal,
to anchor: NSLayoutDimension,
multiplier: CGFloat = 1,
constant: CGFloat = 0
) -> NSLayoutConstraint {
switch relation {
case .equal:
return constraint(equalTo: anchor, constant: constant)
return constraint(equalTo: anchor, multiplier: multiplier, constant: constant)
case .greaterThanOrEqual:
return constraint(greaterThanOrEqualTo: anchor, constant: constant)
return constraint(greaterThanOrEqualTo: anchor, multiplier: multiplier, constant: constant)
case .lessThanOrEqual:
return constraint(lessThanOrEqualTo: anchor, constant: constant)
return constraint(lessThanOrEqualTo: anchor, multiplier: multiplier, constant: constant)
@unknown default:
return constraint(equalTo: anchor, constant: constant)
return constraint(equalTo: anchor, multiplier: multiplier, constant: constant)
}
}

Expand Down
29 changes: 25 additions & 4 deletions Tests/LayoutTests/UIKit/NSLayoutDimensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
@MainActor
final class NSLayoutDimensionTests: XCTestCase {

func testConstraintIsRelationToAnchorConstant_givenDefaults() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenDefaults() {

// GIVEN

Expand All @@ -35,7 +35,28 @@ final class NSLayoutDimensionTests: XCTestCase {
expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorConstant_givenConstant() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenMultiplier() {

// GIVEN

let viewA: UIView = .init()
let viewB: UIView = .init()
let expected: NSLayoutConstraint = viewA
.width
.constraint(equalTo: viewB.width, multiplier: 23)

// WHEN

let constraint: NSLayoutConstraint = viewA
.width
.constraint(to: viewB.width, multiplier: 23)

// THEN

expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorMultiplierConstant_givenConstant() {

// GIVEN

Expand All @@ -56,7 +77,7 @@ final class NSLayoutDimensionTests: XCTestCase {
expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorConstant_givenGreaterThanOrEqualRelation() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenGreaterThanOrEqualRelation() {

// GIVEN

Expand All @@ -77,7 +98,7 @@ final class NSLayoutDimensionTests: XCTestCase {
expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorConstant_givenLessThanOrEqualRelation() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenLessThanOrEqualRelation() {

// GIVEN

Expand Down

0 comments on commit fb8fb79

Please sign in to comment.