Skip to content

Commit

Permalink
Fix String description of the router and add tests (#93)
Browse files Browse the repository at this point in the history
* fix description

* refactor description

* reorder description to match the strategy

* add router description test

* add path component description test

* refactor router description
  • Loading branch information
stevapple authored Apr 21, 2020
1 parent ee590c4 commit 60c2a16
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
32 changes: 17 additions & 15 deletions Sources/RoutingKit/TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,33 @@ extension TrieRouter {
}

var description: String {
self.subpathDescriptions.joined(separator: "\n")
}

var subpathDescriptions: [String] {
var desc: [String] = []
if let (name, parameter) = self.parameter {
for (name, constant) in self.constants {
desc.append("β†’ \(name)")
desc.append(parameter.description.indented())
desc += constant.subpathDescriptions.indented()
}
if let _ = self.catchall {
desc.append("β†’ *")
if let (name, parameter) = self.parameter {
desc.append("β†’ :\(name)")
desc += parameter.subpathDescriptions.indented()
}
if let anything = self.anything {
desc.append("β†’ :")
desc.append(anything.description.indented())
desc.append("β†’ *")
desc += anything.subpathDescriptions.indented()
}
for (name, constant) in self.constants {
desc.append("β†’ \(name)")
desc.append(constant.description.indented())
if let _ = self.catchall {
desc.append("β†’ **")
}
return desc.joined(separator: "\n")
return desc
}
}
}

private extension String {
func indented() -> String {
return self.split(separator: "\n").map { line in
return " " + line
}.joined(separator: "\n")
private extension Array where Element == String {
func indented() -> [String] {
return self.map { " " + $0 }
}
}
44 changes: 43 additions & 1 deletion Tests/RoutingKitTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class RouterTests: XCTestCase {
let router = TrieRouter<Int>()
router.register(42, at: [.constant("path"), .constant("TO"), .constant("fOo")])
var params = Parameters()
XCTAssertEqual(router.route(path: ["PATH", "tO", "FOo"], parameters: &params), nil)
XCTAssertNil(router.route(path: ["PATH", "tO", "FOo"], parameters: &params))
XCTAssertEqual(router.route(path: ["path", "TO", "fOo"], parameters: &params), 42)
}

Expand Down Expand Up @@ -127,4 +127,46 @@ final class RouterTests: XCTestCase {
XCTAssertEqual(router.route(path: ["v1", "test", "foo"], parameters: &params), "b")
XCTAssertEqual(router.route(path: ["v1", "foo"], parameters: &params), "c")
}

func testRouterDescription() throws {
// Use simple routing to eliminate the impact of registration order
let constA: PathComponent = "a"
let constOne: PathComponent = "1"
let paramOne: PathComponent = .parameter("1")
let anything: PathComponent = .anything
let catchall: PathComponent = .catchall
let router = TrieRouter<Int>()
router.register(0, at: [constA, anything])
router.register(1, at: [constA, constOne, catchall])
router.register(2, at: [constA, constOne, anything])
router.register(3, at: [anything, constA, paramOne])
router.register(4, at: [catchall])
// Manually build description
let desc = """
β†’ \(constA)
β†’ \(constOne)
β†’ \(anything)
β†’ \(catchall)
β†’ \(anything)
β†’ \(anything)
β†’ \(constA)
β†’ \(paramOne)
β†’ \(catchall)
"""
XCTAssertEqual(router.description, desc)
}

func testPathComponentDescription() throws {
let paths = [
"aaaaa/bbbb/ccc/dd",
"123/:45/6/789",
"123/**",
"*/*/*/**",
":12/12/*"
]
for path in paths {
XCTAssertEqual(path.pathComponents.string, path)
XCTAssertEqual(("/" + path).pathComponents.string, path)
}
}
}

0 comments on commit 60c2a16

Please sign in to comment.