Skip to content

Commit dd3fbdb

Browse files
author
Luc Dion
authored
Merge pull request #37 from mirego/add_vCenter_hCenter_unit_tests
Add 50 unit tests for `hCenter()` and `vCenter()` methods
2 parents d44d396 + 51107ce commit dd3fbdb

File tree

3 files changed

+235
-11
lines changed

3 files changed

+235
-11
lines changed

PinLayout/PinLayoutImpl.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// POSSIBILITY OF SUCH DAMAGE.
2727
import UIKit
2828

29+
public var unitTestLastWarning: String?
30+
2931
#if DEBUG
3032
public var PinLayoutLogConflicts = true
3133
#else
@@ -1164,17 +1166,17 @@ extension PinLayoutImpl {
11641166

11651167
fileprivate func warn(_ text: String, _ context: Context) {
11661168
guard PinLayoutLogConflicts else { return }
1167-
print("\n👉 PinLayout Warning: \(context()) won't be applied, \(text)\n")
1169+
displayWarning("\n👉 PinLayout Warning: \(context()) won't be applied, \(text)\n")
11681170
}
11691171

11701172
fileprivate func warnPropertyAlreadySet(_ propertyName: String, propertyValue: CGFloat, _ context: Context) {
11711173
guard PinLayoutLogConflicts else { return }
1172-
print("\n👉 PinLayout Conflict: \(context()) won't be applied since it value has already been set to \(propertyValue).\n")
1174+
displayWarning("\n👉 PinLayout Conflict: \(context()) won't be applied since it value has already been set to \(propertyValue).\n")
11731175
}
11741176

11751177
fileprivate func warnPropertyAlreadySet(_ propertyName: String, propertyValue: CGSize, _ context: Context) {
11761178
guard PinLayoutLogConflicts else { return }
1177-
print("\n👉 PinLayout Conflict: \(context()) won't be applied since it value has already been set to CGSize(width: \(propertyValue.width), height: \(propertyValue.height)).\n")
1179+
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")
11781180
}
11791181

11801182
fileprivate func warnConflict(_ context: Context, _ properties: [String: CGFloat]) {
@@ -1184,6 +1186,12 @@ extension PinLayoutImpl {
11841186
warning += " \(key): \(value)\n"
11851187
}
11861188

1187-
print(warning)
1189+
displayWarning(warning)
1190+
}
1191+
1192+
fileprivate func displayWarning(_ text: String) {
1193+
print(text)
1194+
unitTestLastWarning = text
11881195
}
11891196
}
1197+

PinLayoutTests/MarginsSpec.swift

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ class MarginsSpec: QuickSpec {
291291
}
292292
}
293293

294+
//
295+
// bottom & height
296+
//
294297
describe("the result of top&bottom margins when the bottom coordinate and the height are specified") {
295298
it("should adjust the aView") {
296299
aView.pin.bottom(260).height(100).margin(10)
@@ -343,6 +346,9 @@ class MarginsSpec: QuickSpec {
343346
}
344347
}
345348

349+
//
350+
// top & bottom
351+
//
346352
describe("the result of top&bottom margins when the top and bottom coordinate are specified") {
347353
it("should adjust the aView") {
348354
aView.pin.top(140).bottom(160).margin(10)
@@ -408,5 +414,113 @@ class MarginsSpec: QuickSpec {
408414
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 150.0, width: 200.0, height: 80.0)))
409415
}
410416
}
417+
418+
//
419+
// hCenter
420+
//
421+
describe("the result of top&bottom margins when the hCenter is specified") {
422+
it("should adjust the aView") {
423+
aView.pin.hCenter(100).margin(10)
424+
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 100.0, width: 200.0, height: 120.0)))
425+
}
426+
427+
it("should adjust the aView") {
428+
aView.pin.hCenter(100).width(100).pinEdges().margin(10)
429+
expect(aView.frame).to(equal(CGRect(x: 60.0, y: 100.0, width: 80.0, height: 120.0)))
430+
}
431+
432+
it("should obtain the same result with or without pinEdges()") {
433+
aView.pin.hCenter(100).margin(10)
434+
let frameWithoutPinEdgest = aView.frame
435+
436+
aView.pin.hCenter(100).pinEdges().margin(10)
437+
438+
expect(aView.frame).to(equal(frameWithoutPinEdgest))
439+
}
440+
441+
it("should adjust the aView") {
442+
aView.pin.hCenter(100).width(100).pinEdges().marginLeft(10)
443+
expect(aView.frame).to(equal(CGRect(x: 60.0, y: 100.0, width: 90, height: 120.0)))
444+
}
445+
446+
it("should adjust the aView") {
447+
aView.pin.hCenter(100).width(100).pinEdges().marginRight(10)
448+
expect(aView.frame).to(equal(CGRect(x: 50, y: 100.0, width: 90, height: 120.0)))
449+
}
450+
451+
it("should adjust the aView") {
452+
aView.pin.hCenter(100).width(100).pinEdges().marginLeft(10).marginRight(10)
453+
expect(aView.frame).to(equal(CGRect(x: 60.0, y: 100.0, width: 80.0, height: 120.0)))
454+
}
455+
456+
it("should adjust the aView") {
457+
aView.pin.hCenter(100).width(50).height(100).margin(10)
458+
expect(aView.frame).to(equal(CGRect(x: 85.0, y: 100.0, width: 50.0, height: 100.0)))
459+
}
460+
}
461+
462+
//
463+
// vCenter
464+
//
465+
describe("the result of top&bottom margins when the vCenter is specified") {
466+
it("should adjust the aView") {
467+
aView.pin.vCenter(100).margin(10)
468+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 40.0, width: 200.0, height: 120.0)))
469+
}
470+
471+
it("should adjust the aView") {
472+
aView.pin.vCenter(100).height(100).pinEdges().margin(10)
473+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 60.0, width: 200.0, height: 80.0)))
474+
}
475+
476+
it("should adjust the aView") {
477+
aView.pin.vCenter(100).marginTop(10)
478+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 40.0, width: 200.0, height: 120.0)))
479+
}
480+
481+
it("should adjust the aView") {
482+
aView.pin.vCenter(100).height(100).pinEdges().marginVertical(10)
483+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 60.0, width: 200.0, height: 80.0)))
484+
}
485+
486+
it("should adjust the aView") {
487+
aView.pin.vCenter(100).height(100).pinEdges().marginTop(20).marginBottom(20)
488+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 70.0, width: 200.0, height: 60.0)))
489+
}
490+
491+
it("should adjust the aView") {
492+
aView.pin.vCenter(100).height(100).pinEdges().marginTop(10)
493+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 60.0, width: 200.0, height: 90.0)))
494+
}
495+
496+
it("should adjust the aView") {
497+
aView.pin.vCenter(100).height(100).pinEdges().marginBottom(10)
498+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 50.0, width: 200.0, height: 90.0)))
499+
}
500+
it("should adjust the aView") {
501+
aView.pin.vCenter(50%).margin(10)
502+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 140.0, width: 200.0, height: 120.0)))
503+
}
504+
}
505+
506+
//
507+
// hCenter and vCenter
508+
//
509+
describe("the result of top&bottom margins when the hCenter and vCenter are specified") {
510+
it("should adjust the aView") {
511+
aView.pin.hCenter(100).vCenter(100).margin(10)
512+
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 40.0, width: 200.0, height: 120.0)))
513+
}
514+
515+
it("should adjust the aView") {
516+
aView.pin.hCenter(100).vCenter(100).width(100).pinEdges().marginTop(10)
517+
expect(aView.frame).to(equal(CGRect(x: 50.0, y: 40.0, width: 100.0, height: 120.0)))
518+
}
519+
520+
it("should adjust the aView") {
521+
aView.pin.hCenter(50%).vCenter(50%)
522+
expect(aView.frame).to(equal(CGRect(x: 100, y: 140, width: 200.0, height: 120.0)))
523+
}
524+
}
411525
}
412526
}

PinLayoutTests/PinEdgesSpec.swift

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class PinEdgesSpec: QuickSpec {
5454
aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5))
5555
aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100)
5656
rootView.addSubview(aView)
57+
58+
unitTestLastWarning = nil
5759
}
5860

5961
//
@@ -266,9 +268,6 @@ class PinEdgesSpec: QuickSpec {
266268
expect(aView.frame).to(equal(noParameterFrame))
267269
}
268270

269-
// CGRect(x: 140, y: 100, width: 200, height: 100)
270-
271-
272271
it("should adjust the aView") {
273272
aView.pin.right(-20)
274273
expect(aView.frame).to(equal(CGRect(x: 220, y: 100.0, width: 200.0, height: 100.0)))
@@ -307,10 +306,113 @@ class PinEdgesSpec: QuickSpec {
307306
}
308307
}
309308

309+
//
310+
// hCenter
311+
//
312+
describe("the result of hCenter(...)") {
313+
it("should adjust the aView") {
314+
aView.pin.hCenter()
315+
expect(aView.frame).to(equal(CGRect(x: 100, y: 100.0, width: 200.0, height: 100.0)))
316+
}
317+
318+
it("should adjust the aView") {
319+
aView.pin.hCenter(0)
320+
expect(aView.frame).to(equal(CGRect(x: -100, y: 100.0, width: 200.0, height: 100.0)))
321+
}
322+
323+
it("should have the same position without or with a 0 parameter value") {
324+
aView.pin.hCenter()
325+
let noParameterFrame = aView.frame
326+
327+
aView.pin.hCenter(aView.superview!.frame.width / 2)
328+
expect(aView.frame).to(equal(noParameterFrame))
329+
}
330+
331+
it("should adjust the aView") {
332+
aView.pin.hCenter(-20)
333+
expect(aView.frame).to(equal(CGRect(x: -120, y: 100.0, width: 200.0, height: 100.0)))
334+
}
335+
336+
it("should not apply hCenter") {
337+
aView.pin.left().hCenter(-20)
338+
expect(aView.frame).to(equal(CGRect(x: 0, y: 100.0, width: 200.0, height: 100.0)))
339+
expect(unitTestLastWarning).to(contain(["hCenter", "won't be applied", "left"]))
340+
}
341+
342+
it("should warns that the view is not added to any view") {
343+
let unAttachedView = UIView(frame: CGRect(x: 10, y: 10, width: 10, height: 10))
344+
unAttachedView.pin.hCenter(20%)
345+
346+
expect(unAttachedView.frame).to(equal(CGRect(x: 10, y: 10, width: 10, height: 10)))
347+
}
348+
349+
it("should adjust the aView") {
350+
aView.pin.hCenter(40%)
351+
expect(aView.frame).to(equal(CGRect(x: 60, y: 100.0, width: 200.0, height: 100.0)))
352+
}
353+
354+
it("should adjust the aView") {
355+
aView.pin.hCenter(-20%)
356+
expect(aView.frame).to(equal(CGRect(x: -180, y: 100.0, width: 200.0, height: 100.0)))
357+
}
358+
}
310359

311-
// TODO: Test hCenter and vCenter!!!!!!
312-
313-
314-
360+
//
361+
// vCenter
362+
//
363+
describe("the result of vCenter(...)") {
364+
it("should adjust the aView") {
365+
aView.pin.vCenter()
366+
expect(aView.frame).to(equal(CGRect(x: 140, y: 150.0, width: 200.0, height: 100.0)))
367+
}
368+
369+
it("should adjust the aView") {
370+
aView.pin.vCenter(0)
371+
expect(aView.frame).to(equal(CGRect(x: 140, y: -50.0, width: 200.0, height: 100.0)))
372+
}
373+
374+
it("should have the same position without or with a 0 parameter value") {
375+
aView.pin.vCenter()
376+
let noParameterFrame = aView.frame
377+
378+
aView.pin.vCenter(aView.superview!.frame.height / 2)
379+
expect(aView.frame).to(equal(noParameterFrame))
380+
}
381+
382+
it("should adjust the aView") {
383+
aView.pin.vCenter(-20)
384+
expect(aView.frame).to(equal(CGRect(x: 140, y: -70.0, width: 200.0, height: 100.0)))
385+
}
386+
387+
it("should adjust the aView") {
388+
aView.pin.top().vCenter(-20)
389+
expect(aView.frame).to(equal(CGRect(x: 140, y: 0.0, width: 200.0, height: 100.0)))
390+
expect(unitTestLastWarning).to(contain(["vCenter", "won't be applied", "top"]))
391+
}
392+
393+
it("should warns that the view is not added to any view") {
394+
let unAttachedView = UIView(frame: CGRect(x: 10, y: 10, width: 200.0, height: 10))
395+
unAttachedView.pin.vCenter(20%)
396+
397+
expect(unAttachedView.frame).to(equal(CGRect(x: 10, y: 10, width: 200.0, height: 10)))
398+
expect(unitTestLastWarning).to(contain(["vCenter", "won't be applied", "view must be added"]))
399+
}
400+
401+
it("should adjust the aView") {
402+
aView.pin.vCenter(20%)
403+
expect(aView.frame).to(equal(CGRect(x: 140, y: 30.0, width: 200.0, height: 100.0)))
404+
}
405+
406+
it("should adjust the aView") {
407+
aView.pin.vCenter(-20%)
408+
expect(aView.frame).to(equal(CGRect(x: 140, y: -130.0, width: 200.0, height: 100.0)))
409+
}
410+
411+
it("should adjust the aView") {
412+
aView.pin.top().vCenter(-20%)
413+
expect(aView.frame).to(equal(CGRect(x: 140, y: 0.0, width: 200.0, height: 100.0)))
414+
expect(unitTestLastWarning).to(contain(["vCenter", "won't be applied", "top"]))
415+
}
416+
}
315417
}
316418
}

0 commit comments

Comments
 (0)