Skip to content

Commit

Permalink
Try to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jan 31, 2022
1 parent d8cf2e2 commit aef16d7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion test/CommandLineUtils.Tests/AttributeValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public OptionBuilderApp(TestConsole testConsole)
: base(testConsole)
{
Option("-e|--email", "Email", CommandOptionType.SingleValue)
.Accepts().EmailAddress();
.Accepts().EmailAddress("Invalid Email");

Option("-n|--name", "Name", CommandOptionType.SingleValue)
.Accepts().MinLength(1);
Expand All @@ -115,6 +115,9 @@ public OptionBuilderApp(TestConsole testConsole)

Option("-r|--regex", "Regex", CommandOptionType.SingleValue)
.Accepts().RegularExpression("^abc.*");

Option("-m|--mode", "Mode", CommandOptionType.SingleValue)
.Accepts().Satisfies<ModeValidationAttribute>("With an error message from model validation");
}
}

Expand All @@ -132,6 +135,9 @@ private class OptionApp
[Option, RegularExpression("^abc.*")]
public string? Regex { get; }

[Option, ModeValidation]
public string? Mode { get; }

private void OnExecute() { }
}

Expand All @@ -149,6 +155,8 @@ private void OnExecute() { }
[InlineData(new[] { "-a", "abcdefghijk" }, 1)]
[InlineData(new[] { "-r", "abcdefghijk" }, 0)]
[InlineData(new[] { "-r", "xyz" }, 1)]
[InlineData(new[] { "-m", "xyz" }, 1)]
[InlineData(new[] { "-m", "mode" }, 0)]
public void ValidatesAttributesOnOption(string[] args, int exitCode)
{
Assert.Equal(exitCode, CommandLineApplication.Execute<OptionApp>(new TestConsole(_output), args));
Expand Down Expand Up @@ -179,5 +187,14 @@ public override bool IsValid(object value)
&& app.Arg1 != null && app.Arg1.Contains("good")
&& app.Arg2 != null && app.Arg2.Contains("good");
}

[AttributeUsage(AttributeTargets.Property)]
private sealed class ModeValidationAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value is string text && text.Contains("mode");
}
}
}
}

0 comments on commit aef16d7

Please sign in to comment.