-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreditCardValidationRuleTests.swift
More file actions
164 lines (119 loc) · 4.05 KB
/
CreditCardValidationRuleTests.swift
File metadata and controls
164 lines (119 loc) · 4.05 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Validator
// Copyright © 2025 Space Code. All rights reserved.
//
@testable import ValidatorCore
import XCTest
// MARK: - CreditCardValidationRuleTests
final class CreditCardValidationRuleTests: XCTestCase {
// MARK: - Visa
func test_validate_visaCardValid_shouldReturnTrue() {
// given
let rule = CreditCardValidationRule(types: [.visa], error: String.error)
let validVisa = "4111111111111111"
// when
let isValid = rule.validate(input: validVisa)
// then
XCTAssertTrue(isValid)
}
func test_validate_visaCardInvalid_shouldReturnFalse() {
// given
let rule = CreditCardValidationRule(types: [.visa], error: String.error)
let invalidVisa = "411111111111112"
// when
let isValid = rule.validate(input: invalidVisa)
// then
XCTAssertFalse(isValid)
}
// MARK: - MasterCard
func test_validate_masterCardValid_shouldReturnTrue() {
// given
let rule = CreditCardValidationRule(types: [.masterCard], error: String.error)
let validMasterCard = "5500000000000004"
// when
let isValid = rule.validate(input: validMasterCard)
// then
XCTAssertTrue(isValid)
}
func test_validate_masterCardInvalid_shouldReturnFalse() {
// given
let rule = CreditCardValidationRule(types: [.masterCard], error: String.error)
let invalidMasterCard = "550000000000"
// when
let isValid = rule.validate(input: invalidMasterCard)
// then
XCTAssertFalse(isValid)
}
// MARK: - American Express
func test_validate_amexValid_shouldReturnTrue() {
// given
let rule = CreditCardValidationRule(types: [.amex], error: String.error)
let validAmex = "340000000000009"
// when
let isValid = rule.validate(input: validAmex)
// then
XCTAssertTrue(isValid)
}
func test_validate_amexInvalid_shouldReturnFalse() {
// given
let rule = CreditCardValidationRule(types: [.amex], error: String.error)
let invalidAmex = "3400000000000099"
// when
let isValid = rule.validate(input: invalidAmex)
// then
XCTAssertFalse(isValid)
}
// MARK: - JCB
func test_validate_jcbValid_shouldReturnTrue() {
// given
let rule = CreditCardValidationRule(types: [.jcb], error: String.error)
let validJCB = "3528000000000007"
// when
let isValid = rule.validate(input: validJCB)
// then
XCTAssertTrue(isValid)
}
// MARK: - UnionPay
func test_validate_unionPayValid_shouldReturnTrue() {
// given
let rule = CreditCardValidationRule(types: [.unionPay], error: String.error)
let validUnionPay = "6221260000000000"
// when
let isValid = rule.validate(input: validUnionPay)
// then
XCTAssertTrue(isValid)
}
// MARK: - Multiple Types
func test_validate_multipleTypesValid_shouldReturnTrue() {
// given
let rule = CreditCardValidationRule(types: [.visa, .amex, .masterCard], error: String.error)
let validVisa = "4111111111111111"
// when
let isValid = rule.validate(input: validVisa)
// then
XCTAssertTrue(isValid)
}
// MARK: - Unknown Card Type
func test_validate_unknownCardType_shouldReturnFalse() {
// given
let rule = CreditCardValidationRule(types: [.visa], error: String.error)
let randomCard = "9999999999999999"
// when
let isValid = rule.validate(input: randomCard)
// then
XCTAssertFalse(isValid)
}
// MARK: - Empty Input
func test_validate_emptyString_shouldReturnFalse() {
// given
let rule = CreditCardValidationRule(types: [.visa], error: String.error)
// when
let isValid = rule.validate(input: "")
// then
XCTAssertFalse(isValid)
}
}
// MARK: Constants
private extension String {
static let error = "Invalid card number"
}