File: TestMassFormatter.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (121 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

class TestMassFormatter: XCTestCase {
    let formatter: MassFormatter = MassFormatter()
    
    override func setUp() {
        formatter.numberFormatter.locale = Locale(identifier: "en_US")
        formatter.isForPersonMassUse = false
        super.setUp()
    }
    
    func test_stringFromKilogramsImperialRegion() {
        XCTAssertEqual(formatter.string(fromKilograms: -100), "-220.462 lb")
        XCTAssertEqual(formatter.string(fromKilograms: 0.00001), "0 oz")
        XCTAssertEqual(formatter.string(fromKilograms: 0.0001), "0.004 oz")
        XCTAssertEqual(formatter.string(fromKilograms: 1), "2.205 lb")
        XCTAssertEqual(formatter.string(fromKilograms: 100), "220.462 lb")
    }
    
    func test_stringFromKilogramsMetricRegion() {
        formatter.numberFormatter.locale = Locale(identifier: "de_DE")
        XCTAssertEqual(formatter.string(fromKilograms: -100), "-100 kg")
        XCTAssertEqual(formatter.string(fromKilograms: -1), "-1 kg")
        XCTAssertEqual(formatter.string(fromKilograms: 1000), "1.000 kg")
    }
    
    func test_stringFromKilogramsMetricRegionPersonMassUse() {
        formatter.numberFormatter.locale = Locale(identifier: "en_GB")
        formatter.isForPersonMassUse = true
        XCTAssertEqual(formatter.string(fromKilograms: -100), "-100 kg")
        XCTAssertEqual(formatter.string(fromKilograms: -1), "-1 kg")
        XCTAssertEqual(formatter.string(fromKilograms: 1000), "1,000 kg")
    }
    
    func test_stringFromValue() {
        formatter.unitStyle = .long
        XCTAssertEqual(formatter.string(fromValue: 0.002, unit: .kilogram),"0.002 kilograms")
        XCTAssertEqual(formatter.string(fromValue: 0, unit: .stone), "0 stones")
        XCTAssertEqual(formatter.string(fromValue: 1, unit: .stone), "1 stone")
        XCTAssertEqual(formatter.string(fromValue: 2.4, unit: .stone), "2 stones, 5.6 pounds")
        
        formatter.unitStyle = .short
        XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit: .kilogram), "0kg")
        XCTAssertEqual(formatter.string(fromValue: 6, unit: .pound), "6#")
        XCTAssertEqual(formatter.string(fromValue: 2.4, unit: .stone), "2st 5.6#")
        XCTAssertEqual(formatter.string(fromValue: 123456, unit: .stone), "123,456st")
        
        formatter.unitStyle = .medium
        XCTAssertEqual(formatter.string(fromValue: 0.00000001, unit: .kilogram), "0 kg")
        XCTAssertEqual(formatter.string(fromValue: 2.4, unit: .stone), "2 st, 5.6 lb")
        XCTAssertEqual(formatter.string(fromValue: 2.0, unit: .stone), "2 st")
        XCTAssertEqual(formatter.string(fromValue: 123456.78, unit: .stone), "123,456 st, 10.92 lb")
    }
    
	func test_unitStringFromKilograms() {
        var unit = MassFormatter.Unit.kilogram
        
        // imperial
        XCTAssertEqual(formatter.unitString(fromKilograms: -100000, usedUnit: &unit), "lb")
        XCTAssertEqual(unit, .pound)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 0, usedUnit: &unit), "lb")
        XCTAssertEqual(unit, .pound)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 0.0001, usedUnit: &unit), "oz")
        XCTAssertEqual(unit, .ounce)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 0.4535, usedUnit: &unit), "oz")
        XCTAssertEqual(unit, .ounce)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 0.4536, usedUnit: &unit), "lb")
        XCTAssertEqual(unit, .pound)
        
        // metric
        formatter.numberFormatter.locale = Locale(identifier: "de_DE")
        XCTAssertEqual(formatter.unitString(fromKilograms: -100000, usedUnit: &unit), "kg")
        XCTAssertEqual(unit, .kilogram)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 0, usedUnit: &unit), "kg")
        XCTAssertEqual(unit, .kilogram)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 0.0001, usedUnit: &unit), "g")
        XCTAssertEqual(unit, .gram)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 1.000, usedUnit: &unit), "g")
        XCTAssertEqual(unit, .gram)
        
        XCTAssertEqual(formatter.unitString(fromKilograms: 1.001, usedUnit: &unit), "kg")
        XCTAssertEqual(unit, .kilogram)
    }
    
    func test_unitStringFromValue() {
        formatter.unitStyle = .long
        XCTAssertEqual(formatter.unitString(fromValue: 0.002, unit: .kilogram), "kilograms")
        XCTAssertEqual(formatter.unitString(fromValue: 0.100, unit: .gram), "grams")
        XCTAssertEqual(formatter.unitString(fromValue: 2.000, unit: .pound), "pounds")
        XCTAssertEqual(formatter.unitString(fromValue: 2.002, unit: .ounce), "ounces")
        XCTAssertEqual(formatter.unitString(fromValue: 2.002, unit: .stone), "stone")
        
        formatter.unitStyle = .medium
        XCTAssertEqual(formatter.unitString(fromValue: 0.002, unit: .kilogram), "kg")
        XCTAssertEqual(formatter.unitString(fromValue: 0.100, unit: .gram), "g")
        XCTAssertEqual(formatter.unitString(fromValue: 2.000, unit: .pound), "lb")
        XCTAssertEqual(formatter.unitString(fromValue: 2.002, unit: .ounce), "oz")
        XCTAssertEqual(formatter.unitString(fromValue: 2.002, unit: .stone), "st")
        
        formatter.unitStyle = .short
        XCTAssertEqual(formatter.unitString(fromValue: 0.002, unit: .kilogram), "kg")
        XCTAssertEqual(formatter.unitString(fromValue: 0.100, unit: .gram), "g")
        XCTAssertEqual(formatter.unitString(fromValue: 2.000, unit: .pound), "lb")
        XCTAssertEqual(formatter.unitString(fromValue: 2.002, unit: .ounce), "oz")
        XCTAssertEqual(formatter.unitString(fromValue: 2.002, unit: .stone), "st")
    }
}