Skip to content

Commit 62fffcd

Browse files
committed
Improve method that validates width and height values. Now handle gracefully NaN and Infinity values.
1 parent 692b404 commit 62fffcd

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

Example/PinLayoutSample.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 46;
6+
objectVersion = 48;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -516,7 +516,7 @@
516516
};
517517
};
518518
buildConfigurationList = 249EFE3A1E64FAFE00165E39 /* Build configuration list for PBXProject "PinLayoutSample" */;
519-
compatibilityVersion = "Xcode 3.2";
519+
compatibilityVersion = "Xcode 8.0";
520520
developmentRegion = en;
521521
hasScannedForEncodings = 0;
522522
knownRegions = (

Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PODS:
22
- Nimble (8.0.2)
3-
- PinLayout (1.8.9)
3+
- PinLayout (1.8.10)
44
- Quick (2.1.0)
55
- SwiftLint (0.35.0)
66

@@ -22,10 +22,10 @@ EXTERNAL SOURCES:
2222

2323
SPEC CHECKSUMS:
2424
Nimble: 622629381bda1dd5678162f21f1368cec7cbba60
25-
PinLayout: f7237c104696f409470947fee3ac82e239cac367
25+
PinLayout: 641bc9679f73d3da35e04bb5180547cf55fd92d7
2626
Quick: 4be43f6634acfa727dd106bdf3929ce125ffa79d
2727
SwiftLint: 5553187048b900c91aa03552807681bb6b027846
2828

2929
PODFILE CHECKSUM: 9b1d14a0f41ae1e8ebc931fa3d033da6c545223e
3030

31-
COCOAPODS: 1.7.1
31+
COCOAPODS: 1.7.5

Sources/Impl/PinLayout+Coordinates.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,33 +328,33 @@ extension PinLayout {
328328
}
329329

330330
internal func validateWidth(_ width: CGFloat, context: Context) -> Bool {
331-
if width < 0 {
331+
guard width.isNormal && width >= 0 else {
332332
warnWontBeApplied("the width (\(width)) must be greater than or equal to zero.", context)
333333
return false
334-
} else {
335-
return true
336334
}
335+
336+
return true
337337
}
338338

339339
internal func validateComputedWidth(_ width: CGFloat?) -> Bool {
340-
if let width = width, width < 0 {
340+
if let width = width, !width.isNormal || width < 0 {
341341
return false
342342
} else {
343343
return true
344344
}
345345
}
346346

347347
internal func validateHeight(_ height: CGFloat, context: Context) -> Bool {
348-
if height < 0 {
348+
guard height.isNormal && height >= 0 else {
349349
warnWontBeApplied("the height (\(height)) must be greater than or equal to zero.", context)
350350
return false
351-
} else {
352-
return true
353351
}
352+
353+
return true
354354
}
355355

356356
internal func validateComputedHeight(_ height: CGFloat?) -> Bool {
357-
if let height = height, height < 0 {
357+
if let height = height, !height.isNormal || height < 0 {
358358
return false
359359
} else {
360360
return true

Tests/Common/AdjustSizeSpec.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ class AdjustSizeSpec: QuickSpec {
114114
aView.pin.width(of: aViewChild)
115115
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 50.0, height: 60.0)))
116116
}
117+
118+
it("should warn about negative width value") {
119+
aView.pin.width(-2)
120+
expect(Pin.lastWarningText).to(contain(["width", "won't be applied", "the width", "must be greater than or equal to zero"]))
121+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
122+
}
123+
124+
it("should warn about NaN width value") {
125+
aView.pin.width(CGFloat.nan)
126+
expect(Pin.lastWarningText).to(contain(["width", "nan", "won't be applied", "the width", "must be greater than or equal to zero"]))
127+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
128+
}
129+
130+
it("should warn about infinity width value") {
131+
aView.pin.width(CGFloat.infinity)
132+
expect(Pin.lastWarningText).to(contain(["width", "inf", "won't be applied", "the width", "must be greater than or equal to zero"]))
133+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
134+
}
117135
}
118136

119137
describe("the result of the height(...) methods") {
@@ -147,6 +165,24 @@ class AdjustSizeSpec: QuickSpec {
147165
aView.pin.height(of: aViewChild)
148166
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 100.0, height: 30.0)))
149167
}
168+
169+
it("should warn about negative height value") {
170+
aView.pin.height(-2)
171+
expect(Pin.lastWarningText).to(contain(["height", "won't be applied", "the height", "must be greater than or equal to zero"]))
172+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
173+
}
174+
175+
it("should warn about NaN height value") {
176+
aView.pin.height(CGFloat.nan)
177+
expect(Pin.lastWarningText).to(contain(["height", "nan", "won't be applied", "the height", "must be greater than or equal to zero"]))
178+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
179+
}
180+
181+
it("should warn about infinity height value") {
182+
aView.pin.height(CGFloat.infinity)
183+
expect(Pin.lastWarningText).to(contain(["height", "inf", "won't be applied", "the height", "must be greater than or equal to zero"]))
184+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
185+
}
150186
}
151187

152188
describe("the result of the size(...) methods") {

0 commit comments

Comments
 (0)