Skip to content

Commit 3cd8476

Browse files
authored
Merge pull request #206 from layoutBox/feature/improve_width_height_value_validation
Improve method that validates `width` and `height` values
2 parents 692b404 + 8f7a188 commit 3cd8476

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-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 >= 0, width.isFinite 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.isFinite || 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 >= 0, height.isFinite 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.isFinite || height < 0 {
358358
return false
359359
} else {
360360
return true

Tests/Common/AdjustSizeSpec.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ 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 accept a width of 0") {
119+
aView.pin.width(0)
120+
expect(Pin.lastWarningText).to(beNil())
121+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 0.0, height: 60.0)))
122+
}
123+
124+
it("should warn about negative width value") {
125+
aView.pin.width(-2)
126+
expect(Pin.lastWarningText).to(contain(["width", "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 NaN width value") {
131+
aView.pin.width(CGFloat.nan)
132+
expect(Pin.lastWarningText).to(contain(["width", "nan", "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+
}
135+
136+
it("should warn about -NaN width value") {
137+
aView.pin.width(-CGFloat.nan)
138+
expect(Pin.lastWarningText).to(contain(["width", "nan", "won't be applied", "the width", "must be greater than or equal to zero"]))
139+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
140+
}
141+
142+
it("should warn about infinity width value") {
143+
aView.pin.width(CGFloat.infinity)
144+
expect(Pin.lastWarningText).to(contain(["width", "inf", "won't be applied", "the width", "must be greater than or equal to zero"]))
145+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
146+
}
147+
148+
it("should warn about -infinity width value") {
149+
aView.pin.width(-CGFloat.infinity)
150+
expect(Pin.lastWarningText).to(contain(["width", "inf", "won't be applied", "the width", "must be greater than or equal to zero"]))
151+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
152+
}
117153
}
118154

119155
describe("the result of the height(...) methods") {
@@ -147,6 +183,42 @@ class AdjustSizeSpec: QuickSpec {
147183
aView.pin.height(of: aViewChild)
148184
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 100.0, height: 30.0)))
149185
}
186+
187+
it("should accept a height of 0") {
188+
aView.pin.height(0)
189+
expect(Pin.lastWarningText).to(beNil())
190+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 0.0)))
191+
}
192+
193+
it("should warn about negative height value") {
194+
aView.pin.height(-2)
195+
expect(Pin.lastWarningText).to(contain(["height", "won't be applied", "the height", "must be greater than or equal to zero"]))
196+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
197+
}
198+
199+
it("should warn about NaN height value") {
200+
aView.pin.height(CGFloat.nan)
201+
expect(Pin.lastWarningText).to(contain(["height", "nan", "won't be applied", "the height", "must be greater than or equal to zero"]))
202+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
203+
}
204+
205+
it("should warn about -NaN height value") {
206+
aView.pin.height(-CGFloat.nan)
207+
expect(Pin.lastWarningText).to(contain(["height", "nan", "won't be applied", "the height", "must be greater than or equal to zero"]))
208+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
209+
}
210+
211+
it("should warn about infinity height value") {
212+
aView.pin.height(CGFloat.infinity)
213+
expect(Pin.lastWarningText).to(contain(["height", "inf", "won't be applied", "the height", "must be greater than or equal to zero"]))
214+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
215+
}
216+
217+
it("should warn about -infinity height value") {
218+
aView.pin.height(-CGFloat.infinity)
219+
expect(Pin.lastWarningText).to(contain(["height", "inf", "won't be applied", "the height", "must be greater than or equal to zero"]))
220+
expect(aView.frame).to(equal(CGRect(x: 140, y: 100.0, width: 100.0, height: 60.0)))
221+
}
150222
}
151223

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

0 commit comments

Comments
 (0)