-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add New Extension
MeasurementExtensions
(#936)
* Add New Extension `MeasurementExtensions` Add static initializers to create measurement instances presenting angle in various unit types. * PR Improvements * Update CHANGELOG.md Co-authored-by: Henry <thisisthefoxe@gmail.com> * Update Sources/SwifterSwift/Foundation/MeasurementExtensions.swift Co-authored-by: Guy Kogus <guy.kogus@gmail.com> * Update CHANGELOG.md Co-authored-by: Guy Kogus <guy.kogus@gmail.com> * Update Copyright - Happy New Year! * Update Test Cases Extract a top-level constant for angle value. Update function call for `.degrees(_:)`. * Update Sources/SwifterSwift/Foundation/MeasurementExtensions.swift Co-authored-by: Luciano Almeida <passos.luciano@outlook.com> * PR Improvement Use explicit `return`. Co-authored-by: Henry <thisisthefoxe@gmail.com> Co-authored-by: Guy Kogus <guy.kogus@gmail.com> Co-authored-by: Luciano Almeida <passos.luciano@outlook.com>
- Loading branch information
1 parent
472335c
commit 564cb36
Showing
4 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
52 changes: 52 additions & 0 deletions
52
Sources/SwifterSwift/Foundation/MeasurementExtensions.swift
This file contains 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,52 @@ | ||
// MeasurementExtensions.swift - Copyright 2021 SwifterSwift | ||
|
||
#if canImport(Foundation) | ||
import Foundation | ||
|
||
// MARK: - Methods | ||
@available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) | ||
public extension Measurement where UnitType == UnitAngle { | ||
/// SwifterSwift: Create a `Measurement` for an angle with a specified value in degrees. | ||
/// - Parameter value: The quantity of the angle in degree. | ||
/// - Returns: Measurement for an angle with unit degrees. | ||
static func degrees(_ value: Double) -> Measurement { | ||
return Measurement(value: value, unit: .degrees) | ||
} | ||
|
||
/// SwifterSwift: Create a Measurement for an angle with a specified value in arc minutes. | ||
/// - Parameter value: The quantity of the angle in arc minutes. | ||
/// - Returns: Measurement for an angle with unit arc minutes. | ||
static func arcMinutes(_ value: Double) -> Measurement { | ||
return Measurement(value: value, unit: .arcMinutes) | ||
} | ||
|
||
/// SwifterSwift: Create a Measurement for an angle with a specified value in arc seconds. | ||
/// - Parameter value: The quantity of the angle in arc seconds. | ||
/// - Returns: Measurement for an angle with unit arc seconds. | ||
static func arcSeconds(_ value: Double) -> Measurement { | ||
return Measurement(value: value, unit: .arcSeconds) | ||
} | ||
|
||
/// SwifterSwift: Create a Measurement for an angle with a specified value in radians. | ||
/// - Parameter value: The quantity of the angle in radians. | ||
/// - Returns: Measurement for an angle with unit radians. | ||
static func radians(_ value: Double) -> Measurement { | ||
return Measurement(value: value, unit: .radians) | ||
} | ||
|
||
/// SwifterSwift: Create a Measurement for an angle with a specified value in gradians. | ||
/// - Parameter value: The quantity of the angle in gradians. | ||
/// - Returns: Measurement for an angle with unit gradians. | ||
static func gradians(_ value: Double) -> Measurement { | ||
return Measurement(value: value, unit: .gradians) | ||
} | ||
|
||
/// SwifterSwift: Create a Measurement for an angle with a specified value in revolutions. | ||
/// - Parameter value: The quantity of the angle in revolutions. | ||
/// - Returns: Measurement for an angle with unit revolutions. | ||
static func revolutions(_ value: Double) -> Measurement { | ||
return Measurement(value: value, unit: .revolutions) | ||
} | ||
} | ||
|
||
#endif |
This file contains 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 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,50 @@ | ||
// MeasurementExtensionsTests.swift - Copyright 2021 SwifterSwift | ||
|
||
@testable import SwifterSwift | ||
import XCTest | ||
|
||
#if canImport(Foundation) | ||
import Foundation | ||
|
||
@available(OSX 10.12, tvOS 10.0, watchOS 3.0, *) | ||
class MeasurementExtensionsTests: XCTestCase { | ||
private let angleValue = 2.28 | ||
|
||
func testDegrees() { | ||
let angle = Measurement.degrees(angleValue) | ||
XCTAssertEqual(angle.unit, UnitAngle.degrees) | ||
XCTAssertEqual(angle.value, angleValue) | ||
} | ||
|
||
func testArcMinutes() { | ||
let angle = Measurement.arcMinutes(angleValue) | ||
XCTAssertEqual(angle.unit, UnitAngle.arcMinutes) | ||
XCTAssertEqual(angle.value, angleValue) | ||
} | ||
|
||
func testArcSeconds() { | ||
let angle = Measurement.arcSeconds(angleValue) | ||
XCTAssertEqual(angle.unit, UnitAngle.arcSeconds) | ||
XCTAssertEqual(angle.value, angleValue) | ||
} | ||
|
||
func testRadians() { | ||
let angle = Measurement.radians(angleValue) | ||
XCTAssertEqual(angle.unit, UnitAngle.radians) | ||
XCTAssertEqual(angle.value, angleValue) | ||
} | ||
|
||
func testGradians() { | ||
let angle = Measurement.gradians(angleValue) | ||
XCTAssertEqual(angle.unit, UnitAngle.gradians) | ||
XCTAssertEqual(angle.value, angleValue) | ||
} | ||
|
||
func testRevolutions() { | ||
let angle = Measurement.revolutions(angleValue) | ||
XCTAssertEqual(angle.unit, UnitAngle.revolutions) | ||
XCTAssertEqual(angle.value, angleValue) | ||
} | ||
} | ||
|
||
#endif |