Skip to content

Commit 13312ec

Browse files
authored
Add utility methods to work with column registry (#55)
This change introduces two helper methods to reduce boilerplate in defining custom columns.
1 parent 8e48f03 commit 13312ec

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Sources/Benchmark/BenchmarkColumn.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public struct BenchmarkColumn: Hashable {
6969
}
7070
}
7171

72+
/// Create a copy of this column with a different name.
73+
public func renamed(_ newName: String) -> BenchmarkColumn {
74+
return BenchmarkColumn(
75+
name: newName, value: value, alignment: alignment, formatter: formatter)
76+
}
77+
7278
/// Registry that represents a mapping from known column
7379
/// names to their corresponding column values. This
7480
/// registry can be modified to add custom user-defined
@@ -157,6 +163,11 @@ public struct BenchmarkColumn: Hashable {
157163
return result
158164
}()
159165

166+
/// Adds given column to the registry.
167+
public static func register(_ column: BenchmarkColumn) {
168+
registry[column.name] = column
169+
}
170+
160171
public static func == (lhs: Self, rhs: Self) -> Bool {
161172
return lhs.name == rhs.name
162173
}

Tests/BenchmarkTests/BenchmarkColumnTests.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,22 @@ final class BenchmarkColumnTests: XCTestCase {
4747
}
4848
}
4949

50+
func testRegisterColumn() {
51+
BenchmarkColumn.register(
52+
BenchmarkColumn.registry["time"]!.renamed("foobar"))
53+
XCTAssertTrue(BenchmarkColumn.registry["foobar"] != nil)
54+
}
55+
56+
func testRenamed() {
57+
let time = BenchmarkColumn.registry["time"]!
58+
XCTAssertEqual(time.name, "time")
59+
let mytime = time.renamed("mytime")
60+
XCTAssertEqual(mytime.name, "mytime")
61+
}
62+
5063
static var allTests = [
51-
("testKnownColumns", testKnownColumns)
64+
("testKnownColumns", testKnownColumns),
65+
("testRegisterColumn", testRegisterColumn),
66+
("testRenamed", testRenamed),
5267
]
5368
}

0 commit comments

Comments
 (0)