Skip to content

Commit 6a24362

Browse files
author
Luc Dion
committed
Add unit tests
1 parent 46cf5ea commit 6a24362

File tree

3 files changed

+101
-48
lines changed

3 files changed

+101
-48
lines changed

Sources/PinLayoutImpl+Coordinates.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ extension PinLayoutImpl {
103103
} else if let _hCenter = _hCenter {
104104
warnConflict(context, ["Horizontal Center": _hCenter])
105105
} else if let _right = _right, _right != value {
106-
if let layoutSuperview = layoutSuperview(context) {
107-
warnPropertyAlreadySet("right", propertyValue: layoutSuperview.frame.width - _right, context)
106+
if let superview = view.superview {
107+
warnPropertyAlreadySet("right", propertyValue: superview.frame.width - _right, context)
108108
} else {
109109
warnPropertyAlreadySet("right", propertyValue: _right, context)
110110
}
@@ -141,7 +141,11 @@ extension PinLayoutImpl {
141141
} else if let _vCenter = _vCenter {
142142
warnConflict(context, ["Vertical Center": _vCenter])
143143
} else if let _bottom = _bottom, _bottom != value {
144-
warnPropertyAlreadySet("bottom", propertyValue: _bottom, context)
144+
if let superview = view.superview {
145+
warnPropertyAlreadySet("bottom", propertyValue: superview.frame.height - _bottom, context)
146+
} else {
147+
warnPropertyAlreadySet("bottom", propertyValue: _bottom, context)
148+
}
145149
} else {
146150
_bottom = value
147151
}

Sources/PinLayoutImpl.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,23 @@ class PinLayoutImpl: PinLayout {
222222

223223
@discardableResult
224224
func all() -> PinLayout {
225-
func context() -> String { return "all()" }
226-
top(context)
227-
bottom(context)
228-
right(context)
229-
left(context)
225+
top({ "all() top coordinate" })
226+
bottom({ "all() bottom coordinate" })
227+
right({ "all() right coordinate" })
228+
left({ "all() left coordinate" })
230229
return self
231230
}
232231

233232
@discardableResult
234233
func horizontally() -> PinLayout {
235-
func context() -> String { return "horizontally()" }
236-
right(context)
237-
left(context)
234+
right({ "horizontally() right coordinate" })
235+
left({ "horizontally() left coordinate" })
238236
return self
239237
}
240238
@discardableResult
241239
func vertically() -> PinLayout {
242-
func context() -> String { return "vertically()" }
243-
top(context)
244-
bottom(context)
240+
top({ "vertically() top coordinate" })
241+
bottom({ "vertically() bottom coordinate" })
245242
return self
246243
}
247244

Tests/PinEdgesSpec.swift

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ class PinEdgesSpec: QuickSpec {
4444
viewController = UIViewController()
4545

4646
rootView = BasicView(text: "", color: .white)
47-
rootView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
4847
viewController.view.addSubview(rootView)
4948

5049
aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5))
51-
aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100)
5250
rootView.addSubview(aView)
5351

5452
bView = BasicView(text: "View B", color: UIColor.blue.withAlphaComponent(0.5))
55-
bView.frame = CGRect(x: 160, y: 120, width: 110, height: 80)
5653
rootView.addSubview(bView)
5754

5855
bViewChild = BasicView(text: "View B Child", color: UIColor.blue.withAlphaComponent(0.7))
59-
bViewChild.frame = CGRect(x: 40, y: 10, width: 60, height: 20)
6056
bView.addSubview(bViewChild)
57+
58+
rootView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
59+
aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100)
60+
bView.frame = CGRect(x: 160, y: 120, width: 110, height: 80)
61+
bViewChild.frame = CGRect(x: 40, y: 10, width: 60, height: 20)
6162
}
6263

6364
//
@@ -456,62 +457,113 @@ class PinEdgesSpec: QuickSpec {
456457
}
457458

458459
//
459-
// all / horizontall / vertically
460+
// all()
460461
//
461-
describe("the result of bottom(...)") {
462+
describe("the result of all()") {
462463
it("should adjust the aView") {
463464
aView.pin.all()
464-
expect(aView.frame).to(equal(CGRect(x: 140, y: 300.0, width: 200.0, height: 100.0)))
465+
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400.0)))
465466
}
466467

467468
it("should adjust the aView") {
468-
aView.pin.bottom(0)
469-
expect(aView.frame).to(equal(CGRect(x: 140, y: 300.0, width: 200.0, height: 100.0)))
469+
aView.pin.all().margin(10)
470+
expect(aView.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 380.0, height: 380.0)))
470471
}
471472

472-
it("should have the same position without or with a 0 parameter value") {
473-
aView.pin.bottom()
474-
let noParameterFrame = aView.frame
475-
476-
aView.pin.bottom(0)
477-
expect(aView.frame).to(equal(noParameterFrame))
473+
it("should adjust the bViewChild") {
474+
bViewChild.pin.all()
475+
expect(bViewChild.frame).to(equal(CGRect(x: 0.0, y: 0.0, width: 110.0, height: 80.0)))
478476
}
479477

478+
it("should adjust the bViewChild") {
479+
bViewChild.pin.all().margin(10)
480+
expect(bViewChild.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 90.0, height: 60.0)))
481+
}
482+
483+
it("should warn") {
484+
aView.pin.top(20).all()
485+
expect(_pinlayoutUnitTestLastWarning).to(contain(["all() top coordinate", "won't be applied", "already been set to 20.0"]))
486+
}
487+
it("should warn") {
488+
aView.pin.left(20).all()
489+
expect(_pinlayoutUnitTestLastWarning).to(contain(["all() left coordinate", "won't be applied", "already been set to 20.0"]))
490+
}
491+
it("should warn") {
492+
aView.pin.right(20).all()
493+
expect(_pinlayoutUnitTestLastWarning).to(contain(["all() right coordinate", "won't be applied", "already been set to 20.0"]))
494+
}
495+
496+
it("should warn") {
497+
aView.pin.bottom(20).all()
498+
expect(_pinlayoutUnitTestLastWarning).to(contain(["all() bottom coordinate", "won't be applied", "already been set to 20.0"]))
499+
}
500+
}
501+
502+
//
503+
// horizontally()
504+
//
505+
describe("the result of horizontally()") {
480506
it("should adjust the aView") {
481-
aView.pin.bottom(-20)
482-
expect(aView.frame).to(equal(CGRect(x: 140, y: 320.0, width: 200.0, height: 100.0)))
507+
aView.pin.horizontally()
508+
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 100.0, width: 400.0, height: 100.0)))
483509
}
484510

485511
it("should adjust the aView") {
486-
aView.pin.top().bottom(-20)
487-
expect(aView.frame).to(equal(CGRect(x: 140, y: 0.0, width: 200.0, height: 420.0)))
512+
aView.pin.horizontally().margin(10)
513+
expect(aView.frame).to(equal(CGRect(x: 10.0, y: 100.0, width: 380.0, height: 100.0)))
488514
}
489515

490-
it("should warns that the view is not added to any view") {
491-
let unAttachedView = UIView(frame: CGRect(x: 10, y: 10, width: 10, height: 10))
492-
unAttachedView.pin.bottom(20%)
493-
494-
expect(unAttachedView.frame).to(equal(CGRect(x: 10, y: 10, width: 10, height: 10)))
516+
it("should adjust the bViewChild") {
517+
bViewChild.pin.horizontally()
518+
expect(bViewChild.frame).to(equal(CGRect(x: 0.0, y: 10.0, width: 110.0, height: 20.0)))
519+
}
520+
521+
it("should adjust the bViewChild") {
522+
bViewChild.pin.horizontally().margin(10)
523+
expect(bViewChild.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 90.0, height: 20.0)))
495524
}
496525

526+
it("should warn") {
527+
aView.pin.left(20).horizontally()
528+
expect(_pinlayoutUnitTestLastWarning).to(contain(["horizontally() left coordinate", "won't be applied", "already been set to 20.0"]))
529+
}
530+
it("should warn") {
531+
aView.pin.right(20).horizontally()
532+
expect(_pinlayoutUnitTestLastWarning).to(contain(["horizontally() right coordinate", "won't be applied", "already been set to 20.0"]))
533+
}
534+
}
535+
536+
//
537+
// vertically()
538+
//
539+
describe("the result of vertically()") {
497540
it("should adjust the aView") {
498-
aView.pin.bottom(20%)
499-
expect(aView.frame).to(equal(CGRect(x: 140, y: 220.0, width: 200.0, height: 100.0)))
541+
aView.pin.vertically()
542+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 0.0, width: 200.0, height: 400.0)))
500543
}
501544

502545
it("should adjust the aView") {
503-
aView.pin.top().bottom(20%)
504-
expect(aView.frame).to(equal(CGRect(x: 140, y: 0.0, width: 200.0, height: 320.0)))
546+
aView.pin.vertically().margin(10)
547+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 10.0, width: 200.0, height: 380.0)))
505548
}
506549

507-
it("should adjust the aView") {
508-
aView.pin.bottom(-20%)
509-
expect(aView.frame).to(equal(CGRect(x: 140, y: 380.0, width: 200.0, height: 100.0)))
550+
it("should adjust the bViewChild") {
551+
bViewChild.pin.vertically()
552+
expect(bViewChild.frame).to(equal(CGRect(x: 40.0, y: 0.0, width: 60.0, height: 80.0)))
510553
}
511554

512-
it("should adjust the aView") {
513-
aView.pin.top().bottom(-20%)
514-
expect(aView.frame).to(equal(CGRect(x: 140, y: 0.0, width: 200.0, height: 480.0)))
555+
it("should adjust the bViewChild") {
556+
bViewChild.pin.vertically().margin(10)
557+
expect(bViewChild.frame).to(equal(CGRect(x: 40.0, y: 10.0, width: 60.0, height: 60.0)))
558+
}
559+
560+
it("should warn") {
561+
aView.pin.top(20).vertically()
562+
expect(_pinlayoutUnitTestLastWarning).to(contain(["vertically() top coordinate", "won't be applied", "already been set to 20.0"]))
563+
}
564+
it("should warn") {
565+
aView.pin.bottom(20).vertically()
566+
expect(_pinlayoutUnitTestLastWarning).to(contain(["vertically() bottom coordinate", "won't be applied", "already been set to 20.0"]))
515567
}
516568
}
517569
}

0 commit comments

Comments
 (0)