From 885f0cae2272443d7ab4e8f9d049ff2e8010b295 Mon Sep 17 00:00:00 2001 From: Tibor Bodecs Date: Sat, 26 Feb 2022 17:24:51 +0100 Subject: [PATCH] async await init support --- Sources/SwiftSgml/Tag.swift | 6 ++++++ Tests/SwiftSgmlTests/TagTests.swift | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Sources/SwiftSgml/Tag.swift b/Sources/SwiftSgml/Tag.swift index 8cc6f5f..1d49659 100644 --- a/Sources/SwiftSgml/Tag.swift +++ b/Sources/SwiftSgml/Tag.swift @@ -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?) { diff --git a/Tests/SwiftSgmlTests/TagTests.swift b/Tests/SwiftSgmlTests/TagTests.swift index 15e1cb9..36aac7b 100644 --- a/Tests/SwiftSgmlTests/TagTests.swift +++ b/Tests/SwiftSgmlTests/TagTests.swift @@ -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), """ + + hello + + """) } + +}