-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add FXIOS-14689 #31753 β unit test to existing extension to increase code coverage #31756
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
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
196 changes: 196 additions & 0 deletions
196
firefox-ios/firefox-ios-tests/Tests/ClientTests/Extensions/CGRectExtensionsTests.swift
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 |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
|
||
| import XCTest | ||
|
|
||
| @testable import Client | ||
|
|
||
| final class CGRectExtensionsTests: XCTestCase { | ||
| // MARK: - init(width:height:) | ||
|
|
||
| func testInitWithWidthHeight_createsRectAtOrigin() { | ||
| let rect = CGRect(width: 100, height: 50) | ||
|
|
||
| XCTAssertEqual(rect.origin.x, 0) | ||
| XCTAssertEqual(rect.origin.y, 0) | ||
| XCTAssertEqual(rect.size.width, 100) | ||
| XCTAssertEqual(rect.size.height, 50) | ||
| } | ||
|
|
||
| func testInitWithWidthHeight_withZeroValues() { | ||
| let rect = CGRect(width: 0, height: 0) | ||
|
|
||
| XCTAssertEqual(rect, .zero) | ||
| } | ||
|
|
||
| func testInitWithWidthHeight_withNegativeValues() { | ||
| let rect = CGRect(width: -10, height: -20) | ||
|
|
||
| XCTAssertEqual(rect.size.width, -10) | ||
| XCTAssertEqual(rect.size.height, -20) | ||
| } | ||
|
|
||
| // MARK: - init(size:) | ||
|
|
||
| func testInitWithSize_createsRectAtZeroOrigin() { | ||
| let size = CGSize(width: 200, height: 150) | ||
| let rect = CGRect(size: size) | ||
|
|
||
| XCTAssertEqual(rect.origin, .zero) | ||
| XCTAssertEqual(rect.size, size) | ||
| } | ||
|
|
||
| func testInitWithSize_withEmptySize() { | ||
| let rect = CGRect(size: .zero) | ||
|
|
||
| XCTAssertEqual(rect, .zero) | ||
| } | ||
|
|
||
| // MARK: - center (getter) | ||
|
|
||
| func testCenterGetter_returnsCorrectCenter() { | ||
| let rect = CGRect(x: 0, y: 0, width: 100, height: 50) | ||
|
|
||
| let center = rect.center | ||
|
|
||
| XCTAssertEqual(center.x, 50) | ||
| XCTAssertEqual(center.y, 25) | ||
| } | ||
|
|
||
| func testCenterGetter_withNonZeroOrigin() { | ||
| let rect = CGRect(x: 20, y: 30, width: 100, height: 50) | ||
|
|
||
| let center = rect.center | ||
|
|
||
| // Center should be relative to size, not absolute position | ||
| XCTAssertEqual(center.x, 50) | ||
| XCTAssertEqual(center.y, 25) | ||
| } | ||
|
|
||
| func testCenterGetter_withZeroRect() { | ||
| let rect = CGRect.zero | ||
|
|
||
| let center = rect.center | ||
|
|
||
| XCTAssertEqual(center, .zero) | ||
| } | ||
|
|
||
| // MARK: - center (setter) | ||
|
|
||
| func testCenterSetter_updatesOrigin() { | ||
| var rect = CGRect(x: 0, y: 0, width: 100, height: 50) | ||
|
|
||
| rect.center = CGPoint(x: 150, y: 100) | ||
|
|
||
| // Origin should be adjusted so center is at (150, 100) | ||
| XCTAssertEqual(rect.origin.x, 100) // 150 - 50 (half width) | ||
| XCTAssertEqual(rect.origin.y, 75) // 100 - 25 (half height) | ||
| XCTAssertEqual(rect.size.width, 100) | ||
| XCTAssertEqual(rect.size.height, 50) | ||
| } | ||
|
|
||
| func testCenterSetter_preservesSize() { | ||
| var rect = CGRect(x: 0, y: 0, width: 200, height: 100) | ||
| let originalSize = rect.size | ||
|
|
||
| rect.center = CGPoint(x: 50, y: 50) | ||
|
|
||
| XCTAssertEqual(rect.size, originalSize) | ||
| } | ||
|
|
||
| func testCenterSetter_withZeroCenter() { | ||
| var rect = CGRect(x: 100, y: 100, width: 80, height: 60) | ||
|
|
||
| rect.center = .zero | ||
|
|
||
| // Expected 80 / 2 = 40 | ||
| XCTAssertEqual(rect.origin.x, -40) | ||
| // Expected 60 / 2 = 30 | ||
| XCTAssertEqual(rect.origin.y, -30) | ||
| } | ||
|
|
||
| // MARK: - updateWidth(byPercentage:) | ||
|
|
||
| func testUpdateWidth_with100Percent_returnsOriginalWidth() { | ||
| let rect = CGRect(x: 10, y: 20, width: 100, height: 50) | ||
|
|
||
| let updated = rect.updateWidth(byPercentage: 1.0) | ||
|
|
||
| XCTAssertEqual(updated.size.width, 100) | ||
| XCTAssertEqual(updated.origin, rect.origin) | ||
| XCTAssertEqual(updated.size.height, rect.size.height) | ||
| } | ||
|
|
||
| func testUpdateWidth_with50Percent_halvesWidth() { | ||
| let rect = CGRect(x: 10, y: 20, width: 100, height: 50) | ||
|
|
||
| let updated = rect.updateWidth(byPercentage: 0.5) | ||
|
|
||
| XCTAssertEqual(updated.size.width, 50) | ||
| XCTAssertEqual(updated.origin, rect.origin) | ||
| XCTAssertEqual(updated.size.height, rect.size.height) | ||
| } | ||
|
|
||
| func testUpdateWidth_with200Percent_doublesWidth() { | ||
| let rect = CGRect(x: 5, y: 10, width: 50, height: 30) | ||
|
|
||
| let updated = rect.updateWidth(byPercentage: 2.0) | ||
|
|
||
| XCTAssertEqual(updated.size.width, 100) | ||
| XCTAssertEqual(updated.origin, rect.origin) | ||
| XCTAssertEqual(updated.size.height, rect.size.height) | ||
| } | ||
|
|
||
| func testUpdateWidth_withZeroPercent_returnsZeroWidth() { | ||
| let rect = CGRect(x: 0, y: 0, width: 100, height: 50) | ||
|
|
||
| let updated = rect.updateWidth(byPercentage: 0.0) | ||
|
|
||
| XCTAssertEqual(updated.size.width, 0) | ||
| XCTAssertEqual(updated.size.height, rect.size.height) | ||
| } | ||
|
|
||
| func testUpdateWidth_preservesOriginAndHeight() { | ||
| let rect = CGRect(x: 25, y: 35, width: 80, height: 60) | ||
|
|
||
| let updated = rect.updateWidth(byPercentage: 0.75) | ||
|
|
||
| XCTAssertEqual(updated.origin.x, 25) | ||
| XCTAssertEqual(updated.origin.y, 35) | ||
| XCTAssertEqual(updated.size.height, 60) | ||
| // Expected 80 * 0.75 = 60 | ||
| XCTAssertEqual(updated.size.width, 60) | ||
| } | ||
|
|
||
| func testUpdateWidth_withNegativePercentage() { | ||
| let rect = CGRect(x: 0, y: 0, width: 100, height: 50) | ||
|
|
||
| let updated = rect.updateWidth(byPercentage: -0.5) | ||
|
|
||
| XCTAssertEqual(updated.size.width, -50) | ||
| } | ||
|
|
||
| // MARK: - Integration Tests | ||
|
|
||
| func testCombinedOperations_initWithSizeThenUpdateCenter() { | ||
| var rect = CGRect(size: CGSize(width: 100, height: 50)) | ||
| rect.center = CGPoint(x: 200, y: 100) | ||
|
|
||
| XCTAssertEqual(rect.origin.x, 150) | ||
| XCTAssertEqual(rect.origin.y, 75) | ||
| XCTAssertEqual(rect.size.width, 100) | ||
| XCTAssertEqual(rect.size.height, 50) | ||
| } | ||
|
|
||
| func testCombinedOperations_updateWidthThenGetCenter() { | ||
| let rect = CGRect(x: 0, y: 0, width: 100, height: 50) | ||
| let updated = rect.updateWidth(byPercentage: 0.5) | ||
|
|
||
| let center = updated.center | ||
|
|
||
| // Half of new width and height (50) | ||
| XCTAssertEqual(center.x, 25) | ||
| XCTAssertEqual(center.y, 25) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.