Improved the "callAsFunction(_: )" function for the PrepareURIPathUseCase use case in the library target.

This commit is contained in:
2025-09-30 16:34:48 +02:00
parent 2c3474a1b8
commit 0fea58d295
3 changed files with 82 additions and 90 deletions
@@ -12,6 +12,8 @@
import Testing
import struct HummingbirdDocC.Resource
@testable import struct HummingbirdDocC.PrepareURIPathUseCase
@Suite("Prepare URI Path Use Case", .tags(.useCase))
@@ -26,7 +28,7 @@ struct PrepareURIPathUseCaseTests {
))
func `extract data with URI root not suffixed with forward slash`(
uri uriPath: String,
expects result: PrepareURIPathUseCase.PreparedURIPaths?
expects result: Resource?
) throws {
try assertData(
uriRoot: .uriRoot,
@@ -41,7 +43,7 @@ struct PrepareURIPathUseCaseTests {
))
func `extract data with URI root suffixed with forward slash`(
uri uriPath: String,
expects result: PrepareURIPathUseCase.PreparedURIPaths?
expects result: Resource?
) throws {
try assertData(
uriRoot: .uriRootSlashed,
@@ -56,7 +58,7 @@ struct PrepareURIPathUseCaseTests {
))
func data_withURIRoot_notSuffixed_withForwardSlash(
uri uriPath: String,
expects result: PrepareURIPathUseCase.PreparedURIPaths?
expects result: Resource?
) throws {
try assertData(
uriRoot: .uriRoot,
@@ -71,7 +73,7 @@ struct PrepareURIPathUseCaseTests {
))
func data_withURIRoot_suffixed_withForwardSlash(
uri uriPath: String,
expects result: PrepareURIPathUseCase.PreparedURIPaths?
expects result: Resource?
) throws {
try assertData(
uriRoot: .uriRootSlashed,
@@ -94,30 +96,20 @@ private extension PrepareURIPathUseCaseTests {
/// - Parameters:
/// - uriRoot: A URI path to initialize the use case with.
/// - uriPath: A URI path to use with the use case.
/// - result: An expected result coming out of the use case.
/// - resource: An expected resource coming out of the use case, if any.
func assertData(
uriRoot: String,
uriPath: String,
expects result: PrepareURIPathUseCase.PreparedURIPaths?
expects resource: Resource?
) throws {
// GIVEN
let useCase = PrepareURIPathUseCase(uriRoot: uriRoot)
// WHEN
let output = useCase(uriPath)
let result = useCase(uriPath)
// THEN
if !uriPath.contains(uriRoot) {
#expect(output == nil)
} else {
#expect(output != nil)
let data = try #require(output)
#expect(data.archiveName == result?.archiveName)
#expect(data.archivePath == result?.archivePath)
#expect(data.resourcePath == result?.resourcePath)
}
#expect(result == resource)
}
}
@@ -126,29 +118,29 @@ private extension PrepareURIPathUseCaseTests {
private extension Input {
/// A list of URI paths to match against the root URI path not suffixed with a forward slash.
static let prepareURIPaths: [String] = [.uriOffset, .uriRoot, .uriOther]
static let prepareURIPaths: [String] = [.uriRoot, .uriOffset, .uriOther]
/// A list of URI paths to match against the root URI path suffixed with a forward slash.
static let prepareURIPathsSlashed: [String] = [.uriOffsetSlashed, .uriRootSlashed, .uriOther]
static let prepareURIPathsSlashed: [String] = [.uriRootSlashed, .uriOffsetSlashed, .uriOther]
}
private extension Output {
/// A list of expected outputs for the URI path samples, regardless their match against suffixed or not suffixed root URI paths.
static let prepareURIPaths: [PrepareURIPathUseCase.PreparedURIPaths?] = [
("SomeArchive", "somearchive", "/SomeArchive.doccarchive", "/SomeArchive/some/content/path"),
(.empty, .empty, .empty, .Path.forwardSlash),
static let prepareURIPaths: [Resource?] = [
.init(archiveName: .empty, relativePath: .Path.forwardSlash),
.init(archiveName: "SomeArchive", relativePath: "/some/content/path"),
nil
]
}
private extension String {
/// A root URI path to initialize the use case with.
static let uriRoot: Self = "/some/path"
static let uriRoot = "/some/path"
/// A root URI path suffixed with a forward slash to initialize the use case with.
static let uriRootSlashed: Self = "/some/path/"
static let uriRootSlashed = "/some/path/"
/// A URI path prefixed with a root URI path not suffixed with a forward slash.
static let uriOffset: Self = .uriRoot + "/SomeArchive/some/content/path"
static let uriOffset = .uriRoot + "/SomeArchive/some/content/path"
/// A URI path prefixed with a root URI path suffixed with a forward slash.
static let uriOffsetSlashed: Self = .uriRootSlashed + "SomeArchive/some/content/path"
static let uriOffsetSlashed = .uriRootSlashed + "SomeArchive/some/content/path"
/// A URI path not related to any root URI path.
static let uriOther: Self = "/some/other/path"
static let uriOther = "/some/other/path"
}