Skip to content

Commit

Permalink
Merge pull request #13 from RougeWare/feature/ArrayBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
KyNorthstar authored Jun 12, 2022
2 parents a276f67 + 3aed13c commit 5ee6a70
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
47 changes: 47 additions & 0 deletions Sources/CollectionTools/ArrayBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ import Foundation



/// Build an array with DSL-style syntax
///
/// This can be used to great effect when needing to use logic while building an array, like this:
///
/// ```
/// func buildTabs(@ArrayBuilder _ builder: () -> Tab) -> { builder() }
///
/// self.tabsArray = buildTabs {
/// homeTab
/// searchTab
///
/// if !isGuestMode {
/// profileTab
/// }
///
/// settingsTab
/// }
/// ```
///
/// - Parameter builder: The result-builder block
@resultBuilder
public struct ArrayBuilder<Element> {

Expand Down Expand Up @@ -124,3 +144,30 @@ public struct ArrayBuilder<Element> {
// }
// }
}



public extension Array {

/// Build an array with DSL-style syntax
///
/// This can be used to great effect when needing to use logic while building an array, like this:
///
/// ```
/// self.tabsArray = Array {
/// homeTab
/// searchTab
///
/// if !isGuestMode {
/// profileTab
/// }
///
/// settingsTab
/// }
/// ```
///
/// - Parameter builder: The result-builder block
init(@ArrayBuilder<Element> _ builder: () -> Self) {
self = builder()
}
}
38 changes: 17 additions & 21 deletions Tests/CollectionToolsTests/ArrayBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,37 @@ import CollectionTools



func build<Element>(@ArrayBuilder<Element> _ builder: () -> [Element]) -> [Element] { builder() }



final class ArrayBuilderTests: XCTestCase {

func testEmpty() {
XCTAssertEqual([Int](), build {})
XCTAssertEqual([Int](), .init {})
}


func testSingle() {
XCTAssertEqual([1], build { 1 })
XCTAssertEqual(["1"], build { "1" })
XCTAssertEqual([nil] as [Int?], build { nil as Int? })
XCTAssertEqual([1], .init { 1 })
XCTAssertEqual(["1"], .init { "1" })
XCTAssertEqual([nil] as [Int?], .init { nil as Int? })
}


func testArray() {
XCTAssertEqual([1, 2, 3, 4, 5, 6], build {
XCTAssertEqual([1, 2, 3, 4, 5, 6], .init {
[1, 2, 3, 4, 5, 6]
})
}


func testArrays() {
XCTAssertEqual([1, 2, 3, 4, 5, 6], build {
XCTAssertEqual([1, 2, 3, 4, 5, 6], .init {
[1, 2, 3]
[4, 5, 6]
})
}


func testVariableNumberOfElements() {
XCTAssertEqual([1, 2, 3, 4, 5, 6], build {
XCTAssertEqual([1, 2, 3, 4, 5, 6], .init {
1
2
3
Expand All @@ -53,7 +49,7 @@ final class ArrayBuilderTests: XCTestCase {
6
})

XCTAssertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], build {
XCTAssertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], .init {
1
2
3
Expand Down Expand Up @@ -82,7 +78,7 @@ final class ArrayBuilderTests: XCTestCase {

let three = 3

XCTAssertEqual([1, 3], build {
XCTAssertEqual([1, 3], .init {
if true {
1
}
Expand All @@ -100,7 +96,7 @@ final class ArrayBuilderTests: XCTestCase {


func testLimitedAvailability() {
XCTAssertEqual([1, 4], build {
XCTAssertEqual([1, 4], .init {
if #available(iOS 1, macOS 1, macCatalyst 1, tvOS 1, watchOS 1, Windows 1, OpenBSD 1, *) {
1
}
Expand Down Expand Up @@ -128,7 +124,7 @@ final class ArrayBuilderTests: XCTestCase {
[5, 5, 5, 5, 5],
[6, 6, 6, 6, 6]
],
build {
.init {
[1, 1, 1, 1, 1]
[2, 2, 2, 2, 2]
[3, 3, 3, 3, 3]
Expand Down Expand Up @@ -161,7 +157,7 @@ final class ArrayBuilderTests: XCTestCase {
[19, 19, 19, 19, 19],
[20, 20, 20, 20, 20]
] as [[Int]],
build {
.init {
[1, 1, 1, 1, 1]
[2, 2, 2, 2, 2]
[3, 3, 3, 3, 3]
Expand Down Expand Up @@ -192,7 +188,7 @@ final class ArrayBuilderTests: XCTestCase {
let seven = 7
let eleven = 11

XCTAssertEqual([1, 3, 7, 11], build {
XCTAssertEqual([1, 3, 7, 11], .init {
if true {
1
}
Expand Down Expand Up @@ -234,30 +230,30 @@ final class ArrayBuilderTests: XCTestCase {


func testArrayThenElements() {
XCTAssertEqual([1, 2, 3, 4, 5, 6], build {
XCTAssertEqual([1, 2, 3, 4, 5, 6], .init {
[1, 2, 3]
4
5
6
})

XCTAssertEqual([0, 1, 2, 3, 4, 5, 6], build {
XCTAssertEqual([0, 1, 2, 3, 4, 5, 6], .init {
0
[1, 2, 3]
4
5
6
})

XCTAssertEqual([0, 1, 2, 3, 4, 5, 6], build {
XCTAssertEqual([0, 1, 2, 3, 4, 5, 6], .init {
0
1
[2, 3, 4]
5
6
})

XCTAssertEqual([0, 1, 2, 3, 4, 5, 6, 7, 8], build {
XCTAssertEqual([0, 1, 2, 3, 4, 5, 6, 7, 8], .init {
0
1
2
Expand Down

0 comments on commit 5ee6a70

Please sign in to comment.