Ejemplo n.º 1
0
        public void ParsesGroupsWithSameNameCorrectly()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <NamedGroupWithCommands>();
            builder.RegisterModule <NamedGroupWithAdditionalCommands>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Single(root.Children);
            var groupNode = Assert.IsType <GroupNode>(root.Children[0]);

            Assert.Equal("a", groupNode.Key);

            Assert.Equal(2, groupNode.GroupTypes.Count);
            Assert.Equal(typeof(NamedGroupWithCommands), groupNode.GroupTypes[0]);
            Assert.Equal(typeof(NamedGroupWithAdditionalCommands), groupNode.GroupTypes[1]);

            Assert.Equal(6, groupNode.Children.Count);
            var command1 = Assert.IsType <CommandNode>(groupNode.Children[0]);
            var command2 = Assert.IsType <CommandNode>(groupNode.Children[1]);
            var command3 = Assert.IsType <CommandNode>(groupNode.Children[2]);
            var command4 = Assert.IsType <CommandNode>(groupNode.Children[3]);
            var command5 = Assert.IsType <CommandNode>(groupNode.Children[4]);
            var command6 = Assert.IsType <CommandNode>(groupNode.Children[5]);

            Assert.Equal("b", command1.Key);
            Assert.Equal("c", command2.Key);
            Assert.Equal("d", command3.Key);
            Assert.Equal("e", command4.Key);
            Assert.Equal("f", command5.Key);
            Assert.Equal("g", command6.Key);
        }
Ejemplo n.º 2
0
                public void ReturnsUnsuccessfulIfMultipleNamedGroupsWithTheSameNameHaveADefaultPermissionAttribute()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <AtMostOneDefaultPermissionAttributeAllowed.Named.GroupOne>();
                    builder.RegisterModule <AtMostOneDefaultPermissionAttributeAllowed.Named.GroupTwo>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands();

                    ResultAssert.Unsuccessful(result);
                }
Ejemplo n.º 3
0
        public void ParsesUnnamedGroupWithNestedUnnamedGroupWithCommandsCorrectly()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <UnnamedGroupWithNestedUnnamedGroupWithCommands>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Equal(4, root.Children.Count);
            Assert.IsType <CommandNode>(root.Children[0]);
            Assert.IsType <CommandNode>(root.Children[1]);
            Assert.IsType <CommandNode>(root.Children[2]);
            Assert.IsType <CommandNode>(root.Children[3]);

            var command1 = (CommandNode)root.Children[0];
            var command2 = (CommandNode)root.Children[1];
            var command3 = (CommandNode)root.Children[2];
            var command4 = (CommandNode)root.Children[3];

            Assert.Equal("a", command1.Key);
            Assert.Equal("b", command2.Key);
            Assert.Equal("c", command3.Key);
            Assert.Equal("d", command4.Key);
        }
Ejemplo n.º 4
0
                public void CanExcludeNestedGroup()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <ExcludedNestedGroup>();

                    var tree = builder.Build();

                    var result   = tree.CreateApplicationCommands();
                    var commands = result.Entity !;

                    var group = commands.Single();

                    Assert.Equal("a", group.Name);

                    Assert.Collection
                    (
                        group.Options.Value,
                        c =>
                    {
                        Assert.Equal(SubCommand, c.Type);
                        Assert.Equal("d", c.Name);
                    }
                    );
                }
Ejemplo n.º 5
0
        public void ParsesNamedGroupWithCommandsCorrectly()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <NamedGroupWithCommands>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Single(root.Children);
            Assert.IsType <GroupNode>(root.Children[0]);

            var groupNode = (GroupNode)root.Children[0];

            Assert.Equal("a", groupNode.Key);

            var groupType = Assert.Single(groupNode.GroupTypes);

            Assert.Equal(typeof(NamedGroupWithCommands), groupType);

            Assert.Equal(3, groupNode.Children.Count);
            Assert.IsType <CommandNode>(groupNode.Children[0]);
            Assert.IsType <CommandNode>(groupNode.Children[1]);
            Assert.IsType <CommandNode>(groupNode.Children[2]);

            var command1 = (CommandNode)groupNode.Children[0];
            var command2 = (CommandNode)groupNode.Children[1];
            var command3 = (CommandNode)groupNode.Children[2];

            Assert.Equal("b", command1.Key);
            Assert.Equal("c", command2.Key);
            Assert.Equal("d", command3.Key);
        }
Ejemplo n.º 6
0
        public void ParsesGroupWithVariousCollectionTypesCorrectly()
        {
            void AssertIsCollectionCommand(IChildNode childNode)
            {
                Assert.IsType <CommandNode>(childNode);

                var commandNode = (CommandNode)childNode;

                Assert.Collection
                (
                    commandNode.Shape.Parameters,
                    p => Assert.IsType <PositionalCollectionParameterShape>(p)
                );
            }

            var builder = new CommandTreeBuilder();

            builder.RegisterModule <GroupWithVariousCollectionTypes>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Collection
            (
                root.Children,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand,
                AssertIsCollectionCommand
            );
        }
Ejemplo n.º 7
0
                public void CreatesRequiredOptionsCorrectly()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <CommandsWithRequiredOrOptionalParameters>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out var commands);

                    ResultAssert.Successful(result);
                    Assert.NotNull(commands);

                    var requiredCommand   = commands !.First(c => c.Name == "required");
                    var requiredParameter = requiredCommand.Options.Value !.First();

                    Assert.True(requiredParameter.IsRequired.HasValue);
                    Assert.True(requiredParameter.IsRequired.Value);

                    var optionalCommand   = commands.First(c => c.Name == "optional");
                    var optionalParameter = optionalCommand.Options.Value !.First();

                    if (optionalParameter.IsRequired.HasValue)
                    {
                        Assert.False(optionalParameter.IsRequired.Value);
                    }
                }
Ejemplo n.º 8
0
        public void ParsesUnnamedGroupWithCommandsWithNestedNamedGroupWithCommandsCorrectly()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <UnnamedGroupWithCommandsWithNestedNamedGroupWithCommands>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Equal(3, root.Children.Count);

            var command1 = (CommandNode)root.Children[0];

            Assert.Equal("a", command1.Key);

            var command2 = (CommandNode)root.Children[1];

            Assert.Equal("b", command2.Key);

            var groupNode = (GroupNode)root.Children[2];

            Assert.Single(groupNode.Children);
            var command3 = (CommandNode)groupNode.Children[0];

            Assert.Equal("d", command3.Key);
        }
            public void SearchIsUnsuccessfulIfNoMatchingCommandExists()
            {
                var builder = new CommandTreeBuilder();

                builder.RegisterModule <NamedGroupWithCommandsWithNestedNamedGroupWithCommands>();

                var tree = builder.Build();

                var result = tree.Search("a d c");

                Assert.Empty(result);
            }
            public void CanFindCommandByCommandAliasAndGroupAlias()
            {
                var builder = new CommandTreeBuilder();

                builder.RegisterModule <AliasedGroupWithAliasedCommand>();

                var tree = builder.Build();

                var result = tree.Search("t c");

                Assert.NotEmpty(result);
            }
            public void SearchIsSuccessfulIfAMatchingCommandExists()
            {
                var builder = new CommandTreeBuilder();

                builder.RegisterModule <NamedGroupWithCommandsWithNestedNamedGroupWithCommands>();

                var tree = builder.Build();

                var result = tree.Search("a c d", new Dictionary <string, IReadOnlyList <string> >());

                Assert.NotEmpty(result);
            }
Ejemplo n.º 12
0
                public void ReturnsUnsuccessfulIfGroupsAreTooDeeplyNested()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <TooDeeplyNested>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Unsuccessful(result);
                }
Ejemplo n.º 13
0
                public void ReturnsUnsuccessfulIfThereAreOverloadsInAGroup()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <OverloadsAreNotSupportedInGroups>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Unsuccessful(result);
                }
Ejemplo n.º 14
0
                public void ReturnsSuccessForValidTree()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <ValidCommandGroup>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Successful(result);
                }
Ejemplo n.º 15
0
                public void ReturnsUnsuccessfulIfACommandHasTooManyParameters()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <TooManyCommandParameters>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Unsuccessful(result);
                }
            public void CanFindCommandByCommandAliasAndGroupAlias()
            {
                var builder = new CommandTreeBuilder();

                builder.RegisterModule <AliasedGroupWithAliasedCommand>();

                var tree = builder.Build();

                var result = tree.Search("t c", new Dictionary <string, IReadOnlyList <string> >());

                Assert.NotEmpty(result);
            }
Ejemplo n.º 17
0
                public void ReturnsUnsuccessfulIfAParameterDescriptionIsTooLong()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <TooLongParameterDescription>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands();

                    ResultAssert.Unsuccessful(result);
                }
Ejemplo n.º 18
0
                public void ReturnsUnsuccessfulIfThereAreTooManyRootLevelCommands()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <TooManyCommands>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Unsuccessful(result);
                }
Ejemplo n.º 19
0
                public void ReturnsSuccessfulIfAnEnumHasTooManyChoices()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <TooManyChoiceValues>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Successful(result);
                }
Ejemplo n.º 20
0
                public void ReturnsUnsuccessfulIfACommandContainsASwitchParameter()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <SwitchesAreNotSupported>();

                    var tree = builder.Build();

                    var result = tree.CreateApplicationCommands(out _);

                    ResultAssert.Unsuccessful(result);
                }
Ejemplo n.º 21
0
                public void CanExcludeGroup()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <ExcludedGroup>();

                    var tree = builder.Build();

                    var result   = tree.CreateApplicationCommands();
                    var commands = result.Entity !;

                    Assert.Empty(commands);
                }
Ejemplo n.º 22
0
                public void CreatesUngroupedTopLevelCommandsWithDefaultPermissionCorrectly()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <MultipleCommandsWithDefaultPermission.GroupOne>();
                    builder.RegisterModule <MultipleCommandsWithDefaultPermission.GroupTwo>();

                    var tree = builder.Build();

                    var result   = tree.CreateApplicationCommands();
                    var commands = result.Entity;

                    ResultAssert.Successful(result);
                    Assert.NotNull(commands);

                    Assert.Equal(2, commands !.Count);
                    var a = commands[0];
                    var b = commands[1];

                    Assert.True(a.DefaultPermission.Value);
                    Assert.False(b.DefaultPermission.Value);
                }
Ejemplo n.º 23
0
                public void NestedEmptyGroupsAreOptimizedOut()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <NestedGroupThatIsEmptyAfterExclusion>();

                    var tree = builder.Build();

                    var result   = tree.CreateApplicationCommands();
                    var commands = result.Entity !;

                    Assert.Empty(commands);
                }
Ejemplo n.º 24
0
        public void IncludesConfiguredDescriptions()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <DescribedGroup>();

            var tree = builder.Build();
            var root = tree.Root;

            var group     = (GroupNode)root.Children.Single();
            var command   = (CommandNode)group.Children.Single();
            var parameter = command.Shape.Parameters.Single();

            Assert.Equal("Group description", group.Description);
            Assert.Equal("Command description", command.Shape.Description);
            Assert.Equal("Parameter description", parameter.Description);
        }
Ejemplo n.º 25
0
                public void DoesNotCreateChoicesIfAnEnumHasTooManyChoices()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <TooManyChoiceValues>();

                    var tree = builder.Build();

                    _ = tree.CreateApplicationCommands(out var commands);

                    var command = commands !.Single();

                    if (command.Choices.HasValue)
                    {
                        Assert.Empty(command.Choices.Value !);
                    }
                }
Ejemplo n.º 26
0
                public void CreatesNamedGroupWithDefaultPermissionCorrectly()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <NamedGroupWithDefaultPermission>();

                    var tree = builder.Build();

                    var result   = tree.CreateApplicationCommands();
                    var commands = result.Entity;

                    ResultAssert.Successful(result);
                    Assert.NotNull(commands);

                    var command = commands !.SingleOrDefault();

                    Assert.True(command !.DefaultPermission.Value);
                }
Ejemplo n.º 27
0
                public void CanFindCommandWithDifferentCasing()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <GroupWithCasingDifferences>();

                    var tree = builder.Build();

                    var options = new TreeSearchOptions(StringComparison.OrdinalIgnoreCase);

                    var result = tree.Search
                                 (
                        "test somecommand",
                        searchOptions: options
                                 );

                    Assert.NotEmpty(result);
                }
Ejemplo n.º 28
0
                public void CreatesValidTreeCorrectly()
                {
                    var builder = new CommandTreeBuilder();

                    builder.RegisterModule <ValidCommandGroup>();

                    var tree = builder.Build();

                    var result   = tree.CreateApplicationCommands();
                    var commands = result.Entity;

                    ResultAssert.Successful(result);

                    Assert.NotNull(commands);
                    Assert.Equal(2, commands !.Count);

                    var topLevelCommand = commands.FirstOrDefault(c => c.Name == "top-level-command");

                    Assert.NotNull(topLevelCommand);

                    var topLevelGroup = commands.FirstOrDefault(c => c.Name == "top-level-group");

                    Assert.NotNull(topLevelGroup);

                    Assert.True(topLevelGroup !.Options.HasValue);
                    Assert.Equal(2, topLevelGroup.Options.Value.Count);

                    var firstNestedCommand = topLevelGroup.Options.Value.FirstOrDefault(c => c.Type == SubCommand);

                    Assert.NotNull(firstNestedCommand);

                    var nestedGroup = topLevelGroup.Options.Value.FirstOrDefault(c => c.Type == SubCommandGroup);

                    Assert.NotNull(nestedGroup);

                    Assert.True(nestedGroup !.Options.HasValue);
                    Assert.Single(nestedGroup.Options.Value);

                    var secondNestedCommand = nestedGroup.Options.Value.FirstOrDefault(c => c.Type == SubCommand);

                    Assert.NotNull(secondNestedCommand);
                }
Ejemplo n.º 29
0
        public void ParsesGroupWithVariousReturnTypesCorrectly()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <GroupWithVariousReturnTypes>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Collection
            (
                root.Children,
                a => Assert.Equal(nameof(a), a.Key),
                b => Assert.Equal(nameof(b), b.Key),
                c => Assert.Equal(nameof(c), c.Key),
                d => Assert.Equal(nameof(d), d.Key),
                e => Assert.Equal(nameof(e), e.Key),
                f => Assert.Equal(nameof(f), f.Key)
            );
        }
Ejemplo n.º 30
0
        public void ParsesNamedGroupWithCommandsWithNestedNamedGroupWithCommandsCorrectly()
        {
            var builder = new CommandTreeBuilder();

            builder.RegisterModule <NamedGroupWithCommandsWithNestedNamedGroupWithCommands>();

            var tree = builder.Build();
            var root = tree.Root;

            Assert.Single(root.Children);
            Assert.IsType <GroupNode>(root.Children[0]);

            var groupNode = (GroupNode)root.Children[0];

            Assert.Equal("a", groupNode.Key);

            var groupType = Assert.Single(groupNode.GroupTypes);

            Assert.Equal(typeof(NamedGroupWithCommandsWithNestedNamedGroupWithCommands), groupType);

            Assert.Equal(2, groupNode.Children.Count);
            Assert.Contains(groupNode.Children, n => n is GroupNode);
            Assert.Contains(groupNode.Children, n => n is CommandNode);

            var command1 = (CommandNode)groupNode.Children.First(n => n is CommandNode);

            Assert.Equal("b", command1.Key);

            var nestedGroupNode = (GroupNode)groupNode.Children.First(n => n is GroupNode);

            Assert.Equal("c", nestedGroupNode.Key);

            Assert.Single(nestedGroupNode.Children);
            var command2 = (CommandNode)nestedGroupNode.Children[0];

            Assert.Equal("d", command2.Key);
        }