Skip to content

Commit

Permalink
Fix tests on german machine (#83)
Browse files Browse the repository at this point in the history
* fix tests on german machine

* remove comments

* change to rerun tests
  • Loading branch information
Patrick-Kladek authored Jul 17, 2024
1 parent be19416 commit 4f3e8ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
22 changes: 18 additions & 4 deletions MapboxCoreNavigation/DistanceFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ open class DistanceFormatter: LengthFormatter {

/// Indicates the most recently used unit
public private(set) var unit: LengthFormatter.Unit = .millimeter

public var locale: Locale {
set {
self.numberFormatter.locale = newValue
self.nonFractionalLengthFormatter.numberFormatter.locale = newValue
}
get {
self.numberFormatter.locale
}
}

// Rounding tables for metric, imperial, and UK measurement systems. The last threshold is used as a default.
lazy var roundingTableMetric: RoundingTable = .init(thresholds: [.init(maximumDistance: 25, roundingIncrement: 5, unit: .meter, maximumFractionDigits: 0),
Expand All @@ -157,17 +167,21 @@ open class DistanceFormatter: LengthFormatter {
@objc public init(approximate: Bool = false) {
self.approx = approximate
super.init()
numberFormatter.locale = .nationalizedCurrent
self.locale = .nationalizedCurrent
}

public required init?(coder decoder: NSCoder) {
self.approx = decoder.decodeBool(forKey: "approximate")
super.init(coder: decoder)

let localeIdentifier = (decoder.decodeObject(of: NSString.self, forKey: "locale") as? String)!
self.locale = Locale(identifier: localeIdentifier)
}

override open func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
aCoder.encode(self.approx, forKey: "approximate")
aCoder.encode(self.locale.identifier, forKey: "locale")
}

func threshold(for distance: CLLocationDistance) -> RoundingTable.Threshold {
Expand Down Expand Up @@ -209,9 +223,9 @@ open class DistanceFormatter: LengthFormatter {
@available(iOS 10.0, *)
@objc(measurementOfDistance:)
public func measurement(of distance: CLLocationDistance) -> Measurement<UnitLength> {
let threshold = threshold(for: distance)
numberFormatter.maximumFractionDigits = threshold.maximumFractionDigits
numberFormatter.roundingIncrement = threshold.roundingIncrement as NSNumber
let threshold = self.threshold(for: distance)
self.numberFormatter.maximumFractionDigits = threshold.maximumFractionDigits
self.numberFormatter.roundingIncrement = threshold.roundingIncrement as NSNumber
self.unit = threshold.unit
return threshold.measurement(for: distance)
}
Expand Down
24 changes: 12 additions & 12 deletions MapboxCoreNavigationTests/DistanceFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ class DistanceFormatterTests: XCTestCase {
super.setUp()
}

func assertDistance(_ distance: CLLocationDistance, displayed: String, quantity: String) {
func assertDistance(_ distance: CLLocationDistance, displayed: String, quantity: String, file: StaticString = #file, line: UInt = #line) {
let displayedString = self.distanceFormatter.string(from: distance)
XCTAssertEqual(displayedString, displayed, "Displayed: '\(displayedString)' should be equal to \(displayed)")
XCTAssertEqual(displayedString, displayed, "Displayed: '\(displayedString)' should be equal to \(displayed)", file: file, line: line)

let attributedString = self.distanceFormatter.attributedString(for: distance as NSNumber)
XCTAssertEqual(attributedString?.string, displayed, "Displayed: '\(attributedString?.string ?? "")' should be equal to \(displayed)")
XCTAssertEqual(attributedString?.string, displayed, "Displayed: '\(attributedString?.string ?? "")' should be equal to \(displayed)", file: file, line: line)
guard let checkedAttributedString = attributedString else {
return
}

let quantityRange = checkedAttributedString.string.range(of: quantity)
XCTAssertNotNil(quantityRange, "Displayed: '\(checkedAttributedString.string)' should contain \(quantity)")
XCTAssertNotNil(quantityRange, "Displayed: '\(checkedAttributedString.string)' should contain \(quantity)", file: file, line: line)
guard let checkedQuantityRange = quantityRange else {
return
}

var effectiveQuantityRange = NSRange(location: NSNotFound, length: 0)
let quantityAttrs = checkedAttributedString.attributes(at: checkedQuantityRange.lowerBound.utf16Offset(in: checkedAttributedString.string), effectiveRange: &effectiveQuantityRange)
XCTAssertEqual(quantityAttrs[NSAttributedString.Key.quantity] as? NSNumber, distance as NSNumber, "'\(quantity)' should have quantity \(distance)")
XCTAssertEqual(effectiveQuantityRange.length, quantity.count)
XCTAssertEqual(quantityAttrs[NSAttributedString.Key.quantity] as? NSNumber, distance as NSNumber, "'\(quantity)' should have quantity \(distance)", file: file, line: line)
XCTAssertEqual(effectiveQuantityRange.length, quantity.count, file: file, line: line)

guard checkedQuantityRange.upperBound.utf16Offset(in: checkedAttributedString.string) < checkedAttributedString.length else {
return
}
let unitAttrs = checkedAttributedString.attributes(at: checkedQuantityRange.upperBound.utf16Offset(in: checkedAttributedString.string), effectiveRange: nil)
XCTAssertNil(unitAttrs[NSAttributedString.Key.quantity], "Unit should not be emphasized like a quantity")
XCTAssertNil(unitAttrs[NSAttributedString.Key.quantity], "Unit should not be emphasized like a quantity", file: file, line: line)
}

func testDistanceFormatters_US() {
NavigationSettings.shared.distanceUnit = .mile
self.distanceFormatter.numberFormatter.locale = Locale(identifier: "en-US")
self.distanceFormatter.locale = Locale(identifier: "en-US")

self.assertDistance(0, displayed: "0 ft", quantity: "0")
self.assertDistance(oneFeet * 50, displayed: "50 ft", quantity: "50")
Expand All @@ -63,7 +63,7 @@ class DistanceFormatterTests: XCTestCase {

func testDistanceFormatters_DE() {
NavigationSettings.shared.distanceUnit = .kilometer
self.distanceFormatter.numberFormatter.locale = Locale(identifier: "de-DE")
self.distanceFormatter.locale = Locale(identifier: "de-DE")

self.assertDistance(0, displayed: "0 m", quantity: "0")
self.assertDistance(4, displayed: "5 m", quantity: "5")
Expand All @@ -79,15 +79,15 @@ class DistanceFormatterTests: XCTestCase {
self.assertDistance(999, displayed: "1 km", quantity: "1")
self.assertDistance(1000, displayed: "1 km", quantity: "1")
self.assertDistance(1001, displayed: "1 km", quantity: "1")
self.assertDistance(2500, displayed: "2.5 km", quantity: "2.5")
self.assertDistance(2900, displayed: "2.9 km", quantity: "2.9")
self.assertDistance(2500, displayed: "2,5 km", quantity: "2,5")
self.assertDistance(2900, displayed: "2,9 km", quantity: "2,9")
self.assertDistance(3000, displayed: "3 km", quantity: "3")
self.assertDistance(3500, displayed: "4 km", quantity: "4")
}

func testDistanceFormatters_GB() {
NavigationSettings.shared.distanceUnit = .mile
self.distanceFormatter.numberFormatter.locale = Locale(identifier: "en-GB")
self.distanceFormatter.locale = Locale(identifier: "en-GB")

self.assertDistance(0, displayed: "0 yd", quantity: "0")
self.assertDistance(oneYard * 4, displayed: "0 yd", quantity: "0")
Expand Down

0 comments on commit 4f3e8ad

Please sign in to comment.