Added (first version of) sample Hummingbird app. (#4)
This PR contains the work done to: * Implemented a basic `Hummingbird` application in which to integrate the `HummingbirdDocC` library. * Added the *ArgumentParser* package dependency to the `Package.swift` file; * Added a new *sample* target to the `Package.swift` file; * Added library and documentation tasks to the `Makefile` file. Reviewed-on: #4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,116 +0,0 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Hummingbird DocC Middleware open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Hummingbird DocC Middleware project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Hummingbird DocC Middleware project authors
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import Testing
|
||||
|
||||
import struct HummingbirdCore.URI
|
||||
|
||||
@testable import struct DocCMiddleware.CheckURIUseCase
|
||||
|
||||
@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
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Assertions
|
||||
|
||||
private 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.
|
||||
/// - result: An expected result coming out of the use case.
|
||||
func assertURI(
|
||||
_ uriPath: String,
|
||||
expects result: String?
|
||||
) {
|
||||
// GIVEN
|
||||
let uri = URI(uriPath)
|
||||
|
||||
// WHEN
|
||||
let output = useCase(uri)
|
||||
|
||||
// THEN
|
||||
#expect(output == result)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
private extension Input {
|
||||
/// A list of non-encoded URI samples.
|
||||
static let nonEncodedURIs: [String] = ["/", "/some/known/path", "", "/some/../path", "some/other/path"]
|
||||
/// A list of percent-encoded URI samples.
|
||||
static let percentEncodedURIs: [String] = ["%2F", "/some%2Fknown%3Fpath", "%20", "/some/%2E%2E/path", "some%2Fother%3Fpath"]
|
||||
}
|
||||
|
||||
private extension Output {
|
||||
/// A list of expected outputs for the non-encoded URI samples.
|
||||
static let nonEncodedURIs: [String?] = ["/", "/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]
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import enum DocCMiddleware.AssetFile
|
||||
@testable import enum HummingbirdDocC.AssetFile
|
||||
|
||||
@Suite("Asset File", .tags(.enumeration))
|
||||
struct AssetFileTests {
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import enum DocCMiddleware.AssetFolder
|
||||
@testable import enum HummingbirdDocC.AssetFolder
|
||||
|
||||
@Suite("Asset Folder", .tags(.enumeration))
|
||||
struct AssetFolderTests {
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import enum DocCMiddleware.DocumentationFolder
|
||||
@testable import enum HummingbirdDocC.DocumentationFolder
|
||||
|
||||
@Suite("Documentation Type", .tags(.enumeration))
|
||||
struct DocumentationTypeTests {
|
||||
+1
-1
@@ -17,7 +17,7 @@ import struct Hummingbird.HTTPResponse
|
||||
import struct Hummingbird.Request
|
||||
import struct Logging.Logger
|
||||
|
||||
@testable import DocCMiddleware
|
||||
@testable import HummingbirdDocC
|
||||
|
||||
@Suite("Logger Metadata Helpers", .tags(.extension))
|
||||
struct LoggerMetadata_HelpersTests {
|
||||
@@ -0,0 +1,97 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Hummingbird DocC Middleware open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Hummingbird DocC Middleware project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Hummingbird DocC Middleware project authors
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import HummingbirdDocC
|
||||
|
||||
@Suite("String Helpers", .tags(.extension))
|
||||
struct String_HelpersTests {
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
#if swift(>=6.2)
|
||||
@Test(arguments: zip(
|
||||
Input.prefixesToSubtract,
|
||||
Output.prefixesToSubtract
|
||||
))
|
||||
func `subtract`(
|
||||
prefix: String,
|
||||
expects newString: String?
|
||||
) {
|
||||
assertSubtract(
|
||||
string: .sample,
|
||||
prefix: prefix,
|
||||
expects: newString
|
||||
)
|
||||
}
|
||||
#else
|
||||
@Test("subtract", arguments: zip(
|
||||
Input.prefixesToSubtract,
|
||||
Output.prefixesToSubtract
|
||||
))
|
||||
func subtract(
|
||||
prefix: String,
|
||||
expects newString: String?
|
||||
) {
|
||||
assertSubtract(
|
||||
string: .sample,
|
||||
prefix: prefix,
|
||||
expects: newString
|
||||
)
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Assertions
|
||||
|
||||
private extension String_HelpersTests {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Asserts a string subtraction.
|
||||
/// - Parameters:
|
||||
/// - string: A string from where the subtraction will occur.
|
||||
/// - prefix: A prefix to subtract from a string.
|
||||
/// - newString: An expected new string created out of the subtraction, if any.
|
||||
func assertSubtract(
|
||||
string: String,
|
||||
prefix: String,
|
||||
expects newString: String?
|
||||
) {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = string.subtract(prefix)
|
||||
|
||||
// THEN
|
||||
#expect(result == newString)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
private extension Input {
|
||||
/// A list of prefix strings.
|
||||
static let prefixesToSubtract: [String] = ["Some", .sample, "some", "Else"]
|
||||
}
|
||||
|
||||
private extension Output {
|
||||
/// A list of outcomes that are expected from subtracting the prefix substrings out of the sample string.
|
||||
static let prefixesToSubtract: [String?] = ["thing", .empty, nil, nil]
|
||||
}
|
||||
|
||||
private extension String {
|
||||
/// A sample string.
|
||||
static let sample = "Something"
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Hummingbird DocC Middleware open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Hummingbird DocC Middleware project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Hummingbird DocC Middleware project authors
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import struct HummingbirdDocC.Resource
|
||||
|
||||
@Suite("Resource", .tags(.model))
|
||||
struct ResourceTests {
|
||||
|
||||
// MARK: Properties tests
|
||||
|
||||
#if swift(>=6.2)
|
||||
@Test
|
||||
func `archive path`() {
|
||||
assertArchivePath(
|
||||
archiveName: "SomeDocument",
|
||||
expects: "/SomeDocument.doccarchive"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
func `archive reference`() {
|
||||
assertArchiveReference(
|
||||
archiveName: "SomeDocument",
|
||||
expects: "somedocument"
|
||||
)
|
||||
}
|
||||
#else
|
||||
@Test("archive path")
|
||||
func archivePath() {
|
||||
assertArchivePath(
|
||||
archiveName: "SomeDocument",
|
||||
expects: "/SomeDocument.doccarchive"
|
||||
)
|
||||
}
|
||||
|
||||
@Test("archive reference")
|
||||
func archiveReference() {
|
||||
assertArchiveReference(
|
||||
archiveName: "SomeDocument",
|
||||
expects: "somedocument"
|
||||
)
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Assertions
|
||||
|
||||
private extension ResourceTests {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Asserts the `archivePath` computed property of a resource.
|
||||
/// - Parameters:
|
||||
/// - archiveName: A name of the archive the resource belongs to.
|
||||
/// - archivePath: An expected path to a documentation archive related to a given archive name.
|
||||
func assertArchivePath(
|
||||
archiveName: String,
|
||||
expects archivePath: String
|
||||
) {
|
||||
// GIVEN
|
||||
var resource = Resource(
|
||||
archiveName: archiveName,
|
||||
relativePath: .empty
|
||||
)
|
||||
|
||||
// WHEN
|
||||
let result = resource.archivePath
|
||||
|
||||
// THEN
|
||||
#expect(result == archivePath)
|
||||
}
|
||||
|
||||
/// Asserts the `archiveReference` computed property of a resource.
|
||||
/// - Parameters:
|
||||
/// - archiveName: A name of the archive the resource belongs to.
|
||||
/// - archiveReference: An expected reference related to a given archive name.
|
||||
func assertArchiveReference(
|
||||
archiveName: String,
|
||||
expects archiveReference: String
|
||||
) {
|
||||
// GIVEN
|
||||
var resource = Resource(
|
||||
archiveName: archiveName,
|
||||
relativePath: .empty
|
||||
)
|
||||
|
||||
// WHEN
|
||||
let result = resource.archiveReference
|
||||
|
||||
// THEN
|
||||
#expect(result == archiveReference)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Hummingbird DocC Middleware open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Hummingbird DocC Middleware project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Hummingbird DocC Middleware project authors
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import Testing
|
||||
|
||||
import struct HummingbirdCore.URI
|
||||
|
||||
@testable import struct HummingbirdDocC.CheckURIUseCase
|
||||
|
||||
@Suite("Check URI use case", .tags(.useCase))
|
||||
struct CheckURIUseCaseTests {
|
||||
|
||||
// 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
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Assertions
|
||||
|
||||
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.
|
||||
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
|
||||
|
||||
extension Input {
|
||||
/// A list of non-encoded URI samples.
|
||||
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.
|
||||
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",
|
||||
]
|
||||
}
|
||||
|
||||
extension Output {
|
||||
/// A list of expected outputs for the non-encoded URI samples.
|
||||
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.
|
||||
fileprivate static let percentEncodedURIs: [String?] = [
|
||||
.Sample.uriRoot + .Path.forwardSlash,
|
||||
.Sample.uriRoot + "/some/known?path",
|
||||
nil,
|
||||
nil,
|
||||
]
|
||||
}
|
||||
+21
-29
@@ -12,7 +12,9 @@
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import struct DocCMiddleware.PrepareURIPathUseCase
|
||||
import struct HummingbirdDocC.Resource
|
||||
|
||||
@testable import struct HummingbirdDocC.PrepareURIPathUseCase
|
||||
|
||||
@Suite("Prepare URI Path Use Case", .tags(.useCase))
|
||||
struct PrepareURIPathUseCaseTests {
|
||||
@@ -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"
|
||||
}
|
||||
+2
-2
@@ -18,7 +18,7 @@ import struct Hummingbird.HTTPResponse
|
||||
import struct Hummingbird.Request
|
||||
import struct Logging.Logger
|
||||
|
||||
@testable import struct DocCMiddleware.RedirectURIUseCase
|
||||
@testable import struct HummingbirdDocC.RedirectURIUseCase
|
||||
|
||||
@Suite("Redirect URI Use Case", .tags(.useCase))
|
||||
struct RedirectURIUseCaseTests {
|
||||
@@ -107,7 +107,7 @@ private extension RedirectURIUseCaseTests {
|
||||
.contentLength: "0"
|
||||
])
|
||||
|
||||
let events = await logHandler.entries
|
||||
let events = logHandler.entries
|
||||
|
||||
if shouldEventBeLogged(logLevel) {
|
||||
#expect(!events.isEmpty)
|
||||
+2
-2
@@ -18,7 +18,7 @@ import struct Hummingbird.HTTPResponse
|
||||
import struct Hummingbird.Request
|
||||
import struct Logging.Logger
|
||||
|
||||
@testable import struct DocCMiddleware.ServeURIUseCase
|
||||
@testable import struct HummingbirdDocC.ServeURIUseCase
|
||||
|
||||
@Suite("Serve URI Use Case", .tags(.useCase))
|
||||
struct ServeURIUseCaseTests {
|
||||
@@ -190,7 +190,7 @@ private extension ServeURIUseCaseTests {
|
||||
#expect(contentLength == 0)
|
||||
}
|
||||
|
||||
let events = await logHandler.entries
|
||||
let events = logHandler.entries
|
||||
|
||||
if shouldEventBeLogged(
|
||||
logLevel: logLevel,
|
||||
+35
-32
@@ -20,7 +20,8 @@ import struct Hummingbird.LocalFileSystem
|
||||
import struct Hummingbird.Request
|
||||
import struct Logging.Logger
|
||||
|
||||
@testable import struct DocCMiddleware.DocCMiddleware
|
||||
@testable import struct HummingbirdDocC.DocCConfiguration
|
||||
@testable import struct HummingbirdDocC.DocCMiddleware
|
||||
|
||||
@Suite("DocC Middleware", .tags(.middleware))
|
||||
struct DocCMiddlewareTests {
|
||||
@@ -290,21 +291,17 @@ private extension DocCMiddlewareTests {
|
||||
/// - configuration: A type that contains the parameters to configure the middleware.
|
||||
/// - logger: A type that interacts with the logging system.
|
||||
func assertInit(
|
||||
configuration: DocCMiddleware<LocalFileSystem>.Configuration,
|
||||
configuration: DocCConfiguration,
|
||||
logger: Logger = .test()
|
||||
) {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let middleware = DocCMiddleware(
|
||||
let middleware = DocCMiddleware<RequestContextMock, LocalFileSystem>(
|
||||
configuration: configuration,
|
||||
logger: logger
|
||||
)
|
||||
|
||||
// THEN
|
||||
#expect(middleware.configuration.folderRoot == configuration.folderRoot)
|
||||
#expect(middleware.configuration.uriRoot == configuration.uriRoot)
|
||||
#expect(middleware.configuration.threadPool === configuration.threadPool)
|
||||
|
||||
#expect(middleware.logger.label == logger.label)
|
||||
#expect(middleware.logger.logLevel == logger.logLevel)
|
||||
#expect(middleware.logger.metadataProvider == nil)
|
||||
@@ -318,23 +315,19 @@ private extension DocCMiddlewareTests {
|
||||
/// - logger: A type that interacts with the logging system.
|
||||
/// - fileProvider: A type that conforms to the protocol that defines file system interactions, if any.
|
||||
func assertInit<FileSystemProvider: FileProvider>(
|
||||
configuration: DocCMiddleware<FileSystemProvider>.Configuration,
|
||||
configuration: DocCConfiguration,
|
||||
logger: Logger = .test(),
|
||||
fileProvider: FileSystemProvider
|
||||
) {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let middleware = DocCMiddleware(
|
||||
let middleware = DocCMiddleware<RequestContextMock, FileSystemProvider>(
|
||||
configuration: configuration,
|
||||
fileProvider: fileProvider,
|
||||
logger: logger
|
||||
)
|
||||
|
||||
// THEN
|
||||
#expect(middleware.configuration.folderRoot == configuration.folderRoot)
|
||||
#expect(middleware.configuration.uriRoot == configuration.uriRoot)
|
||||
#expect(middleware.configuration.threadPool === configuration.threadPool)
|
||||
|
||||
#expect(middleware.logger.label == logger.label)
|
||||
#expect(middleware.logger.logLevel == logger.logLevel)
|
||||
#expect(middleware.logger.metadataProvider == nil)
|
||||
@@ -362,13 +355,13 @@ private extension DocCMiddlewareTests {
|
||||
handler: logHandler
|
||||
)
|
||||
|
||||
let context: any RequestContext = RequestContextMock(logger: logger)
|
||||
let context: RequestContextMock = .init(logger: logger)
|
||||
let request: Request = .test(
|
||||
method: .get,
|
||||
path: uriPath
|
||||
)
|
||||
|
||||
let middleware = DocCMiddleware(
|
||||
let middleware = DocCMiddleware<RequestContextMock, FileProviderMock>(
|
||||
configuration: .init(
|
||||
uriRoot: .Sample.uriRoot,
|
||||
folderRoot: .Sample.uriFolder
|
||||
@@ -385,7 +378,7 @@ private extension DocCMiddlewareTests {
|
||||
// THEN
|
||||
#expect(result.status == statusCode)
|
||||
|
||||
let events = await logHandler.entries
|
||||
let events = logHandler.entries
|
||||
|
||||
if statusCode == .movedPermanently, let uriRedirect {
|
||||
#expect(result.body.contentLength == 0)
|
||||
@@ -448,13 +441,13 @@ private extension DocCMiddlewareTests {
|
||||
default: .init(fileIdentifier: .init(), shouldLoadFile: false)
|
||||
}
|
||||
|
||||
let context: any RequestContext = RequestContextMock(logger: logger)
|
||||
let context: RequestContextMock = .init(logger: logger)
|
||||
let request: Request = .test(
|
||||
method: .get,
|
||||
path: uriPath
|
||||
)
|
||||
|
||||
let middleware = DocCMiddleware(
|
||||
let middleware = DocCMiddleware<RequestContextMock, FileProviderMock>(
|
||||
configuration: .init(
|
||||
uriRoot: .Sample.uriRoot,
|
||||
folderRoot: .Sample.uriFolder
|
||||
@@ -483,7 +476,7 @@ private extension DocCMiddlewareTests {
|
||||
#expect(contentLength == 0)
|
||||
}
|
||||
|
||||
let events = await logHandler.entries
|
||||
let events = logHandler.entries
|
||||
|
||||
if shouldEventBeLogged(
|
||||
logLevel: logLevel,
|
||||
@@ -552,7 +545,12 @@ private extension DocCMiddlewareTests {
|
||||
|
||||
private extension Input {
|
||||
/// A list of relative URI paths to match against the URI path redirections done by the middleware.
|
||||
static let redirectURIPaths: [String] = [.empty, .Path.forwardSlash, "/documentation", "/tutorials"]
|
||||
static let redirectURIPaths: [String] = [
|
||||
.empty,
|
||||
.Path.forwardSlash,
|
||||
"/documentation",
|
||||
"/tutorials"
|
||||
]
|
||||
/// A list of relative URI paths to match against the URI path servings done by the middleware.
|
||||
static let serveURIPaths: [String] = [
|
||||
"/documentation/",
|
||||
@@ -574,22 +572,27 @@ private extension Input {
|
||||
|
||||
private extension Output {
|
||||
/// A list of expected relative URI path redirections outputs coming out of the URI path redirections done by the middleware.
|
||||
static let redirectURIPaths: [String] = [.Path.forwardSlash, "/documentation", "/documentation/", "/tutorials/"]
|
||||
static let redirectURIPaths: [String] = [
|
||||
"/documentation",
|
||||
"/documentation",
|
||||
"/documentation/",
|
||||
"/tutorials/"
|
||||
]
|
||||
/// A list of expected relative file URI paths of the logged messages coming out of the URI path servings done by the middleware.
|
||||
static let serveURIFilePaths: [String] = [
|
||||
"/SomeDocument.doccarchive/documentation/somedocument/index.html",
|
||||
"/SomeDocument.doccarchive/tutorials/somedocument/index.html",
|
||||
"/SomeDocument.doccarchive/data/documentation/somedocument.json",
|
||||
"/SomeDocument.doccarchive/SomeDocument/favicon.ico",
|
||||
"/SomeDocument.doccarchive/SomeDocument/favicon.svg",
|
||||
"/SomeDocument.doccarchive/SomeDocument/theme-settings.json",
|
||||
"/SomeDocument.doccarchive/SomeDocument/css/file.css",
|
||||
"/SomeDocument.doccarchive/SomeDocument/data/data.bin",
|
||||
"/SomeDocument.doccarchive/SomeDocument/downloads/file.txt",
|
||||
"/SomeDocument.doccarchive/SomeDocument/images/image.png",
|
||||
"/SomeDocument.doccarchive/SomeDocument/img/image.jpg",
|
||||
"/SomeDocument.doccarchive/SomeDocument/index/file",
|
||||
"/SomeDocument.doccarchive/SomeDocument/js/file.js",
|
||||
"/SomeDocument.doccarchive/SomeDocument/videos/video.mp4"
|
||||
"/SomeDocument.doccarchive/favicon.ico",
|
||||
"/SomeDocument.doccarchive/favicon.svg",
|
||||
"/SomeDocument.doccarchive/theme-settings.json",
|
||||
"/SomeDocument.doccarchive/css/file.css",
|
||||
"/SomeDocument.doccarchive/data/data.bin",
|
||||
"/SomeDocument.doccarchive/downloads/file.txt",
|
||||
"/SomeDocument.doccarchive/images/image.png",
|
||||
"/SomeDocument.doccarchive/img/image.jpg",
|
||||
"/SomeDocument.doccarchive/index/file",
|
||||
"/SomeDocument.doccarchive/js/file.js",
|
||||
"/SomeDocument.doccarchive/videos/video.mp4"
|
||||
]
|
||||
}
|
||||
+1
-1
@@ -53,5 +53,5 @@ extension Logger {
|
||||
|
||||
private extension String {
|
||||
/// A label to assign to a test logger instance.
|
||||
static let loggerLabel = "test.hummingbird-docc-middleware.logger"
|
||||
static let loggerLabel = "test.hummingbird-docc.logger"
|
||||
}
|
||||
+2
@@ -22,6 +22,8 @@ extension Tag {
|
||||
@Tag static var `extension`: Self
|
||||
/// Tag that indicate a test case for a middleware type.
|
||||
@Tag static var middleware: Self
|
||||
/// Tag that indicate a test case for a model type.
|
||||
@Tag static var model: Self
|
||||
/// Tag that indicate a test case for a use case type.
|
||||
@Tag static var useCase: Self
|
||||
|
||||
+27
-15
@@ -33,9 +33,7 @@ struct LogHandlerMock {
|
||||
// MARK: Computed
|
||||
|
||||
/// A list of all the logged events that are being persisted in the recorder.
|
||||
var entries: [LogEntry] {
|
||||
get async { await recorder.entries }
|
||||
}
|
||||
var entries: [LogEntry] { recorder.entries }
|
||||
|
||||
}
|
||||
|
||||
@@ -63,13 +61,25 @@ struct LogEntry: Equatable {
|
||||
// MARK: - LogRecorder
|
||||
|
||||
extension LogHandlerMock {
|
||||
/// An actor that persists all the events logged by the ``LogHandlerMock`` mock handler.
|
||||
actor LogRecorder {
|
||||
/// A class that records all the events logged by the ``LogHandlerMock`` mock handler.
|
||||
///
|
||||
/// This class conforms to the `Sendable` protocol by using the `@unchecked` modifier because a `NSLock`type is used to handle the access to the logged events in a thread-safe way.
|
||||
final class LogRecorder: @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A list of all the logged events persisted in a thread-safe way.
|
||||
private(set) var _entries: [LogEntry] = []
|
||||
|
||||
/// A type that coordinates the access to the persisted logged events in a thread-safe way.
|
||||
private let lock: NSLock = .init()
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// A list of all the logged events.
|
||||
private(set) var entries: [LogEntry] = []
|
||||
var entries: [LogEntry] {
|
||||
lock.withLock { _entries }
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@@ -84,13 +94,15 @@ extension LogHandlerMock {
|
||||
metadata: Logger.Metadata?,
|
||||
message: Logger.Message,
|
||||
source: String
|
||||
) async {
|
||||
entries += [.init(
|
||||
level: level,
|
||||
metadata: metadata,
|
||||
message: message,
|
||||
source: source
|
||||
)]
|
||||
) {
|
||||
lock.withLock {
|
||||
_entries += [.init(
|
||||
level: level,
|
||||
metadata: metadata,
|
||||
message: message,
|
||||
source: source
|
||||
)]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -130,12 +142,12 @@ extension LogHandlerMock: LogHandler {
|
||||
function: String,
|
||||
line: UInt
|
||||
) {
|
||||
Task { await recorder.record(
|
||||
recorder.record(
|
||||
level: level,
|
||||
metadata: metadata,
|
||||
message: message,
|
||||
source: source
|
||||
)}
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user