示例#1
0
        private CommandFactory CreateCommandFactory(
            Action <CommandRegistry> commandRegistryConfigurator           = null,
            Action <CommandServices> commandServicesConfigurator           = null,
            Action <ScopedObjectRegistry> scopedObjectRegistryConfigurator = null)
        {
            var nameValidator       = new NameValidator();
            var descriptorGenerator = new CommandAttributeInspector();
            var registry            = new CommandRegistry(nameValidator, descriptorGenerator);

            if (commandRegistryConfigurator != null)
            {
                commandRegistryConfigurator.Invoke(registry);
            }

            var services = new CommandServices();

            if (commandServicesConfigurator != null)
            {
                commandServicesConfigurator.Invoke(services);
            }

            var sessionObjects = new ScopedObjectRegistry();

            sessionObjects.Register <VariableCollection>();

            if (scopedObjectRegistryConfigurator != null)
            {
                scopedObjectRegistryConfigurator.Invoke(sessionObjects);
            }

            return(new CommandFactory(registry, services, sessionObjects));
        }
示例#2
0
        private Session CreateSession(
            Action <ScopedObjectRegistry> sessionObjectsConfigurator = null,
            params Type[] commandTypes)
        {
            var parser              = new Parser();
            var nameValidator       = new NameValidator();
            var descriptorGenerator = new CommandAttributeInspector();
            var registry            = new CommandRegistry(nameValidator, descriptorGenerator);

            foreach (var commandType in commandTypes)
            {
                registry.Register(commandType);
            }

            var services      = new CommandServices();
            var scopedObjects = new ScopedObjectRegistry();

            scopedObjects.Register <VariableCollection>();
            sessionObjectsConfigurator?.Invoke(scopedObjects);

            var factory = new CommandFactory(registry, services, scopedObjects);

            var variables = scopedObjects.Resolve(typeof(VariableCollection)) as VariableCollection;
            var replacer  = new VariableReplacer();
            var binder    = new CommandParameterBinder(registry, replacer, variables);

            return(new Session(parser, factory, binder));
        }
示例#3
0
        public void Register_TypeMissingParameterlessConstructor_ThrowsException()
        {
            // arrange
            var    sut       = new ScopedObjectRegistry();
            Action sutAction = () => sut.Register <NoParameterlessConstructorObject>();

            // act, assert
            var ex = Assert.Throws <InvalidOperationException>(sutAction);

            Assert.Contains("Type Chel.UnitTests.SampleObjects.NoParameterlessConstructorObject requires a parameterless constructor", ex.Message);
        }
示例#4
0
        public void Resolve_TypeNotRegistered_ReturnsNull()
        {
            // arrange
            var sut = new ScopedObjectRegistry();

            // act
            var result = sut.Resolve(typeof(GoodObject));

            // assert
            Assert.Null(result);
        }
示例#5
0
        public void Ctor_CommandServicesNull_ThrowsException()
        {
            // arrange
            var    registry      = Substitute.For <ICommandRegistry>();
            var    scopedObjects = new ScopedObjectRegistry();
            Action sutAction     = () => new CommandFactory(registry, null, scopedObjects);

            // act, assert
            var ex = Assert.Throws <ArgumentNullException>(sutAction);

            Assert.Equal("commandServices", ex.ParamName);
        }
示例#6
0
        public void Resolve_RegisteredType_ReturnsInstance()
        {
            // arrange
            var sut = new ScopedObjectRegistry();

            sut.Register <GoodObject>();

            // act
            var result = sut.Resolve(typeof(GoodObject));

            // assert
            Assert.IsType <GoodObject>(result);
        }
示例#7
0
        public void Resolve_RegisteredType_ReturnsSameInstance()
        {
            // arrange
            var sut = new ScopedObjectRegistry();

            sut.Register <GoodObject>();

            // act
            var result1 = sut.Resolve(typeof(GoodObject));
            var result2 = sut.Resolve(typeof(GoodObject));

            // assert
            Assert.Same(result1, result2);
        }
示例#8
0
        public void CreateScope_WhenCalled_IncludesRegisteredTypes()
        {
            // arrange
            var sut = new ScopedObjectRegistry();

            sut.Register <GoodObject>();

            // act
            var scope = sut.CreateScope();

            // assert
            var result = scope.Resolve(typeof(GoodObject));

            Assert.IsType <GoodObject>(result);
        }
示例#9
0
        public void CreateScope_WhenCalled_ExcludesInstances()
        {
            // arrange
            var sut = new ScopedObjectRegistry();

            sut.Register <GoodObject>();
            var result1 = sut.Resolve(typeof(GoodObject));

            // act
            var scope = sut.CreateScope();

            // assert
            var result2 = scope.Resolve(typeof(GoodObject));

            Assert.NotSame(result1, result2);
        }