Skip to content

Commit

Permalink
test URLComponentsParser with convenience parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineDominion committed Dec 2, 2023
1 parent 4e430d7 commit 7379b32
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AppKit

extension NSAppleEventDescriptor {
@usableFromInline
var urlComponents: URLComponents? {
guard let urlString = self.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue
else { return nil }
Expand Down
22 changes: 22 additions & 0 deletions Sources/URLSchemer/Parsers/URLComponentsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ extension URLComponents {
}
}

extension URLComponents {
public func parse<Parsers: ActionParser, Output: Action>(
@OneOfBuilder<StringAction, Output> _ build: () -> Parsers
) rethrows -> Output
where Parsers.Input == StringAction, Parsers.Output == Output
{
return try Self.parse(self, build)
}

public static func parse<Parsers: ActionParser, Output: Action>(
_ input: URLComponents,
@OneOfBuilder<StringAction, Output> _ build: () -> Parsers
) rethrows -> Output
where Parsers.Input == StringAction, Parsers.Output == Output
{
let combined = parser.flatMap {
OneOf(build)
}
return try combined.parse(input)
}
}

public struct URLComponentsParser: ActionParser {
@inlinable
public init() { }
Expand Down
2 changes: 1 addition & 1 deletion Tests/URLSchemerTests/AppControlParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

final class AppControlParserTests: XCTestCase {
func testTerminate() throws {
let action = try URLComponents.parser.flatMap {
let action = try URLComponentsParser().flatMap {
AppControlParser()
}.parse(XCTUnwrap(URLComponents(string: "example://app/control/terminate")))

Expand Down
6 changes: 0 additions & 6 deletions Tests/URLSchemerTests/FlatMapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ final class FlatMapTests: XCTestCase {
}
}

struct PassthroughParser<Input: Action>: ActionParser {
func parse(_ input: Input) throws -> some Action {
input
}
}

func testSuccess() {
let result = Just(ActionStub(subject: "apple"))
.flatMap {
Expand Down
7 changes: 7 additions & 0 deletions Tests/URLSchemerTests/Helpers/Passthrough.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import URLSchemer

struct Passthrough<Input: Action>: ActionParser {
func parse(_ input: Input) throws -> Input {
input
}
}
17 changes: 17 additions & 0 deletions Tests/URLSchemerTests/URLComponentsParserTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import URLSchemer
import XCTest

final class URLComponentsParserTests: XCTestCase {
func testParse() throws {
let components = try XCTUnwrap(URLComponents(string: "example://module/subject/verb"))
XCTAssertEqual(try URLComponentsParser().parse(components), StringAction(module: "module", subject: "subject", verb: "verb"))
}

func testParseWithFlatMap() throws {
let components = try XCTUnwrap(URLComponents(string: "myapp://amazing/action/execute/withpower?intensity=9000"))
let result = try components.parse {
Passthrough<StringAction>()
}
XCTAssertEqual(result, StringAction(module: "amazing", subject: "action", verb: "execute", object: "withpower", payload: ["intensity":"9000"]))
}
}

0 comments on commit 7379b32

Please sign in to comment.