diff --git a/Sources/Beton/URL/URL+ExpressibleByStringLiteral.swift b/Sources/Beton/URL/URL+ExpressibleByStringLiteral.swift new file mode 100644 index 0000000..b89d926 --- /dev/null +++ b/Sources/Beton/URL/URL+ExpressibleByStringLiteral.swift @@ -0,0 +1,17 @@ +import Foundation + +extension URL: ExpressibleByStringLiteral { + /// A convenience initializer that allows you to express a + /// [`URL`](https://developer.apple.com/documentation/foundation/url) using a string literal. + /// + /// ```swift + /// let assignmentExample: URL = "https://www.21gram.consulting/en-US" + /// let (pageDownloadExample, _) = try await URLSession.shared.data(from: "https://www.21gram.consulting/en-US") + /// ``` + public init(stringLiteral value: String) { + guard let url = URL(string: value) else { + preconditionFailure("Couldn't initialize URL from literal as it resolved to nil.") + } + self = url + } +} diff --git a/Tests/UnitTests/Beton/URL/URLTest+ExpressibleByStringLiteral.swift b/Tests/UnitTests/Beton/URL/URLTest+ExpressibleByStringLiteral.swift new file mode 100644 index 0000000..9b87654 --- /dev/null +++ b/Tests/UnitTests/Beton/URL/URLTest+ExpressibleByStringLiteral.swift @@ -0,0 +1,9 @@ +import Foundation +import XCTest +@testable import Beton + +extension URLTest { + func testInit_stringLiteral() { + XCTAssertEqual("https://google.com", URL(string: "https://google.com")) + } +} diff --git a/Tests/UnitTests/Beton/URL/URLTest.swift b/Tests/UnitTests/Beton/URL/URLTest.swift new file mode 100644 index 0000000..8ac4556 --- /dev/null +++ b/Tests/UnitTests/Beton/URL/URLTest.swift @@ -0,0 +1,6 @@ +import Foundation +import XCTest +@testable import Beton + +class URLTest: XCTestCase { +}