Skip to content

Commit 1211b4d

Browse files
committed
Implemented IntegerInputFormatter + tests
1 parent 1311721 commit 1211b4d

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
/// `UnitInputFormatter` implementation for integer number input (positive, negative, or zero)
4+
///
5+
struct IntegerInputFormatter: UnitInputFormatter {
6+
private let numberFormatter: NumberFormatter = {
7+
let numberFormatter = NumberFormatter()
8+
numberFormatter.allowsFloats = false
9+
return numberFormatter
10+
}()
11+
12+
func isValid(input: String) -> Bool {
13+
guard input.isEmpty == false else {
14+
// Allows empty input to be replaced by 0.
15+
return true
16+
}
17+
return numberFormatter.number(from: input) != nil
18+
}
19+
20+
func format(input text: String?) -> String {
21+
guard let text = text, text.isEmpty == false else {
22+
return "0"
23+
}
24+
25+
let formattedText = numberFormatter.number(from: text)?.stringValue ?? "0"
26+
return formattedText
27+
}
28+
}

WooCommerce/Classes/ViewRelated/Products/Edit Product/Product Settings/Menu Order/ProductMenuOrderViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ private extension ProductMenuOrderViewController {
139139
}
140140
}, onTextDidBeginEditing: {
141141
//TODO: Add analytics track
142-
}, inputFormatter: PositiveIntegerInputFormatter())
142+
}, inputFormatter: IntegerInputFormatter())
143143
cell.configure(viewModel: viewModel)
144144
cell.textField.applyBodyStyle()
145-
cell.textField.keyboardType = .decimalPad
145+
cell.textField.keyboardType = .numbersAndPunctuation
146146
}
147147
}
148148

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@
270270
456CB50E2444BFAC00992A05 /* ProductPurchaseNoteViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 456CB50C2444BFAC00992A05 /* ProductPurchaseNoteViewController.xib */; };
271271
457151AB243B6E8000EB2DFA /* ProductSlugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 457151A9243B6E8000EB2DFA /* ProductSlugViewController.swift */; };
272272
457151AC243B6E8000EB2DFA /* ProductSlugViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 457151AA243B6E8000EB2DFA /* ProductSlugViewController.xib */; };
273+
45745A32245306EC00570823 /* IntegerInputFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45745A31245306EC00570823 /* IntegerInputFormatter.swift */; };
274+
45745A3424530BD600570823 /* IntegerInputFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45745A3324530BD600570823 /* IntegerInputFormatterTests.swift */; };
273275
4580BA7423F192D400B5F764 /* ProductSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4580BA7223F192D400B5F764 /* ProductSettingsViewController.swift */; };
274276
4580BA7523F192D400B5F764 /* ProductSettingsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4580BA7323F192D400B5F764 /* ProductSettingsViewController.xib */; };
275277
4580BA7723F19D4A00B5F764 /* ProductSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4580BA7623F19D4A00B5F764 /* ProductSettingsViewModel.swift */; };
@@ -1088,6 +1090,8 @@
10881090
456CB50C2444BFAC00992A05 /* ProductPurchaseNoteViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ProductPurchaseNoteViewController.xib; sourceTree = "<group>"; };
10891091
457151A9243B6E8000EB2DFA /* ProductSlugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductSlugViewController.swift; sourceTree = "<group>"; };
10901092
457151AA243B6E8000EB2DFA /* ProductSlugViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ProductSlugViewController.xib; sourceTree = "<group>"; };
1093+
45745A31245306EC00570823 /* IntegerInputFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegerInputFormatter.swift; sourceTree = "<group>"; };
1094+
45745A3324530BD600570823 /* IntegerInputFormatterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegerInputFormatterTests.swift; sourceTree = "<group>"; };
10911095
4580BA7223F192D400B5F764 /* ProductSettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductSettingsViewController.swift; sourceTree = "<group>"; };
10921096
4580BA7323F192D400B5F764 /* ProductSettingsViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ProductSettingsViewController.xib; sourceTree = "<group>"; };
10931097
4580BA7623F19D4A00B5F764 /* ProductSettingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductSettingsViewModel.swift; sourceTree = "<group>"; };
@@ -2057,6 +2061,7 @@
20572061
45B9C64223A91CB6007FC4C5 /* PriceInputFormatter.swift */,
20582062
021E2A1D23AA24C600B1DE07 /* StringInputFormatter.swift */,
20592063
020B2F8E23BD9F1F00BD79AD /* PositiveIntegerInputFormatter.swift */,
2064+
45745A31245306EC00570823 /* IntegerInputFormatter.swift */,
20602065
45F5A3C023DF206B007D40E5 /* ShippingInputFormatter.swift */,
20612066
);
20622067
path = UnitInputFormatter;
@@ -2534,6 +2539,7 @@
25342539
D82DFB4B225F303200EFE2CB /* EmptyListMessageWithActionTests.swift */,
25352540
D8F82AC422AF903700B67E4B /* IconsTests.swift */,
25362541
020B2F9023BDD71500BD79AD /* PositiveIntegerInputFormatterTests.swift */,
2542+
45745A3324530BD600570823 /* IntegerInputFormatterTests.swift */,
25372543
D8C11A6122E24C4A00D4A88D /* LedgerTableViewCellTests.swift */,
25382544
D83F593C225B4B5000626E75 /* ManualTrackingViewControllerTests.swift */,
25392545
D8AB131D225DC25F002BB5D1 /* MockOrders.swift */,
@@ -4658,6 +4664,7 @@
46584664
F997174523DC068500592D8E /* XLPagerStrip+AccessibilityIdentifier.swift in Sources */,
46594665
D8C2A28B231931D100F503E9 /* ReviewViewModel.swift in Sources */,
46604666
B541B223218A29A6008FE7C1 /* NSParagraphStyle+Woo.swift in Sources */,
4667+
45745A32245306EC00570823 /* IntegerInputFormatter.swift in Sources */,
46614668
B50BB4162141828F00AF0F3C /* FooterSpinnerView.swift in Sources */,
46624669
02FE89C9231FB31400E85EF8 /* FeatureFlagService.swift in Sources */,
46634670
B5980A6321AC879F00EBF596 /* Bundle+Woo.swift in Sources */,
@@ -4849,6 +4856,7 @@
48494856
02E4FD812306AA890049610C /* StatsTimeRangeBarViewModelTests.swift in Sources */,
48504857
45FBDF34238D33F100127F77 /* MockProduct.swift in Sources */,
48514858
6856D2A5C2076F5BF14F2C11 /* KeyboardStateProviderTests.swift in Sources */,
4859+
45745A3424530BD600570823 /* IntegerInputFormatterTests.swift in Sources */,
48524860
6856D806DE7DB61522D54044 /* NSMutableAttributedStringHelperTests.swift in Sources */,
48534861
6856DF20E1BDCC391635F707 /* AgeTests.swift in Sources */,
48544862
6856DE479EC3B2265AC1F775 /* Calendar+Extensions.swift in Sources */,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import XCTest
2+
@testable import WooCommerce
3+
4+
final class IntegerInputFormatterTests: XCTestCase {
5+
private let formatter = IntegerInputFormatter()
6+
7+
// MARK: test cases for `isValid(input:)`
8+
9+
func testEmptyInputIsValid() {
10+
let input = ""
11+
XCTAssertTrue(formatter.isValid(input: input))
12+
}
13+
14+
func testAlphanumericInputIsNotValid() {
15+
let input = "06two"
16+
XCTAssertFalse(formatter.isValid(input: input))
17+
}
18+
19+
func testDecimalInputIsNotValid() {
20+
let input = "9990.52"
21+
XCTAssertFalse(formatter.isValid(input: input))
22+
}
23+
24+
func testTrailingPointInputIsValid() {
25+
let input = "9990."
26+
XCTAssertFalse(formatter.isValid(input: input))
27+
}
28+
29+
func testIntegerInputIsValid() {
30+
let input = "888888"
31+
XCTAssertTrue(formatter.isValid(input: input))
32+
}
33+
34+
func testNegativeIntegerInputIsValid() {
35+
let input = "-888888"
36+
XCTAssertTrue(formatter.isValid(input: input))
37+
}
38+
39+
// MARK: test cases for `format(input:)`
40+
41+
func testFormattingEmptyInput() {
42+
let input = ""
43+
XCTAssertEqual(formatter.format(input: input), "0")
44+
}
45+
46+
func testFormattingInputWithLeadingZeros() {
47+
let input = "0012391"
48+
XCTAssertEqual(formatter.format(input: input), "12391")
49+
}
50+
51+
func testFormattingIntegerInput() {
52+
let input = "314200"
53+
XCTAssertEqual(formatter.format(input: input), "314200")
54+
}
55+
56+
func testFormattingNegativeIntegerInput() {
57+
let input = "-3412424214"
58+
XCTAssertEqual(formatter.format(input: input), "-3412424214")
59+
}
60+
}

0 commit comments

Comments
 (0)