-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throwing errors #22
Throwing errors #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,8 +55,8 @@ class ComponentScopeTests: XCTestCase { | |
container.register { ServiceImp1() as Service } | ||
|
||
//when | ||
let service1 = container.resolve() as Service | ||
let service2 = container.resolve() as Service | ||
let service1 = try! container.resolve() as Service | ||
let service2 = try! container.resolve() as Service | ||
|
||
//then | ||
XCTAssertFalse((service1 as! ServiceImp1) === (service2 as! ServiceImp1)) | ||
|
@@ -67,8 +67,8 @@ class ComponentScopeTests: XCTestCase { | |
container.register(.Singleton) { ServiceImp1() as Service } | ||
|
||
//when | ||
let service1 = container.resolve() as Service | ||
let service2 = container.resolve() as Service | ||
let service1 = try! container.resolve() as Service | ||
let service2 = try! container.resolve() as Service | ||
|
||
//then | ||
XCTAssertTrue((service1 as! ServiceImp1) === (service2 as! ServiceImp1)) | ||
|
@@ -90,14 +90,14 @@ class ComponentScopeTests: XCTestCase { | |
|
||
func testThatItReusesInstanceInObjectGraphScopeDuringResolve() { | ||
//given | ||
container.register(.ObjectGraph) { [unowned container] in Client(server: container.resolve()) as Client } | ||
container.register(.ObjectGraph) { [unowned container] in Client(server: try! container.resolve()) as Client } | ||
|
||
container.register(.ObjectGraph) { Server() as Server }.resolveDependencies { container, server in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR I know, but now that I read that again I'm wondering: shouldn't we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, cause here we don't capture container from outer context, we pass it as an argument at runtime. So the reference should be released as soon as block returns. I suppose... Speaking of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Don't we? I mean, (If the block were not stored as a strong property by the container but used right away, we would be able to mark it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've created separate issue #23 to discuss that. Will probably add some unit test for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
server.client = container.resolve() as Client | ||
server.client = try! container.resolve() as Client | ||
} | ||
|
||
//when | ||
let client = container.resolve() as Client | ||
let client = try! container.resolve() as Client | ||
|
||
//then | ||
let server = client.server | ||
|
@@ -106,16 +106,16 @@ class ComponentScopeTests: XCTestCase { | |
|
||
func testThatItDoesNotReuseInstanceInObjectGraphScopeInNextResolve() { | ||
//given | ||
container.register(.ObjectGraph) { [unowned container] in Client(server: container.resolve()) as Client } | ||
container.register(.ObjectGraph) { [unowned container] in Client(server: try! container.resolve()) as Client } | ||
container.register(.ObjectGraph) { Server() as Server }.resolveDependencies { container, server in | ||
server.client = container.resolve() as Client | ||
server.client = try! container.resolve() as Client | ||
} | ||
|
||
//when | ||
let client = container.resolve() as Client | ||
let client = try! container.resolve() as Client | ||
let server = client.server | ||
|
||
let anotherClient = container.resolve() as Client | ||
let anotherClient = try! container.resolve() as Client | ||
let anotherServer = anotherClient.server | ||
|
||
//then | ||
|
@@ -127,12 +127,12 @@ class ComponentScopeTests: XCTestCase { | |
//given | ||
var service2: Service? | ||
container.register(.ObjectGraph) { ServiceImp1() as Service }.resolveDependencies { (c, _) in | ||
service2 = c.resolve(tag: "service") as Service | ||
service2 = try! c.resolve(tag: "service") as Service | ||
} | ||
container.register(tag: "service", .ObjectGraph) { ServiceImp2() as Service} | ||
|
||
//when | ||
let service1 = container.resolve(tag: "tag") as Service | ||
let service1 = try! container.resolve(tag: "tag") as Service | ||
|
||
//then | ||
XCTAssertTrue(service1 is ServiceImp1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// Dip | ||
// | ||
// Copyright (c) 2015 Olivier Halligon <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import XCTest | ||
@testable import Dip | ||
|
||
class DefinitionTests: XCTestCase { | ||
|
||
typealias F1 = ()->Service | ||
typealias F2 = (String)->Service | ||
|
||
let tag1 = DependencyContainer.Tag.String("tag1") | ||
let tag2 = DependencyContainer.Tag.String("tag2") | ||
|
||
func testThatDefinitionKeyIsEqualBy_Type_Factory_Tag() { | ||
let equalKey1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag1) | ||
let equalKey2 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag1) | ||
|
||
XCTAssertEqual(equalKey1, equalKey2) | ||
XCTAssertEqual(equalKey1.hashValue, equalKey2.hashValue) | ||
} | ||
|
||
func testThatDefinitionKeysWithDifferentTypesAreNotEqual() { | ||
let keyWithDifferentType1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: nil) | ||
let keyWithDifferentType2 = DefinitionKey(protocolType: AnyObject.self, factoryType: F1.self, associatedTag: nil) | ||
|
||
XCTAssertNotEqual(keyWithDifferentType1, keyWithDifferentType2) | ||
XCTAssertNotEqual(keyWithDifferentType1.hashValue, keyWithDifferentType2.hashValue) | ||
} | ||
|
||
func testThatDefinitionKeysWithDifferentFactoriesAreNotEqual() { | ||
let keyWithDifferentFactory1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: nil) | ||
let keyWithDifferentFactory2 = DefinitionKey(protocolType: Service.self, factoryType: F2.self, associatedTag: nil) | ||
|
||
XCTAssertNotEqual(keyWithDifferentFactory1, keyWithDifferentFactory2) | ||
XCTAssertNotEqual(keyWithDifferentFactory1.hashValue, keyWithDifferentFactory2.hashValue) | ||
} | ||
|
||
func testThatDefinitionKeysWithDifferentTagsAreNotEqual() { | ||
let keyWithDifferentTag1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag1) | ||
let keyWithDifferentTag2 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag2) | ||
|
||
XCTAssertNotEqual(keyWithDifferentTag1, keyWithDifferentTag2) | ||
XCTAssertNotEqual(keyWithDifferentTag1.hashValue, keyWithDifferentTag2.hashValue) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed way better, I like that change 👍