diff --git a/Sources/PinLayoutImpl.swift b/Sources/PinLayoutImpl.swift index 0ee3914c..9836ca14 100644 --- a/Sources/PinLayoutImpl.swift +++ b/Sources/PinLayoutImpl.swift @@ -594,43 +594,26 @@ class PinLayoutImpl: PinLayout { // @discardableResult func size(_ size: CGSize) -> PinLayout { - if isSizeNotSet({ return "size(CGSize(width: \(size.width), height: \(size.height)))" }) { - width(size.width) - height(size.height) - } - return self + return setSize(size, { return "size(CGSize(width: \(size.width), height: \(size.height)))" }) } @discardableResult func size(_ sideLength: CGFloat) -> PinLayout { - if isSizeNotSet({ return "size(sideLength: \(sideLength))" }) { - width(sideLength) - height(sideLength) - } - return self + return setSize(CGSize(width: sideLength, height: sideLength), { return "size(sideLength: \(sideLength))" }) } @discardableResult func size(_ percent: Percent) -> PinLayout { func context() -> String { return "size(\(percent))" } - if isSizeNotSet(context) { - guard let layoutSuperview = layoutSuperview(context) else { return self } - setWidth(percent.of(layoutSuperview.frame.width), context) - setHeight(percent.of(layoutSuperview.frame.height), context) - } - return self + guard let layoutSuperview = layoutSuperview(context) else { return self } + let size = CGSize(width: percent.of(layoutSuperview.frame.width), height: percent.of(layoutSuperview.frame.height)) + return setSize(size, context) } @discardableResult func size(of view: UIView) -> PinLayout { func context() -> String { return "size(of \(view))" } - let viewSize = view.frame.size - - if isSizeNotSet(context) { - setWidth(viewSize.width, context) - setHeight(viewSize.height, context) - } - return self + return setSize(view.frame.size, context) } @discardableResult @@ -867,7 +850,7 @@ extension PinLayoutImpl { if let _left = _left, let _right = _right { warnConflict(context, ["left": _left, "right": _right]) - } else if let width = width { + } else if let width = width, width != value { warnPropertyAlreadySet("width", propertyValue: width, context) } else { width = value @@ -883,7 +866,7 @@ extension PinLayoutImpl { if let _top = _top, let _bottom = _bottom { warnConflict(context, ["top": _top, "bottom": _bottom]) - } else if let height = height { + } else if let height = height, height != value { warnPropertyAlreadySet("height", propertyValue: height, context) } else { height = value @@ -891,19 +874,10 @@ extension PinLayoutImpl { return self } - fileprivate func isSizeNotSet(_ context: Context) -> Bool { - if let _top = _top, let _bottom = _bottom { - warnConflict(context, ["top": _top, "bottom": _bottom]) - return false - } else if let height = height { - warnConflict(context, ["height": height]) - return false - } else if let width = width { - warnConflict(context, ["width": width]) - return false - } else { - return true - } + fileprivate func setSize(_ size: CGSize, _ context: Context) -> PinLayout { + setWidth(size.width, { return "\(context())'s width" }) + setHeight(size.height, { return "\(context())'s height" }) + return self } fileprivate func computeCoordinates(_ point: CGPoint, _ layoutSuperview: UIView, _ referenceView: UIView, _ referenceSuperview: UIView) -> CGPoint { diff --git a/Tests/AdjustSizeSpec.swift b/Tests/AdjustSizeSpec.swift index c89801b2..30088cf2 100644 --- a/Tests/AdjustSizeSpec.swift +++ b/Tests/AdjustSizeSpec.swift @@ -57,6 +57,8 @@ class AdjustSizeSpec: QuickSpec { } beforeEach { + unitTestLastWarning = nil + viewController = UIViewController() rootView = BasicView(text: "", color: .white) @@ -169,9 +171,16 @@ class AdjustSizeSpec: QuickSpec { expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 200.0, height: 200.0))) } - it("should warn that size() won't be applied") { + it("should warn that size()'s width won't be applied") { aView.pin.width(90).size(CGSize(width: 25, height: 25)) - expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0))) + expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 25.0))) + expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"])) + } + + it("should warn that size()'s height won't be applied") { + aView.pin.height(90).size(CGSize(width: 25, height: 25)) + expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 25.0, height: 90.0))) + expect(unitTestLastWarning).to(contain(["size", "height", "won't be applied", "value has already been set"])) } it("should adjust the size of aView by calling a size(...) method") { @@ -179,19 +188,22 @@ class AdjustSizeSpec: QuickSpec { expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 50.0, height: 30.0))) } - it("should warn that size(of) won't be applied") { + it("should warn that size(of)'s width won't be applied") { aView.pin.width(90).size(of: aViewChild) - expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0))) + expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 30.0))) + expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"])) } - it("should warn that size() won't be applied") { + it("should warn that size()'s width won't be applied") { aView.pin.width(90).size(20) - expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0))) + expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 20.0))) + expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"])) } - it("should warn that size() won't be applied") { + it("should warn that size()'s width won't be applied") { aView.pin.width(90).size(50%) - expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0))) + expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 200.0))) + expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"])) } } diff --git a/Tests/PinEdgesSpec.swift b/Tests/PinEdgesSpec.swift index 84b90650..325e79a5 100644 --- a/Tests/PinEdgesSpec.swift +++ b/Tests/PinEdgesSpec.swift @@ -45,6 +45,8 @@ class PinEdgesSpec: QuickSpec { */ beforeEach { + unitTestLastWarning = nil + viewController = UIViewController() rootView = BasicView(text: "", color: .white) @@ -54,8 +56,6 @@ class PinEdgesSpec: QuickSpec { aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5)) aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100) rootView.addSubview(aView) - - unitTestLastWarning = nil } //