Skip to content

Commit 0c530f9

Browse files
author
Luc Dion
authored
Merge pull request #75 from mirego/improve_warnings_info
Warning now display more context information
2 parents d559d54 + 9c5b010 commit 0c530f9

File tree

6 files changed

+50
-35
lines changed

6 files changed

+50
-35
lines changed

Podfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ use_frameworks!
33
workspace 'PinLayout.xcworkspace'
44

55
target 'PinLayoutTests' do
6+
platform :ios, "8.0"
67
project 'PinLayout.xcodeproj'
78

89
pod 'Quick'
910
pod 'Nimble', :inhibit_warnings => true
1011
end
1112

1213
target 'PinLayoutSample' do
14+
platform :ios, "8.0"
1315
project 'Example/PinLayoutSample.xcodeproj'
1416

1517
# Debug only

Sources/Impl/UnitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626
// POSSIBILITY OF SUCH DAMAGE.
2727

28-
import Foundation
28+
import UIKit
2929

3030
public var _pinlayoutUnitTestLastWarning: String?
3131

Sources/Percent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626
// POSSIBILITY OF SUCH DAMAGE.
2727

28-
import Foundation
28+
import UIKit
2929

3030
public struct Percent {
3131
let value: CGFloat

Sources/PinLayoutImpl+Warning.swift

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,44 @@ extension PinLayoutImpl {
1515

1616
internal func relativeEdgeContext(method: String, edge: VerticalEdge) -> String {
1717
let edge = edge as! VerticalEdgeImpl
18-
return "\(method)(to: \(edge.type.rawValue), of: \(edge.view))"
18+
return "\(method)(to: .\(edge.type.rawValue), of: \(viewDescription(edge.view)))"
1919
}
2020

2121
internal func relativeEdgeContext(method: String, edge: HorizontalEdge) -> String {
2222
let edge = edge as! HorizontalEdgeImpl
23-
return "\(method)(to: \(edge.type.rawValue), of: \(edge.view))"
23+
return "\(method)(to: .\(edge.type.rawValue), of: \(viewDescription(edge.view))"
2424
}
2525

2626
internal func relativeAnchorContext(method: String, anchor: Anchor) -> String {
2727
let anchor = anchor as! AnchorImpl
28-
return "\(method)(to: \(anchor.type.rawValue), of: \(anchor.view))"
28+
return "\(method)(to: .\(anchor.type.rawValue), of: \(viewDescription(anchor.view)))"
2929
}
3030

3131
internal func warn(_ text: String, _ context: Context) {
3232
guard Pin.logWarnings else { return }
33-
warn("\(context()) won't be applied, \(text)\n")
33+
warn("\(context()) won't be applied, \(text)")
3434
}
3535

3636
internal func warn(_ text: String) {
3737
guard Pin.logWarnings else { return }
38-
displayWarning("\n👉 PinLayout Warning: \(text)\n")
38+
displayWarning("PinLayout Warning: \(text)")
3939
}
4040

4141
internal func warnPropertyAlreadySet(_ propertyName: String, propertyValue: CGFloat, _ context: Context) {
4242
guard Pin.logWarnings else { return }
43-
displayWarning("\n👉 PinLayout Conflict: \(context()) won't be applied since it value has already been set to \(propertyValue).\n")
43+
displayWarning("PinLayout Conflict: \(context()) won't be applied since it value has already been set to \(propertyValue).")
4444
}
4545

4646
internal func warnPropertyAlreadySet(_ propertyName: String, propertyValue: CGSize, _ context: Context) {
4747
guard Pin.logWarnings else { return }
48-
displayWarning("\n👉 PinLayout Conflict: \(context()) won't be applied since it value has already been set to CGSize(width: \(propertyValue.width), height: \(propertyValue.height)).\n")
48+
displayWarning("PinLayout Conflict: \(context()) won't be applied since it value has already been set to CGSize(width: \(propertyValue.width), height: \(propertyValue.height)).")
4949
}
5050

5151
internal func warnConflict(_ context: Context, _ properties: [String: CGFloat]) {
5252
guard Pin.logWarnings else { return }
53-
var warning = "\n👉 PinLayout Conflict: \(context()) won't be applied since it conflicts with the following already set properties:\n"
53+
var warning = "PinLayout Conflict: \(context()) won't be applied since it conflicts with the following already set properties:"
5454
properties.forEach { (property) in
55-
warning += " \(property.key): \(property.value)\n"
55+
warning += "\n \(property.key): \(property.value)"
5656
}
5757

5858
displayWarning(warning)
@@ -83,12 +83,31 @@ extension PinLayoutImpl {
8383
}
8484

8585
internal func displayWarning(_ text: String) {
86-
print(text)
86+
var displayText = "\n👉 \(text)"
87+
displayText += "\n (Layouted view info: Type: \(viewName(view)), Frame: \(view.frame)"
88+
89+
var currentView = view
90+
var hierarchy: [String] = []
91+
while let parent = currentView.superview {
92+
hierarchy.insert("\(type(of: parent))", at: 0)
93+
currentView = parent
94+
}
95+
if hierarchy.count > 0 {
96+
displayText += ", Superviews: \(hierarchy.flatMap({ $0 }).joined(separator: " -> "))"
97+
}
98+
99+
displayText += ", Tag: \(view.tag))\n"
100+
101+
print(displayText)
87102
_pinlayoutUnitTestLastWarning = text
88103
}
89104

90105
internal func viewDescription(_ view: UIView) -> String {
91-
return "\"\(view.description)\""
106+
return "(\(viewName(view)), Frame: \(view.frame))"
107+
}
108+
109+
internal func viewName(_ view: UIView) -> String {
110+
return "\(type(of: view))"
92111
}
93112
}
94113

Sources/PinLayoutImpl.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class PinLayoutImpl: PinLayout {
576576

577577
@discardableResult
578578
func width(of view: UIView) -> PinLayout {
579-
return setWidth(view.frame.width, { return "width(of: \(view))" })
579+
return setWidth(view.frame.width, { return "width(of: \(viewDescription(view)))" })
580580
}
581581

582582
@discardableResult
@@ -619,7 +619,7 @@ class PinLayoutImpl: PinLayout {
619619

620620
@discardableResult
621621
func height(of view: UIView) -> PinLayout {
622-
return setHeight(view.frame.height, { return "height(of: \(view))" })
622+
return setHeight(view.frame.height, { return "height(of: \(viewDescription(view)))" })
623623
}
624624

625625
@discardableResult
@@ -671,7 +671,7 @@ class PinLayoutImpl: PinLayout {
671671

672672
@discardableResult
673673
func size(of view: UIView) -> PinLayout {
674-
func context() -> String { return "size(of \(view))" }
674+
func context() -> String { return "size(of \(viewDescription(view)))" }
675675
return setSize(view.frame.size, context)
676676
}
677677

@@ -808,7 +808,7 @@ extension PinLayoutImpl {
808808
if let superview = referenceView.superview {
809809
return superview
810810
} else {
811-
warn("the reference view \(viewDescription(referenceView)) is invalid. UIViews must be added as a sub-view before being used as a reference.", context)
811+
warn("the reference view \(viewDescription(referenceView)) must be added as a sub-view before being used as a reference.", context)
812812
return nil
813813
}
814814
}
@@ -914,10 +914,12 @@ extension PinLayoutImpl {
914914

915915
fileprivate func handlePinEdges() {
916916
guard shouldPinEdges else { return }
917-
if let width = applyMinMax(toWidth: width) {
918-
if _left != nil && _right != nil {
919-
warn("pinEdges() won't be applied horizontally, left and right coordinates are already set.")
920-
} else if let left = _left {
917+
guard _top == nil || _bottom == nil || _left == nil || _right == nil else {
918+
return warn("pinEdges() won't be applied, top, left, bottom and right coordinates are already set.")
919+
}
920+
921+
if let width = applyMinMax(toWidth: width), _left == nil || _right == nil {
922+
if let left = _left {
921923
// convert the width into a right
922924
assert(self._right == nil)
923925
self._right = left + width
@@ -938,10 +940,8 @@ extension PinLayoutImpl {
938940
}
939941
}
940942

941-
if let height = applyMinMax(toHeight: height) {
942-
if _top != nil && _bottom != nil {
943-
warn("pinEdges() won't be applied vertically, top and bottom coordinates are already set.")
944-
} else if let top = _top {
943+
if let height = applyMinMax(toHeight: height), _top == nil || _bottom == nil {
944+
if let top = _top {
945945
// convert the height into a bottom
946946
assert(self._bottom == nil)
947947
self._bottom = top + height

Tests/WarningSpec.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,10 @@ class WarningSpec: QuickSpec {
6464
// pinEdges warnings
6565
//
6666
describe("pinEdges() should warn ") {
67-
it("test when left & right & width is set") {
68-
aView.pin.left().right().width(100%).pinEdges()
69-
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 100.0, width: 400.0, height: 60.0)))
70-
expect(_pinlayoutUnitTestLastWarning).to(contain(["pinEdges()", "won't be applied", "horizontally"]))
71-
}
72-
73-
it("test when top & bottom & height is set") {
74-
aView.pin.top().bottom().height(100%).pinEdges()
75-
expect(aView.frame).to(equal(CGRect(x: 40.0, y: 0.0, width: 100.0, height: 400.0)))
76-
expect(_pinlayoutUnitTestLastWarning).to(contain(["pinEdges()", "won't be applied", "vertically"]))
67+
it("test when top, left, bottom and right is set") {
68+
aView.pin.top().bottom().left().right().width(100%).pinEdges()
69+
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0)))
70+
expect(_pinlayoutUnitTestLastWarning).to(contain(["pinEdges()", "won't be applied", "top, left, bottom and right coordinates are already set"]))
7771
}
7872
}
7973
}

0 commit comments

Comments
 (0)