From b9326373a25caca04c6dad1797cf4f350d974069 Mon Sep 17 00:00:00 2001 From: Christopher Fuller Date: Tue, 12 Dec 2023 16:37:26 -0800 Subject: [PATCH] Improve tests (#256) * Improve tests * Improve tests --- Tests/LayoutTests/LayoutItemTests.swift | 3 +- Tests/LayoutTests/LayoutTests.swift | 143 +++++++++++++++--- .../UIKit/NSLayoutConstraintTests.swift | 10 +- .../UIKit/UIView+FramesTests.swift | 6 +- 4 files changed, 129 insertions(+), 33 deletions(-) diff --git a/Tests/LayoutTests/LayoutItemTests.swift b/Tests/LayoutTests/LayoutItemTests.swift index f3de7b36..6f163c1e 100644 --- a/Tests/LayoutTests/LayoutItemTests.swift +++ b/Tests/LayoutTests/LayoutItemTests.swift @@ -34,11 +34,12 @@ final class LayoutItemTests: XCTestCase { // WHEN - _ = view.id("identifier2") + let identifiedView: UIView = view.id("identifier2") // THEN expect(view.identifier) == "identifier2" + expect(identifiedView) === view } func testSizeWidthAndHeight_andWithPriority() { diff --git a/Tests/LayoutTests/LayoutTests.swift b/Tests/LayoutTests/LayoutTests.swift index a6bc81f4..23646014 100644 --- a/Tests/LayoutTests/LayoutTests.swift +++ b/Tests/LayoutTests/LayoutTests.swift @@ -947,14 +947,14 @@ final class LayoutTests: XCTestCase { } } - func testAddItemsVariadically() { + func testAddItemsWithVariadic() { // GIVEN let view: UIView = .init() let layout: Layout = .init(view) - let pinkView: UIView = pinkView - let yellowView: UIView = yellowView + let view1: UIView = .init() + let view2: UIView = .init() // THEN @@ -962,13 +962,13 @@ final class LayoutTests: XCTestCase { // WHEN - layout.addItems(pinkView.id("pinkView"), yellowView.id("yellowView")) + layout.addItems(view1.id("view1"), view2.id("view2")) // THEN expect(layout.items.count) == 2 - expect(layout.items["pinkView"]) === pinkView - expect(layout.items["yellowView"]) === yellowView + expect(layout.items["view1"]) === view1 + expect(layout.items["view2"]) === view2 } func testAddItemsWithArray() { @@ -977,8 +977,8 @@ final class LayoutTests: XCTestCase { let view: UIView = .init() let layout: Layout = .init(view) - let pinkView: UIView = pinkView - let yellowView: UIView = yellowView + let view1: UIView = .init() + let view2: UIView = .init() // THEN @@ -986,16 +986,16 @@ final class LayoutTests: XCTestCase { // WHEN - layout.addItems([pinkView.id("pinkView"), yellowView.id("yellowView")]) + layout.addItems([view1.id("view1"), view2.id("view2")]) // THEN expect(layout.items.count) == 2 - expect(layout.items["pinkView"]) === pinkView - expect(layout.items["yellowView"]) === yellowView + expect(layout.items["view1"]) === view1 + expect(layout.items["view2"]) === view2 } - func testWithPriority() { + func testActivate_andDeactivate() { // GIVEN @@ -1027,20 +1027,78 @@ final class LayoutTests: XCTestCase { // THEN - expect(layout.constraints[0].priority) == UILayoutPriority.required - expect(layout.constraints[1].priority) == UILayoutPriority.required + expect(layout.constraints[0].isActive) == false + expect(layout.constraints[1].isActive) == false + + // WHEN + + layout.activate() + + // THEN + + expect(layout.constraints[0].isActive) == true + expect(layout.constraints[1].isActive) == true + + // WHEN + + layout.deactivate() + + // THEN + + expect(layout.constraints[0].isActive) == false + expect(layout.constraints[1].isActive) == false + } + + func testRequire() { + + // GIVEN + + let view: UIView = .init() + let subview: UIView = .init() + let heightConstraint: NSLayoutConstraint = .init( + item: subview, + attribute: .height, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 100 + ) + let widthConstraint: NSLayoutConstraint = .init( + item: subview, + attribute: .width, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 100 + ) + let layout: Layout = .init(view, subview) // WHEN - layout.withPriority(.high) + heightConstraint.priority = .high + widthConstraint.priority = .high + + layout.adding(heightConstraint, widthConstraint) // THEN expect(layout.constraints[0].priority) == UILayoutPriority.high expect(layout.constraints[1].priority) == UILayoutPriority.high + + // WHEN + + let requiredLayout: Layout = layout.require() + + // THEN + + expect(layout.constraints[0].priority) == UILayoutPriority.required + expect(layout.constraints[1].priority) == UILayoutPriority.required + expect(requiredLayout) === layout } - func testActivate_andDeactivate() { + func testWithPriority() { // GIVEN @@ -1072,26 +1130,63 @@ final class LayoutTests: XCTestCase { // THEN - expect(layout.constraints[0].isActive) == false - expect(layout.constraints[1].isActive) == false + expect(layout.constraints[0].priority) == UILayoutPriority.required + expect(layout.constraints[1].priority) == UILayoutPriority.required // WHEN - layout.activate() + let highPriorityLayout: Layout = layout.withPriority(.high) // THEN - expect(layout.constraints[0].isActive) == true - expect(layout.constraints[1].isActive) == true + expect(layout.constraints[0].priority) == UILayoutPriority.high + expect(layout.constraints[1].priority) == UILayoutPriority.high + expect(highPriorityLayout) === layout + } + + func testPrioritize() { + + // GIVEN + + let view: UIView = .init() + let subview: UIView = .init() + let heightConstraint: NSLayoutConstraint = .init( + item: subview, + attribute: .height, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 100 + ) + let widthConstraint: NSLayoutConstraint = .init( + item: subview, + attribute: .width, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 100 + ) + let layout: Layout = .init(view, subview) // WHEN - layout.deactivate() + layout.adding(heightConstraint, widthConstraint) // THEN - expect(layout.constraints[0].isActive) == false - expect(layout.constraints[1].isActive) == false + expect(layout.constraints[0].priority) == UILayoutPriority.required + expect(layout.constraints[1].priority) == UILayoutPriority.required + + // WHEN + + layout.prioritize(.high) + + // THEN + + expect(layout.constraints[0].priority) == UILayoutPriority.high + expect(layout.constraints[1].priority) == UILayoutPriority.high } func testUpdate() { diff --git a/Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift b/Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift index 3ff1439d..4ceb2305 100644 --- a/Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift +++ b/Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift @@ -115,17 +115,14 @@ final class NSLayoutConstraintTests: XCTestCase { let constraint: NSLayoutConstraint = .init() constraint.priority = .high - // THEN - - expect(constraint.priority) == .high - // WHEN - _ = constraint.require() + let requiredConstraint: NSLayoutConstraint = constraint.require() // THEN expect(constraint.priority) == .required + expect(requiredConstraint) === constraint } func testWithPriority() { @@ -140,11 +137,12 @@ final class NSLayoutConstraintTests: XCTestCase { // WHEN - _ = constraint.withPriority(.high) + let highPriorityConstraint: NSLayoutConstraint = constraint.withPriority(.high) // THEN expect(constraint.priority) == .high + expect(highPriorityConstraint) === constraint } func testPrioritize() { diff --git a/Tests/LayoutTests/UIKit/UIView+FramesTests.swift b/Tests/LayoutTests/UIKit/UIView+FramesTests.swift index 4f162686..fc1d3b63 100644 --- a/Tests/LayoutTests/UIKit/UIView+FramesTests.swift +++ b/Tests/LayoutTests/UIKit/UIView+FramesTests.swift @@ -23,11 +23,12 @@ final class UIViewFramesTests: XCTestCase { // WHEN - _ = view.usingFrames() + let framedView: UIView = view.usingFrames() // THEN expect(view.translatesAutoresizingMaskIntoConstraints) == true + expect(framedView) === view } func testDisablingIntrinsicSize() { @@ -45,7 +46,7 @@ final class UIViewFramesTests: XCTestCase { // WHEN - _ = view.disablingIntrinsicSize() + let viewWithoutIntrinsicSize: UIView = view.disablingIntrinsicSize() // THEN @@ -53,5 +54,6 @@ final class UIViewFramesTests: XCTestCase { expect(view.contentHuggingPriority(for: .vertical)) == .disabled expect(view.contentCompressionResistancePriority(for: .horizontal)) == .disabled expect(view.contentCompressionResistancePriority(for: .vertical)) == .disabled + expect(viewWithoutIntrinsicSize) === view } }