Example #1
0
        public async Task CanExecuteSequential()
        {
            var context = new CommandContext();
            var config  = new MariCommandsOptions();

            var executor = CreateExecutor();

            var commandMock = new Mock <ICommand>();

            commandMock.SetupGet(a => a.Module.Type).Returns(typeof(TestModuleClassExecutor));
            commandMock.SetupGet(a => a.Module.Invoker).Returns(DefaultModuleInvoker.Create(typeof(TestModuleClassExecutor)));
            commandMock.Setup(a => a.GetRunMode(config)).Returns(RunMode.Sequential);
            commandMock.SetupGet(a => a.Executor).Returns(executor);

            var command = commandMock.Object;
            var args    = new object[0];

            context.Command = command;
            context.Args    = args;

            await ExecuteMiddlewareAsync(context);

            Assert.NotNull(context.Result);
            Assert.True(context.Result.Success);
            Assert.IsType <SuccessResult>(context.Result);
            Assert.NotNull(context.Command);
            Assert.Equal(command, context.Command);
            Assert.NotNull(context.Args);
            Assert.Equal(args, context.Args);
        }
        public void CanCreateInvoker()
        {
            var moduleType = typeof(TestModuleClassInvoker);

            var invoker = DefaultModuleInvoker.Create(moduleType);

            Assert.NotNull(invoker);
        }
        public void CanInstantiateModule()
        {
            var services = new ServiceCollection();

            using var provider = services.BuildServiceProvider(true);

            var scopeFactory = provider.GetRequiredService <IServiceScopeFactory>();

            using var scope = provider.CreateScope();

            var moduleType = typeof(TestModuleClassInvoker);

            var invoker  = DefaultModuleInvoker.Create(moduleType);
            var instance = invoker.CreateInstance(scope.ServiceProvider);

            Assert.NotNull(invoker);
            Assert.NotNull(instance);
            Assert.IsType <TestModuleClassInvoker>(instance);
        }
Example #4
0
        public async Task InvokeResultFiltersWhenExecuted()
        {
            // Act
            await using var context = new CommandContext();

            var config = new MariCommandsOptions();

            config.Filters.Add(typeof(TestResultFilter));

            var executor = CreateExecutor();

            var commandMock = new Mock <ICommand>();

            commandMock.SetupGet(a => a.Module.Type).Returns(typeof(TestModuleClassExecutor));
            commandMock.SetupGet(a => a.Module.Invoker).Returns(DefaultModuleInvoker.Create(typeof(TestModuleClassExecutor)));
            commandMock.Setup(a => a.GetRunMode(config)).Returns(RunMode.Sequential);
            commandMock.SetupGet(a => a.Executor).Returns(executor);

            var command = commandMock.Object;
            var args    = new object[0];

            context.Command = command;
            context.Args    = args;

            // Arrange
            await ExecuteMiddlewareAsync(context, config, true);

            var singletonDependency = context.CommandServices.GetRequiredService <SingletonDependency>();

            // Assert
            Assert.NotNull(context.Result);
            Assert.True(context.Result.Success);
            Assert.IsType <SuccessResult>(context.Result);
            Assert.NotNull(context.Command);
            Assert.Equal(command, context.Command);
            Assert.NotNull(context.Args);
            Assert.Equal(args, context.Args);
            Assert.True(singletonDependency.Invoked);
        }
Example #5
0
        public async Task ChooseHigherPriorityFromMatches()
        {
            var context = new CommandContext();
            var config  = new MariCommandsOptions
            {
                AutoDisposeContext = false
            };

            var alias    = "testAlias";
            var executor = CreateExecutor();

            var command1Mock = new Mock <ICommand>();
            var command2Mock = new Mock <ICommand>();

            command1Mock.SetupGet(a => a.Module.Type).Returns(typeof(TestModuleClassExecutor));
            command1Mock.SetupGet(a => a.Module.Invoker).Returns(DefaultModuleInvoker.Create(typeof(TestModuleClassExecutor)));
            command1Mock.Setup(a => a.GetRunMode(config)).Returns(RunMode.Sequential);
            command1Mock.SetupGet(a => a.Executor).Returns(executor);
            command1Mock.SetupGet(a => a.Priority).Returns(3);

            command2Mock.SetupGet(a => a.Module.Type).Returns(typeof(TestModuleClassExecutor));
            command2Mock.SetupGet(a => a.Module.Invoker).Returns(DefaultModuleInvoker.Create(typeof(TestModuleClassExecutor)));
            command2Mock.Setup(a => a.GetRunMode(config)).Returns(RunMode.Sequential);
            command2Mock.SetupGet(a => a.Executor).Returns(executor);
            command2Mock.SetupGet(a => a.Priority).Returns(2);

            var command1 = command1Mock.Object;
            var command2 = command2Mock.Object;

            var match1Mock = new Mock <ICommandMatch>();
            var match2Mock = new Mock <ICommandMatch>();

            match1Mock.SetupGet(a => a.Command).Returns(command1);
            match1Mock.SetupGet(a => a.Alias).Returns(alias);

            match2Mock.SetupGet(a => a.Command).Returns(command2);
            match2Mock.SetupGet(a => a.Alias).Returns(alias);

            var match1 = match1Mock.Object;
            var match2 = match2Mock.Object;

            var matches = new List <ICommandMatch>
            {
                match1,
                match2,
            };

            var args = new object[0];

            context.Features.Set <ICommandMatchesFeature>(new CommandMatchesFeature
            {
                CommandMatches = matches,
            });
            context.Features.Set <IArgumentParserFeature>(new ArgumentParserFeature
            {
                CommandArgs = new Dictionary <ICommandMatch, object[]>
                {
                    { match1, args },
                    { match2, args },
                }
            });

            await ExecuteMiddlewareAsync(context, config);

            var commandMatchesFeature = context.Features.Get <ICommandMatchesFeature>();
            var argumentParserFeature = context.Features.Get <IArgumentParserFeature>();

            Assert.NotNull(context.Result);
            Assert.NotNull(commandMatchesFeature);
            Assert.NotNull(argumentParserFeature);
            Assert.True(context.Result.Success);
            Assert.IsType <SuccessResult>(context.Result);
            Assert.NotNull(context.Command);
            Assert.NotNull(context.Args);
            Assert.NotNull(context.Alias);
            Assert.Equal(command1, context.Command);
            Assert.Equal(args, context.Args);
            Assert.Equal(alias, context.Alias);
        }
 private IModuleInvoker GetInvoker(Type type)
 => DefaultModuleInvoker.Create(type);