Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reformatting specs
  • Loading branch information
jdewind committed May 18, 2012
1 parent 390fad0 commit 31a4272
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 151 deletions.
101 changes: 50 additions & 51 deletions Specs/BasicUsageSpecs.m
Expand Up @@ -3,92 +3,91 @@
#import "Fixtures.h"

SPEC_BEGIN(BasicUsageSpecs)

beforeEach(^{
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
});

it(@"correctly builds a registered object", ^{
id engine = [[JSObjection defaultInjector] getObject:[Engine class]];

assertThat(engine, isNot(nilValue()));
id engine = [[JSObjection defaultInjector] getObject:[Engine class]];
assertThat(engine, isNot(nilValue()));
});

it(@"returns nil for a non-registered object", ^{
Class newClass = objc_allocateClassPair([NSObject class], "MyFooClass", 0);
objc_registerClassPair(newClass);
assertThat([[JSObjection defaultInjector] getObject:newClass], is(nilValue()));
Class newClass = objc_allocateClassPair([NSObject class], "MyFooClass", 0);
objc_registerClassPair(newClass);
assertThat([[JSObjection defaultInjector] getObject:newClass], is(nilValue()));
});

it(@"correctly builds and object with dependencies", ^{
Car *car = [[JSObjection defaultInjector] getObject:[Car class]];

assertThat(car, isNot(nilValue()));

assertThat(car.engine, isNot(nilValue()));
assertThat(car.engine, is(instanceOf([Engine class])));

assertThat(car.brakes, isNot(nilValue()));
assertThat(car.brakes, is(instanceOf([Brakes class])));
Car *car = [[JSObjection defaultInjector] getObject:[Car class]];
assertThat(car, isNot(nilValue()));
assertThat(car.engine, isNot(nilValue()));
assertThat(car.engine, is(instanceOf([Engine class])));
assertThat(car.brakes, isNot(nilValue()));
assertThat(car.brakes, is(instanceOf([Brakes class])));
});

it(@"defaults to returning a new instance", ^{
id thomas = [[JSObjection defaultInjector] getObject:[Engine class]];
id gordan = [[JSObjection defaultInjector] getObject:[Engine class]];

assertThat(thomas, isNot(sameInstance(gordan)));
id thomas = [[JSObjection defaultInjector] getObject:[Engine class]];
id gordan = [[JSObjection defaultInjector] getObject:[Engine class]];
assertThat(thomas, isNot(sameInstance(gordan)));
});

it(@"will return the same instance if it is registered as a singleton", ^{
id carFactory1 = [[JSObjection defaultInjector] getObject:[CarFactory class]];
id carFactory2 = [[JSObjection defaultInjector] getObject:[CarFactory class]];

assertThat(carFactory1, isNot(nilValue()));
assertThat(carFactory1, is(sameInstance(carFactory2)));
id carFactory1 = [[JSObjection defaultInjector] getObject:[CarFactory class]];
id carFactory2 = [[JSObjection defaultInjector] getObject:[CarFactory class]];
assertThat(carFactory1, isNot(nilValue()));
assertThat(carFactory1, is(sameInstance(carFactory2)));
});

it(@"ensures that singletons are properly registered even if they have not been referenced", ^{
// Ensure that the class is initialized before attempting to retrieve it.
id holder1 = [[JSObjection defaultInjector] getObject:[SingletonItemHolder class]];
id holder2 = [[JSObjection defaultInjector] getObject:[SingletonItemHolder class]];

assertThat([holder1 singletonItem], is(sameInstance([holder2 singletonItem])));
// Ensure that the class is initialized before attempting to retrieve it.
id holder1 = [[JSObjection defaultInjector] getObject:[SingletonItemHolder class]];
id holder2 = [[JSObjection defaultInjector] getObject:[SingletonItemHolder class]];
assertThat([holder1 singletonItem], is(sameInstance([holder2 singletonItem])));
});

it(@"will not return the same instance per injector if object is a singleton", ^{
id carFactory1 = [[JSObjection defaultInjector] getObject:[CarFactory class]];
id carFactory2 = [[JSObjection createInjector] getObject:[CarFactory class]];
assertThat(carFactory1, isNot(sameInstance(carFactory2)));
id carFactory1 = [[JSObjection defaultInjector] getObject:[CarFactory class]];
id carFactory2 = [[JSObjection createInjector] getObject:[CarFactory class]];
assertThat(carFactory1, isNot(sameInstance(carFactory2)));
});

it(@"returns nil if the class is nil", ^{
assertThat([[JSObjection defaultInjector] getObject:nil], is(nilValue()));
assertThat([[JSObjection defaultInjector] getObject:nil], is(nilValue()));
});

it(@"doesn't blow up if a nil class is passed into register", ^{
[JSObjection registerClass:nil lifeCycle:JSObjectionInstantiationRuleSingleton];
[JSObjection registerClass:nil lifeCycle:JSObjectionInstantiationRuleSingleton];
});

it(@"calls awakeFromObjection when an object has been constructed", ^{
id engine = [[JSObjection defaultInjector] getObject:[Engine class]];
id car = [[JSObjection defaultInjector] getObject:[Car class]];
id engine = [[JSObjection defaultInjector] getObject:[Engine class]];
id car = [[JSObjection defaultInjector] getObject:[Car class]];

assertThatBool([engine awake], equalToBool(YES));
assertThatBool([car awake], equalToBool(YES));
assertThatBool([engine awake], equalToBool(YES));
assertThatBool([car awake], equalToBool(YES));
});

it(@"returns a JSObjectFactory for the given injector context", ^{
JSObjectionInjector *injector1 = [JSObjection createInjector];
JSObjectionInjector *injector2 = [JSObjection defaultInjector];

JSObjectFactoryHolder *holder1 = [injector1 getObject:[JSObjectFactoryHolder class]];
JSObjectFactoryHolder *holder2 = [injector2 getObject:[JSObjectFactoryHolder class]];

SingletonItem *item1 = [holder1.objectFactory getObject:[SingletonItem class]];
SingletonItem *item2 = [holder2.objectFactory getObject:[SingletonItem class]];

[[item1 shouldNot] equal:item2];
JSObjectionInjector *injector1 = [JSObjection createInjector];
JSObjectionInjector *injector2 = [JSObjection defaultInjector];
JSObjectFactoryHolder *holder1 = [injector1 getObject:[JSObjectFactoryHolder class]];
JSObjectFactoryHolder *holder2 = [injector2 getObject:[JSObjectFactoryHolder class]];
SingletonItem *item1 = [holder1.objectFactory getObject:[SingletonItem class]];
SingletonItem *item2 = [holder2.objectFactory getObject:[SingletonItem class]];
[[item1 shouldNot] equal:item2];
});

SPEC_END
2 changes: 1 addition & 1 deletion Specs/CircularDependencyFixtures.h
Expand Up @@ -5,7 +5,7 @@

@interface SingletonBar : NSObject <BarProtocol>
{
SingletonFoo *foo;
SingletonFoo *foo;
}

@property(nonatomic, retain) SingletonFoo *foo;
Expand Down
22 changes: 10 additions & 12 deletions Specs/CircularDependencySpecs.m
Expand Up @@ -3,20 +3,18 @@
#import "Fixtures.h"

SPEC_BEGIN(CircularDependencySpecs)

describe(@"circular dependencies", ^{
beforeEach(^{
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
});
beforeEach(^{
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
});

it(@"are resolved between singletons", ^{
SingletonFoo *foo = [[JSObjection defaultInjector] getObject:[SingletonFoo class]];
SingletonBar *bar = [[JSObjection defaultInjector] getObject:[SingletonBar class]];
it(@"are resolved between singletons", ^{
SingletonFoo *foo = [[JSObjection defaultInjector] getObject:[SingletonFoo class]];
SingletonBar *bar = [[JSObjection defaultInjector] getObject:[SingletonBar class]];

assertThat(foo, is(sameInstance(bar.foo)));
assertThat(foo.bar, is(sameInstance(bar)));
});
assertThat(foo, is(sameInstance(bar.foo)));
assertThat(foo.bar, is(sameInstance(bar)));
});
});

SPEC_END
16 changes: 8 additions & 8 deletions Specs/InheritanceSpecs.m
Expand Up @@ -48,19 +48,19 @@ @implementation NoInheritance

SPEC_BEGIN(InheritanceSpecs)
beforeEach(^{
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
});

it(@"coalesces dependencies from parent to child", ^{
Programmer *programmer = [[JSObjection defaultInjector] getObject:[Programmer class]];
assertThat(programmer, is(notNilValue()));
assertThat(programmer.favoriteLanguages, is(notNilValue()));
assertThat(programmer.attributes, is(notNilValue()));
Programmer *programmer = [[JSObjection defaultInjector] getObject:[Programmer class]];
assertThat(programmer, is(notNilValue()));
assertThat(programmer.favoriteLanguages, is(notNilValue()));
assertThat(programmer.attributes, is(notNilValue()));
});

it(@"does not throw a fit if the base class does not implement .objectionRequires", ^{
NoInheritance *noParentObjectWithRequires = [[JSObjection defaultInjector] getObject:[NoInheritance class]];
assertThat(noParentObjectWithRequires.something, is(notNilValue()));
NoInheritance *noParentObjectWithRequires = [[JSObjection defaultInjector] getObject:[NoInheritance class]];
assertThat(noParentObjectWithRequires.something, is(notNilValue()));
});
SPEC_END
6 changes: 3 additions & 3 deletions Specs/InjectionErrorFixtures.h
@@ -1,15 +1,15 @@
#import <Foundation/Foundation.h>

@interface UnsupportedPropertyObject : NSObject {
NSInteger myInteger;
NSInteger myInteger;
}

@property(nonatomic, assign) NSInteger myInteger;
@end

@interface BadPropertyObject : NSObject
{
NSObject *someObject;
NSObject *someObject;
}

@property(nonatomic, retain) NSObject *someObject;
Expand All @@ -18,7 +18,7 @@

@interface ReadOnlyPropertyObject : NSObject
{
NSObject *_someObject;
NSObject *_someObject;
}

@property(nonatomic, readonly) NSObject *someObject;
Expand Down
29 changes: 14 additions & 15 deletions Specs/InjectionErrorsSpecs.m
Expand Up @@ -5,33 +5,32 @@
SPEC_BEGIN(InjectionErrorsSpecs)

beforeEach(^{
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
});

it(@"throws an exception if property type is not an object", ^{
[[theBlock(^{
[[JSObjection defaultInjector] getObject:[UnsupportedPropertyObject class]];
}) should] raiseWithReason:@"Unable to determine class type for property declaration: 'myInteger'"];
[[theBlock(^{
[[JSObjection defaultInjector] getObject:[UnsupportedPropertyObject class]];
}) should] raiseWithReason:@"Unable to determine class type for property declaration: 'myInteger'"];
});

it(@"throws an exception if property cannot be found", ^{
[[theBlock(^{
[[JSObjection defaultInjector] getObject:[BadPropertyObject class]];
}) should] raiseWithReason:@"Unable to find property declaration: 'badProperty'"];
[[theBlock(^{
[[JSObjection defaultInjector] getObject:[BadPropertyObject class]];
}) should] raiseWithReason:@"Unable to find property declaration: 'badProperty'"];
});

it(@"throws if an object requires a protocol that does not exist in the context", ^{
[[theBlock(^{
[[JSObjection defaultInjector] getObject:[ManualCar class]];
}) should] raiseWithReason:@"Cannot find an instance that is bound to the protocol 'GearBox' to assign to the property 'gearBox'"];

[[theBlock(^{
[[JSObjection defaultInjector] getObject:[ManualCar class]];
}) should] raiseWithReason:@"Cannot find an instance that is bound to the protocol 'GearBox' to assign to the property 'gearBox'"];
});

it(@"throws if instantiation rule is not valid", ^{
[[theBlock(^{
[JSObjection registerClass:[CarFactory class] lifeCycle:3];
}) should] raiseWithReason:@"Invalid Instantiation Rule"];
[[theBlock(^{
[JSObjection registerClass:[CarFactory class] lifeCycle:3];
}) should] raiseWithReason:@"Invalid Instantiation Rule"];
});


Expand Down

0 comments on commit 31a4272

Please sign in to comment.