Skip to content

Commit

Permalink
name conventions in ParamsValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
zadykian committed Apr 17, 2022
1 parent 3a09d86 commit ec736de
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/ConsoleAppFramework/ParamsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public interface IParamsValidator
/// <inheritdoc />
public class ParamsValidator : IParamsValidator
{
private readonly ConsoleAppOptions options;

public ParamsValidator(ConsoleAppOptions options) => this.options = options;

/// <inheritdoc />
ValidationResult? IParamsValidator.ValidateParameters(
IEnumerable<(ParameterInfo Parameter, object? Value)> parameters)
Expand All @@ -38,10 +42,13 @@ public class ParamsValidator : IParamsValidator

var errorMessage = string.Join(Environment.NewLine,
invalidParameters
.Select(tuple => $"{tuple.Parameter.Name!.ToLower()} ({tuple.Value}): {tuple.Result!.ErrorMessage}")
.Select(tuple =>
$"{options.NameConverter(tuple.Parameter.Name!)} " +
$"({tuple.Value}): " +
$"{tuple.Result!.ErrorMessage}")
);

return new ValidationResult($"Some parameters have invalid value:{Environment.NewLine}{errorMessage}");
return new ValidationResult($"Some parameters have invalid values:{Environment.NewLine}{errorMessage}");
}

private static ValidationResult? Validate(ParameterInfo parameterInfo, object? value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using Microsoft.Extensions.Hosting;
using Xunit;

// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedParameter.Global

Expand All @@ -22,20 +22,44 @@ public void Validate_String_Length_Test()
const string optionValue = "too-large-string-value";

var args = new[] { nameof(AppWithValidationAttributes.StrLength), $"--{optionName}", optionValue };
Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<AppWithValidationAttributes>(args);
ConsoleApp.Run<AppWithValidationAttributes>(args);

// Validation fails, so StrLength command is not executed.
console.Output.Should().NotContain(AppWithValidationAttributes.StrLengthOutput);
// Validation should fail, so StrLength command should not be executed.
console.Output.Should().NotContain(AppWithValidationAttributes.Output);

console.Output.Should().Contain(optionName);
console.Output.Should().Contain(optionValue);
}

[Fact]
public void Command_With_Multiple_Params()
{
using var console = new CaptureConsoleOutput();

var args = new[]
{
nameof(AppWithValidationAttributes.MultipleParams),
"--second-arg", "10",
"--first-arg", "invalid-email-address"
};

ConsoleApp.Run<AppWithValidationAttributes>(args);

// Validation should fail, so StrLength command should not be executed.
console.Output.Should().NotContain(AppWithValidationAttributes.Output);
}

/// <inheritdoc />
internal class AppWithValidationAttributes : ConsoleAppBase
{
public const string StrLengthOutput = $"hello from {nameof(StrLength)}";
public const string Output = $"hello from {nameof(AppWithValidationAttributes)}";

[Command(nameof(StrLength))]
public void StrLength([StringLength(maximumLength: 8)] string arg) => Console.WriteLine(Output);

public void StrLength([StringLength(maximumLength: 8)] string arg) => Console.WriteLine(StrLengthOutput);
[Command(nameof(MultipleParams))]
public void MultipleParams(
[EmailAddress] string firstArg,
[Range(0, 2)] int secondArg) => Console.WriteLine(Output);
}
}

0 comments on commit ec736de

Please sign in to comment.