This PR contains the work done to define a `Page` protocol that extracts the HTML scaffolding that `IndexPage` and `ErrorPage` pages duplicated, so every page of the website declares only what makes it unique — its content, title, and assets — while the document structure lives in one place. Also cleans up imports across the workspace. Reviewed-on: rock-n-code/loud-amsterdam#22 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
198 lines
4.3 KiB
Swift
198 lines
4.3 KiB
Swift
import Testing
|
|
|
|
@testable import WebsiteLibrary
|
|
|
|
@Suite("StaticFile enumeration")
|
|
struct StaticFileTests {
|
|
|
|
// MARK: Type aliases
|
|
|
|
typealias File = StaticFile
|
|
typealias FileExtension = StaticFile.Extension
|
|
|
|
// MARK: Computed tests
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.fileExtensions
|
|
))
|
|
func `file extensions`(
|
|
for file: File,
|
|
expects extensions: [FileExtension]
|
|
) {
|
|
#expect(file.fileExtensions == extensions)
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.fileNames
|
|
))
|
|
func `file name`(
|
|
for file: File,
|
|
expects fileName: String
|
|
) {
|
|
#expect(file.fileName == fileName)
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
Self.extensions,
|
|
Self.contentTypes
|
|
))
|
|
func `content type`(
|
|
for fileExtension: FileExtension,
|
|
expects contentType: String
|
|
) {
|
|
#expect(fileExtension.contentType == contentType)
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
Self.extensions,
|
|
Self.subdirectories
|
|
))
|
|
func `subdirectory`(
|
|
for fileExtension: FileExtension,
|
|
expects subdirectory: String?
|
|
) {
|
|
#expect(fileExtension.subdirectory == subdirectory)
|
|
}
|
|
|
|
// MARK: Method tests
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.relativePaths
|
|
))
|
|
func `relative path for`(
|
|
for file: File,
|
|
expects relativePaths: [String]
|
|
) {
|
|
for (fileExtension, relativePath) in zip(file.fileExtensions, relativePaths) {
|
|
#expect(file.relativePath(for: fileExtension) == relativePath)
|
|
}
|
|
}
|
|
|
|
@Test(arguments: zip(
|
|
File.allCases,
|
|
Self.relativePaths
|
|
))
|
|
func `url path for`(
|
|
for file: File,
|
|
expects relativePaths: [String]
|
|
) {
|
|
for (fileExtension, relativePath) in zip(file.fileExtensions, relativePaths) {
|
|
#expect(file.urlPath(for: fileExtension) == "/\(relativePath)")
|
|
}
|
|
}
|
|
|
|
@Test(arguments: [
|
|
"",
|
|
".",
|
|
"Resources/Static"
|
|
])
|
|
func `path relative to`(
|
|
_ basePath: String
|
|
) {
|
|
for file in File.allCases {
|
|
for fileExtension in file.fileExtensions {
|
|
let pathRelativeToBasePath = file.path(
|
|
relativeTo: basePath,
|
|
for: fileExtension
|
|
)
|
|
let relativePath = file.relativePath(for: fileExtension)
|
|
|
|
if basePath.isEmpty {
|
|
#expect(pathRelativeToBasePath == relativePath)
|
|
} else {
|
|
#expect(pathRelativeToBasePath == "\(basePath)/\(relativePath)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: CaseIterable tests
|
|
|
|
@Test
|
|
func `all cases`() {
|
|
#expect(File.allCases.count == 11)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension StaticFileTests {
|
|
|
|
// MARK: Constants
|
|
|
|
static let extensions: [FileExtension] = [
|
|
.css,
|
|
.js,
|
|
.png,
|
|
.ico,
|
|
.svg,
|
|
.txt,
|
|
.webmanifest,
|
|
.xml
|
|
]
|
|
static let contentTypes: [String] = [
|
|
"text/css",
|
|
"text/javascript",
|
|
"image/png",
|
|
"image/vnd.microsoft.icon",
|
|
"image/svg+xml",
|
|
"text/plain",
|
|
"application/manifest+json",
|
|
"application/xml"
|
|
]
|
|
static let subdirectories: [String?] = [
|
|
"css",
|
|
"js",
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil,
|
|
nil
|
|
]
|
|
static let fileExtensions: [[FileExtension]] = [
|
|
[.png],
|
|
[.css, .js],
|
|
[.ico],
|
|
[.svg],
|
|
[.png],
|
|
[.png],
|
|
[.css, .js],
|
|
[.txt],
|
|
[.css, .js],
|
|
[.webmanifest],
|
|
[.xml]
|
|
]
|
|
static let fileNames: [String] = [
|
|
"apple-touch-icon",
|
|
"error",
|
|
"favicon",
|
|
"icon",
|
|
"icon-192",
|
|
"icon-512",
|
|
"index",
|
|
"robots",
|
|
"shared",
|
|
"site",
|
|
"sitemap"
|
|
]
|
|
static let relativePaths: [[String]] = [
|
|
["apple-touch-icon.png"],
|
|
["css/error.css", "js/error.js"],
|
|
["favicon.ico"],
|
|
["icon.svg"],
|
|
["icon-192.png"],
|
|
["icon-512.png"],
|
|
["css/index.css", "js/index.js"],
|
|
["robots.txt"],
|
|
["css/shared.css", "js/shared.js"],
|
|
["site.webmanifest"],
|
|
["sitemap.xml"]
|
|
]
|
|
|
|
}
|