From 2c3474a1b8b58ae4a9bb85374be6e5c53b115cf4 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Tue, 30 Sep 2025 15:49:53 +0200 Subject: [PATCH] Added the "init(uriRoot:)" initializer function for the CheckURIUseCase use case in the library target and also, improved its test cases. --- .../Internal/Use Cases/CheckURIUseCase.swift | 20 ++- .../Use Cases/CheckURIUseCaseTests.swift | 157 +++++++++++------- 2 files changed, 112 insertions(+), 65 deletions(-) diff --git a/Sources/HummingbirdDocC/Internal/Use Cases/CheckURIUseCase.swift b/Sources/HummingbirdDocC/Internal/Use Cases/CheckURIUseCase.swift index 81ddf5e..6c72f94 100644 --- a/Sources/HummingbirdDocC/Internal/Use Cases/CheckURIUseCase.swift +++ b/Sources/HummingbirdDocC/Internal/Use Cases/CheckURIUseCase.swift @@ -14,6 +14,22 @@ import struct HummingbirdCore.URI /// A use case that checks whether a given URI against a set of conditions, to determine whether the URI could be used by the middleware or not. struct CheckURIUseCase { + + // MARK: Properties + + /// A root path that prefixes the documentation resource. + private let uriRoot: String + + // MARK: Initializers + + /// Initializes this use case. + /// + /// > important: It is assumed that the `uriRoot` parameter is not empty and that it is prefixed by the `/` character. + /// + /// - Parameter uriRoot: A root path that prefixes the documentation resource. + init(uriRoot: String) { + self.uriRoot = uriRoot + } // MARK: Functions @@ -23,8 +39,8 @@ struct CheckURIUseCase { func callAsFunction(_ uri: URI) -> String? { guard let uriPath = uri.path.removingPercentEncoding, - !uriPath.contains(.Path.previousFolder), - uriPath.hasPrefix(.Path.forwardSlash) + uriPath.hasPrefix(uriRoot), + !uriPath.contains(.Path.previousFolder) else { return nil } diff --git a/Tests/HummingbirdDocC/Tests/Internal/Use Cases/CheckURIUseCaseTests.swift b/Tests/HummingbirdDocC/Tests/Internal/Use Cases/CheckURIUseCaseTests.swift index 083697c..8efd8b2 100644 --- a/Tests/HummingbirdDocC/Tests/Internal/Use Cases/CheckURIUseCaseTests.swift +++ b/Tests/HummingbirdDocC/Tests/Internal/Use Cases/CheckURIUseCaseTests.swift @@ -18,99 +18,130 @@ import struct HummingbirdCore.URI @Suite("Check URI use case", .tags(.useCase)) struct CheckURIUseCaseTests { - - // MARK: Properties - - private let useCase: CheckURIUseCase = .init() - + // MARK: Use case tests -#if swift(>=6.2) - @Test(arguments: zip( - Input.nonEncodedURIs, - Output.nonEncodedURIs - )) - func `check non encoded URIs`( - uri uriPath: String, - expects result: String? - ) { - assertURI(uriPath, expects: result) - } - - @Test(arguments: zip( - Input.percentEncodedURIs, - Output.percentEncodedURIs - )) - func `check percent-encoded URIs`( - uri uriPath: String, - expects result: String? - ) { - assertURI(uriPath, expects: result) - } -#else - @Test("check non-encoded URIs", arguments: zip( - Input.nonEncodedURIs, - Output.nonEncodedURIs - )) - func check_nonEncodedURIs( - uri uriPath: String, - expects result: String? - ) { - assertURI(uriPath, expects: result) - } - - @Test("check percent-encoded URIs", arguments: zip( - Input.percentEncodedURIs, - Output.percentEncodedURIs - )) - func check_percentEncodedURIs( - uri uriPath: String, - expects result: String? - ) { - assertURI(uriPath, expects: result) - } -#endif + #if swift(>=6.2) + @Test( + arguments: zip( + Input.nonEncodedURIs, + Output.nonEncodedURIs + ) + ) + func `check non encoded URIs`( + uri uriPath: String, + expects result: String? + ) { + assertURI(uriPath, expects: result) + } + + @Test( + arguments: zip( + Input.percentEncodedURIs, + Output.percentEncodedURIs + ) + ) + func `check percent-encoded URIs`( + uri uriPath: String, + expects result: String? + ) { + assertURI(uriPath, expects: result) + } + #else + @Test( + "check non-encoded URIs", + arguments: zip( + Input.nonEncodedURIs, + Output.nonEncodedURIs + ) + ) + func check_nonEncodedURIs( + uri uriPath: String, + expects result: String? + ) { + assertURI(uriPath, expects: result) + } + + @Test( + "check percent-encoded URIs", + arguments: zip( + Input.percentEncodedURIs, + Output.percentEncodedURIs + ) + ) + func check_percentEncodedURIs( + uri uriPath: String, + expects result: String? + ) { + assertURI(uriPath, expects: result) + } + #endif } // MARK: - Assertions -private extension CheckURIUseCaseTests { - +extension CheckURIUseCaseTests { + // MARK: Functions - + /// Asserts a URI path provided by the ``CheckURIPathUseCase`` use case based on a given path and an expected result. /// - Parameters: /// - uriPath: A URI path to use with a URI type. + /// - uriRoot: A URI path that prefixes the `DocC` documentation resources. /// - result: An expected result coming out of the use case. - func assertURI( + fileprivate func assertURI( _ uriPath: String, + uriRoot: String = .Sample.uriRoot, expects result: String? ) { // GIVEN + let useCase = CheckURIUseCase(uriRoot: uriRoot) let uri = URI(uriPath) - + // WHEN let output = useCase(uri) - + // THEN #expect(output == result) } - + } // MARK: - Constants -private extension Input { +extension Input { /// A list of non-encoded URI samples. - static let nonEncodedURIs: [String] = ["/", "/some/known/path", "", "/some/../path", "some/other/path"] + fileprivate static let nonEncodedURIs: [String] = [ + .Sample.uriRoot + .empty, + .Sample.uriRoot + .Path.forwardSlash, + .Sample.uriRoot + "/some/known/path", + .Sample.uriRoot + "/some/../path", + "some/other/root/some/known/path", + ] /// A list of percent-encoded URI samples. - static let percentEncodedURIs: [String] = ["%2F", "/some%2Fknown%3Fpath", "%20", "/some/%2E%2E/path", "some%2Fother%3Fpath"] + fileprivate static let percentEncodedURIs: [String] = [ + .Sample.uriRoot + "%2F", + .Sample.uriRoot + "/some%2Fknown%3Fpath", + .Sample.uriRoot + "/some/%2E%2E/path", + "some/other%2Froot/some%2Fknown%3Fpath", + ] } -private extension Output { +extension Output { /// A list of expected outputs for the non-encoded URI samples. - static let nonEncodedURIs: [String?] = ["/", "/some/known/path", "/", nil, nil] + fileprivate static let nonEncodedURIs: [String?] = [ + .Sample.uriRoot, + .Sample.uriRoot + .Path.forwardSlash, + .Sample.uriRoot + "/some/known/path", + nil, + nil, + ] /// A list of expected outputs for the percent-encoded URI samples. - static let percentEncodedURIs: [String?] = ["/", "/some/known?path", nil, nil, nil] + fileprivate static let percentEncodedURIs: [String?] = [ + .Sample.uriRoot + .Path.forwardSlash, + .Sample.uriRoot + "/some/known?path", + nil, + nil, + ] }