Skip to content

Commit

Permalink
Add tests and incorporate review
Browse files Browse the repository at this point in the history
  • Loading branch information
philippzagar committed Dec 6, 2023
1 parent 7c1f97a commit 55df7f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Spezi/Dependencies/DependencyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct DependencyCollection: DependencyDeclaration {
/// - Parameters:
/// - type: The generic type resulting from the passed closure, has to conform to ``Module``.
/// - singleEntry: Closure returning a dependency conforming to ``Module``, stored within the ``DependencyCollection``.
public init<Dependency: Module>(for type: Dependency.Type = Dependency.self, singleEntry: (() -> Dependency)? = nil) {
public init<Dependency: Module>(for type: Dependency.Type = Dependency.self, singleEntry: @escaping (() -> Dependency)) {
self.init(DependencyContext(for: type, defaultValue: singleEntry))
}

Expand Down
47 changes: 47 additions & 0 deletions Tests/SpeziTests/DependenciesTests/DependencyBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Spezi
import XCTRuntimeAssertions

private protocol ExampleTypeConstraint: Module {}

private final class ExampleDependencyModule: ExampleTypeConstraint {}

@resultBuilder
private enum ExampleDependencyBuilder: DependencyCollectionBuilder {
/// An auto-closure expression, providing the default dependency value, building the ``DependencyCollection``.
public static func buildExpression<L: ExampleTypeConstraint>(_ expression: @escaping @autoclosure () -> L) -> DependencyCollection {

Check failure on line 19 in Tests/SpeziTests/DependenciesTests/DependencyBuilderTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Lower ACL than Parent Violation: Ensure declarations have a lower access control level than their enclosing parent (lower_acl_than_parent)
DependencyCollection(singleEntry: expression)
}
}

private class ExampleModule: Module {
@Dependency var dependencies: [any Module]


public init(

Check failure on line 28 in Tests/SpeziTests/DependenciesTests/DependencyBuilderTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Lower ACL than Parent Violation: Ensure declarations have a lower access control level than their enclosing parent (lower_acl_than_parent)
@ExampleDependencyBuilder _ dependencies: () -> DependencyCollection
) {
self._dependencies = Dependency(dependencies)
}
}

private class ExampleConfiguration {

Check failure on line 35 in Tests/SpeziTests/DependenciesTests/DependencyBuilderTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Convenience Type Violation: Types used for hosting only static members should be implemented as a caseless enum to avoid instantiation (convenience_type)
static let exampleModule = ExampleModule {
ExampleDependencyModule()
}
}


final class DependencyBuilderTests: XCTestCase {
func testDependencyBuilder() throws {
XCTAssertEqual(ExampleConfiguration.exampleModule.dependencies.count, 1)
_ = try XCTUnwrap(ExampleConfiguration.exampleModule.dependencies[0] as? ExampleDependencyModule)
}
}

0 comments on commit 55df7f9

Please sign in to comment.