From 9b4e393397e875f4887c84f79aee4fbe2e55b31c Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Tue, 17 Oct 2023 15:34:17 -0700 Subject: [PATCH 1/2] Move extension to NSLayoutYAxisAnchor.swift --- Sources/Layout/LayoutAnchoring.swift | 20 -------------- Sources/Layout/NSLayoutYAxisAnchor.swift | 34 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 Sources/Layout/NSLayoutYAxisAnchor.swift diff --git a/Sources/Layout/LayoutAnchoring.swift b/Sources/Layout/LayoutAnchoring.swift index 979cc954..87a160ab 100644 --- a/Sources/Layout/LayoutAnchoring.swift +++ b/Sources/Layout/LayoutAnchoring.swift @@ -94,26 +94,6 @@ extension NSLayoutXAxisAnchor { } } -extension NSLayoutYAxisAnchor { - - public func constraint( - is relation: NSLayoutConstraint.Relation = .equal, - to anchor: NSLayoutYAxisAnchor, - constant: CGFloat = 0 - ) -> NSLayoutConstraint { - switch relation { - case .equal: - return constraint(equalTo: anchor, constant: constant) - case .greaterThanOrEqual: - return constraint(greaterThanOrEqualTo: anchor, constant: constant) - case .lessThanOrEqual: - return constraint(lessThanOrEqualTo: anchor, constant: constant) - @unknown default: - return constraint(equalTo: anchor, constant: constant) - } - } -} - extension NSLayoutDimension { public func constraint( diff --git a/Sources/Layout/NSLayoutYAxisAnchor.swift b/Sources/Layout/NSLayoutYAxisAnchor.swift new file mode 100644 index 00000000..437eb732 --- /dev/null +++ b/Sources/Layout/NSLayoutYAxisAnchor.swift @@ -0,0 +1,34 @@ +// +// All Contributions by Match Group +// +// Copyright © 2023 Tinder (Match Group, LLC) +// +// Licensed under the Match Group Modified 3-Clause BSD License. +// See https://github.com/Tinder/Layout/blob/main/LICENSE for license information. +// + +import UIKit + +extension NSLayoutYAxisAnchor { + + // swiftlint:disable function_default_parameter_at_end + + public func constraint( + is relation: NSLayoutConstraint.Relation = .equal, + to anchor: NSLayoutYAxisAnchor, + constant: CGFloat = 0 + ) -> NSLayoutConstraint { + switch relation { + case .equal: + return constraint(equalTo: anchor, constant: constant) + case .greaterThanOrEqual: + return constraint(greaterThanOrEqualTo: anchor, constant: constant) + case .lessThanOrEqual: + return constraint(lessThanOrEqualTo: anchor, constant: constant) + @unknown default: + return constraint(equalTo: anchor, constant: constant) + } + } + + // swiftlint:enable function_default_parameter_at_end +} From 1d6d939fca9d41da1566d42849ccefcd01ea62f6 Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Mon, 30 Oct 2023 15:55:12 -0700 Subject: [PATCH 2/2] Move tests --- Tests/LayoutTests/LayoutAnchoringTests.swift | 84 ----------------- .../NSLayoutYAxisAnchorTests.swift | 92 ++++++++++++++++++- 2 files changed, 91 insertions(+), 85 deletions(-) diff --git a/Tests/LayoutTests/LayoutAnchoringTests.swift b/Tests/LayoutTests/LayoutAnchoringTests.swift index 540b3719..4ee654ff 100644 --- a/Tests/LayoutTests/LayoutAnchoringTests.swift +++ b/Tests/LayoutTests/LayoutAnchoringTests.swift @@ -142,90 +142,6 @@ final class LayoutAnchoringTests: XCTestCase { expect(layoutSupport.height) == view.height } - func testConstraintToYAnchor_withDefaults() { - - // GIVEN - - let viewA: UIView = .init() - let viewB: UIView = .init() - let expected: NSLayoutConstraint = viewA - .centerY - .constraint(equalTo: viewB.centerY) - - // WHEN - - let constraint: NSLayoutConstraint = viewA - .centerY - .constraint(to: viewB.centerY) - - // THEN - - expect(constraint).to(match(expected)) - } - - func testConstraintToYAnchor_withConstant() { - - // GIVEN - - let viewA: UIView = .init() - let viewB: UIView = .init() - let expected: NSLayoutConstraint = viewA - .centerY - .constraint(equalTo: viewB.centerY, constant: 50) - - // WHEN - - let constraint: NSLayoutConstraint = viewA - .centerY - .constraint(is: .equal, to: viewB.centerY, constant: 50) - - // THEN - - expect(constraint).to(match(expected)) - } - - func testConstraintToYAnchor_withGreaterThanOrEqualRelation() { - - // GIVEN - - let viewA: UIView = .init() - let viewB: UIView = .init() - let expected: NSLayoutConstraint = viewA - .centerY - .constraint(greaterThanOrEqualTo: viewB.centerY) - - // WHEN - - let constraint: NSLayoutConstraint = viewA - .centerY - .constraint(is: .greaterThanOrEqual, to: viewB.centerY) - - // THEN - - expect(constraint).to(match(expected)) - } - - func testConstraintToYAnchor_withLessThanOrEqualRelation() { - - // GIVEN - - let viewA: UIView = .init() - let viewB: UIView = .init() - let expected: NSLayoutConstraint = viewA - .centerY - .constraint(lessThanOrEqualTo: viewB.centerY) - - // WHEN - - let constraint: NSLayoutConstraint = viewA - .centerY - .constraint(is: .lessThanOrEqual, to: viewB.centerY) - - // THEN - - expect(constraint).to(match(expected)) - } - func testConstraintToLayoutDimension_withDefaults() { // GIVEN diff --git a/Tests/LayoutTests/NSLayoutYAxisAnchorTests.swift b/Tests/LayoutTests/NSLayoutYAxisAnchorTests.swift index b294ac4f..ab0d94e1 100644 --- a/Tests/LayoutTests/NSLayoutYAxisAnchorTests.swift +++ b/Tests/LayoutTests/NSLayoutYAxisAnchorTests.swift @@ -7,4 +7,94 @@ // See https://github.com/Tinder/Layout/blob/main/LICENSE for license information. // -import Foundation +@testable import Layout +import Nimble +import XCTest + +@MainActor +final class NSLayoutYAxisAnchorTests: XCTestCase { + + func testConstraintToYAnchor_withDefaults() { + + // GIVEN + + let viewA: UIView = .init() + let viewB: UIView = .init() + let expected: NSLayoutConstraint = viewA + .centerY + .constraint(equalTo: viewB.centerY) + + // WHEN + + let constraint: NSLayoutConstraint = viewA + .centerY + .constraint(to: viewB.centerY) + + // THEN + + expect(constraint).to(match(expected)) + } + + func testConstraintToYAnchor_withConstant() { + + // GIVEN + + let viewA: UIView = .init() + let viewB: UIView = .init() + let expected: NSLayoutConstraint = viewA + .centerY + .constraint(equalTo: viewB.centerY, constant: 50) + + // WHEN + + let constraint: NSLayoutConstraint = viewA + .centerY + .constraint(is: .equal, to: viewB.centerY, constant: 50) + + // THEN + + expect(constraint).to(match(expected)) + } + + func testConstraintToYAnchor_withGreaterThanOrEqualRelation() { + + // GIVEN + + let viewA: UIView = .init() + let viewB: UIView = .init() + let expected: NSLayoutConstraint = viewA + .centerY + .constraint(greaterThanOrEqualTo: viewB.centerY) + + // WHEN + + let constraint: NSLayoutConstraint = viewA + .centerY + .constraint(is: .greaterThanOrEqual, to: viewB.centerY) + + // THEN + + expect(constraint).to(match(expected)) + } + + func testConstraintToYAnchor_withLessThanOrEqualRelation() { + + // GIVEN + + let viewA: UIView = .init() + let viewB: UIView = .init() + let expected: NSLayoutConstraint = viewA + .centerY + .constraint(lessThanOrEqualTo: viewB.centerY) + + // WHEN + + let constraint: NSLayoutConstraint = viewA + .centerY + .constraint(is: .lessThanOrEqual, to: viewB.centerY) + + // THEN + + expect(constraint).to(match(expected)) + } +}