|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | +#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) |
| 11 | + import Foundation |
| 12 | + import XCTest |
| 13 | +#else |
| 14 | + import SwiftFoundation |
| 15 | + import SwiftXCTest |
| 16 | +#endif |
| 17 | + |
| 18 | +class TestNSEnergyFormatter: XCTestCase { |
| 19 | + let formatter: EnergyFormatter = EnergyFormatter() |
| 20 | + |
| 21 | + static var allTests: [(String, (TestNSEnergyFormatter) -> () throws -> Void)] { |
| 22 | + return [ |
| 23 | + ("test_stringFromJoulesJoulesRegion", test_stringFromJoulesJoulesRegion), |
| 24 | + ("test_stringFromJoulesCaloriesRegion", test_stringFromJoulesCaloriesRegion), |
| 25 | + ("test_stringFromJoulesCaloriesRegionFoodEnergyUse", test_stringFromJoulesCaloriesRegionFoodEnergyUse), |
| 26 | + ("test_stringFromValue", test_stringFromValue), |
| 27 | + ("test_unitStringFromValue", test_unitStringFromValue), |
| 28 | + ("test_unitStringFromJoules", test_unitStringFromJoules) |
| 29 | + ] |
| 30 | + } |
| 31 | + |
| 32 | + override func setUp() { |
| 33 | + formatter.numberFormatter.locale = Locale(identifier: "en_US") |
| 34 | + formatter.isForFoodEnergyUse = false |
| 35 | + super.setUp() |
| 36 | + } |
| 37 | + |
| 38 | + func test_stringFromJoulesJoulesRegion() { |
| 39 | + formatter.numberFormatter.locale = Locale(identifier: "de_DE") |
| 40 | + XCTAssertEqual(formatter.string(fromJoules: -100000), "-100 kJ") |
| 41 | + XCTAssertEqual(formatter.string(fromJoules: -1), "-0,001 kJ") |
| 42 | + XCTAssertEqual(formatter.string(fromJoules: 100000000), "100.000 kJ") |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + func test_stringFromJoulesCaloriesRegion() { |
| 47 | + XCTAssertEqual(formatter.string(fromJoules: -10000), "-2.39 kcal") |
| 48 | + XCTAssertEqual(formatter.string(fromJoules: 0.00001), "0 cal") |
| 49 | + XCTAssertEqual(formatter.string(fromJoules: 0.0001), "0 cal") |
| 50 | + XCTAssertEqual(formatter.string(fromJoules: 1), "0.239 cal") |
| 51 | + XCTAssertEqual(formatter.string(fromJoules: 10000), "2.39 kcal") |
| 52 | + } |
| 53 | + |
| 54 | + func test_stringFromJoulesCaloriesRegionFoodEnergyUse() { |
| 55 | + formatter.isForFoodEnergyUse = true |
| 56 | + XCTAssertEqual(formatter.string(fromJoules: -1), "-0 Cal") |
| 57 | + XCTAssertEqual(formatter.string(fromJoules: 0.001), "0 cal") |
| 58 | + XCTAssertEqual(formatter.string(fromJoules: 0.1), "0.024 cal") |
| 59 | + XCTAssertEqual(formatter.string(fromJoules: 1), "0.239 cal") |
| 60 | + XCTAssertEqual(formatter.string(fromJoules: 10000), "2.39 Cal") |
| 61 | + } |
| 62 | + |
| 63 | + func test_stringFromValue() { |
| 64 | + formatter.unitStyle = Formatter.UnitStyle.long |
| 65 | + XCTAssertEqual(formatter.string(fromValue: 0.002, unit: EnergyFormatter.Unit.kilojoule),"0.002 kilojoules") |
| 66 | + XCTAssertEqual(formatter.string(fromValue:0, unit:EnergyFormatter.Unit.joule), "0 joules") |
| 67 | + XCTAssertEqual(formatter.string(fromValue:1, unit:EnergyFormatter.Unit.joule), "1 joule") |
| 68 | + |
| 69 | + formatter.unitStyle = Formatter.UnitStyle.short |
| 70 | + XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit:EnergyFormatter.Unit.kilocalorie), "0kcal") |
| 71 | + XCTAssertEqual(formatter.string(fromValue: 2.4, unit: EnergyFormatter.Unit.calorie), "2.4cal") |
| 72 | + XCTAssertEqual(formatter.string(fromValue: 123456, unit: EnergyFormatter.Unit.calorie), "123,456cal") |
| 73 | + |
| 74 | + formatter.unitStyle = Formatter.UnitStyle.medium |
| 75 | + formatter.isForFoodEnergyUse = true |
| 76 | + XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit: EnergyFormatter.Unit.calorie), "0 cal") |
| 77 | + XCTAssertEqual(formatter.string(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "987,654,321 Cal") |
| 78 | + |
| 79 | + formatter.isForFoodEnergyUse = false |
| 80 | + XCTAssertEqual(formatter.string(fromValue: 5.3, unit: EnergyFormatter.Unit.kilocalorie), "5.3 kcal") |
| 81 | + XCTAssertEqual(formatter.string(fromValue: 873.2345, unit: EnergyFormatter.Unit.calorie), "873.234 cal") |
| 82 | + } |
| 83 | + |
| 84 | + func test_unitStringFromJoules() { |
| 85 | + var unit = EnergyFormatter.Unit.joule |
| 86 | + XCTAssertEqual(formatter.unitString(fromJoules: -100000, usedUnit: &unit), "kcal") |
| 87 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie) |
| 88 | + |
| 89 | + XCTAssertEqual(formatter.unitString(fromJoules: 0, usedUnit: &unit), "kcal") |
| 90 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie) |
| 91 | + |
| 92 | + XCTAssertEqual(formatter.unitString(fromJoules: 0.0001, usedUnit: &unit), "cal") |
| 93 | + XCTAssertEqual(unit, EnergyFormatter.Unit.calorie) |
| 94 | + |
| 95 | + XCTAssertEqual(formatter.unitString(fromJoules: 4184, usedUnit: &unit), "cal") |
| 96 | + XCTAssertEqual(unit, EnergyFormatter.Unit.calorie) |
| 97 | + |
| 98 | + XCTAssertEqual(formatter.unitString(fromJoules: 4185, usedUnit: &unit), "kcal") |
| 99 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie) |
| 100 | + |
| 101 | + XCTAssertEqual(formatter.unitString(fromJoules: 100000, usedUnit: &unit), "kcal") |
| 102 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilocalorie) |
| 103 | + |
| 104 | + formatter.numberFormatter.locale = Locale(identifier: "de_DE") |
| 105 | + XCTAssertEqual(formatter.unitString(fromJoules: -100000, usedUnit: &unit), "kJ") |
| 106 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilojoule) |
| 107 | + |
| 108 | + XCTAssertEqual(formatter.unitString(fromJoules: 0, usedUnit: &unit), "kJ") |
| 109 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilojoule) |
| 110 | + |
| 111 | + XCTAssertEqual(formatter.unitString(fromJoules: 0.0001, usedUnit: &unit), "J") |
| 112 | + XCTAssertEqual(unit, EnergyFormatter.Unit.joule) |
| 113 | + |
| 114 | + XCTAssertEqual(formatter.unitString(fromJoules: 1000, usedUnit: &unit), "J") |
| 115 | + XCTAssertEqual(unit, EnergyFormatter.Unit.joule) |
| 116 | + |
| 117 | + XCTAssertEqual(formatter.unitString(fromJoules: 1000.01, usedUnit: &unit), "kJ") |
| 118 | + XCTAssertEqual(unit, EnergyFormatter.Unit.kilojoule) |
| 119 | + } |
| 120 | + |
| 121 | + func test_unitStringFromValue() { |
| 122 | + formatter.isForFoodEnergyUse = true |
| 123 | + formatter.unitStyle = Formatter.UnitStyle.long |
| 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") |
| 130 | + |
| 131 | + formatter.unitStyle = Formatter.UnitStyle.short |
| 132 | + XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.calorie), "cal") |
| 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") |
| 138 | + |
| 139 | + formatter.unitStyle = Formatter.UnitStyle.medium |
| 140 | + XCTAssertEqual(formatter.unitString(fromValue: 0.00000001, unit: EnergyFormatter.Unit.kilocalorie), "kcal") |
| 141 | + XCTAssertEqual(formatter.unitString(fromValue: 987654321, unit: EnergyFormatter.Unit.kilocalorie), "kcal") |
| 142 | + |
| 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") |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments