Permalink
Browse files

Added tests for the initializebasedInstanceProvider + leaves it up to…

… the instanceProvider to set "isInitialized" to true instead of relying on the "instance" property. (Providers can be opinionated about what does it mean to be "initialized"
  • Loading branch information...
cburgdorf committed Mar 30, 2012
1 parent 55d3a93 commit 2adaf999665ae410d909eba49af83e3a7188ff46
Showing with 228 additions and 2 deletions.
  1. +7 −1 src/lightcore.instanceprovider.js
  2. +1 −1 src/lightcore.js
  3. +218 −0 tests/initializeBasedInstanceProviderTests.js
  4. +2 −0 tests/testrunner.html
@@ -8,14 +8,20 @@
lightcore.instanceProvider = {};
lightcore.instanceProvider.constructorBased = function(registration, dependencies){
- return new registration.type(dependencies);
+
+ var instance = new registration.type(dependencies);
+ registration.isInitialized = true;
+
+ return instance;
};
lightcore.instanceProvider.initializeBased = function(registration, dependencies){
if (!registration.instance){
registration.instance = new registration.type();
}
registration.instance.initialize(dependencies);
+ registration.isInitialized = true;
+
return registration.instance;
};
View
@@ -9,7 +9,7 @@
};
var createInstanceOrReuseExistingOne = function(registration, dependencies){
- if (registration.singleton && registration.instance){
+ if (registration.singleton && registration.isInitialized){
return registration.instance;
}
@@ -0,0 +1,218 @@
+module("initializeBasedInstnaceProviderTests", {
+ setup:function () {
+ lightcore.defaultInstanceProvider = lightcore.instanceProvider.initializeBased;
+ }
+});
+
+test('can resolve types', function () {
+
+ function SomeType() {
+ var self = this;
+ self.initialize = function(){
+ self.test = "foo";
+ };
+ }
+
+ lightcore.register("SomeType", SomeType);
+ lightcore.addRegistration({
+ name:"SomeType",
+ type:SomeType,
+ instance:new SomeType()
+ });
+ var instance = lightcore.resolve("SomeType");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+});
+
+test('multiple resolves result in multiple initialize calls', function () {
+
+ function SomeType() {
+ var self = this;
+ self.initCount = 0;
+ self.initialize = function(){
+ self.test = "foo";
+ self.initCount++;
+ };
+ }
+
+ lightcore.register("SomeType", SomeType);
+ lightcore.addRegistration({
+ name: "SomeType",
+ type: SomeType,
+ instance: new SomeType()
+ });
+
+ var instance1 = lightcore.resolve("SomeType");
+ var instance2 = lightcore.resolve("SomeType");
+
+ ok(instance1 !== undefined);
+ ok(instance1.test === "foo");
+ ok(instance2 !== undefined);
+ ok(instance2.test === "foo");
+ ok(instance1.initCount === 2)
+});
+
+
+
+test('multiple resolves result in one initialize call with singleton option', function () {
+
+ function SomeType() {
+ var self = this;
+ self.initCount = 0;
+ self.initialize = function(){
+ self.test = "foo";
+ self.initCount++;
+ };
+ }
+
+ lightcore.register("SomeType", SomeType);
+ lightcore.addRegistration({
+ name: "SomeType",
+ type: SomeType,
+ instance: new SomeType(),
+ singleton: true
+ });
+
+ var instance1 = lightcore.resolve("SomeType");
+ var instance2 = lightcore.resolve("SomeType");
+
+ ok(instance1 !== undefined);
+ ok(instance1.test === "foo");
+ ok(instance2 !== undefined);
+ ok(instance2.test === "foo");
+ ok(instance1.initCount === 1)
+});
+
+
+/*
+
+test('can resolve types depending on other types', function () {
+
+ function TypeA() {
+ }
+
+ function SomeType(options) {
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+ this.test = "foo";
+ }
+
+ SomeType.requires = ["TypeA"];
+
+ lightcore.register("TypeA", TypeA);
+ lightcore.register("SomeType", SomeType);
+ var instance = lightcore.resolve("SomeType");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+});
+
+test('can resolve types depending on other types (3 levels)', function () {
+
+ function TypeA() {
+ }
+
+ function TypeB(options) {
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+ }
+
+ TypeB.requires = ["TypeA"];
+
+ function TypeC(options) {
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+
+ this.test = "foo";
+ }
+
+ TypeC.requires = ["TypeB"];
+
+ lightcore.register("TypeA", TypeA);
+ lightcore.register("TypeB", TypeB);
+ lightcore.register("TypeC", TypeC);
+
+ var instance = lightcore.resolve("TypeC");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+});
+
+test('can resolve types depending on multiple other types', function () {
+
+ function TypeA() {
+ }
+
+ function TypeB(options) {
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+ }
+
+ TypeB.requires = ["TypeA"];
+
+ function TypeC(options) {
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+
+ if (options.TypeB === undefined) {
+ throw "missing argument [typeB]"
+ }
+
+ this.test = "foo";
+ }
+
+ TypeC.requires = ["TypeB", "TypeA"];
+
+ lightcore.register("TypeA", TypeA);
+ lightcore.register("TypeB", TypeB);
+ lightcore.register("TypeC", TypeC);
+
+ var instance = lightcore.resolve("TypeC");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+});
+
+test('nested singletons are only created once', function () {
+
+ var creations = 0;
+
+ function TypeA() {
+ creations++;
+ }
+
+ function TypeB(options) {
+ creations++;
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+ }
+
+ TypeB.requires = ["TypeA"];
+
+ function TypeC(options) {
+ creations++;
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+
+ if (options.TypeB === undefined) {
+ throw "missing argument [typeB]"
+ }
+
+ this.test = "foo";
+ }
+
+ TypeC.requires = ["TypeB", "TypeA"];
+
+ lightcore.register("TypeA", TypeA, true);
+ lightcore.register("TypeB", TypeB, true);
+ lightcore.register("TypeC", TypeC, true);
+
+ var instance = lightcore.resolve("TypeC");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+ equal(3, creations);
+});*/
View
@@ -7,11 +7,13 @@
<script type="text/javascript" src="../libs/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="../libs/rx.min.js"> </script>
<script type="text/javascript" src="../libs/rx.aggregates.min.js"></script>
+ <script type="text/javascript" src="../libs/rx.time.min.js"></script>
<script type="text/javascript" src="../libs/rx.html.min.js"></script>
<script type="text/javascript" src="../src/lightcore.core.js"></script>
<script type="text/javascript" src="../src/lightcore.instanceprovider.js"></script>
<script type="text/javascript" src="../src/lightcore.js"></script>
<script type="text/javascript" src="tests.js"></script>
+ <script type="text/javascript" src="initializeBasedInstanceProviderTests.js"></script>
</head>
<body>
<h1 id="qunit-header">QUnit Test Suite</h1>

0 comments on commit 2adaf99

Please sign in to comment.