Skip to content

Commit

Permalink
Adding NSEquality conformance and Unit Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerrad Thramer committed Nov 8, 2017
1 parent ca54c74 commit 70a74fb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
27 changes: 22 additions & 5 deletions MapboxDirections/MBRouteOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,6 @@ open class RouteOptions: NSObject, NSSecureCoding, NSCopying{
}

// MARK: NSCopying
/**
Returns a copy of the object.
- parameter zone: an NSZone to use in the copy operation.
- returns: A copy of the object.
*/
open func copy(with zone: NSZone? = nil) -> Any {
let copy = RouteOptions(waypoints: waypoints, profileIdentifier: profileIdentifier)
copy.allowsUTurnAtWaypoint = allowsUTurnAtWaypoint
Expand All @@ -478,6 +473,28 @@ open class RouteOptions: NSObject, NSSecureCoding, NSCopying{
copy.includesSpokenInstructions = includesSpokenInstructions
return copy
}

//MARK: - OBJ-C Equality
open override func isEqual(_ object: Any?) -> Bool {
guard let opts = object as? RouteOptions else { return false }
return isEqual(to: opts)
}

@objc(isEqualToRouteOptions:)
open func isEqual(to routeOptions: RouteOptions?) -> Bool {
guard let other = routeOptions else { return false }
guard waypoints == other.waypoints,
profileIdentifier == other.profileIdentifier,
allowsUTurnAtWaypoint == other.allowsUTurnAtWaypoint,
includesSteps == other.includesSteps,
shapeFormat == other.shapeFormat,
routeShapeResolution == other.routeShapeResolution,
attributeOptions == other.attributeOptions,
includesExitRoundaboutManeuver == other.includesExitRoundaboutManeuver,
locale == other.locale,
includesSpokenInstructions == other.includesSpokenInstructions else { return false }
return true
}
}

// MARK: Support for Directions API v4
Expand Down
42 changes: 33 additions & 9 deletions MapboxDirectionsTests/RouteOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ import XCTest

class RouteOptionsTests: XCTestCase {
func testCoding() {
let coordinates = [
CLLocationCoordinate2D(latitude: 52.5109, longitude: 13.4301),
CLLocationCoordinate2D(latitude: 52.5080, longitude: 13.4265),
CLLocationCoordinate2D(latitude: 52.5021, longitude: 13.4316),
]

let options = RouteOptions(coordinates: coordinates, profileIdentifier: .automobileAvoidingTraffic)
options.locale = Locale(identifier: "en")


let options = RouteOptions.testInstance
let encodedData = NSMutableData()
let keyedArchiver = NSKeyedArchiver(forWritingWith: encodedData)
keyedArchiver.requiresSecureCoding = true
Expand All @@ -25,6 +18,7 @@ class RouteOptionsTests: XCTestCase {

XCTAssertNotNil(unarchivedOptions)

let coordinates = RouteOptions.testCoordinates
let unarchivedWaypoints = unarchivedOptions.waypoints
XCTAssertEqual(unarchivedWaypoints.count, coordinates.count)
XCTAssertEqual(unarchivedWaypoints[0].coordinate.latitude, coordinates[0].latitude)
Expand All @@ -39,4 +33,34 @@ class RouteOptionsTests: XCTestCase {
XCTAssertEqual(unarchivedOptions.includesSpokenInstructions, options.includesSpokenInstructions)
XCTAssertEqual(unarchivedOptions.distanceMeasurementSystem, options.distanceMeasurementSystem)
}
func testCopying() {
let testInstance = RouteOptions.testInstance
guard let copy = testInstance.copy() as? RouteOptions else { return XCTFail("RouteOptions copy method should an object of same type") }
XCTAssertNotNil(copy, "Copy should not be nil.")
XCTAssertTrue(testInstance == copy, "Test Instance and copy should be semantically equivalent.")
XCTAssertFalse(testInstance === copy, "Test Instance and copy should not be identical.")

}
}

private extension RouteOptions {
static var testCoordinates: [CLLocationCoordinate2D] {
return [
CLLocationCoordinate2D(latitude: 52.5109, longitude: 13.4301),
CLLocationCoordinate2D(latitude: 52.5080, longitude: 13.4265),
CLLocationCoordinate2D(latitude: 52.5021, longitude: 13.4316),
]
}
static var testInstance: RouteOptions {
let opts = RouteOptions(coordinates: self.testCoordinates, profileIdentifier: .automobileAvoidingTraffic)
opts.locale = Locale(identifier: "en_US")
opts.allowsUTurnAtWaypoint = true
opts.shapeFormat = .polyline
opts.routeShapeResolution = .full
opts.attributeOptions = [.congestionLevel]
opts.includesExitRoundaboutManeuver = true
opts.includesSpokenInstructions = true

return opts
}
}

0 comments on commit 70a74fb

Please sign in to comment.