Skip to content

Commit

Permalink
Improve tests (#256)
Browse files Browse the repository at this point in the history
* Improve tests

* Improve tests
  • Loading branch information
tinder-cfuller authored Dec 13, 2023
1 parent b35c649 commit b932637
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Tests/LayoutTests/LayoutItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
143 changes: 119 additions & 24 deletions Tests/LayoutTests/LayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -947,28 +947,28 @@ 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

expect(layout.items.isEmpty) == true

// 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() {
Expand All @@ -977,25 +977,25 @@ 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

expect(layout.items.isEmpty) == true

// 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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 4 additions & 6 deletions Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions Tests/LayoutTests/UIKit/UIView+FramesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -45,13 +46,14 @@ final class UIViewFramesTests: XCTestCase {

// WHEN

_ = view.disablingIntrinsicSize()
let viewWithoutIntrinsicSize: UIView = view.disablingIntrinsicSize()

// THEN

expect(view.contentHuggingPriority(for: .horizontal)) == .disabled
expect(view.contentHuggingPriority(for: .vertical)) == .disabled
expect(view.contentCompressionResistancePriority(for: .horizontal)) == .disabled
expect(view.contentCompressionResistancePriority(for: .vertical)) == .disabled
expect(viewWithoutIntrinsicSize) === view
}
}

0 comments on commit b932637

Please sign in to comment.