public void Should_Register_Command_Settings_When_Configuring_Application()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            app.Run(new[]
            {
                "animal", "4", "dog", "12",
            });

            // Then
            registrar.Registrations.ContainsKey(typeof(DogSettings)).ShouldBeTrue();
            registrar.Registrations[typeof(DogSettings)].Count.ShouldBe(1);
            registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings));
            registrar.Registrations.ContainsKey(typeof(MammalSettings)).ShouldBeTrue();
            registrar.Registrations[typeof(MammalSettings)].Count.ShouldBe(1);
            registrar.Registrations[typeof(MammalSettings)].ShouldContain(typeof(MammalSettings));
        }
        public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode()
        {
            // Given
            var capturedContext = default(CommandContext);

            var resolver = new FakeTypeResolver();
            var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

            resolver.Register(new DogSettings());
            resolver.Register(command);

            var registrar = new FakeTypeRegistrar(resolver);
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                });
            });

            // When
            var result = app.Run(new[] { "animal", "4", "dog", "12", "--foo" });

            // Then
            capturedContext.ShouldNotBeNull();
            capturedContext.Remaining.Parsed.Count.ShouldBe(1);
            capturedContext.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
        }
        public void Should_Register_Commands_When_Configuring_Application()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

            // When
            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <GenericCommand <FooCommandSettings> >("foo");
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // Then
            registrar.Registrations.ContainsKey(typeof(ICommand)).ShouldBeTrue();
            registrar.Registrations[typeof(ICommand)].Count.ShouldBe(3);
            registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(GenericCommand <FooCommandSettings>));
            registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(DogCommand));
            registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(HorseCommand));
        }
Example #4
0
            public void Should_Inject_Dependency_Using_A_Given_Registrar()
            {
                // Given
                var dependency = new FakeDependency();
                var registrar  = new FakeTypeRegistrar {
                    TypeResolverFactory = FakeTypeResolver.Factory
                };

                registrar.RegisterInstance(typeof(FakeDependency), dependency);
                var app = new CommandAppTester(registrar);

                app.SetDefaultCommand <GenericCommand <InjectSettings> >();
                app.Configure(config => config.PropagateExceptions());

                // When
                var result = app.Run(new[]
                {
                    "--name", "foo",
                    "--age", "35",
                });

                // Then
                result.ExitCode.ShouldBe(0);
                result.Settings.ShouldBeOfType <InjectSettings>().And(injected =>
                {
                    injected.ShouldNotBeNull();
                    injected.Fake.ShouldBeSameAs(dependency);
                    injected.Name.ShouldBe("Hello foo");
                    injected.Age.ShouldBe(35);
                });
            }
Example #5
0
        public void Should_Register_Remaining_Arguments()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.AddCommand <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            app.Run(new[] { "animal", "--foo", "f", "dog", "--bar", "b", "--name", "Rufus" });

            // Then
            registrar.Instances.ContainsKey(typeof(IArguments)).ShouldBeTrue();
            registrar.Instances[typeof(IArguments)].Single().As <IArguments>(args =>
            {
                args.Count.ShouldBe(2);
                args.Contains("--foo").ShouldBeTrue();
                args["--foo"].Single().ShouldBe("f");
                args.Contains("--bar").ShouldBeTrue();
                args["--bar"].Single().ShouldBe("b");
            });
        }
        public void Should_Register_Default_Command_Settings_When_Configuring_Application()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp <DogCommand>(registrar);

            // When
            app.Configure(config =>
            {
                config.PropagateExceptions();
            });

            // Then
            registrar.Registrations.ContainsKey(typeof(DogSettings));
            registrar.Registrations[typeof(DogSettings)].Count.ShouldBe(1);
            registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings));
        }
Example #7
0
    public void Should_Register_Default_Command_When_Configuring_Application()
    {
        // Given
        var registrar = new FakeTypeRegistrar();
        var app       = new CommandApp <DogCommand>(registrar);

        app.Configure(config =>
        {
            config.PropagateExceptions();
        });

        // When
        app.Run(new[]
        {
            "12", "4",
        });

        // Then
        registrar.Registrations.ContainsKey(typeof(DogCommand)).ShouldBeTrue();
        registrar.Registrations[typeof(DogCommand)].ShouldContain(typeof(DogCommand));
    }
        public void Should_Throw_When_Encountering_Unknown_Option_In_Strict_Mode()
        {
            // Given
            var registrar = new FakeTypeRegistrar();
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.UseStrictParsing();
                config.AddCommand <DogCommand>("dog");
            });

            // When
            var result = Record.Exception(() => app.Run(new[] { "dog", "--foo" }));

            // Then
            result.ShouldBeOfType <ParseException>().And(ex =>
            {
                ex.Message.ShouldBe("Unknown option 'foo'.");
            });
        }