From e46f329310b5370a9681df35380d5521d8c26b88 Mon Sep 17 00:00:00 2001 From: Eman Haroutunian <104394983+tinder-emanharoutunian@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:44:46 -0800 Subject: [PATCH] Add tests for default parameters (#240) * Update Layout snapshot * Update LayoutItem snapshots * Update Tests/LayoutTests/LayoutItemTests.swift Co-authored-by: Garric Nahapetian <31713341+tinder-garricnahapetian@users.noreply.github.com> * Test defaults in LayoutItemTests.swift * Test defaults in LayoutTests.swift * Add tests for UIView+AutoLayout * Update Tests/LayoutTests/LayoutItemTests.swift Co-authored-by: Garric Nahapetian <31713341+tinder-garricnahapetian@users.noreply.github.com> * Implement review feedback --------- Co-authored-by: Garric Nahapetian <31713341+tinder-garricnahapetian@users.noreply.github.com> --- Tests/LayoutTests/LayoutItemTests.swift | 24 +++ Tests/LayoutTests/LayoutTests.swift | 31 +++- .../LayoutTests/UIView+AutoLayoutTests.swift | 148 +++++++++++++++--- 3 files changed, 181 insertions(+), 22 deletions(-) diff --git a/Tests/LayoutTests/LayoutItemTests.swift b/Tests/LayoutTests/LayoutItemTests.swift index 2c9826b7..6ec0995e 100644 --- a/Tests/LayoutTests/LayoutItemTests.swift +++ b/Tests/LayoutTests/LayoutItemTests.swift @@ -46,6 +46,7 @@ final class LayoutItemTests: XCTestCase { view.layout { pinkView .to([.top, .leading]) + .size(width: 150, height: 250, priority: .high) .size(width: 100, height: 200) yellowView .to([.top, .trailing]) @@ -60,6 +61,7 @@ final class LayoutItemTests: XCTestCase { view.layout { pinkView .to([.top, .leading]) + .size(CGSize(width: 150, height: 250), priority: .high) .size(CGSize(width: 100, height: 200)) yellowView .to([.top, .trailing]) @@ -82,6 +84,7 @@ final class LayoutItemTests: XCTestCase { pinkView .to([.top, .leading]) .size(height: 100) + .size(is: .greaterThanOrEqual, width: 100, priority: .high) .size(width: 50) // With High Priority @@ -142,6 +145,7 @@ final class LayoutItemTests: XCTestCase { pinkView .to([.top, .leading]) .size(width: 50) + .size(is: .greaterThanOrEqual, height: 150, priority: .high) .size(height: 100) // With Priority @@ -209,6 +213,7 @@ final class LayoutItemTests: XCTestCase { view.layout { pinkView .to([.top, .leading]) + .square(200, priority: .high) .square(100) yellowView .to([.top, .trailing]) @@ -224,6 +229,7 @@ final class LayoutItemTests: XCTestCase { pinkView .to([.top, .leading]) .size(height: 100) + .aspectRatio(0.5, priority: .high) .aspectRatio(0.75) yellowView .to([.top, .trailing]) @@ -242,6 +248,7 @@ final class LayoutItemTests: XCTestCase { pinkView .size(width: 50, height: 50) + .center(offset: UIOffset(horizontal: 50, vertical: 50), priority: .high) .center() // With Custom Offset @@ -269,6 +276,7 @@ final class LayoutItemTests: XCTestCase { pinkView .size(width: 100, height: 100) .to(.leading) + .center(.vertical, offset: 50, multiplier: 0.5, priority: .high) .center(.vertical) // Horizontal Axis @@ -276,6 +284,7 @@ final class LayoutItemTests: XCTestCase { yellowView .size(width: 100, height: 100) .to(.top) + .center(.horizontal, offset: 50, multiplier: 0.5, priority: .high) .center(.horizontal) // With Offset @@ -333,6 +342,8 @@ final class LayoutItemTests: XCTestCase { pinkView .size(width: 100, height: 100) + .to(.top, is: .greaterThanOrEqual, multiplier: 0.5, constant: 50, priority: .high) + .to(.leading, is: .greaterThanOrEqual, multiplier: 0.5, constant: 50, priority: .high) .to(.top) .to(.leading) @@ -416,6 +427,7 @@ final class LayoutItemTests: XCTestCase { pinkView .size(width: 100, height: 100) + .to([.bottom, .leading], constant: 50, priority: .high) .to([.bottom, .leading]) // To Bottom Trailing with Constant @@ -440,6 +452,8 @@ final class LayoutItemTests: XCTestCase { // Insets pinkView + .toEdges(insets: NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0), + priority: .high) .toEdges(insets: NSDirectionalEdgeInsets(top: 0, leading: 5, bottom: 10, trailing: 15)) // Insets with Priority @@ -463,6 +477,7 @@ final class LayoutItemTests: XCTestCase { // To Edges with Insets pinkView + .toEdges(insets: UIEdgeInsets(top: 0, left: 5, bottom: 10, right: 15), priority: .high) .toEdges(insets: UIEdgeInsets(top: 0, left: 5, bottom: 10, right: 15)) // To Edges with Insets and Priority @@ -482,6 +497,7 @@ final class LayoutItemTests: XCTestCase { pinkView .size(height: 100) + .toEdges([.top, .leading, .trailing], inset: 50, priority: .high) .toEdges([.top, .leading, .trailing]) // To Bottom, Leading and Trailing with Inset @@ -508,6 +524,7 @@ final class LayoutItemTests: XCTestCase { // Defaults pinkView + .toEdges([.top, .leading], inset: 50, priority: .high) .toEdges() // To Top, Left and Right @@ -542,6 +559,7 @@ final class LayoutItemTests: XCTestCase { pinkView .size(height: 100) .to(.top) + .toSideEdges(inset: 50, priority: .high) .toSideEdges() // To Side Edges with Inset @@ -594,6 +612,7 @@ final class LayoutItemTests: XCTestCase { assertLayout { view in view.layout { pinkView + .toMargins([.top, .leading, .trailing, .bottom], inset: 50, priority: .high) .toMargins([.top, .leading, .trailing, .bottom]) blueView .toMargins([.top, .leading, .trailing, .bottom], inset: 25) @@ -652,6 +671,7 @@ final class LayoutItemTests: XCTestCase { pinkView .size(width: 100, height: 100) .to(.leading) + .toBottomMargin(minInset: 200, priority: .high) .toBottomMargin(minInset: 100) // To Bottom Trailing with Priority @@ -672,6 +692,8 @@ final class LayoutItemTests: XCTestCase { // To Insets pinkView + .toSafeArea(insets: NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5), + priority: .high) .toSafeArea(insets: NSDirectionalEdgeInsets(top: 0, leading: 5, bottom: 10, trailing: 15)) // To Insets with Priority @@ -696,6 +718,7 @@ final class LayoutItemTests: XCTestCase { // To Insets pinkView + .toSafeArea(insets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5), priority: .high) .toSafeArea(insets: UIEdgeInsets(top: 0, left: 5, bottom: 10, right: 15)) // To Insets with Priority @@ -724,6 +747,7 @@ final class LayoutItemTests: XCTestCase { assertLayout { view in view.layout { pinkView + .toSafeArea(canonical: [.top, .left], inset: 50, priority: .high) .toSafeArea() blueView .toSafeArea(inset: 0, priority: .low) diff --git a/Tests/LayoutTests/LayoutTests.swift b/Tests/LayoutTests/LayoutTests.swift index b430a510..00771600 100644 --- a/Tests/LayoutTests/LayoutTests.swift +++ b/Tests/LayoutTests/LayoutTests.swift @@ -404,6 +404,11 @@ final class LayoutTests: XCTestCase { // To Anchor + layout.constrain(pinkView.leading, + is: .greaterThanOrEqual, + to: view.leading, + constant: 50, + priority: .high) layout.constrain(pinkView.leading, to: view.leading) // To Anchor with Constant @@ -491,6 +496,7 @@ final class LayoutTests: XCTestCase { // THEN + // swiftlint:disable:next closure_body_length assertLayout { view in let layout: Layout = view.layout { @@ -509,6 +515,12 @@ final class LayoutTests: XCTestCase { // To Anchor + layout.constrain(pinkView.height, + is: .greaterThanOrEqual, + to: yellowView.height, + multiplier: 0.5, + constant: 50, + priority: .high) layout.constrain(pinkView.height, to: yellowView.height) // To Anchor with Constant @@ -719,6 +731,7 @@ final class LayoutTests: XCTestCase { // THEN + // swiftlint:disable:next closure_body_length assertLayout { view in let layout: Layout = view.layout { @@ -736,6 +749,11 @@ final class LayoutTests: XCTestCase { // Horizontal With Views and Alignment + layout.horizontal([pinkView, yellowView], + spacing: 20, + direction: .leftToRight, + priority: .high, + alignment: .centerY) layout.horizontal([pinkView, yellowView], alignment: .centerY) // Spacing, Direction and Priority @@ -781,6 +799,12 @@ final class LayoutTests: XCTestCase { // Horizontal With Views and Alignment + layout.horizontal([pinkView, yellowView], + spacing: 20, + direction: .leftToRight, + priority: .high, + alignment: .top, + .bottom) layout.horizontal([pinkView, yellowView], alignment: .top, .bottom) return layout @@ -815,11 +839,13 @@ final class LayoutTests: XCTestCase { // Vertical With Views and Alignment + layout.vertical([pinkView, yellowView], spacing: 20, priority: .high, alignment: .centerX) layout.vertical([pinkView, yellowView], alignment: .centerX) // Spacing - layout.vertical([blueView, greenView], spacing: 12, alignment: .centerX) + layout.vertical([blueView, greenView], spacing: 20, priority: .low, alignment: .centerX) + layout.vertical([blueView, greenView], spacing: 12, priority: .high, alignment: .centerX) return layout } @@ -846,6 +872,7 @@ final class LayoutTests: XCTestCase { // Vertical With Views and Alignment + layout.vertical([pinkView, yellowView], spacing: 20, priority: .high, alignment: .leading, .trailing) layout.vertical([pinkView, yellowView], alignment: .leading, .trailing) return layout @@ -866,6 +893,7 @@ final class LayoutTests: XCTestCase { .size(width: 100, height: 100) .to(.top) ) + .center(pinkView, between: view.leading, and: view.leading, priority: .high) .center(pinkView, between: view.leading, and: view.trailing) } } @@ -892,6 +920,7 @@ final class LayoutTests: XCTestCase { // Center Between + layout.center(yellowView, between: view.top, and: view.centerY, priority: .high) layout.center(yellowView, between: view.top, and: view.bottom) // With Priority diff --git a/Tests/LayoutTests/UIView+AutoLayoutTests.swift b/Tests/LayoutTests/UIView+AutoLayoutTests.swift index 893eb9ec..245f3137 100644 --- a/Tests/LayoutTests/UIView+AutoLayoutTests.swift +++ b/Tests/LayoutTests/UIView+AutoLayoutTests.swift @@ -28,6 +28,28 @@ final class UIViewAutoLayoutTests: XCTestCase { expect(view.translatesAutoresizingMaskIntoConstraints) == false } + func testConstrainingSize_withDefaults() { + + // GIVEN + + let frame: CGRect = .init(x: 0, y: 0, width: 10, height: 20) + let view: UIView = .init(frame: frame) + + // THEN + + expect(view.translatesAutoresizingMaskIntoConstraints) == true + + // WHEN + + view.constrainingSize() + + // THEN + + expect(view.translatesAutoresizingMaskIntoConstraints) == false + expect(view.constraints.first { $0.firstAttribute == .width }?.constant) == frame.width + expect(view.constraints.first { $0.firstAttribute == .height }?.constant) == frame.height + } + func testConstrainingSize() { // GIVEN @@ -42,6 +64,27 @@ final class UIViewAutoLayoutTests: XCTestCase { expect(view.constraints.first { $0.firstAttribute == .height }?.constant) == size.height } + func testConstrainingWidth_withDefaults() { + + // GIVEN + + let width: CGFloat = 10 + let view: UIView = .init(frame: CGRect(x: 0, y: 0, width: width, height: 0)) + + // THEN + + expect(view.translatesAutoresizingMaskIntoConstraints) == true + + // WHEN + + view.constrainingWidth() + + // THEN + + expect(view.translatesAutoresizingMaskIntoConstraints) == false + expect(view.constraints.first { $0.firstAttribute == .width }?.constant) == width + } + func testConstrainingWidth() { // GIVEN @@ -55,6 +98,27 @@ final class UIViewAutoLayoutTests: XCTestCase { expect(view.constraints.first { $0.firstAttribute == .width }?.constant) == width } + func testConstrainingHeight_withDefaults() { + + // GIVEN + + let height: CGFloat = 20 + let view: UIView = .init(frame: CGRect(x: 0, y: 0, width: 0, height: height)) + + // THEN + + expect(view.translatesAutoresizingMaskIntoConstraints) == true + + // WHEN + + view.constrainingHeight() + + // THEN + + expect(view.translatesAutoresizingMaskIntoConstraints) == false + expect(view.constraints.first { $0.firstAttribute == .height }?.constant) == height + } + func testConstrainingHeight() { // GIVEN @@ -68,6 +132,37 @@ final class UIViewAutoLayoutTests: XCTestCase { expect(view.constraints.first { $0.firstAttribute == .height }?.constant) == height } + func testConstrainToSuperviewWithDirectionalEdgeInsets_withDefaults() { + + // GIVEN + + let superview: UIView = .init() + let view: UIView = .init() + superview.addSubview(view) + + // THEN + + expect(superview.constraints).to(beEmpty()) + + // WHEN + + view.constrain(to: superview) + + // THEN + + let constraints: [NSLayoutConstraint] = superview.constraints + + expect(superview.constraints.count) == 4 + expect(constraints[0].firstAttribute) == .leading + expect(constraints[0].constant) == 0 + expect(constraints[1].firstAttribute) == .trailing + expect(constraints[1].constant) == 0 + expect(constraints[2].firstAttribute) == .top + expect(constraints[2].constant) == 0 + expect(constraints[3].firstAttribute) == .bottom + expect(constraints[3].constant) == 0 + } + func testConstrainToSuperviewWithDirectionalEdgeInsets() { // GIVEN @@ -273,6 +368,9 @@ final class UIViewAutoLayoutTests: XCTestCase { let ratio: CGFloat = 0.5 let view: UIView = .init() + + // WHEN + let constraint: NSLayoutConstraint = view.aspectConstraint(ratio) // THEN @@ -281,6 +379,30 @@ final class UIViewAutoLayoutTests: XCTestCase { expect(constraint.secondAttribute) == .height expect(constraint.relation) == .equal expect(constraint.multiplier) == ratio + + // GIVEN + + let superview2: UIView = .init() + let view2: UIView = .init() + superview2.addSubview(view2) + + // WHEN + + // Edge Constraints With Insets + let insets: DirectionalInsets = .init(top: 0, leading: 5, bottom: 10, trailing: 15) + let constraints2: [NSLayoutConstraint] = view2.edgeConstraints(insetBy: insets) + + // THEN + + expect(constraints2.count) == 4 + expect(constraints2[0].firstAttribute) == .top + expect(constraints2[0].constant) == 0 + expect(constraints2[1].firstAttribute) == .bottom + expect(constraints2[1].constant) == -10 + expect(constraints2[2].firstAttribute) == .leading + expect(constraints2[2].constant) == 5 + expect(constraints2[3].firstAttribute) == .trailing + expect(constraints2[3].constant) == -15 } func testEdgeConstraints() { @@ -348,35 +470,16 @@ final class UIViewAutoLayoutTests: XCTestCase { expect(constraints[3].constant) == -insets.right } - func testEdgeConstraintsInsetByDirectionalInsets() { + func testCenterConstraints() { // GIVEN let superview: UIView = .init() let view: UIView = .init() superview.addSubview(view) - let insets: NSDirectionalEdgeInsets = .init(top: 1, leading: 2, bottom: 3, trailing: 4) - let constraints: [NSLayoutConstraint] = view.edgeConstraints(insetBy: insets) - // THEN + // WHEN - expect(constraints[0].firstAttribute) == .top - expect(constraints[0].constant) == insets.top - expect(constraints[1].firstAttribute) == .bottom - expect(constraints[1].constant) == -insets.bottom - expect(constraints[2].firstAttribute) == .leading - expect(constraints[2].constant) == insets.leading - expect(constraints[3].firstAttribute) == .trailing - expect(constraints[3].constant) == -insets.trailing - } - - func testCenterConstraints() { - - // GIVEN - - let superview: UIView = .init() - let view: UIView = .init() - superview.addSubview(view) let constraints1: [NSLayoutConstraint] = view.centerConstraints() // THEN @@ -389,6 +492,9 @@ final class UIViewAutoLayoutTests: XCTestCase { // GIVEN let offset: UIOffset = .init(horizontal: 5, vertical: 10) + + // WHEN + let constraints2: [NSLayoutConstraint] = view.centerConstraints(offsetBy: offset) // THEN