Skip to content

Commit

Permalink
Move Result helps to unit tests only (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlingding authored Apr 3, 2020
1 parent 7088d46 commit 9137864
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
14 changes: 11 additions & 3 deletions GeospatialSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/* Begin PBXBuildFile section */
60944696240FE3B70021D3A6 /* GeoJsonSimpleViolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60944695240FE3B70021D3A6 /* GeoJsonSimpleViolation.swift */; };
60A857EF24360DB6008554C6 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_47 /* Result.swift */; };
OBJ_100 /* GeodesicCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* GeodesicCalculator.swift */; };
OBJ_101 /* GeoJson.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* GeoJson.swift */; };
OBJ_102 /* GeoJsonDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_14 /* GeoJsonDictionary.swift */; };
Expand Down Expand Up @@ -53,7 +54,6 @@
OBJ_128 /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_44 /* Array.swift */; };
OBJ_129 /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_45 /* Bundle.swift */; };
OBJ_130 /* FloatingPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_46 /* FloatingPoint.swift */; };
OBJ_131 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_47 /* Result.swift */; };
OBJ_132 /* GeoJsonParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_49 /* GeoJsonParser.swift */; };
OBJ_133 /* InvalidGeoJson.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_50 /* InvalidGeoJson.swift */; };
OBJ_134 /* WktParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_51 /* WktParser.swift */; };
Expand Down Expand Up @@ -202,6 +202,14 @@
path = Geodesic;
sourceTree = "<group>";
};
60A857EE24360D9C008554C6 /* Extensions */ = {
isa = PBXGroup;
children = (
OBJ_47 /* Result.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
OBJ_10 /* Calculator */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -267,7 +275,6 @@
OBJ_44 /* Array.swift */,
OBJ_45 /* Bundle.swift */,
OBJ_46 /* FloatingPoint.swift */,
OBJ_47 /* Result.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -310,6 +317,7 @@
isa = PBXGroup;
children = (
OBJ_54 /* Data */,
60A857EE24360D9C008554C6 /* Extensions */,
OBJ_59 /* Performance */,
OBJ_61 /* Test */,
);
Expand Down Expand Up @@ -561,6 +569,7 @@
OBJ_162 /* GeometryCollectionTests.swift in Sources */,
OBJ_163 /* LineStringTests.swift in Sources */,
OBJ_164 /* MultiLineStringTests.swift in Sources */,
60A857EF24360DB6008554C6 /* Result.swift in Sources */,
OBJ_165 /* MultiPointTests.swift in Sources */,
OBJ_166 /* MultiPolygonTests.swift in Sources */,
OBJ_167 /* PointTests.swift in Sources */,
Expand Down Expand Up @@ -609,7 +618,6 @@
OBJ_128 /* Array.swift in Sources */,
OBJ_129 /* Bundle.swift in Sources */,
OBJ_130 /* FloatingPoint.swift in Sources */,
OBJ_131 /* Result.swift in Sources */,
OBJ_132 /* GeoJsonParser.swift in Sources */,
OBJ_133 /* InvalidGeoJson.swift in Sources */,
OBJ_134 /* WktParser.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public func == (lhs: GeoJsonObject?, rhs: GeoJsonObject?) -> Bool {
case .featureCollection:
guard let lhs = lhs as? GeoJson.FeatureCollection, let rhs = rhs as? GeoJson.FeatureCollection, lhs.features.count == rhs.features.count else { return false }

for feature in lhs.features where !rhs.features.contains { $0 == feature } { return false }
for feature in lhs.features where !rhs.features.contains(where: { $0 == feature }) { return false }

return true
case .feature:
Expand All @@ -93,13 +93,13 @@ public func == (lhs: GeoJsonObject?, rhs: GeoJsonObject?) -> Bool {

guard lhs.objectGeometries.count == rhs.objectGeometries.count else { return false }

for geometry in lhs.objectGeometries where !rhs.objectGeometries.contains { $0 == geometry } { return false }
for geometry in lhs.objectGeometries where !rhs.objectGeometries.contains(where: { $0 == geometry }) { return false }

return true
case .multiPolygon:
guard let lhs = lhs as? GeoJson.MultiPolygon, let rhs = rhs as? GeoJson.MultiPolygon, lhs.polygons.count == rhs.polygons.count else { return false }

for polygon in lhs.polygons where !rhs.polygons.contains { $0 == polygon } { return false }
for polygon in lhs.polygons where !rhs.polygons.contains(where: { $0 == polygon }) { return false }

return true
case .polygon:
Expand All @@ -109,7 +109,7 @@ public func == (lhs: GeoJsonObject?, rhs: GeoJsonObject?) -> Bool {
case .multiLineString:
guard let lhs = lhs as? GeoJson.MultiLineString, let rhs = rhs as? GeoJson.MultiLineString, lhs.lines.count == rhs.lines.count else { return false }

for line in lhs.lines where !rhs.lines.contains { $0 == line } { return false }
for line in lhs.lines where !rhs.lines.contains(where: { $0 == line }) { return false }

return true
case .lineString:
Expand All @@ -119,7 +119,7 @@ public func == (lhs: GeoJsonObject?, rhs: GeoJsonObject?) -> Bool {
case .multiPoint:
guard let lhs = lhs as? GeoJson.MultiPoint, let rhs = rhs as? GeoJson.MultiPoint, lhs.points.count == rhs.points.count else { return false }

for point in lhs.points where !rhs.points.contains { $0 == point } { return false }
for point in lhs.points where !rhs.points.contains(where: { $0 == point }) { return false }

return true
case .point:
Expand All @@ -134,7 +134,7 @@ public func == (lhs: [GeoJsonObject]?, rhs: [GeoJsonObject]?) -> Bool {

guard let lhs = lhs, let rhs = rhs, lhs.count == rhs.count else { return false }

for geoJsonObject in lhs where !rhs.contains { $0 == geoJsonObject } { return false }
for geoJsonObject in lhs where !rhs.contains(where: { $0 == geoJsonObject }) { return false }

return true
}
2 changes: 1 addition & 1 deletion Sources/GeospatialSwift/API/Geodesic/GeodesicPolygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public func == (lhs: GeodesicPolygon, rhs: GeodesicPolygon) -> Bool {

guard lhs.mainRing == rhs.mainRing else { return false }

for linearRing in lhs.negativeRings where !(rhs.negativeRings).contains { $0 == linearRing } { return false }
for linearRing in lhs.negativeRings where !(rhs.negativeRings).contains(where: { $0 == linearRing }) { return false }

return true
}
16 changes: 12 additions & 4 deletions Sources/GeospatialSwift/Parser/GeoJsonParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal struct GeoJsonParser {
}

func geoJsonObject(fromValidatedGeoJson geoJson: GeoJsonDictionary) -> GeoJsonObject {
return geoJsonObject(validatedGeoJson: geoJson, type: geoJsonObjectTypeResult(geoJson: geoJson).success!)
return geoJsonObject(validatedGeoJson: geoJson, type: geoJsonObjectType(validatedGeoJson: geoJson))
}

func geoJsonGeometry(fromGeoJson geoJson: GeoJsonDictionary) -> Result<GeoJsonGeometry, InvalidGeoJson> {
Expand All @@ -70,7 +70,7 @@ internal struct GeoJsonParser {
}

func geoJsonGeometry(fromValidatedGeoJson geoJson: GeoJsonDictionary) -> GeoJsonGeometry {
return geoJsonGeometry(validatedGeoJson: geoJson, type: geoJsonObjectTypeResult(geoJson: geoJson).success!)
return geoJsonGeometry(validatedGeoJson: geoJson, type: geoJsonObjectType(validatedGeoJson: geoJson))
}

func geoJsonCoordinatesGeometry(fromGeoJson geoJson: GeoJsonDictionary) -> Result<GeoJsonCoordinatesGeometry, InvalidGeoJson> {
Expand All @@ -86,7 +86,7 @@ internal struct GeoJsonParser {
}

func geoJsonCoordinatesGeometry(fromValidatedGeoJson geoJson: GeoJsonDictionary) -> GeoJsonCoordinatesGeometry {
return geoJsonCoordinatesGeometry(validatedGeoJson: geoJson, type: geoJsonObjectTypeResult(geoJson: geoJson).success!)
return geoJsonCoordinatesGeometry(validatedGeoJson: geoJson, type: geoJsonObjectType(validatedGeoJson: geoJson))
}
}

Expand Down Expand Up @@ -133,6 +133,10 @@ extension GeoJsonParser {
return GeoJsonObjectType(name: typeName).flatMap { .success($0) } ?? .failure(InvalidGeoJson(reason: "Invalid GeoJson Geometry type: \(typeName)"))
}

private func geoJsonObjectType(validatedGeoJson geoJson: GeoJsonDictionary) -> GeoJsonObjectType {
return GeoJsonObjectType(name: geoJson["type"] as! String)!
}

private func geoJsonObject(validatedGeoJson geoJson: GeoJsonDictionary, type: GeoJsonObjectType) -> GeoJsonObject {
switch type {
case .feature: return GeoJson.Feature(geoJson: geoJson)
Expand All @@ -152,7 +156,7 @@ extension GeoJsonParser {
}

private func geoJsonCoordinatesGeometry(validatedGeoJson geoJson: GeoJsonDictionary, type: GeoJsonObjectType) -> GeoJsonCoordinatesGeometry {
let coordinatesJson = self.coordinatesJson(geoJson: geoJson).success!
let coordinatesJson = self.coordinatesJson(validatedGeoJson: geoJson)

switch type {
case .point: return GeoJson.Point(coordinatesJson: coordinatesJson)
Expand All @@ -168,4 +172,8 @@ extension GeoJsonParser {
private func coordinatesJson(geoJson: GeoJsonDictionary) -> Result<[Any], InvalidGeoJson> {
(geoJson["coordinates"] as? [Any]).flatMap { .success($0) } ?? .failure(.init(reason: "A valid GeoJson Coordinates Geometry must have a valid \"coordinates\" array"))
}

private func coordinatesJson(validatedGeoJson geoJson: GeoJsonDictionary) -> [Any] {
geoJson["coordinates"] as! [Any]
}
}
9 changes: 9 additions & 0 deletions Sources/GeospatialSwift/Parser/WktParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@ fileprivate extension NumberFormatter {
return formatter
}()
}

fileprivate extension Result {
var success: Success? {
switch self {
case .success(let success): return success
case .failure: return nil
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extension Result {
internal extension Result {
var succeeded: Bool {
switch self {
case .success: return true
Expand Down

0 comments on commit 9137864

Please sign in to comment.