-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUITextFieldTests.swift
More file actions
72 lines (55 loc) · 1.95 KB
/
UITextFieldTests.swift
File metadata and controls
72 lines (55 loc) · 1.95 KB
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
//
// Validator
// Copyright © 2023 Space Code. All rights reserved.
//
import ValidatorCore
import ValidatorUI
import XCTest
#if canImport(UIKit)
import UIKit
#endif
#if os(iOS)
final class UITextFieldTests: XCTestCase {
// MARK: Tests
@MainActor
func test_thatTextFieldValidationReturnsValid_whenInputValueIsValid() {
// given
let textField = UITextField()
textField.validateOnInputChange(isEnabled: true)
textField.add(rule: LengthValidationRule(max: .max, error: String.error))
// when
textField.text = String(String.text.prefix(.max))
var result: ValidationResult?
textField.validationHandler = { result = $0 }
textField.validate(rules: textField.validationRules)
// when
if case .valid = result {}
else { XCTFail("The result must be equal to the valid value") }
}
@MainActor
func test_thatTextFieldValidationReturnsInvalid_whenInputValueIsInvalid() {
// given
let textField = UITextField()
textField.validateOnInputChange(isEnabled: true)
textField.add(rule: LengthValidationRule(max: .max, error: String.error))
// when
textField.text = .text
var result: ValidationResult?
textField.validationHandler = { result = $0 }
textField.validate(rules: textField.validationRules)
// when
if case let .invalid(errors) = result {
XCTAssertEqual(errors.count, 1)
XCTAssertEqual(errors.first?.message, .error)
} else { XCTFail("The result must be equal to the invalid value") }
}
}
#endif
private extension String {
static let text: String = "lorem ipsum lorem ipsum lorem ipsum"
static let error: String = "error"
}
private extension Int {
static let min = 0
static let max = 10
}