Skip to content

Commit

Permalink
Make set of found parameter names from a route more easily accessible (
Browse files Browse the repository at this point in the history
…#121)

* Make set of found parameter names from a route more easily accessible

* Cleanup tests
  • Loading branch information
gwynne committed Mar 20, 2023
1 parent 2866ee7 commit 80d9235
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: test
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
on:
pull_request:
push:
branches:
- main
pull_request: { branches: ['*'] }
push: { branches: ['main'] }


env:
LOG_LEVEL: info
SWIFT_DETERMINISTIC_HASHING: 1
Expand All @@ -14,11 +17,10 @@ jobs:
uses: vapor/ci/.github/workflows/run-unit-tests.yml@reusable-workflows
with:
with_coverage: true
with_tsan: true

upstream-check:
runs-on: ubuntu-latest
container: swift:5.7
container: swift:5.7-jammy
steps:
- name: Check out self
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.4
// swift-tools-version:5.5.2
import PackageDescription

let package = Package(
Expand Down
8 changes: 8 additions & 0 deletions Sources/RoutingKit/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public struct Parameters {
private var catchall: Catchall
public let logger: Logger

#if swift(>=5.7) // SE-0346
/// Return a list of all parameter names which were captured. Does not include values listed in the catchall.
public var allNames: some Collection<String> { self.values.keys }
#else
/// Return a list of all parameter names which were captured. Does not include values listed in the catchall.
public var allNames: AnyCollection<String> { .init(self.values.keys) }
#endif

/// Creates a new `Parameters`.
///
/// Pass this into the `Router.route(path:parameters:)` method to fill with values.
Expand Down
22 changes: 9 additions & 13 deletions Tests/RoutingKitTests/RouterPerformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

final class RouterPerformanceTests: XCTestCase {
func testCaseSensitivePerformance() throws {
guard performance(expected: 0.039) else { return }
try performance(expected: 0.039)
let router = TrieRouter(String.self)
for letter in ["a", "b", "c", "d", "e" , "f", "g"] {
router.register(letter, at:[
Expand All @@ -21,7 +21,7 @@ final class RouterPerformanceTests: XCTestCase {
}

func testCaseInsensitivePerformance() throws {
guard performance(expected: 0.050) else { return }
try performance(expected: 0.050)
let router = TrieRouter.init(String.self, options: [.caseInsensitive])
for letter in ["a", "b", "c", "d", "e" , "f", "g"] {
router.register(letter, at: [
Expand All @@ -39,7 +39,7 @@ final class RouterPerformanceTests: XCTestCase {
}

func testCaseInsensitiveRoutingMatchFirstPerformance() throws {
guard performance(expected: 0.062) else { return }
try performance(expected: 0.062)
let router = TrieRouter.init(String.self, options: [.caseInsensitive])
for letter in ["aaaaaaaa", "aaaaaaab", "aaaaaaac", "aaaaaaad", "aaaaaaae" , "aaaaaaaf", "aaaaaaag"] {
router.register(letter, at: [
Expand All @@ -57,7 +57,7 @@ final class RouterPerformanceTests: XCTestCase {
}

func testCaseInsensitiveRoutingMatchLastPerformance() throws {
guard performance(expected: 0.063) else { return }
try performance(expected: 0.063)
let router = TrieRouter.init(String.self, options: [.caseInsensitive])
for letter in ["aaaaaaaa", "aaaaaaab", "aaaaaaac", "aaaaaaad", "aaaaaaae" , "aaaaaaaf", "aaaaaaag"] {
router.register(letter, at: [
Expand All @@ -75,7 +75,7 @@ final class RouterPerformanceTests: XCTestCase {
}

func testMinimalRouterCaseSensitivePerformance() throws {
guard performance(expected: 0.022) else { return }
try performance(expected: 0.022)
let router = TrieRouter.init(String.self, options: [.caseInsensitive])
for letter in ["a"] {
router.register(letter, at: [
Expand All @@ -92,7 +92,7 @@ final class RouterPerformanceTests: XCTestCase {
}

func testMinimalRouterCaseInsensitivePerformance() throws {
guard performance(expected: 0.017) else { return }
try performance(expected: 0.017)
let router = TrieRouter.init(String.self)
for letter in ["a"] {
router.register(letter, at: [
Expand All @@ -110,7 +110,7 @@ final class RouterPerformanceTests: XCTestCase {


func testMinimalEarlyFailPerformance() throws {
guard performance(expected: 0.016) else { return }
try performance(expected: 0.016)
let router = TrieRouter.init(String.self)
for letter in ["aaaaaaaaaaaaaa"] {
router.register(letter, at: [
Expand All @@ -127,11 +127,7 @@ final class RouterPerformanceTests: XCTestCase {
}
}

func performance(expected seconds: Double, name: String = #function) -> Bool {
guard !_isDebugAssertConfiguration() else {
print("[PERFORMANCE] Skipping \(name) in debug build mode")
return false
}
func performance(expected seconds: Double, name: String = #function, file: StaticString = #filePath, line: UInt = #line) throws {
try XCTSkipUnless(!_isDebugAssertConfiguration(), "[PERFORMANCE] Skipping \(name) in debug build mode", file: file, line: line)
print("[PERFORMANCE] \(name) expected: \(seconds) seconds")
return true
}
20 changes: 20 additions & 0 deletions Tests/RoutingKitTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,24 @@ final class RouterTests: XCTestCase {
}
}
}

func testParameterNamesFetch() throws {
let router = TrieRouter<Int>()
router.register(42, at: ["foo", ":bar", ":baz", ":bam"])
router.register(24, at: ["bar", ":bar", "**"])

var params1 = Parameters()
XCTAssertNil(router.route(path: ["foo"], parameters: &params1))
XCTAssertTrue(params1.getCatchall().isEmpty)

var params2 = Parameters()
XCTAssertEqual(router.route(path: ["foo", "a", "b", "c"], parameters: &params2), 42)
XCTAssertEqual(Set(params2.allNames), ["bar", "baz", "bam"]) // Set will compare equal regardless of ordering
XCTAssertTrue(params2.getCatchall().isEmpty)

var params3 = Parameters()
XCTAssertEqual(router.route(path: ["bar", "baz", "bam"], parameters: &params3), 24)
XCTAssertEqual(Set(params3.allNames), ["bar"])
XCTAssertEqual(params3.getCatchall(), ["bam"])
}
}

0 comments on commit 80d9235

Please sign in to comment.