Example #1
0
        public async Task CanCreateBagWithDefaultValues()
        {
            var tracker = new BagTracker();

            using (var context = CreateContext(
                       commands => commands
                       .Add("c", Track <BagWithDefaultValues>(tracker))
                       ))
            {
                await context.Executor.ExecuteAsync <object>("c", default);

                tracker.Assert <BagWithDefaultValues>(
                    "c",
                    bag =>
                {
                    Assert.IsFalse(bag.Async);
                    //Assert.IsFalse(bag.CanThrow);
                    Assert.IsFalse(bag.BoolOnly);
                    Assert.IsFalse(bag.BoolWithDefaultValue1);
                    Assert.IsTrue(bag.BoolWithDefaultValue2);
                    Assert.IsNull(bag.StringOnly);
                    Assert.AreEqual("foo", bag.StringWithDefaultValue);
                    Assert.AreEqual(0, bag.Int32Only);
                    Assert.IsNull(bag.NullableInt32Only);
                    Assert.AreEqual(3, bag.Int32WithDefaultValue);
                    Assert.AreEqual(DateTime.MinValue, bag.DateTimeOnly);
                    Assert.IsNull(bag.NullableDateTime);
                    Assert.AreEqual(new DateTime(2018, 1, 1), bag.DateTimeWithDefaultValue);
                    Assert.IsNull(bag.ListOnly);
                }
                    );
            }
        }
Example #2
0
 internal static ExecuteCallback <TBag> Track <TBag>(BagTracker bags) where TBag : ICommandBag, new()
 {
     return((name, bag, cancellationToken) =>
     {
         bags.Add(name, bag);
         return Task.CompletedTask;
     });
 }
Example #3
0
 public void DisallowUnsupportedParameterType()
 {
     Assert.That.Throws <DynamicException>(
         () =>
     {
         var bags = new BagTracker();
         using (CreateContext(
                    commands => commands
                    .Add("c", Track <BagWithUnsupportedParameterType>(bags))
                    )) { }
     },
         filter => filter.When(name: "^RegisterCommand"),
         inner => inner.When(name: "^UnsupportedParameterTypeException")
         );
 }
Example #4
0
 public void DisallowNonSequentialParameterPosition()
 {
     Assert.That.Throws <DynamicException>(
         () =>
     {
         var bags = new BagTracker();
         using (CreateContext(
                    commands => commands
                    .Add("c", Track <BagWithInvalidParameterPosition>(bags))
                    )) { }
     },
         filter => filter.When(name: "^RegisterCommand"),
         inner => inner.When(name: "^ParameterPositionException")
         );
 }
Example #5
0
 public void DisallowDuplicateParameterNames()
 {
     Assert.That.Throws <DynamicException>(
         () =>
     {
         var bags = new BagTracker();
         using (CreateContext(
                    commands => commands
                    .Add("c", Track <BagWithDuplicateParameter>(bags))
                    )) { }
     },
         filter => filter.When(name: "^RegisterCommand"),
         inner => inner.When(name: "^DuplicateParameterName")
         );
 }
Example #6
0
 public void DisallowNonExistingCommandName()
 {
     Assert.That.Throws <DynamicException>(
         () =>
     {
         var bags = new BagTracker();
         using (var context = CreateContext(
                    commands => commands
                    .Add("c", Track <SimpleBag>(bags))
                    ))
         {
             context.Executor.ExecuteAsync("b").GetAwaiter().GetResult();
         }
     },
         filter => filter.When(name: "^InvalidCommandLine")
         );
 }
Example #7
0
        public async Task CanCreateBagWithCommandLineValues()
        {
            var tracker = new BagTracker();

            using (var context = CreateContext(
                       commands => commands
                       .Add("c", Track <BagWithMappedValues>(tracker))
                       ))
            {
                await context.Executor.ExecuteAsync <object>("c", default);

                tracker.Assert <BagWithMappedValues>(
                    "c",
                    bag => { }
                    );
            }
        }
Example #8
0
        public void DisallowDuplicateCommandNames()
        {
            var ex = Assert.ThrowsAny <DynamicException>(
                () =>
            {
                var bags = new BagTracker();
                using (CreateContext(
                           commands => commands
                           .Add("c", Track <SimpleBag>(bags))
                           .Add("c", Track <SimpleBag>(bags))
                           )) { }
            }
//                ,
//                filter => filter.When(name: "^RegisterCommand"),
//                inner => inner.When(name: "^DuplicateCommandName")
                );
        }
Example #9
0
        public async Task CanMapParameterByPosition()
        {
            var tracker = new BagTracker();

            using (var context = CreateContext(
                       commands => commands.Add("c", Track <BagWithPositionalValues>(tracker)),
                       (ExecuteExceptionCallback)(ex => throw ex)
                       ))
            {
                await context.Executor.ExecuteAsync <object>("c 3 kmh -ismetric", default);

                tracker.Assert <BagWithPositionalValues>("c", bag =>
                {
                    Assert.AreEqual(3, bag.Speed);
                    Assert.AreEqual("kmh", bag.Unit);
                    Assert.AreEqual(true, bag.IsMetric);
                });
            }
        }
Example #10
0
        public async Task CanCreateBagWithFlagValues()
        {
            var tracker = new BagTracker();

            using (var context = CreateContext(
                       commands => commands
                       .Add("c", Track <SimpleBag>(tracker))
                       ))
            {
                await context.Executor.ExecuteAsync <object>("c -async -canthrow=true", default);

                tracker.Assert <SimpleBag>(
                    "c",
                    bag =>
                {
                    Assert.IsTrue(bag.Async);
                    //Assert.IsTrue(bag.CanThrow);
                }
                    );
            }
        }
Example #11
0
        public async Task CanMapParameterByAlias()
        {
            var tracker = new BagTracker();

            using (var context = CreateContext(
                       commands => commands
                       .Add("c", Track <BagWithAliases>(tracker))
                       ))
            {
                await context.Executor.ExecuteAsync <object>("c -swa=abc", default);

                tracker.Assert <BagWithAliases>(
                    "c",
                    bag =>
                {
                    Assert.IsFalse(bag.Async);
                    //Assert.IsFalse(bag.CanThrow);
                    Assert.AreEqual("abc", bag.StringWithAlias);
                }
                    );
            }
        }