Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Replace _functionBuilder with resultBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Feb 10, 2021
1 parent f3e29fb commit 4baef88
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
10 changes: 7 additions & 3 deletions Sources/CommonMark/Builders/BlockBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import Foundation

@_functionBuilder
@resultBuilder
public enum BlockBuilder {
public static func buildBlock(_ values: BlockConvertible...) -> [Block] {
values.flatMap { $0.asBlocks() }
}

public static func buildIf(_ value: BlockConvertible?) -> BlockConvertible {
value ?? []
public static func buildArray(_ components: [BlockConvertible]) -> [Block] {
components.flatMap { $0.asBlocks() }
}

public static func buildOptional(_ component: BlockConvertible?) -> BlockConvertible {
component ?? []
}

public static func buildEither(first: BlockConvertible) -> BlockConvertible {
Expand Down
38 changes: 0 additions & 38 deletions Sources/CommonMark/Builders/ForEach.swift

This file was deleted.

10 changes: 7 additions & 3 deletions Sources/CommonMark/Builders/InlineBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import Foundation

@_functionBuilder
@resultBuilder
public enum InlineBuilder {
public static func buildBlock(_ values: InlineConvertible...) -> [Inline] {
values.flatMap { $0.asInlines() }
}

public static func buildIf(_ value: InlineConvertible?) -> InlineConvertible {
value ?? []
public static func buildArray(_ components: [InlineConvertible]) -> [Inline] {
components.flatMap { $0.asInlines() }
}

public static func buildOptional(_ component: InlineConvertible?) -> InlineConvertible {
component ?? []
}

public static func buildEither(first: InlineConvertible) -> InlineConvertible {
Expand Down
8 changes: 6 additions & 2 deletions Sources/CommonMark/Builders/ItemBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Foundation

@_functionBuilder
@resultBuilder
public enum ItemBuilder {
public static func buildBlock(_ values: ItemConvertible...) -> [Item] {
values.flatMap { $0.asItems() }
}

public static func buildIf(_ value: ItemConvertible?) -> ItemConvertible {
public static func buildArray(_ components: [ItemConvertible]) -> [Item] {
components.flatMap { $0.asItems() }
}

public static func buildOptional(_ value: ItemConvertible?) -> ItemConvertible {
value ?? []
}

Expand Down
7 changes: 0 additions & 7 deletions Sources/CommonMark/Core/Item.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by Guille on 6/2/21.
//

import cmark
import Foundation

Expand Down
107 changes: 107 additions & 0 deletions Tests/CommonMarkTests/BlockBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import CommonMark
import XCTest

final class BlockBuilderTests: XCTestCase {
func testBuildBlock() {
// given
@BlockBuilder func build() -> [Block] {
"Hello"
"world!"
}

// when
let result = build()

// then
XCTAssertEqual(
[
.paragraph([.text("Hello")]),
.paragraph([.text("world!")]),
],
result
)
}

func testBuildArray() {
// given
@BlockBuilder func build() -> [Block] {
for i in 0 ... 3 {
"\(i)"
}
}

// when
let result = build()

// then
XCTAssertEqual(
[
.paragraph([.text("0")]),
.paragraph([.text("1")]),
.paragraph([.text("2")]),
.paragraph([.text("3")]),
],
result
)
}

func testBuildOptional() {
@BlockBuilder func build() -> [Block] {
"Something is:"
if true {
BlockQuote {
"true"
}
}
}

// when
let result = build()

// then
XCTAssertEqual(
[
.paragraph([.text("Something is:")]),
.blockQuote(
[.paragraph([.text("true")])]
),
],
result
)
}

func testBuildEither() {
@BlockBuilder func build(_ value: Bool) -> [Block] {
"Something is:"
if value {
BlockQuote {
"true"
}
} else {
"false"
}
}

// when
let result1 = build(true)
let result2 = build(false)

// then
XCTAssertEqual(
[
.paragraph([.text("Something is:")]),
.blockQuote(
[.paragraph([.text("true")])]
),
],
result1
)
XCTAssertEqual(
[
.paragraph([.text("Something is:")]),
.paragraph([.text("false")]),
],
result2
)
}
}
109 changes: 109 additions & 0 deletions Tests/CommonMarkTests/InlineBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import CommonMark
import XCTest

final class InlineBuilderTests: XCTestCase {
func testBuildBlock() {
// given
@InlineBuilder func build() -> [Inline] {
"Hello "
Strong { "world!" }
}

// when
let result = build()

// then
XCTAssertEqual(
[
.text("Hello "),
.strong(
[.text("world!")]
),
],
result
)
}

func testBuildArray() {
// given
@InlineBuilder func build() -> [Inline] {
for i in 0 ... 3 {
"\(i) "
}
}

// when
let result = build()

// then
XCTAssertEqual(
[
.text("0 "),
.text("1 "),
.text("2 "),
.text("3 "),
],
result
)
}

func testBuildOptional() {
@InlineBuilder func build() -> [Inline] {
"Something is "
if true {
Emphasis {
"true"
}
}
}

// when
let result = build()

// then
XCTAssertEqual(
[
.text("Something is "),
.emphasis(
[.text("true")]
),
],
result
)
}

func testBuildEither() {
@InlineBuilder func build(_ value: Bool) -> [Inline] {
"Something is "
if value {
Emphasis {
"true"
}
} else {
"false"
}
}

// when
let result1 = build(true)
let result2 = build(false)

// then
XCTAssertEqual(
[
.text("Something is "),
.emphasis(
[.text("true")]
),
],
result1
)
XCTAssertEqual(
[
.text("Something is "),
.text("false"),
],
result2
)
}
}
Loading

0 comments on commit 4baef88

Please sign in to comment.