private MultipartFormBinder CreateBinder(string content,
                                                 ActionDescriptor actionDescriptor,
                                                 params IRequestReader[] readers)
        {
            var request = new HttpRequestMessage
            {
                Content = new StringContent(content)
            };

            request.Content.Headers.ContentType =
                new MediaTypeHeaderValue("multipart/form-data")
            {
                Parameters =
                {
                    new NameValueHeaderValue("boundary", "some-boundary")
                }
            };
            var configuration   = new Configuration();
            var parameterBinder = new ParameterBinder <BindResult>(configuration,
                                                                   null, actionDescriptor.Action, actionDescriptor.Route,
                                                                   new SimpleTypeMapper(configuration).AsList());
            var argumentBinder = new ArgumentBinder(parameterBinder);

            return(new MultipartFormBinder(request, actionDescriptor,
                                           readers, argumentBinder, parameterBinder, configuration));
        }
Ejemplo n.º 2
0
        public void InRangeSpecifiedRequiredArgumentTest()
        {
            {
                this.cakeArgs.Setup(
                    m => m.HasArgument(requiredArgName)
                    ).Returns(true);

                this.cakeArgs.Setup(
                    m => m.GetArgument(requiredArgName)
                    ).Returns((minValue + 1).ToString());

                RequiredArgument uut = ArgumentBinder.FromArguments <RequiredArgument>(this.cakeContext.Object);
                Assert.AreEqual(minValue + 1, uut.IntProperty);
            }

            {
                this.cakeArgs.Setup(
                    m => m.HasArgument(requiredArgName)
                    ).Returns(true);

                this.cakeArgs.Setup(
                    m => m.GetArgument(requiredArgName)
                    ).Returns((maxValue - 1).ToString());

                RequiredArgument uut = ArgumentBinder.FromArguments <RequiredArgument>(this.cakeContext.Object);
                Assert.AreEqual(maxValue - 1, uut.IntProperty);
            }
        }
Ejemplo n.º 3
0
        public void IntegerArgumentNotHiddenButRequiredTest()
        {
            // Act
            string actualDescription = ArgumentBinder.GetDescription <IntegerArgumentNotHiddenButRequired>(taskDescription);

            // Check

            // -------- Lines that should be there --------

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"--{argumentName}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                argDescription,
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.SourcePrefix}: {BaseAttribute.DefaultArgumentSource}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.TypePrefix}: {typeof( int ).Name}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{IntegerArgumentAttribute.MinValuePrefix}: {minValue}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{IntegerArgumentAttribute.MaxValuePrefix}: {maxValue}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.RequiredPrefix}: {true}",
                actualDescription
                );

            // -------- Lines that should NOT there --------

            // Required argument, default value is not needed.
            // Should not print it.
            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.DefaultValuePrefix,
                actualDescription
                );

            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.ValueIsSecretPrefix,
                actualDescription
                );
        }
        public void EmptyArgumentTest()
        {
            AggregateException e = Assert.Throws <AggregateException>(
                () => ArgumentBinder.FromArguments <EmptyArgument>(this.cakeContext.Object)
                );

            Assert.AreEqual(1, e.InnerExceptions.Count);
            Assert.IsTrue(e.InnerExceptions[0] is AttributeValidationException);
        }
Ejemplo n.º 5
0
        public void UnspecifiedOptionalArgumentTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(false);

            OptionalArgument uut = ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object);

            Assert.IsTrue(uut.BoolProperty);
        }
        public void UnspecifiedOptionalArgumentTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(false);

            OptionalArgument uut = ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object);

            Assert.AreEqual(defaultValue, uut.StringProperty);
        }
Ejemplo n.º 7
0
        public void Bind_BoolPresentNoValue_Maps()
        {
            var paramz = new List <string> {
                "-boolvalue", "-Required='xxx'"
            }.ToArray();

            var instance = new ArgumentBinder().Bind <FakeClass>(paramz);

            Assert.That(instance.BoolValue, Is.True);
        }
Ejemplo n.º 8
0
        public void Hint_ProvidesExamples()
        {
            var hint = new ArgumentBinder().Hint <FakeClass>();

            Assert.That(hint[0], Is.EqualTo("Supported arguments:"));
            Assert.That(hint[1], Is.EqualTo("\t-a=... (string)"));
            Assert.That(hint[2], Is.EqualTo("\t-b=... (string)"));
            Assert.That(hint[3], Is.EqualTo("\t-required=... (string, required)"));
            Assert.That(hint[4], Is.EqualTo("\t-thing=... (string)\r\n\tMethod\r\n"));
            Assert.That(hint[5], Is.EqualTo("\t-boolvalue=... (boolean)"));
        }
Ejemplo n.º 9
0
        public void Setup()
        {
            var converterProvider = new DefaultConverterProvider();

            converterProvider.Initialize(new IConverter[] { new BinaryConverter(), new StringConverter() });
            _argumentBinder = new ArgumentBinder(new[] { new FromBodyArgumentBinder(converterProvider) });
            _requestMapping = new Mock <IRequestMapping>(MockBehavior.Strict);
            _requestMapping.SetupGet(instance => instance.Target).Returns(new TestController());
            _requestMapping.SetupGet(instance => instance.Operation).Returns(typeof(TestController).GetMethod("Upload").ToOperationInfo("api/", Verb.POST));
            _request.SetupGet(instance => instance.Body).Returns(new MemoryStream(Request));
        }
Ejemplo n.º 10
0
        public void Bind_Args_BindsCorrectly()
        {
            var paramz = new List <string> {
                "-A=Aa", "-B=Bb", "-Required='xxx'"
            }.ToArray();

            var instance = new ArgumentBinder().Bind <FakeClass>(paramz);

            Assert.That(instance.A, Is.EqualTo("Aa"));
            Assert.That(instance.B, Is.EqualTo("Bb"));
        }
Ejemplo n.º 11
0
        public void Setup()
        {
            Mock <IConverter> converter = new Mock <IConverter>(MockBehavior.Strict);

            converter.Setup(instance => instance.ConvertTo(It.IsAny <Type>(), It.IsAny <IRequestInfo>())).Returns <Type, IRequestInfo>(Convert);
            converter.Setup(instance => instance.ConvertTo(It.IsAny <Type>(), It.IsAny <string>())).Returns <Type, string>(Convert);
            _fromQueryStringBinder = new Mock <IParameterSourceArgumentBinder <FromQueryStringAttribute> >();
            _binder = new ArgumentBinder(new IParameterSourceArgumentBinder[]
            {
                _fromQueryStringBinder.Object,
            });
        }
        public void DoesNotHaveRequiredArgumentTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(requiredArgName)
                ).Returns(false);

            AggregateException e = Assert.Throws <AggregateException>(
                () => ArgumentBinder.FromArguments <RequiredArgument>(this.cakeContext.Object)
                );

            Assert.AreEqual(1, e.InnerExceptions.Count);
            Assert.IsTrue(e.InnerExceptions[0] is MissingRequiredArgumentException);
        }
Ejemplo n.º 13
0
        public void IntegerArgumentHiddenNotRequiredTest()
        {
            // Act
            string actualDescription = ArgumentBinder.GetDescription <IntegerArgumentHiddenNotRequired>(taskDescription);

            // Check

            // -------- Lines that should be there --------

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"--{argumentName}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                argDescription,
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.SourcePrefix}: {BaseAttribute.DefaultArgumentSource}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.TypePrefix}: {typeof( int ).Name}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.DefaultValuePrefix}: {ArgumentBinder.HiddenString}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{IntegerArgumentAttribute.MinValuePrefix}: {ArgumentBinder.HiddenString}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{IntegerArgumentAttribute.MaxValuePrefix}: {ArgumentBinder.HiddenString}",
                actualDescription
                );

            // -------- Lines that should NOT there --------

            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.RequiredPrefix,
                actualDescription
                );
        }
        public void FilePathArgumentNotHiddenNotRequiredTest()
        {
            // Act
            string actualDescription = ArgumentBinder.GetDescription <FilePathArgumentNotHiddenNotRequiredNotExisting>(taskDescription);

            // Check

            // -------- Lines that should be there --------

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"--{argumentName}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                argDescription,
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.SourcePrefix}: {BaseAttribute.DefaultArgumentSource}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.TypePrefix}: {typeof( FilePath ).Name}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.DefaultValuePrefix}: {defaultValue}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BasePathAttribute.MustExistPrefix}: {false}",
                actualDescription
                );

            // -------- Lines that should NOT there --------

            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.RequiredPrefix,
                actualDescription
                );

            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.ValueIsSecretPrefix,
                actualDescription
                );
        }
Ejemplo n.º 15
0
        public void SpecifiedOptionalArgumentTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(optionalArgName)
                ).Returns(maxValue.ToString());

            OptionalArgument uut = ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object);

            Assert.AreEqual(maxValue, uut.IntProperty);
        }
Ejemplo n.º 16
0
        public void HasRequiredArgumentTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(requiredArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(requiredArgName)
                ).Returns("true");

            RequiredArgument uut = ArgumentBinder.FromArguments <RequiredArgument>(this.cakeContext.Object);

            Assert.IsTrue(uut.BoolProperty);
        }
Ejemplo n.º 17
0
        public void HasRequiredArgumentTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(requiredArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(requiredArgName)
                ).Returns(minValue.ToString());

            RequiredArgument uut = ArgumentBinder.FromArguments <RequiredArgument>(this.cakeContext.Object);

            Assert.AreEqual(minValue, uut.IntProperty);
        }
Ejemplo n.º 18
0
        // ---------------- Functions ----------------

        /// <summary>
        /// Runs git and gets the number of commits on the current branch.
        /// </summary>
        /// <param name="config">
        /// Configuration.  If null, it grabs the configuration
        /// from the passed in command-line arguments.
        /// </param>
        /// <returns>
        /// The number of commits that have happend on the current git branch.
        /// </returns>
        public int Run(GitQueryRevisionNumberConfig config = null)
        {
            if (config == null)
            {
                config = ArgumentBinder.FromArguments <GitQueryRevisionNumberConfig>(this.context);
            }

            int revNumber = -1;

            string onStdOut(string line)
            {
                if (string.IsNullOrWhiteSpace(line) == false)
                {
                    if (int.TryParse(line, out revNumber) == false)
                    {
                        revNumber = -1;
                    }
                }

                return(line);
            };

            ProcessSettings processSettings = new ProcessSettings
            {
                Arguments = ProcessArgumentBuilder.FromString("rev-list --count HEAD"),
                RedirectStandardOutput          = true,
                RedirectedStandardOutputHandler = onStdOut
            };

            this.Run(this.toolSettings, processSettings.Arguments, processSettings, null);

            if (revNumber < 0)
            {
                throw new InvalidOperationException(
                          "Could not get rev number from git"
                          );
            }

            if (config.NoPrint == false)
            {
                context.Information("Current Revision Number: " + revNumber);
            }

            if (string.IsNullOrWhiteSpace(config.OutputFile) == false)
            {
                System.IO.File.WriteAllText(config.OutputFile, revNumber.ToString());
            }

            return(revNumber);
        }
        public void EmptyStringTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(optionalArgName)
                ).Returns(string.Empty);

            OptionalArgument uut = ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object);

            Assert.AreEqual(string.Empty, uut.StringProperty);
        }
Ejemplo n.º 20
0
        public void DirectoryPathArgumentNotHiddenButRequiredTest()
        {
            // Act
            string actualDescription = ArgumentBinder.GetDescription <DirectoryPathArgumentNotHiddenButRequiredNotExisting>(taskDescription);

            // Check

            // -------- Lines that should be there --------

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"--{argumentName}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                argDescription,
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.SourcePrefix}: {BaseAttribute.DefaultArgumentSource}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.TypePrefix}: {typeof( DirectoryPath ).Name}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.RequiredPrefix}: {true}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BasePathAttribute.MustExistPrefix}: {false}",
                actualDescription
                );


            // -------- Lines that should NOT there --------

            // Required argument, default value is not needed.
            // Should not print it.
            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.DefaultValuePrefix,
                actualDescription
                );
        }
        public void SpecifiedOptionalArgumentTest()
        {
            const string value = "My Value";

            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(optionalArgName)
                ).Returns(value);

            OptionalArgument uut = ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object);

            Assert.AreEqual(value, uut.StringProperty);
        }
        public void HasRequiredArgumentTest()
        {
            const string value = "Arg Value";

            this.cakeArgs.Setup(
                m => m.HasArgument(requiredArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(requiredArgName)
                ).Returns(value);

            RequiredArgument uut = ArgumentBinder.FromArguments <RequiredArgument>(this.cakeContext.Object);

            Assert.AreEqual(value, uut.StringProperty);
        }
Ejemplo n.º 23
0
        public void FormatExceptionTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(optionalArgName)
                ).Returns("lolImNotAnInt");

            AggregateException e = Assert.Throws <AggregateException>(
                () => ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object)
                );

            Assert.AreEqual(1, e.InnerExceptions.Count);
            Assert.IsTrue(e.InnerExceptions[0] is ArgumentFormatException);
        }
        public void StringArgumentNullDefaultValueTest()
        {
            // Act
            string actualDescription = ArgumentBinder.GetDescription <StringArgumentNullDefaultValue>(taskDescription);

            // Check

            // -------- Lines that should be there --------

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"--{argumentName}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                argDescription,
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.SourcePrefix}: {BaseAttribute.DefaultArgumentSource}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.TypePrefix}: {typeof( string ).Name}",
                actualDescription
                );

            TestHelpers.EnsureLineExistsFromMultiLineString(
                $"{BaseAttribute.DefaultValuePrefix}: [null]",
                actualDescription
                );

            // -------- Lines that should NOT there --------

            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.RequiredPrefix,
                actualDescription
                );

            TestHelpers.EnsureLineDoesNotExistFromMultiLineString(
                BaseAttribute.ValueIsSecretPrefix,
                actualDescription
                );
        }
Ejemplo n.º 25
0
        public void ArgumentTooSmallTest()
        {
            this.cakeArgs.Setup(
                m => m.HasArgument(optionalArgName)
                ).Returns(true);

            this.cakeArgs.Setup(
                m => m.GetArgument(optionalArgName)
                ).Returns((minValue - 1).ToString());

            AggregateException e = Assert.Throws <AggregateException>(
                () => ArgumentBinder.FromArguments <OptionalArgument>(this.cakeContext.Object)
                );

            Assert.AreEqual(1, e.InnerExceptions.Count);
            Assert.IsTrue(e.InnerExceptions[0] is ArgumentTooSmallException);
        }
        // ---------------- Functions ----------------

        /// <summary>
        /// Runs git on the local repository and returns
        /// the name of the current branch.
        /// </summary>
        /// <param name="config">
        /// Configuration.  If null, it grabs the configuration
        /// from the passed in command-line arguments.
        /// </param>
        public string Run(GitQueryCurrentBranchConfig config = null)
        {
            if (config == null)
            {
                config = ArgumentBinder.FromArguments <GitQueryCurrentBranchConfig>(this.context);
            }

            string branch = null;

            string onStdOut(string line)
            {
                if (string.IsNullOrWhiteSpace(line) == false)
                {
                    branch = line;
                }

                return(line);
            };

            ProcessSettings processSettings = new ProcessSettings
            {
                Arguments = ProcessArgumentBuilder.FromString("rev-parse --abbrev-ref HEAD"),
                RedirectStandardOutput          = true,
                RedirectedStandardOutputHandler = onStdOut
            };

            this.Run(this.toolSettings, processSettings.Arguments, processSettings, null);

            if (branch == null)
            {
                throw new InvalidOperationException(
                          "Could not get the current branch from Git"
                          );
            }

            if (config.NoPrint == false)
            {
                context.Information($"Current Branch: {branch}");
            }

            return(branch);
        }
            // ---------------- Functions ----------------

            public override string ToString()
            {
                return(ArgumentBinder.ConfigToStringHelper(this));
            }
Ejemplo n.º 28
0
 public void Teardown()
 {
     _binder = null;
     _fromQueryStringBinder = null;
 }
Ejemplo n.º 29
0
 public void Setup()
 {
     Mock<IConverter> converter = new Mock<IConverter>(MockBehavior.Strict);
     converter.Setup(instance => instance.ConvertTo(It.IsAny<Type>(), It.IsAny<IRequestInfo>())).Returns<Type, IRequestInfo>(Convert);
     converter.Setup(instance => instance.ConvertTo(It.IsAny<Type>(), It.IsAny<string>())).Returns<Type, string>(Convert);
     _fromQueryStringBinder = new Mock<IParameterSourceArgumentBinder<FromQueryStringAttribute>>();
     _binder = new ArgumentBinder(new IParameterSourceArgumentBinder[]
         {
             _fromQueryStringBinder.Object,
         });
 }
Ejemplo n.º 30
0
        // ----------------- Functions -----------------

        public override void Run(ICakeContext context)
        {
            DeleteHelpersConfig config = ArgumentBinder.FromArguments <DeleteHelpersConfig>(context);

            context.DeleteDirectories(config);
        }
Ejemplo n.º 31
0
 public void Teardown()
 {
     _binder = null;
     _fromQueryStringBinder = null;
 }
Ejemplo n.º 32
0
 public void Teardown()
 {
     _requestMapping = null;
     _argumentBinder = null;
 }