Skip to content

Commit 6026ac4

Browse files
Add missing coverage (#303)
* Add missing coverage * Improve tests Co-authored-by: Christopher Fuller <[email protected]> * Fix typo * Fix typo --------- Co-authored-by: Christopher Fuller <[email protected]>
1 parent ebe20b2 commit 6026ac4

5 files changed

+224
-0
lines changed

Tests/LayoutTests/LayoutItemTests.swift

+42
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,27 @@ final class LayoutItemTests: XCTestCase {
714714

715715
// MARK: - Margins
716716

717+
func testToMargins_throwsAssertion() {
718+
719+
// GIVEN
720+
721+
let superview: UIView = .init()
722+
let view: UIView = .init()
723+
let item: LayoutItem = view.toMargins()
724+
725+
// THEN
726+
727+
expect(item.superviewConstraints(view)).to(throwAssertion())
728+
729+
// WHEN
730+
731+
superview.addSubview(view)
732+
733+
// THEN
734+
735+
expect(item.superviewConstraints(view).count) == 4
736+
}
737+
717738
func testToMarginsInsetsPriorityDirectional() {
718739
assertLayout(devices: Device.portraitTestDevices + Device.modernLandscapeTestDevices) { view in
719740
view.layout {
@@ -845,6 +866,27 @@ final class LayoutItemTests: XCTestCase {
845866

846867
// MARK: - Safe Area
847868

869+
func testToSafeArea_throwsAssertion() {
870+
871+
// GIVEN
872+
873+
let superview: UIView = .init()
874+
let view: UIView = .init()
875+
let item: LayoutItem = view.toSafeArea()
876+
877+
// THEN
878+
879+
expect(item.superviewConstraints(view)).to(throwAssertion())
880+
881+
// WHEN
882+
883+
superview.addSubview(view)
884+
885+
// THEN
886+
887+
expect(item.superviewConstraints(view).count) == 4
888+
}
889+
848890
func testToSafeAreaInsetsPriorityDirectional() {
849891
assertLayout(devices: Device.allTestDevices) { view in
850892
view.layout {

Tests/LayoutTests/LayoutTests.swift

+154
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,40 @@ final class LayoutTests: XCTestCase {
663663
}
664664
}
665665

666+
func testEqualAttributesWithViews_givenEmptyArray() {
667+
668+
// GIVEN
669+
670+
let superview: UIView = .init()
671+
let view1: UIView = .init()
672+
let view2: UIView = .init()
673+
let layout: Layout = superview.layout().addItems(view1, view2)
674+
675+
// WHEN
676+
677+
layout.equal(.top, [])
678+
679+
// THEN
680+
681+
expect(layout.constraints).to(beEmpty())
682+
683+
// WHEN
684+
685+
layout.equal(.top, [view1])
686+
687+
// THEN
688+
689+
expect(layout.constraints).to(beEmpty())
690+
691+
// WHEN
692+
693+
layout.equal(.top, [view1, view2])
694+
695+
// THEN
696+
697+
expect(layout.constraints.count) == 1
698+
}
699+
666700
func testEqualSizeWithViews() {
667701

668702
// GIVEN
@@ -721,6 +755,32 @@ final class LayoutTests: XCTestCase {
721755
}
722756
}
723757

758+
func testCenterViewBetweenLeadingAndTrailingPriority_givenNilSuperview() {
759+
760+
// GIVEN
761+
762+
var superview: UIView? = .init()
763+
let view: UIView = .init()
764+
let siblingView: UIView = .init()
765+
let layout: Layout = superview!.layout().addItems(view, siblingView)
766+
let leadingAnchor: NSLayoutXAxisAnchor = siblingView.trailing
767+
let trailingAnchor: NSLayoutXAxisAnchor = superview!.trailing
768+
769+
// THEN
770+
771+
expect(layout.center(view, between: leadingAnchor, and: trailingAnchor)) === layout
772+
expect(layout.constraints.count) == 3
773+
774+
// WHEN
775+
776+
superview = nil
777+
778+
// THEN
779+
780+
expect(layout.center(view, between: leadingAnchor, and: trailingAnchor)) === layout
781+
expect(layout.constraints.count) == 3
782+
}
783+
724784
func testCenterViewBetweenTopAndBottomPriority() {
725785

726786
// GIVEN
@@ -755,6 +815,32 @@ final class LayoutTests: XCTestCase {
755815
}
756816
}
757817

818+
func testCenterViewBetweenTopAndBottomPriority_givenNilSuperview() {
819+
820+
// GIVEN
821+
822+
var superview: UIView? = .init()
823+
let view: UIView = .init()
824+
let siblingView: UIView = .init()
825+
let layout: Layout = superview!.layout().addItems(view, siblingView)
826+
let topAnchor: NSLayoutYAxisAnchor = siblingView.bottom
827+
let bottomAnchor: NSLayoutYAxisAnchor = superview!.bottom
828+
829+
// THEN
830+
831+
expect(layout.center(view, between: topAnchor, and: bottomAnchor)) === layout
832+
expect(layout.constraints.count) == 3
833+
834+
// WHEN
835+
836+
superview = nil
837+
838+
// THEN
839+
840+
expect(layout.center(view, between: topAnchor, and: bottomAnchor)) === layout
841+
expect(layout.constraints.count) == 3
842+
}
843+
758844
// MARK: - Stack
759845

760846
func testHorizontalViewsSpacingDirectionPriorityAlignment() {
@@ -848,6 +934,40 @@ final class LayoutTests: XCTestCase {
848934
}
849935
}
850936

937+
func testHorizontalViewsSpacingDirectionPriorityAlignment_givenEmptyArray() {
938+
939+
// GIVEN
940+
941+
let superview: UIView = .init()
942+
let view1: UIView = .init()
943+
let view2: UIView = .init()
944+
let layout: Layout = superview.layout().addItems(view1, view2)
945+
946+
// WHEN
947+
948+
layout.horizontal([])
949+
950+
// THEN
951+
952+
expect(layout.constraints).to(beEmpty())
953+
954+
// WHEN
955+
956+
layout.horizontal([view1])
957+
958+
// THEN
959+
960+
expect(layout.constraints).to(beEmpty())
961+
962+
// WHEN
963+
964+
layout.horizontal([view1, view2])
965+
966+
// THEN
967+
968+
expect(layout.constraints.count) == 1
969+
}
970+
851971
func testVerticalViewsSpacingPriorityAlignment() {
852972

853973
// GIVEN
@@ -916,6 +1036,40 @@ final class LayoutTests: XCTestCase {
9161036
}
9171037
}
9181038

1039+
func testVerticalViewsSpacingPriorityAlignment_givenEmptyArray() {
1040+
1041+
// GIVEN
1042+
1043+
let superview: UIView = .init()
1044+
let view1: UIView = .init()
1045+
let view2: UIView = .init()
1046+
let layout: Layout = superview.layout().addItems(view1, view2)
1047+
1048+
// WHEN
1049+
1050+
layout.vertical([])
1051+
1052+
// THEN
1053+
1054+
expect(layout.constraints).to(beEmpty())
1055+
1056+
// WHEN
1057+
1058+
layout.vertical([view1])
1059+
1060+
// THEN
1061+
1062+
expect(layout.constraints).to(beEmpty())
1063+
1064+
// WHEN
1065+
1066+
layout.vertical([view1, view2])
1067+
1068+
// THEN
1069+
1070+
expect(layout.constraints.count) == 1
1071+
}
1072+
9191073
// MARK: - Visual Format Language
9201074

9211075
func testHorizontalWithFormatMetricsOptions_givenDefaults() {

Tests/LayoutTests/UIKit/NSDirectionalEdgeInsetsTests.swift

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import XCTest
1313

1414
final class NSDirectionalEdgeInsetsTests: XCTestCase {
1515

16+
func testEdgeType() {
17+
expect(NSDirectionalEdgeInsets().edgeType == DirectionalEdge.self) == true
18+
}
19+
1620
func testInitWithInset() {
1721

1822
// GIVEN

Tests/LayoutTests/UIKit/UIEdgeInsetsTests.swift

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import XCTest
1414

1515
final class UIEdgeInsetsTests: XCTestCase {
1616

17+
func testEdgeType() {
18+
expect(UIEdgeInsets().edgeType == CanonicalEdge.self) == true
19+
}
20+
1721
func testInitWithInset() {
1822

1923
// GIVEN

Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift

+20
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,26 @@ final class UIViewAutoLayoutTests: XCTestCase {
458458
expect(constraints2.first?.constant) == 5
459459
}
460460

461+
func testConstraintForAttributeIsRelationToSuperviewMultiplierConstant_givenNilSuperview() {
462+
463+
// GIVEN
464+
465+
let superview: UIView = .init()
466+
let view: UIView = .init()
467+
468+
// THEN
469+
470+
expect(view.constraints(toSuperview: [.top])).to(throwAssertion())
471+
472+
// WHEN
473+
474+
superview.addSubview(view)
475+
476+
// THEN
477+
478+
expect(view.constraints(toSuperview: [.top]).count) == 1
479+
}
480+
461481
func testConstraintForAttributeIsRelationToTargetAttributeOfTargetViewMultiplierConstant() {
462482

463483
// GIVEN

0 commit comments

Comments
 (0)