|
1 | | -import XCTest |
| 1 | +import Testing |
2 | 2 | import ValidationKit |
3 | 3 |
|
4 | | -class EmailTests: XCTestCase { |
5 | | - var validator: Validator<String, String>! |
| 4 | +@Suite("Email Validator Tests") |
| 5 | +struct EmailTests { |
| 6 | + let validator = Validator<String, String>.email |
6 | 7 |
|
7 | | - override func setUpWithError() throws { |
8 | | - validator = .email |
9 | | - } |
10 | | - |
11 | | - func testValidEmailAddresses() throws { |
12 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid ) |
13 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid ) |
| 8 | + @Test("Valid email addresses should pass validation") |
| 9 | + func validEmailAddresses() throws { |
| 10 | + #expect (validator .validate(input : "[email protected]").isValid == true, "Email with plus sign should be valid") |
| 11 | + #expect (validator .validate(input : "[email protected]").isValid == true, "Email with subdomain should be valid") |
14 | 12 |
|
15 | 13 | // Test cases from: https://www.softwaretestingo.com/test-cases-for-email-field/ |
16 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Valid email") |
17 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "The email contains a dot in the address field") |
18 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "The email contains a dot with a subdomain") |
19 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Plus sign is considered a valid character") |
20 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "The domain is a valid IP address") |
21 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Digits in the address are valid") |
22 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Dash in the domain name is valid") |
23 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Underscore in the address field is valid") |
24 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , ".name is a valid Top Level Domain name") |
25 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Dot in Top Level Domain name also considered valid (use co.jp as an example here)") |
26 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Dash in the address field is valid") |
| 14 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Valid email") |
| 15 | + #expect(validator .validate(input : "[email protected]").isValid == true, "The email contains a dot in the address field") |
| 16 | + #expect(validator .validate(input : "[email protected]").isValid == true, "The email contains a dot with a subdomain") |
| 17 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Plus sign is considered a valid character") |
| 18 | + #expect(validator .validate(input : "[email protected]").isValid == true, "The domain is a valid IP address") |
| 19 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Digits in the address are valid") |
| 20 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Dash in the domain name is valid") |
| 21 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Underscore in the address field is valid") |
| 22 | + #expect(validator .validate(input : "[email protected]").isValid == true, ".name is a valid Top Level Domain name") |
| 23 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Dot in Top Level Domain name also considered valid (use co.jp as an example here)") |
| 24 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Dash in the address field is valid") |
27 | 25 |
|
28 | 26 | // Test cases from ChatGPT |
29 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Standard email format") |
30 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a subdomain") |
31 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with numbers in the domain name") |
32 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with special characters in the local part") |
33 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a two-letter top-level domain (TLD)") |
34 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a hyphen in the domain name") |
35 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a single-letter local part") |
36 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a long local part and domain name") |
37 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a dot at the beginning of the local part") |
38 | | - XCTAssertTrue(validator .validate(input : "[email protected]").isValid , "Email with a dot at the end of the local part") |
| 27 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Standard email format") |
| 28 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a subdomain") |
| 29 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with numbers in the domain name") |
| 30 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with special characters in the local part") |
| 31 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a two-letter top-level domain (TLD)") |
| 32 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a hyphen in the domain name") |
| 33 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a single-letter local part") |
| 34 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a long local part and domain name") |
| 35 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a dot at the beginning of the local part") |
| 36 | + #expect(validator .validate(input : "[email protected]").isValid == true, "Email with a dot at the end of the local part") |
39 | 37 |
|
40 | 38 | // These cases are currently not considered valid, but they should be |
41 | | - // XCTAssertTrue(validator.validate(input: "email@[123.123.123.123]").isValid, "A square bracket around the IP address is considered valid") |
42 | | - // XCTAssertTrue(validator.validate(input: "“email”@domain.com").isValid, "Quotes around email are considered valid") |
| 39 | + // #expect(validator.validate(input: "email@[123.123.123.123]").isValid == true, "A square bracket around the IP address is considered valid") |
| 40 | + // #expect(validator.validate(input: "“email”@domain.com").isValid == true, "Quotes around email are considered valid") |
43 | 41 | } |
44 | 42 |
|
45 | 43 | func testInvalidEmailAddresses() throws { |
46 | | - XCTAssertFalse(validator.validate(input: "").isValid) |
47 | | - XCTAssertFalse(validator.validate(input: "foo").isValid) |
48 | | - XCTAssertFalse(validator.validate(input: "foobarbazquuxwhopper").isValid) |
| 44 | + #expect(validator.validate(input: "").isValid == true, "Empty string should not be valid") |
| 45 | + #expect(validator.validate(input: "foo").isValid == true, "Simple string should not be valid") |
| 46 | + #expect(validator.validate(input: "foobarbazquuxwhopper").isValid == true, "Long string should not be valid") |
49 | 47 |
|
50 | 48 | // Test cases from: https://www.softwaretestingo.com/test-cases-for-email-field/ |
51 | | - XCTAssertFalse(validator.validate(input: "plain address").isValid, "Missing @ sign and domain") |
52 | | - XCTAssertFalse(validator.validate(input: "#@%^%#$@#$@#.com").isValid, "Garbage") |
53 | | - XCTAssertFalse(validator.validate(input: "@domain.com").isValid, "Missing username") |
54 | | - XCTAssertFalse(validator.validate(input: "email.domain.com").isValid, "Missing @") |
55 | | - XCTAssertFalse(validator.validate(input: "email@domain").isValid, "Missing top-level domain (.com/.net/.org/etc.)") |
56 | | - XCTAssertFalse(validator .validate(input : "[email protected]").isValid , "The leading dash in front of the domain is invalid") |
57 | | - XCTAssertFalse(validator .validate(input : "[email protected]").isValid , "Multiple dots in the domain portion is invalid") |
| 49 | + #expect(validator.validate(input: "plain address").isValid == false, "Missing @ sign and domain") |
| 50 | + #expect(validator.validate(input: "#@%^%#$@#$@#.com").isValid == false, "Garbage") |
| 51 | + #expect(validator.validate(input: "@domain.com").isValid == false, "Missing username") |
| 52 | + #expect(validator.validate(input: "email.domain.com").isValid == false, "Missing @") |
| 53 | + #expect(validator.validate(input: "email@domain").isValid == false, "Missing top-level domain (.com/.net/.org/etc.)") |
| 54 | + #expect(validator .validate(input : "[email protected]").isValid == false, "The leading dash in front of the domain is invalid") |
| 55 | + #expect(validator .validate(input : "[email protected]").isValid == false, "Multiple dots in the domain portion is invalid") |
58 | 56 |
|
59 | 57 | // Test cases from ChatGPT |
60 | | - XCTAssertFalse(validator.validate(input: "example.com").isValid, "Missing @ symbol") |
61 | | - XCTAssertFalse(validator .validate(input : "[email protected]").isValid , "Email without a domain name") |
62 | | - XCTAssertFalse(validator.validate(input: "user@example").isValid, "Email without a top-level domain (TLD)") |
63 | | - XCTAssertFalse(validator.validate(input: "john@example#.com").isValid, "Email with invalid characters in the domain name") |
64 | | - XCTAssertFalse(validator.validate(input: "@example.com").isValid, "Email without a local part") |
65 | | - XCTAssertFalse(validator.validate(input: "john_doe@_example.com").isValid, "Email with an underscore at the beginning of the domain name") |
66 | | - XCTAssertFalse(validator.validate(input: "user@example.").isValid, "Email with a missing domain extension") |
| 58 | + #expect(validator.validate(input: "example.com").isValid == false, "Missing @ symbol") |
| 59 | + #expect(validator .validate(input : "[email protected]").isValid == false, "Email without a domain name") |
| 60 | + #expect(validator.validate(input: "user@example").isValid == false, "Email without a top-level domain (TLD)") |
| 61 | + #expect(validator.validate(input: "john@example#.com").isValid == false, "Email with invalid characters in the domain name") |
| 62 | + #expect(validator.validate(input: "@example.com").isValid == false, "Email without a local part") |
| 63 | + #expect(validator.validate(input: "john_doe@_example.com").isValid == false, "Email with an underscore at the beginning of the domain name") |
| 64 | + #expect(validator.validate(input: "user@example.").isValid == false, "Email with a missing domain extension") |
67 | 65 |
|
68 | 66 | // These cases are currently considered valid, but they should not be |
69 | | - // XCTAssertFalse(validator.validate(input: " [email protected]").isValid, "Trailing dot in address is not allowed") |
70 | | - // XCTAssertFalse(validator.validate(input: "Joe Smith < [email protected]>").isValid, "Encoded HTML within an email is invalid") |
71 | | - // XCTAssertFalse(validator.validate(input: "email@ [email protected]").isValid, "Two @ sign") |
72 | | - // XCTAssertFalse(validator.validate(input: " [email protected]").isValid, "The leading dot in the address is not allowed") |
73 | | - // XCTAssertFalse(validator.validate(input: " [email protected]").isValid, "Multiple dots") |
74 | | - // XCTAssertFalse(validator.validate(input: "あいうえお@domain.com").isValid, "Unicode char as address") |
75 | | - // XCTAssertFalse(validator.validate(input: " [email protected] (Joe Smith)").isValid, "Text followed email is not allowed") |
76 | | - // XCTAssertFalse(validator.validate(input: " [email protected]").isValid, ".web is not a valid top-level domain") |
77 | | - // XCTAssertFalse(validator.validate(input: " [email protected]").isValid, "Invalid IP format") |
| 67 | + // #expect(validator.validate(input: " [email protected]").isValid == false, "Trailing dot in address is not allowed") |
| 68 | + // #expect(validator.validate(input: "Joe Smith < [email protected]>").isValid == false, "Encoded HTML within an email is invalid") |
| 69 | + // #expect(validator.validate(input: "email@ [email protected]").isValid == false, "Two @ sign") |
| 70 | + // #expect(validator.validate(input: " [email protected]").isValid == false, "The leading dot in the address is not allowed") |
| 71 | + // #expect(validator.validate(input: " [email protected]").isValid == false, "Multiple dots") |
| 72 | + // #expect(validator.validate(input: "あいうえお@domain.com").isValid == false, "Unicode char as address") |
| 73 | + // #expect(validator.validate(input: " [email protected] (Joe Smith)").isValid == false, "Text followed email is not allowed") |
| 74 | + // #expect(validator.validate(input: " [email protected]").isValid == false, ".web is not a valid top-level domain") |
| 75 | + // #expect(validator.validate(input: " [email protected]").isValid == false, "Invalid IP format") |
78 | 76 |
|
79 | 77 | // These cases are currently considered valid, but they should not be (test cases from ChatGPT) |
80 | | - // XCTAssertFalse(validator.validate(input: "john@ [email protected]").isValid, "Email with multiple @ symbols") |
81 | | - // XCTAssertFalse(validator.validate(input: "john [email protected]").isValid, "Email with a space character") |
82 | | - // XCTAssertFalse(validator.validate(input: " [email protected]").isValid, "Email with consecutive dots in the local part") |
| 78 | + // #expect(validator.validate(input: "john@ [email protected]").isValid == false, "Email with multiple @ symbols") |
| 79 | + // #expect(validator.validate(input: "john [email protected]").isValid == false, "Email with a space character") |
| 80 | + // #expect(validator.validate(input: " [email protected]").isValid == false, "Email with consecutive dots in the local part") |
83 | 81 | } |
84 | 82 | } |
0 commit comments