Skip to content

Commit 43f4e92

Browse files
author
Karl Weinmeister
committed
Updates to implementation and tests to match Objective-C foundation.
1 parent f9c22ca commit 43f4e92

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

Foundation/NSEnergyFormatter.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extension EnergyFormatter {
1515
case calorie = 1793 // chemistry "calories", abbr "cal"
1616
case kilocalorie = 1794 // kilocalories in general, abbr “kcal”, or “C” in some locales (e.g. US) when usesFoodEnergy is set to YES
1717

18+
// Map Unit to UnitEnergy class to aid with conversions
1819
fileprivate var unitEnergy: UnitEnergy {
1920
switch self {
2021
case .joule:
@@ -28,11 +29,17 @@ extension EnergyFormatter {
2829
}
2930
}
3031

32+
// Reuse symbols defined in UnitEnergy, except for kilocalories, which is defined as "kCal"
3133
fileprivate var symbol: String {
32-
return unitEnergy.symbol
33-
34+
switch self {
35+
case .kilocalorie:
36+
return "kcal"
37+
default:
38+
return unitEnergy.symbol
39+
}
3440
}
3541

42+
// Return singular, full string representation of the energy unit
3643
fileprivate var singularString: String {
3744
switch self {
3845
case .joule:
@@ -45,6 +52,8 @@ extension EnergyFormatter {
4552
return "kilocalorie"
4653
}
4754
}
55+
56+
// Return plural, full string representation of the energy unit
4857
fileprivate var pluralString: String {
4958
return "\(self.singularString)s"
5059
}
@@ -78,6 +87,7 @@ open class EnergyFormatter: Formatter {
7887
guard let formattedValue = numberFormatter.string(from:NSNumber(value: value)) else {
7988
fatalError("Cannot format \(value) as string")
8089
}
90+
8191
let separator = unitStyle == EnergyFormatter.UnitStyle.short ? "" : " "
8292
return "\(formattedValue)\(separator)\(unitString(fromValue: value, unit: unit))"
8393
}
@@ -106,15 +116,18 @@ open class EnergyFormatter: Formatter {
106116

107117
// Return a localized string of the given unit, and if the unit is singular or plural is based on the given number.
108118
open func unitString(fromValue value: Double, unit: Unit) -> String {
119+
120+
//Special case when isForFoodEnergyUse is true
109121
if isForFoodEnergyUse && unit == .kilocalorie {
110-
if unitStyle == .short || unitStyle == .medium {
111-
return Unit.calorie.symbol
112-
} else if value == 1.0 {
113-
return Unit.calorie.singularString
122+
if unitStyle == .short {
123+
return "C"
124+
} else if unitStyle == .medium {
125+
return "Cal"
114126
} else {
115-
return Unit.calorie.pluralString
127+
return "Calories"
116128
}
117129
}
130+
118131
if unitStyle == .short || unitStyle == .medium {
119132
return unit.symbol
120133
} else if value == 1.0 {

TestFoundation/TestNSEnergyFormatter.swift

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,25 @@ class TestNSEnergyFormatter: XCTestCase {
3939
formatter.numberFormatter.locale = Locale(identifier: "de_DE")
4040
XCTAssertEqual(formatter.string(fromJoules: -100000), "-100 kJ")
4141
XCTAssertEqual(formatter.string(fromJoules: -1), "-0,001 kJ")
42-
XCTAssertEqual(formatter.string(fromJoules: 0.00001), "0 J")
43-
XCTAssertEqual(formatter.string(fromJoules: 1), "1 J")
4442
XCTAssertEqual(formatter.string(fromJoules: 100000000), "100.000 kJ")
4543
}
4644

4745

4846
func test_stringFromJoulesCaloriesRegion() {
49-
XCTAssertEqual(formatter.string(fromJoules: -10000), "-2.39 kCal")
47+
XCTAssertEqual(formatter.string(fromJoules: -10000), "-2.39 kcal")
5048
XCTAssertEqual(formatter.string(fromJoules: 0.00001), "0 cal")
5149
XCTAssertEqual(formatter.string(fromJoules: 0.0001), "0 cal")
5250
XCTAssertEqual(formatter.string(fromJoules: 1), "0.239 cal")
53-
XCTAssertEqual(formatter.string(fromJoules: 10000), "2.39 kCal")
51+
XCTAssertEqual(formatter.string(fromJoules: 10000), "2.39 kcal")
5452
}
5553

5654
func test_stringFromJoulesCaloriesRegionFoodEnergyUse() {
5755
formatter.isForFoodEnergyUse = true
58-
XCTAssertEqual(formatter.string(fromJoules: -1), "-0 cal")
56+
XCTAssertEqual(formatter.string(fromJoules: -1), "-0 Cal")
5957
XCTAssertEqual(formatter.string(fromJoules: 0.001), "0 cal")
6058
XCTAssertEqual(formatter.string(fromJoules: 0.1), "0.024 cal")
6159
XCTAssertEqual(formatter.string(fromJoules: 1), "0.239 cal")
62-
XCTAssertEqual(formatter.string(fromJoules: 10000), "2.39 cal")
60+
XCTAssertEqual(formatter.string(fromJoules: 10000), "2.39 Cal")
6361
}
6462

6563
func test_stringFromValue() {
@@ -69,26 +67,26 @@ class TestNSEnergyFormatter: XCTestCase {
6967
XCTAssertEqual(formatter.string(fromValue:1, unit:EnergyFormatter.Unit.joule), "1 joule")
7068

7169
formatter.unitStyle = Formatter.UnitStyle.short
72-
XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit:EnergyFormatter.Unit.kilocalorie), "0kCal")
70+
XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit:EnergyFormatter.Unit.kilocalorie), "0kcal")
7371
XCTAssertEqual(formatter.string(fromValue: 2.4, unit: EnergyFormatter.Unit.calorie), "2.4cal")
7472
XCTAssertEqual(formatter.string(fromValue: 123456, unit: EnergyFormatter.Unit.calorie), "123,456cal")
7573

7674
formatter.unitStyle = Formatter.UnitStyle.medium
7775
formatter.isForFoodEnergyUse = true
7876
XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit: EnergyFormatter.Unit.calorie), "0 cal")
79-
XCTAssertEqual(formatter.string(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "987,654,321 cal")
77+
XCTAssertEqual(formatter.string(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "987,654,321 Cal")
8078

8179
formatter.isForFoodEnergyUse = false
82-
XCTAssertEqual(formatter.string(fromValue: 5.3, unit: EnergyFormatter.Unit.kilocalorie), "5.3 kCal")
80+
XCTAssertEqual(formatter.string(fromValue: 5.3, unit: EnergyFormatter.Unit.kilocalorie), "5.3 kcal")
8381
XCTAssertEqual(formatter.string(fromValue: 873.2345, unit: EnergyFormatter.Unit.calorie), "873.234 cal")
8482
}
8583

8684
func test_unitStringFromJoules() {
8785
var unit = EnergyFormatter.Unit.joule
88-
XCTAssertEqual(formatter.unitString(fromJoules: -100000, usedUnit: &unit), "kCal")
86+
XCTAssertEqual(formatter.unitString(fromJoules: -100000, usedUnit: &unit), "kcal")
8987
XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie)
9088

91-
XCTAssertEqual(formatter.unitString(fromJoules: 0, usedUnit: &unit), "kCal")
89+
XCTAssertEqual(formatter.unitString(fromJoules: 0, usedUnit: &unit), "kcal")
9290
XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie)
9391

9492
XCTAssertEqual(formatter.unitString(fromJoules: 0.0001, usedUnit: &unit), "cal")
@@ -97,10 +95,10 @@ class TestNSEnergyFormatter: XCTestCase {
9795
XCTAssertEqual(formatter.unitString(fromJoules: 4184, usedUnit: &unit), "cal")
9896
XCTAssertEqual(unit, EnergyFormatter.Unit.calorie)
9997

100-
XCTAssertEqual(formatter.unitString(fromJoules: 4185, usedUnit: &unit), "kCal")
98+
XCTAssertEqual(formatter.unitString(fromJoules: 4185, usedUnit: &unit), "kcal")
10199
XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie)
102100

103-
XCTAssertEqual(formatter.unitString(fromJoules: 100000, usedUnit: &unit), "kCal")
101+
XCTAssertEqual(formatter.unitString(fromJoules: 100000, usedUnit: &unit), "kcal")
104102
XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie)
105103

106104
formatter.numberFormatter.locale = Locale(identifier: "de_DE")
@@ -121,21 +119,30 @@ class TestNSEnergyFormatter: XCTestCase {
121119
}
122120

123121
func test_unitStringFromValue() {
122+
formatter.isForFoodEnergyUse = true
124123
formatter.unitStyle = Formatter.UnitStyle.long
125-
XCTAssertEqual(formatter.unitString(fromValue: 0.002, unit: EnergyFormatter.Unit.kilojoule), "kilojoules")
126-
XCTAssertEqual(formatter.unitString(fromValue: 1, unit: EnergyFormatter.Unit.kilojoule), "kilojoule")
124+
XCTAssertEqual(formatter.unitString(fromValue: 1, unit: EnergyFormatter.Unit.kilocalorie), "Calories")
125+
XCTAssertEqual(formatter.unitString(fromValue: 2, unit: EnergyFormatter.Unit.kilocalorie), "Calories")
126+
127+
formatter.unitStyle = Formatter.UnitStyle.medium
128+
XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.kilocalorie), "Cal")
129+
XCTAssertEqual(formatter.unitString(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "Cal")
127130

128131
formatter.unitStyle = Formatter.UnitStyle.short
129132
XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.calorie), "cal")
130-
XCTAssertEqual(formatter.unitString(fromValue: 123456, unit: EnergyFormatter.Unit.joule), "J")
133+
XCTAssertEqual(formatter.unitString(fromValue: 123456, unit: EnergyFormatter.Unit.kilocalorie), "C")
134+
135+
formatter.isForFoodEnergyUse = false
136+
formatter.unitStyle = Formatter.UnitStyle.long
137+
XCTAssertEqual(formatter.unitString(fromValue: 0.002, unit: EnergyFormatter.Unit.kilojoule), "kilojoules")
131138

132139
formatter.unitStyle = Formatter.UnitStyle.medium
133-
formatter.isForFoodEnergyUse = true
134-
XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.kilocalorie), "cal")
135-
XCTAssertEqual(formatter.unitString(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "cal")
140+
XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.kilocalorie), "kcal")
141+
XCTAssertEqual(formatter.unitString(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "kcal")
136142

137-
formatter.isForFoodEnergyUse = false
138-
XCTAssertEqual(formatter.unitString(fromValue: 5.3, unit: EnergyFormatter.Unit.calorie), "cal")
139-
XCTAssertEqual(formatter.unitString(fromValue: 873.2345, unit: EnergyFormatter.Unit.joule), "J")
143+
formatter.unitStyle = Formatter.UnitStyle.short
144+
XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.calorie), "cal")
145+
XCTAssertEqual(formatter.unitString(fromValue: 123456, unit: EnergyFormatter.Unit.joule), "J")
140146
}
147+
141148
}

0 commit comments

Comments
 (0)