Skip to content

Commit 1d71d91

Browse files
author
Steven Grosmark
committed
Update unit tests
1 parent 6f011d8 commit 1d71d91

File tree

4 files changed

+186
-14
lines changed

4 files changed

+186
-14
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//
2+
// ===----------------------------------------------------------------------===//
3+
//
4+
// LayoutViewTests.swift
5+
//
6+
// Created by Steven Grosmark on 08/10/2021
7+
// Copyright © 2021 WW International, Inc.
8+
//
9+
//
10+
// This source file is part of the WWLayout open source project
11+
//
12+
// https://github.com/ww-tech/wwlayout
13+
//
14+
// Copyright © 2017-2021 WW International, Inc.
15+
//
16+
// Licensed under the Apache License, Version 2.0 (the "License");
17+
// you may not use this file except in compliance with the License.
18+
// You may obtain a copy of the License at
19+
//
20+
// http://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
//
28+
// ===----------------------------------------------------------------------===//
29+
//
30+
31+
import XCTest
32+
@testable import WWLayout
33+
34+
class LayoutViewTests: XCTestCase {
35+
36+
private var window: UIWindow!
37+
38+
override func setUp() {
39+
super.setUp()
40+
41+
window = UIWindow()
42+
window.rootViewController = UIViewController()
43+
}
44+
45+
override func tearDown() {
46+
window = nil
47+
48+
super.tearDown()
49+
}
50+
51+
func test_creationInViewController() throws {
52+
// given
53+
let label = UILabel()
54+
window.rootViewController?.view.addSubview(label)
55+
56+
// when
57+
label.layout(horizontalSize: .compact).fill(.superview)
58+
59+
// then
60+
let layoutView = findLayoutView()
61+
XCTAssertNotNil(layoutView, "LayoutView should exist")
62+
XCTAssert(layoutView?.superview === window.rootViewController?.view, "LayoutView should be created at root of the view controller" )
63+
}
64+
65+
func test_creationInViewController_nestedViews() throws {
66+
// given
67+
let label = UILabel()
68+
window.rootViewController?.view.addSubview(UIView(subviews: UIView(subviews: label)))
69+
70+
// when
71+
label.layout(horizontalSize: .compact).fill(.superview)
72+
73+
// then
74+
let layoutView = findLayoutView()
75+
XCTAssertNotNil(layoutView, "LayoutView should exist")
76+
XCTAssert(layoutView?.superview === window.rootViewController?.view, "LayoutView should be created at root of the view controller" )
77+
}
78+
79+
func test_creationInViewController_nestedController() throws {
80+
// given
81+
let childController = UIViewController()
82+
let label = UILabel()
83+
84+
childController.view.addSubview(UIView(subviews: UIView(subviews: label)))
85+
window.rootViewController?.addChild(childController)
86+
window.rootViewController?.view.addSubview(childController.view)
87+
88+
// when
89+
label.layout(horizontalSize: .compact).fill(.superview)
90+
91+
// then
92+
let layoutView = findLayoutView()
93+
XCTAssertNotNil(layoutView, "LayoutView should exist")
94+
XCTAssert(layoutView?.superview === childController.view, "LayoutView should be created at root of the _child_ view controller" )
95+
}
96+
97+
func test_creationInWindow() throws {
98+
// given
99+
let label = UILabel()
100+
let targetSubview = UIView(subviews: UIView(subviews: label))
101+
window.addSubview(targetSubview)
102+
103+
// when
104+
label.layout(horizontalSize: .compact).fill(.superview)
105+
106+
// then
107+
let layoutView = findLayoutView()
108+
XCTAssertNotNil(layoutView, "LayoutView should exist")
109+
XCTAssert(layoutView?.superview === targetSubview, "LayoutView should be created in child of thw window" )
110+
}
111+
112+
private func findLayoutView() -> LayoutView? {
113+
var toCheck: [UIView] = [window, window.rootViewController?.view].compactMap { return $0 }
114+
while !toCheck.isEmpty {
115+
let candidate = toCheck.removeFirst()
116+
if let layoutView = candidate as? LayoutView {
117+
return layoutView
118+
}
119+
toCheck.append(contentsOf: candidate.subviews)
120+
}
121+
return nil
122+
}
123+
124+
}

Tests/WWLayoutTests/StackingTests.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,3 @@ class StackingBelowTests: XCTestCase {
143143
}
144144

145145
}
146-
147-
extension UIView {
148-
149-
func addSubviews(_ views: UIView...) {
150-
views.forEach(addSubview)
151-
}
152-
153-
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// ===----------------------------------------------------------------------===//
3+
//
4+
// UIView+Subviews.swift
5+
//
6+
// Created by Steven Grosmark on 08/11/2021
7+
// Copyright © 2021 WW International, Inc.
8+
//
9+
//
10+
// This source file is part of the WWLayout open source project
11+
//
12+
// https://github.com/ww-tech/wwlayout
13+
//
14+
// Copyright © 2017-2021 WW International, Inc.
15+
//
16+
// Licensed under the Apache License, Version 2.0 (the "License");
17+
// you may not use this file except in compliance with the License.
18+
// You may obtain a copy of the License at
19+
//
20+
// http://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
//
28+
// ===----------------------------------------------------------------------===//
29+
//
30+
31+
import UIKit
32+
33+
extension UIView {
34+
35+
convenience init(subviews: UIView...) {
36+
self.init()
37+
addSubviews(subviews)
38+
}
39+
40+
func addSubviews(_ views: UIView...) {
41+
addSubviews(views)
42+
}
43+
44+
func addSubviews(_ views: [UIView]) {
45+
views.forEach(addSubview)
46+
}
47+
48+
}

WWLayout.xcodeproj/project.pbxproj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
798F25471FD1902F006E6A44 /* Insets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 798F25461FD1902F006E6A44 /* Insets.swift */; };
4141
798F259A1FD19861006E6A44 /* LayoutAxis.swift in Sources */ = {isa = PBXBuildFile; fileRef = 798F25991FD19861006E6A44 /* LayoutAxis.swift */; };
4242
798F259C1FD1988A006E6A44 /* LayoutEdge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 798F259B1FD1988A006E6A44 /* LayoutEdge.swift */; };
43+
B010BA9E26C2F855006DB570 /* LayoutViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B010BA9D26C2F855006DB570 /* LayoutViewTests.swift */; };
44+
B010BF0726C40AEC006DB570 /* UIView+Subviews.swift in Sources */ = {isa = PBXBuildFile; fileRef = B010BF0626C40AEC006DB570 /* UIView+Subviews.swift */; };
4345
/* End PBXBuildFile section */
4446

4547
/* Begin PBXContainerItemProxy section */
@@ -88,6 +90,8 @@
8890
798F25461FD1902F006E6A44 /* Insets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Insets.swift; sourceTree = "<group>"; };
8991
798F25991FD19861006E6A44 /* LayoutAxis.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutAxis.swift; sourceTree = "<group>"; };
9092
798F259B1FD1988A006E6A44 /* LayoutEdge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutEdge.swift; sourceTree = "<group>"; };
93+
B010BA9D26C2F855006DB570 /* LayoutViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutViewTests.swift; sourceTree = "<group>"; };
94+
B010BF0626C40AEC006DB570 /* UIView+Subviews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Subviews.swift"; sourceTree = "<group>"; };
9195
/* End PBXFileReference section */
9296

9397
/* Begin PBXFrameworksBuildPhase section */
@@ -112,17 +116,19 @@
112116
7904E89D23C545740059FE58 /* WWLayoutTests */ = {
113117
isa = PBXGroup;
114118
children = (
115-
7904E89E23C545740059FE58 /* SizeClassTests.swift */,
116-
7904E89F23C545740059FE58 /* SafeAreaTests.swift */,
117-
7904E8A023C545740059FE58 /* TaggingTests.swift */,
118-
7904E8A123C545740059FE58 /* SiblingTests.swift */,
119-
7904E8A223C545740059FE58 /* FillTests.swift */,
120119
7904E8A323C545740059FE58 /* BasicViewToSuperviewTests.swift */,
121120
7904E8A423C545740059FE58 /* ConstraintArrayTests.swift */,
122-
7904E8A523C545740059FE58 /* SwiftCompatibility.swift */,
121+
7904E8A223C545740059FE58 /* FillTests.swift */,
123122
7904E8A623C545740059FE58 /* InsetTests.swift */,
123+
B010BA9D26C2F855006DB570 /* LayoutViewTests.swift */,
124124
7904E8A723C545740059FE58 /* PriorityTests.swift */,
125+
7904E89F23C545740059FE58 /* SafeAreaTests.swift */,
126+
7904E8A123C545740059FE58 /* SiblingTests.swift */,
127+
7904E89E23C545740059FE58 /* SizeClassTests.swift */,
125128
7904E8A823C545740059FE58 /* StackingTests.swift */,
129+
7904E8A523C545740059FE58 /* SwiftCompatibility.swift */,
130+
7904E8A023C545740059FE58 /* TaggingTests.swift */,
131+
B010BF0626C40AEC006DB570 /* UIView+Subviews.swift */,
126132
);
127133
name = WWLayoutTests;
128134
path = Tests/WWLayoutTests;
@@ -342,10 +348,12 @@
342348
7904E8AE23C545740059FE58 /* FillTests.swift in Sources */,
343349
7904E8B123C545740059FE58 /* SwiftCompatibility.swift in Sources */,
344350
7904E8AC23C545740059FE58 /* TaggingTests.swift in Sources */,
351+
B010BF0726C40AEC006DB570 /* UIView+Subviews.swift in Sources */,
345352
7904E8B023C545740059FE58 /* ConstraintArrayTests.swift in Sources */,
346353
7904E8AA23C545740059FE58 /* SizeClassTests.swift in Sources */,
347354
7904E8B423C545740059FE58 /* StackingTests.swift in Sources */,
348355
7904E8B223C545740059FE58 /* InsetTests.swift in Sources */,
356+
B010BA9E26C2F855006DB570 /* LayoutViewTests.swift in Sources */,
349357
7904E8AB23C545740059FE58 /* SafeAreaTests.swift in Sources */,
350358
7904E8AF23C545740059FE58 /* BasicViewToSuperviewTests.swift in Sources */,
351359
7904E8B323C545740059FE58 /* PriorityTests.swift in Sources */,

0 commit comments

Comments
 (0)