public void FormattingThrowsOnNonCommandGroup()
        {
            var    argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));
            Action a       = () => argType.Format(3);

            a.Should().Throw <InvalidCastException>();
        }
        public void SimpleCompletionWorks()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            argType.GetCompletions(new ArgumentCompletionContext(), "N")
            .Should().ContainSingle()
            .And.Equal(nameof(SimpleCommandType.Nested));
        }
        public void FormattingThrowsOnNoSelection()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            var    group = new CommandGroup <SimpleCommandType>(null);
            Action a     = () => argType.Format(group);

            a.Should().Throw <ArgumentOutOfRangeException>();
        }
        public void FormattingWorksOnSelection()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));
            var group   = new CommandGroup <SimpleCommandType>(null)
            {
                Selection = SimpleCommandType.Nested
            };

            argType.Format(group).Should().Be(nameof(SimpleCommandType.Nested));
        }
        public void TestThatDependentTypesAreCorrect()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            argType.DependentTypes.Should().ContainSingle();

            var dependentType = argType.DependentTypes.First();
            var enumType      = dependentType.Should().BeAssignableTo <IEnumArgumentType>().Which;

            enumType.Type.Should().Be(typeof(SimpleCommandType));
        }
        public void ParsingTopLevelLeafCommandSucceeds()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            argType.TryParse(ArgumentParseContext.Default, "Foo", out object result).Should().BeTrue();
            var group = result.Should()
                        .BeOfType <CommandGroup <SimpleCommandType> >()
                        .Which;

            group.HasSelection.Should().BeTrue();
            group.Selection.Should().Be(SimpleCommandType.Foo);
            group.Execute().Should().Be(CommandResult.Success);
            group.InstantiatedCommand
            .Should().NotBeNull()
            .And.BeOfType <FooCommand>()
            .Which.CallCount.Should().Be(1);
        }
        public void ParsingNestedGroupCommandSucceeds()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            argType.TryParse(ArgumentParseContext.Default, "Nested", out object result).Should().BeTrue();
            var group = result.Should()
                        .BeOfType <CommandGroup <SimpleCommandType> >()
                        .Which;

            group.HasSelection.Should().BeTrue();
            group.Selection.Should().Be(SimpleCommandType.Nested);

            var nestedGroup = group.InstantiatedCommand
                              .Should().NotBeNull()
                              .And.BeAssignableTo <ICommandGroup>()
                              .Which;

            nestedGroup.HasSelection.Should().BeFalse();
            nestedGroup.ExecuteAsync(CancellationToken.None).Result.Should().Be(CommandResult.UsageError);
        }
        public void ConstructorThrowsWhenInnerTypeIsNotEnum()
        {
            Action a = () => { var x = new CommandGroupArgumentType(typeof(CommandGroup <Guid>)); };

            a.Should().Throw <ArgumentOutOfRangeException>();
        }
        public void ConstructorThrowsOnNonCommandGroupType()
        {
            Action a = () => { var x = new CommandGroupArgumentType(typeof(List <int>)); };

            a.Should().Throw <ArgumentOutOfRangeException>();
        }
        public void ConstructorThrowsOnDifferentArityGenericType()
        {
            Action a = () => { var x = new CommandGroupArgumentType(typeof(Tuple <int, int>)); };

            a.Should().Throw <ArgumentOutOfRangeException>();
        }
        public void ParsingThrowsOnInvalidCommand()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            argType.TryParse(ArgumentParseContext.Default, "NotACommand", out object result).Should().BeFalse();
        }
        public void DisplayNameIsCorrect()
        {
            var argType = new CommandGroupArgumentType(typeof(CommandGroup <SimpleCommandType>));

            argType.DisplayName.Should().Be(nameof(SimpleCommandType));
        }
        public void ConstructorThrowsOnNonGenericType()
        {
            Action a = () => { var x = new CommandGroupArgumentType(typeof(int)); };

            a.ShouldThrow <ArgumentOutOfRangeException>();
        }