Skip to content

Commit 248b620

Browse files
committed
Code style improvements + added some tests for IntegerInputFormatter with just the minus sign.
1 parent 08a5cf6 commit 248b620

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

WooCommerce/Classes/Tools/UnitInputFormatter/IntegerInputFormatter.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ struct IntegerInputFormatter: UnitInputFormatter {
88
///
99
let defaultValue: String
1010

11+
private let minus: Character = "-"
12+
1113
private let numberFormatter: NumberFormatter = {
1214
let numberFormatter = NumberFormatter()
1315
numberFormatter.allowsFloats = false
@@ -19,7 +21,7 @@ struct IntegerInputFormatter: UnitInputFormatter {
1921
// Allows empty input to be replaced by defaultValue
2022
return true
2123
}
22-
return numberFormatter.number(from: input) != nil || input == "-"
24+
return numberFormatter.number(from: input) != nil || input == String(minus)
2325
}
2426

2527
func format(input text: String?) -> String {
@@ -29,8 +31,7 @@ struct IntegerInputFormatter: UnitInputFormatter {
2931

3032
var formattedText = numberFormatter.number(from: text)?.stringValue ?? defaultValue
3133

32-
// The minus sign is mantained if present
33-
let minus: Character = "-"
34+
// The minus sign is maintained if present
3435
if text.first == minus {
3536
formattedText = String(minus) + formattedText.replacingOccurrences(of: "-", with: "")
3637
}

WooCommerce/WooCommerceTests/Tools/IntegerInputFormatterTests.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XCTest
33
@testable import WooCommerce
44

55
final class IntegerInputFormatterTests: XCTestCase {
6-
private let formatter = IntegerInputFormatter(defaultValue: "0")
6+
private let formatter = IntegerInputFormatter(defaultValue: "1")
77

88
// MARK: test cases for `isValid(input:)`
99

@@ -37,11 +37,16 @@ final class IntegerInputFormatterTests: XCTestCase {
3737
XCTAssertTrue(formatter.isValid(input: input))
3838
}
3939

40+
func testMinusSignInputIsValid() {
41+
let input = "-"
42+
XCTAssertTrue(formatter.isValid(input: input))
43+
}
44+
4045
// MARK: test cases for `format(input:)`
4146

4247
func testFormattingEmptyInput() {
4348
let input = ""
44-
XCTAssertEqual(formatter.format(input: input), "0")
49+
XCTAssertEqual(formatter.format(input: input), "1")
4550
}
4651

4752
func testFormattingInputWithLeadingZeros() {
@@ -58,4 +63,14 @@ final class IntegerInputFormatterTests: XCTestCase {
5863
let input = "-3412424214"
5964
XCTAssertEqual(formatter.format(input: input), "-3412424214")
6065
}
66+
67+
func testFormattingMinusSignInput() {
68+
let input = "-"
69+
XCTAssertEqual(formatter.format(input: input), "-1")
70+
}
71+
72+
func testFormattingMultipleMinusSignInput() {
73+
let input = "--"
74+
XCTAssertEqual(formatter.format(input: input), "-1")
75+
}
6176
}

0 commit comments

Comments
 (0)