示例#1
0
      it('should force new instances by annotation using overridden provider', function() {
        class RouteScope {}

        class Engine {
          start() {}
        }
        annotate(Engine, new Type())

        class MockEngine {
          start() {}
        }
        annotate(MockEngine, new Type())
        annotate(MockEngine, new RouteScope())
        annotate(MockEngine, new Provide(Engine))

        var parent = new Injector([MockEngine])
        var childA = parent.createChild([], [RouteScope])
        var childB = parent.createChild([], [RouteScope])

        var engineFromA = childA.get(Engine)
        var engineFromB = childB.get(Engine)

        expect(engineFromA).not.to.equal(engineFromB)
        expect(engineFromA).to.be.instanceof(MockEngine)
        expect(engineFromB).to.be.instanceof(MockEngine)
      })
示例#2
0
        it('should always create a new instance', function() {
          var constructorSpy = sinon.spy()

          class ExpensiveEngine {
            constructor(power) {
              constructorSpy()
              this.power = power
            }
          }
          annotate(ExpensiveEngine, new Type())
          annotate(ExpensiveEngine, new TransientScope())
          annotate(ExpensiveEngine, new Inject('power'))

          class Car {
            constructor(createEngine) {
              this.createEngine = createEngine
            }
          }
          annotate(Car, new Type())
          annotate(Car, new InjectLazy(ExpensiveEngine))

          var injector = new Injector()
          var car = injector.get(Car)

          var veyronEngine = car.createEngine('power', 1184)
          var mustangEngine = car.createEngine('power', 420)

          expect(veyronEngine).not.to.equal(mustangEngine)
          expect(veyronEngine.power).to.equal(1184)
          expect(mustangEngine.power).to.equal(420)

          var mustangEngine2 = car.createEngine('power', 420)
          expect(mustangEngine).not.to.equal(mustangEngine2)
        })
示例#3
0
      it('should force new instances by annotation', function() {
        class RouteScope {}

        class Engine {
          start() {}
        }
        annotate(Engine, new Type())

        class Car {
          constructor(engine) {
            this.engine = engine
          }
          start() {}
        }
        annotate(Car, new Type())
        annotate(Car, new RouteScope())
        annotate(Car, new Inject(Engine))

        var parent = new Injector([Car, Engine])
        var child = parent.createChild([], [RouteScope])

        var carFromParent = parent.get(Car)
        var carFromChild = child.get(Car)

        expect(carFromChild).not.to.equal(carFromParent)
        expect(carFromChild.engine).to.equal(carFromParent.engine)
      })
示例#4
0
    it('should override providers', function() {
      class Engine {}
      annotate(Engine, new Type())


      class Car {
        constructor(engine) {
          this.engine = engine
        }

        start() {}
      }
      annotate(Car, new Type())
      annotate(Car, new Inject(Engine))

      class MockEngine {
        start() {}
      }
      annotate(MockEngine, new Type())
      annotate(MockEngine, new Provide(Engine))

      var injector = new Injector([MockEngine])
      var car = injector.get(Car)

      expect(car).to.be.instanceof(Car)
      expect(car.engine).to.be.instanceof(MockEngine)
    })
示例#5
0
      it('should always use dependencies from the youngest injector', function() {
        class Foo {}
        annotate(Foo, new Type())
        annotate(Foo, new Inject())

        class AlwaysNewInstance {
          constructor(foo) {
            this.foo = foo
          }
        }
        annotate(AlwaysNewInstance, new Type())
        annotate(AlwaysNewInstance, new TransientScope())
        annotate(AlwaysNewInstance, new Inject(Foo))

        var injector = new Injector([AlwaysNewInstance])
        var child = injector.createChild([Foo]) // force new instance of Foo

        var fooFromChild = child.get(Foo)
        var fooFromParent = injector.get(Foo)

        var alwaysNew1 = child.get(AlwaysNewInstance)
        var alwaysNew2 = child.get(AlwaysNewInstance)
        var alwaysNewFromParent = injector.get(AlwaysNewInstance)

        expect(alwaysNew1.foo).to.equal(fooFromChild)
        expect(alwaysNew2.foo).to.equal(fooFromChild)
        expect(alwaysNewFromParent.foo).to.equal(fooFromParent)
      })
示例#6
0
      it('should support "super" to call a parent constructor', function() {
        class Something {}
        annotate(Something, new Type())

        class Parent {
          constructor(something) {
            this.parentSomething = something
          }
        }
        annotate(Parent, new Type())
        annotate(Parent, new Inject(Something))

        class Child extends Parent {
          constructor(superConstructor, something) {
            superConstructor()
            this.childSomething = something
          }
        }
        annotate(Child, new Type())
        annotate(Child, new Inject(SuperConstructor, Something))

        var injector = new Injector()
        var instance = injector.get(Child)

        expect(instance.parentSomething).to.be.instanceof(Something)
        expect(instance.childSomething).to.be.instanceof(Something)
        expect(instance.childSomething).to.equal(instance.parentSomething)
      })
示例#7
0
      it('should instantiate lazily from a parent injector', function() {
        var constructorSpy = sinon.spy()

        class ExpensiveEngine {
          constructor() {
            constructorSpy()
          }
        }
        annotate(ExpensiveEngine, new Type())

        class Car {
          constructor(createEngine) {
            this.engine = null
            this.createEngine = createEngine
          }

          start() {
            this.engine = this.createEngine()
          }
        }
        annotate(Car, new Type())
        annotate(Car, new InjectLazy(ExpensiveEngine))

        var injector = new Injector([ExpensiveEngine])
        var childInjector = injector.createChild([Car])
        var car = childInjector.get(Car)

        expect(constructorSpy).not.to.have.been.called

        car.start()
        expect(constructorSpy).to.have.been.called
        expect(car.engine).to.be.instanceof(ExpensiveEngine)
      })
示例#8
0
      it('should never cache', function() {
        class Foo {}
        annotate(Foo, new Type())
        annotate(Foo, new TransientScope())

        var injector = new Injector()
        expect(injector.get(Foo)).not.to.equal(injector.get(Foo))
      })
示例#9
0
      it('should throw an error when used in a factory function', function() {
        class Something {}

        annotate(createSomething, new Provide(Something))
        annotate(createSomething, new Inject(SuperConstructor))
        function createSomething() {}

        expect(function() {
          var injector = new Injector([createSomething])
          injector.get(Something)
        }).to.throw(/Only classes with a parent can ask for SuperConstructor!/)
      })
示例#10
0
      it('should force new instance by annotation for default provider', function() {
        class RequestScope {}

        class Foo {}
        annotate(Foo, new Type())
        annotate(Foo, new Inject())
        annotate(Foo, new RequestScope())

        var parent = new Injector()
        var child = parent.createChild([], [RequestScope])

        var fooFromChild = child.get(Foo)
        var fooFromParent = parent.get(Foo)

        expect(fooFromParent).not.to.equal(fooFromChild)
      })
示例#11
0
    it('should show the full path when null/undefined token requested', function() {
      class Foo {}
      annotate(Foo, new Inject(undefined))

      class Bar {}
      annotate(Bar, new Inject(null))

      var injector = new Injector()

      expect(function() {
        injector.get(Foo)
      }).to.throw(/Invalid token "undefined" requested! \(.* -> undefined\)/)

      expect(function() {
        injector.get(Bar)
      }).to.throw(/Invalid token "null" requested! \(.* -> null\)/)
    })
示例#12
0
    it('should throw an error when used in a class without any parent', function() {
      class WithoutParent {}
      annotate(WithoutParent, new Inject(SuperConstructor))

      var injector = new Injector()

      expect(function() {
        injector.get(WithoutParent)
      }).to.throw(/Only classes with a parent can ask for SuperConstructor!/)
    })
示例#13
0
    it('should instantiate a class without dependencies', function() {
      class Car {
        start() {}
      }
      annotate(Car, new Type())

      var injector = new Injector()
      var car = injector.get(Car)

      expect(car).to.be.instanceof(Car)
    })
示例#14
0
    it('should allow factory function', function() {
      class Size {}

      annotate(computeSize, new Provide(Size))
      function computeSize() {
        return 0
      }

      var injector = new Injector([computeSize])
      var size = injector.get(Size)
      expect(size).to.equal(0)
    })
示例#15
0
      it('should create new instance in a child injector', function() {
        class Car {
          start() {}
        }
        annotate(Car, new Type())

        class MockCar {
          start() {}
        }
        annotate(MockCar, new Type())
        annotate(MockCar, new Provide(Car))

        var parent = new Injector([Car])
        var child = parent.createChild([MockCar])

        var carFromParent = parent.get(Car)
        var carFromChild = child.get(Car)

        expect(carFromParent).not.to.equal(carFromChild)
        expect(carFromChild).to.be.instanceof(MockCar)
      })
示例#16
0
      it('should cache default provider in parent injector', function() {
        class Foo {}
        annotate(Foo, new Inject())

        var parent = new Injector()
        var child = parent.createChild([])

        var fooFromChild = child.get(Foo)
        var fooFromParent = parent.get(Foo)

        expect(fooFromParent).to.equal(fooFromChild)
      })
示例#17
0
    it('should resolve dependencies based on @Inject annotation', function() {
      class Engine {
        start() {}
      }
      annotate(Engine, new Type())

      class Car {
        constructor(engine) {
          this.engine = engine
        }

        start() {}
      }
      annotate(Car, new Type())
      annotate(Car, new Inject(Engine))

      var injector = new Injector()
      var car = injector.get(Car)

      expect(car).to.be.instanceof(Car)
      expect(car.engine).to.be.instanceof(Engine)
    })
示例#18
0
      it('should load instances from parent injector', function() {
        class Car {
          start() {}
        }
        annotate(Car, new Type())

        var parent = new Injector([Car])
        var child = parent.createChild([])

        var carFromParent = parent.get(Car)
        var carFromChild = child.get(Car)

        expect(carFromChild).to.equal(carFromParent)
      })
示例#19
0
      it('should support "super" to call multiple parent constructors', function() {
        class Foo {}
        annotate(Foo, new Type())

        class Bar {}
        annotate(Bar, new Type())

        class Parent {
          constructor(foo) {
            this.parentFoo = foo
          }
        }
        annotate(Parent, new Type())
        annotate(Parent, new Inject(Foo))

        class Child extends Parent {
          constructor(superConstructor, foo) {
            superConstructor()
            this.childFoo = foo
          }
        }
        annotate(Child, new Type())
        annotate(Child, new Inject(SuperConstructor, Foo))

        class GrandChild extends Child {
          constructor(superConstructor, foo, bar) {
            superConstructor()
            this.grandChildBar = bar
            this.grandChildFoo = foo
          }

        }

        annotate(GrandChild, new Type())
        annotate(GrandChild, new Inject(SuperConstructor, Foo, Bar))

        var injector = new Injector()
        var instance = injector.get(GrandChild)

        expect(instance.parentFoo).to.be.instanceof(Foo)
        expect(instance.childFoo).to.be.instanceof(Foo)
        expect(instance.grandChildFoo).to.be.instanceof(Foo)
        expect(instance.grandChildBar).to.be.instanceof(Bar)
      })
示例#20
0
    it('should show the full path when error happens in a constructor', function() {
      class Engine {
        constructor() {
          throw new Error('This engine is broken!')
        }
      }

      class Car {
        constructor(e) {}
      }
      annotate(Car, new Inject(Engine))

      var injector = new Injector()

      expect(() => injector.get(Car))
        .to.throw(/Error during instantiation of .*! \(.* -> .*\)/)
    })
示例#21
0
      it('should force new instance by annotation using the lowest overridden provider', function() {
        class RouteScope {}

        class Engine {
          constructor() {}
          start() {}
        }
        annotate(Engine, new Type())
        annotate(Engine, new RouteScope())

        class MockEngine {
          constructor() {}
          start() {}
        }
        annotate(MockEngine, new Type())
        annotate(MockEngine, new Provide(Engine))
        annotate(MockEngine, new RouteScope())

        class DoubleMockEngine {
          start() {}
        }
        annotate(DoubleMockEngine, new Type())
        annotate(DoubleMockEngine, new Provide(Engine))
        annotate(DoubleMockEngine, new RouteScope())

        var parent = new Injector([Engine])
        var child = parent.createChild([MockEngine])
        var grantChild = child.createChild([], [RouteScope])

        var engineFromParent = parent.get(Engine)
        var engineFromChild = child.get(Engine)
        var engineFromGrantChild = grantChild.get(Engine)

        expect(engineFromParent).to.be.instanceof(Engine)
        expect(engineFromChild).to.be.instanceof(MockEngine)
        expect(engineFromGrantChild).to.be.instanceof(MockEngine)
        expect(engineFromGrantChild).not.to.equal(engineFromChild)
      })
示例#22
0
// Copyright 2015, EMC, Inc.

'use strict';

var di = require('di');
var ejs = require('ejs');

module.exports = taskApiServiceFactory;
di.annotate(taskApiServiceFactory, new di.Provide('Http.Services.Api.Tasks'));
di.annotate(taskApiServiceFactory,
    new di.Inject(
        'Protocol.Task',
        'Services.Waterline',
        'Errors',
        'Util',
        'Services.Configuration',
        'Services.Lookup',
        'Services.Environment',
        'Promise',
        'Templates'
    )
);
function taskApiServiceFactory(
    taskProtocol,
    waterline,
    Errors,
    util,
    configuration,
    lookupService,
    Env,
    Promise,
示例#23
0
// Copyright 2015, EMC, Inc.

'use strict';

var di = require('di'),
    express = require('express'),
    path = require('path'),
    zlib = require('zlib'),
    tar = require('tar'),
    path = require('path');

module.exports = skuPackServiceFactory;
di.annotate(skuPackServiceFactory, new di.Provide('Http.Services.SkuPack'));
di.annotate(skuPackServiceFactory,
    new di.Inject(
        '_',
        'Services.Waterline',
        'Logger',
        'FileLoader',
        'Templates',
        'Profiles',
        'Promise',
        'fs',
        'rimraf',
        'Assert',
        'Http.Services.Api.Workflows',
        'Constants',
        'Services.Environment',
        'osTmpdir',
        'uuid',
        'Errors'
示例#24
0
// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di');

module.exports = obmApiServiceFactory;
di.annotate(obmApiServiceFactory, new di.Provide('Http.Services.Api.Obms'));
di.annotate(obmApiServiceFactory,
    new di.Inject(
        'Promise',
        '_'
    )
);
function obmApiServiceFactory(
    Promise,
    _

) {

    function ObmApiService() {
    }

    var obms = [
        {
            service: 'amt-obm-service',
            config: {
                host: {
                    default: 'localhost',
                    type: 'string'
                },
示例#25
0
// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di');
module.exports = taskRunnerFactory;
di.annotate(taskRunnerFactory, new di.Provide('TaskGraph.TaskRunner'));
di.annotate(taskRunnerFactory,
    new di.Inject(
        'Logger',
        'Promise',
        'Constants',
        'Assert',
        'uuid',
        '_',
        'Rx',
        'Task.Task',
        'Task.Messenger',
        'TaskGraph.Store'
    )
);

function taskRunnerFactory(
    Logger,
    Promise,
    Constants,
    assert,
    uuid,
    _,
    Rx,
    Task,
示例#26
0
var di = require('di'),
    jwt = require('jsonwebtoken'),
    passport = require('passport'),
    JwtStrategy = require('passport-jwt').Strategy,
    LocalStrategy = require('passport-local').Strategy,
    BasicStrategy = require('passport-http').BasicStrategy,
    ExtractJwt = require('passport-jwt').ExtractJwt,
    AnonStrategy = require('passport-anonymous').Strategy;

var BAD_REQUEST_STATUS = 400;
var UNAUTHORIZED_STATUS = 401;
var ERROR_STATUS = 500;

module.exports = authServiceFactory;
di.annotate(authServiceFactory, new di.Provide('Auth.Services'));
di.annotate(authServiceFactory,
    new di.Inject(
        'Assert',
        'Services.Configuration',
        '_',
        'Services.Waterline'
    )
);

function authServiceFactory(
    assert,
    configuration,
    _,
    waterline
) {
示例#27
0
// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di');

module.exports = configApiServiceFactory;
di.annotate(configApiServiceFactory, new di.Provide('Http.Services.Api.Config'));
di.annotate(configApiServiceFactory,
    new di.Inject(
        'Services.Configuration',
        '_'
    )
);
function configApiServiceFactory(
    configuration,
    _
) {
    function ConfigApiService() {
    }

    /**
     * Get server configuration
     * @return {Promise}
     */

    ConfigApiService.prototype.configGetAll = function () {
        // get the config

        return configuration.getAll();
    };
示例#28
0
// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di');
var stream = require('stream');

module.exports = dockerJobFactory;

di.annotate(dockerJobFactory, new di.Provide('Job.Docker'));
di.annotate(dockerJobFactory, new di.Inject(
    'Job.Base',
    'Util',
    'Logger',
    'Assert',
    'Constants',
    'Services.Waterline',
    'Services.Messenger',
    'Docker'
));

function dockerJobFactory(
    BaseJob,
    util,
    Logger,
    assert,
    Constants,
    waterline,
    messenger,
    Docker
) {
示例#29
0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    implied. See the License for the specific language governing permissions
    and limitations under the License.
*/

// Some of the code below is adopted from https://github.com/Reactive-Extensions/RxJS

'use strict';

var di = require('di');

module.exports = rxMixins;
di.annotate(rxMixins, new di.Provide('Rx.Mixins'));
di.annotate(rxMixins,
    new di.Inject(
        'Rx',
        'Assert'
    )
);

function rxMixins(
    Rx,
    assert
) {

    var MergeLossyObserver = (function () {
        function MergeLossyObserver(o, concurrentCounter, g) {
            this.o = o;
示例#30
0
// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di');
var util = require('util');
var path = require('path');

module.exports = swaggerFactory;

di.annotate(swaggerFactory, new di.Provide('Http.Services.Swagger'));
di.annotate(swaggerFactory,
    new di.Inject(
            'Promise',
            'Errors',
            '_',
            di.Injector,
            'Views',
            'Assert',
            'Http.Api.Services.Schema',
            'Services.Configuration',
            'Services.Environment',
            'Services.Lookup',
            'Constants',
            'ejs',
            'Services.Waterline'
        )
    );

function swaggerFactory(
    Promise,