Skip to content

Commit fa499f1

Browse files
committed
Write unit tests for URLValidationRule
1 parent ca29de4 commit fa499f1

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import Testing
7+
import ValidatorCore
8+
9+
// MARK: - URLValidationRuleTests
10+
11+
struct URLValidationRuleTests {
12+
// MARK: Properties
13+
14+
private let sut: URLValidationRule
15+
16+
// MARK: Initialization
17+
18+
init() {
19+
sut = URLValidationRule(error: String.error)
20+
}
21+
22+
// MARK: Tests
23+
24+
@Test
25+
func validate_validURL_shouldReturnTrue() {
26+
// given
27+
let url = "https://google.com"
28+
29+
// when
30+
let result = sut.validate(input: url)
31+
32+
// then
33+
#expect(result == true)
34+
}
35+
36+
@Test
37+
func validate_missingScheme_shouldReturnFalse() {
38+
// given
39+
let url = "google.com"
40+
41+
// when
42+
let result = sut.validate(input: url)
43+
44+
// then
45+
#expect(result == false)
46+
}
47+
48+
@Test
49+
func validate_missingHost_shouldReturnFalse() {
50+
// given
51+
let url = "https://"
52+
53+
// when
54+
let result = sut.validate(input: url)
55+
56+
// then
57+
#expect(result == false)
58+
}
59+
60+
// Дополнительные кейсы:
61+
62+
@Test
63+
func validate_emptyString_shouldReturnFalse() {
64+
// given
65+
let url = ""
66+
67+
// when
68+
let result = sut.validate(input: url)
69+
70+
// then
71+
#expect(result == false)
72+
}
73+
74+
@Test
75+
func validate_whitespaceString_shouldReturnFalse() {
76+
// given
77+
let url = " "
78+
79+
// when
80+
let result = sut.validate(input: url)
81+
82+
// then
83+
#expect(result == false)
84+
}
85+
86+
@Test
87+
func validate_ipAddressURL_shouldReturnTrue() {
88+
// given
89+
let url = "http://192.168.0.1"
90+
91+
// when
92+
let result = sut.validate(input: url)
93+
94+
// then
95+
#expect(result == true)
96+
}
97+
98+
@Test
99+
func validate_urlWithPort_shouldReturnTrue() {
100+
// given
101+
let url = "https://localhost:8080"
102+
103+
// when
104+
let result = sut.validate(input: url)
105+
106+
// then
107+
#expect(result == true)
108+
}
109+
110+
@Test
111+
func validate_ftpScheme_shouldReturnFalse() {
112+
// given
113+
let url = "ftp://example.com"
114+
115+
// when
116+
let result = sut.validate(input: url)
117+
118+
// then
119+
#expect(result == true)
120+
}
121+
}
122+
123+
// MARK: Constants
124+
125+
private extension String {
126+
static let error = "URL is invalid"
127+
}

0 commit comments

Comments
 (0)