Skip to content

Commit

Permalink
Resolver.Name conformance to Hashable and Equatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Long committed Oct 5, 2021
1 parent a2b83de commit 473a537
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Resolver Changelog

### 1.4.5

* Resolver.Name conformance to Hashable and Equatable

### 1.4.4

* Reduced code size and improved performance
Expand Down
2 changes: 1 addition & 1 deletion Resolver.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Resolver"
s.version = "1.4.4"
s.version = "1.4.5"
s.summary = "An ultralight Dependency Injection / Service Locator framework for Swift on iOS."
s.homepage = "https://github.com/hmlongco/Resolver"
s.license = "MIT"
Expand Down
10 changes: 8 additions & 2 deletions Sources/Resolver/Resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ extension Resolver {
extension Resolver {

/// Internal class used by Resolver for typed name space support.
public struct Name: ExpressibleByStringLiteral {
let rawValue: String
public struct Name: ExpressibleByStringLiteral, Hashable, Equatable {
public let rawValue: String
public init(_ rawValue: String) {
self.rawValue = rawValue
}
Expand All @@ -368,6 +368,12 @@ extension Resolver {
if let string = string { return Name(string) }
return nil
}
static public func == (lhs: Name, rhs: Name) -> Bool {
return lhs.rawValue == rhs.rawValue
}
public func hash(into hasher: inout Hasher) {
hasher.combine(rawValue)
}
}

}
Expand Down
27 changes: 23 additions & 4 deletions Tests/ResolverTests/ResolverNameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ class ResolverNameTests: XCTestCase {

func testResolverNamedStringRegistrations() {

Task.init {

}

resolver.register { AppConfig() }
resolver.register(name: "host1") { r in r.resolve(AppConfig.self).host1 }
resolver.register(name: "host2") { r in r.resolve(AppConfig.self).host2 }
Expand All @@ -164,4 +160,27 @@ class ResolverNameTests: XCTestCase {
XCTAssertNil(host3)
}

func testResolverNameHashableConformance() {

let registrations: [Resolver.Name:XYZNameService] = [
.fred : XYZNameService("Fred"),
.barney : XYZNameService("Barney"),
]

registrations.forEach { (name, service) in
resolver.register(name: name) { service }
}

let fred: XYZNameService? = resolver.optional(name: .fred)
let barney: XYZNameService? = resolver.optional(name: .barney)

// Check all services resolved
XCTAssertNotNil(fred)
XCTAssertNotNil(barney)

// Check correct service factories called
XCTAssert(fred?.name == "Fred")
XCTAssert(barney?.name == "Barney")
}

}

0 comments on commit 473a537

Please sign in to comment.