Skip to content

Commit

Permalink
async await init support
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Feb 26, 2022
1 parent 0158351 commit 885f0ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/SwiftSgml/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ open class Tag {
public convenience init(@TagBuilder _ builder: () -> [Tag]) {
self.init(builder())
}

/// initialize a new Tag with children using an async throwing builder
public convenience init(@TagBuilder _ builder: () async throws -> [Tag]) async throws {
self.init(try await builder())
}


/// initialize a new Tag with some contents
public convenience init(_ contents: String?) {
Expand Down
20 changes: 20 additions & 0 deletions Tests/SwiftSgmlTests/TagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@ final class TagTests: XCTestCase {
""")
}

func testConvenienceAsyncTagInit() async throws {

func leaf() async throws -> Leaf {
Leaf("hello")
}

let root = try await Root {
try await leaf()
}
let doc = Document {
root
}

XCTAssertEqual(DocumentRenderer().render(doc), """
<root>
<leaf>hello</leaf>
</root>
""")
}

}

0 comments on commit 885f0ca

Please sign in to comment.