Skip to content

Commit 1f75a76

Browse files
committed
External Criterion
1 parent 82d486b commit 1f75a76

File tree

11 files changed

+438
-310
lines changed

11 files changed

+438
-310
lines changed

GoodSwiftUI-Sample/GoodSwiftUI-Sample.xcodeproj/xcshareddata/xcschemes/GoodSwiftUI-Sample.xcscheme

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535
</TestPlanReference>
3636
</TestPlans>
3737
<Testables>
38-
<TestableReference
39-
skipped = "NO"
40-
parallelizable = "YES">
41-
<BuildableReference
42-
BuildableIdentifier = "primary"
43-
BlueprintIdentifier = "3FBFD6562D41194000E6E35A"
44-
BuildableName = "sourcery_iOSUITests.xctest"
45-
BlueprintName = "sourcery_iOSUITests"
46-
ReferencedContainer = "container:GoodSwiftUI-Sample.xcodeproj">
47-
</BuildableReference>
48-
</TestableReference>
4938
<TestableReference
5039
skipped = "NO">
5140
<BuildableReference

GoodSwiftUI-Sample/GoodSwiftUI-Sample/Screens/InputFieldSampleView.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ extension InputFieldSampleView {
130130

131131
private var pinCodeInputField: some View {
132132
// Text field
133-
InputField(text: $password, title: "PIN code", hint: "At least 6 numbers")
133+
InputField(
134+
value: $password,
135+
format: PinCodeFormatStyle(),
136+
title: "PIN code",
137+
hint: "At least 6 numbers"
138+
)
134139

135140
// Multiple custom traits
136141
.inputFieldTraits(
@@ -288,6 +293,42 @@ extension InputFieldSampleView {
288293

289294
}
290295

296+
// MARK: - PinCodeFormatStyle
297+
298+
struct PinCodeFormatStyle: ParseableFormatStyle {
299+
300+
typealias Strategy = PinCodeParseStrategy
301+
typealias FormatInput = String
302+
typealias FormatOutput = String
303+
304+
/// Formats the input string by adding spaces..
305+
func format(_ value: String) -> String {
306+
let separator: Character = " "
307+
let stride: Int = if value.count <= 6 { 3 } else { 4 }
308+
309+
return String(value.enumerated().map {
310+
$0 > 0 && $0 % stride == 0 ? [separator, $1] : [$1]
311+
}.joined())
312+
}
313+
314+
var parseStrategy: PinCodeParseStrategy {
315+
PinCodeParseStrategy()
316+
}
317+
}
318+
319+
// MARK: - ParseStrategy
320+
321+
/// Parses a formatted identifier code by removing spaces before validation.
322+
struct PinCodeParseStrategy: ParseStrategy {
323+
typealias ParseInput = String
324+
typealias ParseOutput = String
325+
326+
func parse(_ value: String) throws -> String {
327+
return value.replacingOccurrences(of: " ", with: "") // Remove spaces before validation
328+
}
329+
}
330+
331+
291332
// MARK: - SwiftUI preview
292333

293334
#Preview {

GoodSwiftUI-Sample/GoodSwiftUI-UITests/GoodSwiftUI_UITestsLaunchTests.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,25 @@ final class InputFieldViewUITests: XCTestCase {
7474
- Automatic text hiding on focus loss
7575
*/
7676
let randomPin = Int.random(in: 100000..<999999)
77-
app.slowlyTypeText(String(randomPin))
77+
let randomPinString = String(randomPin)
78+
let randomPinSpaced = "\(randomPinString.prefix(3)) \(randomPinString.suffix(3))"
79+
app.slowlyTypeText(randomPinString)
7880
app.toolbars.buttons["Done"].tap()
7981

8082
let pinTextField = app.otherElements["pinTextField"]
8183
let pinTextFieldSecureField = pinTextField.secureTextFields.element
8284

83-
XCTAssertEqual(pinTextFieldSecureField.value as? String, "••••••")
85+
// 7 dots - 6 numbers + formatter space in between
86+
XCTAssertEqual(pinTextFieldSecureField.value as? String, "•••••••")
8487
XCTAssertTrue(app.staticTexts["At least 6 numbers"].exists)
8588

8689
pinTextField.buttons["show"].tap()
8790
let pinTextFieldStandardField = pinTextField.textFields.element
88-
XCTAssertEqual(pinTextFieldStandardField.value as? String, String(randomPin))
91+
XCTAssertEqual(pinTextFieldStandardField.value as? String, randomPinSpaced)
8992

9093
app.toolbars.buttons["Done"].tap()
9194
XCTAssertFalse(pinTextFieldStandardField.exists)
92-
XCTAssertEqual(pinTextFieldSecureField.value as? String, "••••••")
95+
XCTAssertEqual(pinTextFieldSecureField.value as? String, "••••••")
9396

9497
/*
9598
VII. Validity group test
@@ -130,8 +133,9 @@ final class InputFieldViewUITests: XCTestCase {
130133

131134
/*
132135
IX. LeftView text is synchronized with PIN code
136+
- Formatter keeps value stored as unformatted, only displays with formatting applied
133137
*/
134-
XCTAssertTrue(customViewsInputField.staticTexts["+421 \(randomPin)"].exists)
138+
XCTAssertTrue(customViewsInputField.staticTexts["+421 \(randomPinString)"].exists)
135139

136140
/*
137141
X. Realtime validation

Sources/GRInputField/Common/ValidationError.swift

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)