Skip to content

Commit 51dcdbf

Browse files
authored
Views and Controls: add an implementation of Offset
Add a definition for `Offset` and associated tests. This is needed to start fleshing out dependencies for `SplitViewController`.
1 parent 05eb1dd commit 51dcdbf

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright © 2021 Saleem Abdulrasool <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
import class Foundation.Scanner
5+
6+
/// A structure that specifies an amount to offset a position.
7+
public struct Offset {
8+
// MARK - Initializing Offsets
9+
10+
public init() {
11+
self = .zero
12+
}
13+
14+
public init(horizontal: Double, vertical: Double) {
15+
self.horizontal = horizontal
16+
self.vertical = vertical
17+
}
18+
19+
// MARK - Getting the Offset Values
20+
21+
public private(set) var horizontal: Double
22+
23+
public private(set) var vertical: Double
24+
}
25+
26+
extension Offset: Equatable {
27+
public static func ==(_ lhs: Offset, _ rhs: Offset) -> Bool {
28+
return lhs.horizontal == rhs.horizontal && lhs.vertical == rhs.vertical
29+
}
30+
}
31+
extension Offset {
32+
/// Returns a string formatted to contain the data from an offset structure.
33+
public static func string(for offset: Offset) -> String {
34+
return String(format: "{%.17g, %.17g}", offset.horizontal, offset.vertical)
35+
}
36+
37+
/// Returns an `Offset` structure corresponding to the data in a given string.
38+
///
39+
/// In general, you should use this function only to convert strings that were
40+
// previously created using the `string(for:)` function.
41+
public static func offset(for string: String) -> Offset {
42+
let scanner: Scanner = Scanner(string: string)
43+
guard scanner.scanCharacter() == "{",
44+
let horizontal = scanner.scanDouble(),
45+
scanner.scanCharacter() == ",",
46+
let vertical = scanner.scanDouble(),
47+
scanner.scanCharacter() == "}" else {
48+
return .zero
49+
}
50+
return Offset(horizontal: horizontal, vertical: vertical)
51+
}
52+
}
53+
54+
extension Offset {
55+
/// A `Offset` struct whose horizontal and vertical fields are set to the
56+
/// value 0.
57+
public static var zero: Offset {
58+
Offset(horizontal: 0.0, vertical: 0.0)
59+
}
60+
}

Tests/UICoreTests/OffsetTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright © 2021 Saleem Abdulrasool <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
import XCTest
5+
import SwiftWin32
6+
7+
final class OffsetTests: XCTestCase {
8+
func testOffsetConstructor() {
9+
XCTAssertEqual(Offset(), .zero)
10+
11+
let offset: Offset = Offset(horizontal: .infinity, vertical: .infinity)
12+
XCTAssertEqual(offset.horizontal, .infinity)
13+
XCTAssertEqual(offset.vertical, .infinity)
14+
}
15+
16+
func testOffsetStringForOffset() {
17+
XCTAssertEqual(Offset.offset(for: "invalid"), .zero)
18+
XCTAssertEqual(Offset.offset(for: "{"), .zero)
19+
XCTAssertEqual(Offset.offset(for: "{1"), .zero)
20+
XCTAssertEqual(Offset.offset(for: "{1,"), .zero)
21+
XCTAssertEqual(Offset.offset(for: "{1,1"), .zero)
22+
XCTAssertEqual(Offset.offset(for: "{1,1}"), Offset(horizontal: 1, vertical: 1))
23+
}
24+
25+
func testOffsetStringRoundtrip() {
26+
let offset: Offset = Offset(horizontal: 1, vertical: 2)
27+
let string: String = Offset.string(for: offset)
28+
XCTAssertEqual(string, "{1, 2}")
29+
XCTAssertEqual(offset, Offset.offset(for: string))
30+
}
31+
32+
static var allTests = [
33+
("testOffsetConstructor", testOffsetConstructor),
34+
("testOffsetStringForOffset", testOffsetStringForOffset),
35+
("testOffsetStringRoundtrip", testOffsetStringRoundtrip),
36+
]
37+
}

0 commit comments

Comments
 (0)