-
Notifications
You must be signed in to change notification settings - Fork 1
Update tests to use Swift Testing #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbernson
wants to merge
2
commits into
main
Choose a base branch
from
swift-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| import XCTest | ||
| import Testing | ||
| import ValidationKit | ||
|
|
||
| class AcceptedTests: XCTestCase { | ||
| func testAccepted() { | ||
| @Suite("Accepted Validator Tests") | ||
| struct AcceptedTests { | ||
| @Test func testAccepted() { | ||
| let validator = Validator<Bool, Bool>.accepted | ||
|
|
||
| XCTAssertFalse(validator.validate(input: false).isValid) | ||
| XCTAssertTrue(validator.validate(input: true).isValid) | ||
| #expect(validator.validate(input: false).isValid == false, "False value should not be accepted") | ||
| #expect(validator.validate(input: true).isValid == true, "True value should be accepted") | ||
|
|
||
| XCTAssertEqual(validator.validate(input: true).value, true) | ||
| #expect(validator.validate(input: true).value == true, "Accepted value should return true") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| import XCTest | ||
| import Testing | ||
| import Foundation | ||
| import ValidationKit | ||
|
|
||
| class DateTests: XCTestCase { | ||
| func testIsDate() { | ||
| @Suite("Date Validator Tests") | ||
| struct DateTests { | ||
| @Test("Date validation with custom formatter") | ||
| func isDate() { | ||
| let dateFormatter = DateFormatter() | ||
| dateFormatter.dateFormat = "YYYY-MM-DD HH:mm:ss" | ||
|
|
||
| let validator = Validator<String, Date>.isDate(formatter: dateFormatter) | ||
|
|
||
| // Valid | ||
| XCTAssertTrue(validator.validate(input: "2021-03-28 12:11:25").isValid) | ||
| // Valid date format | ||
| #expect(validator.validate(input: "2021-03-28 12:11:25").isValid, "Valid date string should pass validation") | ||
|
|
||
| // Invalid | ||
| XCTAssertFalse(validator.validate(input: "2021-03-28").isValid) | ||
| XCTAssertFalse(validator.validate(input: "12:11:25").isValid) | ||
| XCTAssertFalse(validator.validate(input: "test").isValid) | ||
| // Invalid date formats | ||
| #expect(validator.validate(input: "2021-03-28").isValid == false, "Date without time should be invalid") | ||
| #expect(validator.validate(input: "12:11:25").isValid == false, "Time without date should be invalid") | ||
| #expect(validator.validate(input: "test").isValid == false, "Non-date string should be invalid") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,84 +1,82 @@ | ||||||
| import XCTest | ||||||
| import Testing | ||||||
| import ValidationKit | ||||||
|
|
||||||
| class EmailTests: XCTestCase { | ||||||
| var validator: Validator<String, String>! | ||||||
| @Suite("Email Validator Tests") | ||||||
| struct EmailTests { | ||||||
| let validator = Validator<String, String>.email | ||||||
|
|
||||||
| override func setUpWithError() throws { | ||||||
| validator = .email | ||||||
| } | ||||||
|
|
||||||
| func testValidEmailAddresses() throws { | ||||||
| XCTAssertTrue(validator.validate(input: "mathijsb+test@q42.nl").isValid) | ||||||
| XCTAssertTrue(validator.validate(input: "mathijsb@subdomain.q42.nl").isValid) | ||||||
| @Test("Valid email addresses should pass validation") | ||||||
| func validEmailAddresses() throws { | ||||||
| #expect(validator.validate(input: "mathijsb+test@q42.nl").isValid == true, "Email with plus sign should be valid") | ||||||
| #expect(validator.validate(input: "mathijsb@subdomain.q42.nl").isValid == true, "Email with subdomain should be valid") | ||||||
|
|
||||||
| // Test cases from: https://www.softwaretestingo.com/test-cases-for-email-field/ | ||||||
| XCTAssertTrue(validator.validate(input: "email@domain.com").isValid, "Valid email") | ||||||
| XCTAssertTrue(validator.validate(input: "firstname.lastname@domain.com").isValid, "The email contains a dot in the address field") | ||||||
| XCTAssertTrue(validator.validate(input: "email@subdomain.domain.com").isValid, "The email contains a dot with a subdomain") | ||||||
| XCTAssertTrue(validator.validate(input: "firstname+lastname@domain.com").isValid, "Plus sign is considered a valid character") | ||||||
| XCTAssertTrue(validator.validate(input: "email@123.123.123.123").isValid, "The domain is a valid IP address") | ||||||
| XCTAssertTrue(validator.validate(input: "1234567890@domain.com").isValid, "Digits in the address are valid") | ||||||
| XCTAssertTrue(validator.validate(input: "email@domain-one.com").isValid, "Dash in the domain name is valid") | ||||||
| XCTAssertTrue(validator.validate(input: "_______@domain.com").isValid, "Underscore in the address field is valid") | ||||||
| XCTAssertTrue(validator.validate(input: "email@domain.name").isValid, ".name is a valid Top Level Domain name") | ||||||
| XCTAssertTrue(validator.validate(input: "email@domain.co.jp").isValid, "Dot in Top Level Domain name also considered valid (use co.jp as an example here)") | ||||||
| XCTAssertTrue(validator.validate(input: "firstname-lastname@domain.com").isValid, "Dash in the address field is valid") | ||||||
| #expect(validator.validate(input: "email@domain.com").isValid == true, "Valid email") | ||||||
| #expect(validator.validate(input: "firstname.lastname@domain.com").isValid == true, "The email contains a dot in the address field") | ||||||
| #expect(validator.validate(input: "email@subdomain.domain.com").isValid == true, "The email contains a dot with a subdomain") | ||||||
| #expect(validator.validate(input: "firstname+lastname@domain.com").isValid == true, "Plus sign is considered a valid character") | ||||||
| #expect(validator.validate(input: "email@123.123.123.123").isValid == true, "The domain is a valid IP address") | ||||||
| #expect(validator.validate(input: "1234567890@domain.com").isValid == true, "Digits in the address are valid") | ||||||
| #expect(validator.validate(input: "email@domain-one.com").isValid == true, "Dash in the domain name is valid") | ||||||
| #expect(validator.validate(input: "_______@domain.com").isValid == true, "Underscore in the address field is valid") | ||||||
| #expect(validator.validate(input: "email@domain.name").isValid == true, ".name is a valid Top Level Domain name") | ||||||
| #expect(validator.validate(input: "email@domain.co.jp").isValid == true, "Dot in Top Level Domain name also considered valid (use co.jp as an example here)") | ||||||
| #expect(validator.validate(input: "firstname-lastname@domain.com").isValid == true, "Dash in the address field is valid") | ||||||
|
|
||||||
| // Test cases from ChatGPT | ||||||
| XCTAssertTrue(validator.validate(input: "example@example.com").isValid, "Standard email format") | ||||||
| XCTAssertTrue(validator.validate(input: "john.doe@example.co.uk").isValid, "Email with a subdomain") | ||||||
| XCTAssertTrue(validator.validate(input: "user123@example123.com").isValid, "Email with numbers in the domain name") | ||||||
| XCTAssertTrue(validator.validate(input: "john_doe+test@example.com").isValid, "Email with special characters in the local part") | ||||||
| XCTAssertTrue(validator.validate(input: "user@example.io").isValid, "Email with a two-letter top-level domain (TLD)") | ||||||
| XCTAssertTrue(validator.validate(input: "test-email@example-domain.com").isValid, "Email with a hyphen in the domain name") | ||||||
| XCTAssertTrue(validator.validate(input: "a@example.com").isValid, "Email with a single-letter local part") | ||||||
| XCTAssertTrue(validator.validate(input: "thisisaverylongemailaddresswithlotsofcharacters@example.com").isValid, "Email with a long local part and domain name") | ||||||
| XCTAssertTrue(validator.validate(input: ".test@example.com").isValid, "Email with a dot at the beginning of the local part") | ||||||
| XCTAssertTrue(validator.validate(input: "test.@example.com").isValid, "Email with a dot at the end of the local part") | ||||||
| #expect(validator.validate(input: "example@example.com").isValid == true, "Standard email format") | ||||||
| #expect(validator.validate(input: "john.doe@example.co.uk").isValid == true, "Email with a subdomain") | ||||||
| #expect(validator.validate(input: "user123@example123.com").isValid == true, "Email with numbers in the domain name") | ||||||
| #expect(validator.validate(input: "john_doe+test@example.com").isValid == true, "Email with special characters in the local part") | ||||||
| #expect(validator.validate(input: "user@example.io").isValid == true, "Email with a two-letter top-level domain (TLD)") | ||||||
| #expect(validator.validate(input: "test-email@example-domain.com").isValid == true, "Email with a hyphen in the domain name") | ||||||
| #expect(validator.validate(input: "a@example.com").isValid == true, "Email with a single-letter local part") | ||||||
| #expect(validator.validate(input: "thisisaverylongemailaddresswithlotsofcharacters@example.com").isValid == true, "Email with a long local part and domain name") | ||||||
| #expect(validator.validate(input: ".test@example.com").isValid == true, "Email with a dot at the beginning of the local part") | ||||||
| #expect(validator.validate(input: "test.@example.com").isValid == true, "Email with a dot at the end of the local part") | ||||||
|
|
||||||
| // These cases are currently not considered valid, but they should be | ||||||
| // XCTAssertTrue(validator.validate(input: "email@[123.123.123.123]").isValid, "A square bracket around the IP address is considered valid") | ||||||
| // XCTAssertTrue(validator.validate(input: "“email”@domain.com").isValid, "Quotes around email are considered valid") | ||||||
| // #expect(validator.validate(input: "email@[123.123.123.123]").isValid == true, "A square bracket around the IP address is considered valid") | ||||||
| // #expect(validator.validate(input: "“email”@domain.com").isValid == true, "Quotes around email are considered valid") | ||||||
| } | ||||||
|
|
||||||
| func testInvalidEmailAddresses() throws { | ||||||
| XCTAssertFalse(validator.validate(input: "").isValid) | ||||||
| XCTAssertFalse(validator.validate(input: "foo").isValid) | ||||||
| XCTAssertFalse(validator.validate(input: "foobarbazquuxwhopper").isValid) | ||||||
| #expect(validator.validate(input: "").isValid == true, "Empty string should not be valid") | ||||||
|
||||||
| #expect(validator.validate(input: "").isValid == true, "Empty string should not be valid") | |
| #expect(validator.validate(input: "").isValid == false, "Empty string should not be valid") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using
YYYYandDDindateFormatcan lead to unexpected results—use lowercaseyyyy-MM-dd HH:mm:ssfor calendar year and day-of-month.