Skip to content

Commit 5c2315c

Browse files
committed
refactor(options): simplify validation logic for format option
1 parent 423e63c commit 5c2315c

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

src/Options/FormatsOption.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ public FormatsOption()
2121
.Select(token => token.Value)
2222
.Where(value => !string.IsNullOrWhiteSpace(value))
2323
.ToArray();
24-
if(values != null)
24+
foreach(var value in values)
2525
{
26-
foreach(var value in values)
26+
if(!_allowedValues.Contains(value))
2727
{
28-
if(!_allowedValues.Contains(value))
29-
{
30-
result.AddError($"Invalid format '{value}'. Allowed values: {string.Join(", ", _allowedValues)}");
31-
}
28+
result.AddError($"Invalid format '{value}'. Allowed values: {string.Join(", ", _allowedValues)}");
3229
}
3330
}
3431
});

tests/PowerUtils.BenchmarkDotnet.Reporter.Tests/Options/FormatOptionTests.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public void CompareCommand_ShouldHave_FormatsOption()
3333
formatsOption.Aliases.Count.ShouldBe(1);
3434
formatsOption.Aliases.ShouldContain("-f");
3535
formatsOption.Description.ShouldBe("Output format for the report.");
36+
(formatsOption.GetDefaultValue() as string[]).ShouldBe(["console"]);
3637
}
3738

3839
[Theory]
39-
[InlineData("markdown", true)]
40-
[InlineData("jSOn", true)]
41-
[InlineData("HIT-TXT", true)]
42-
[InlineData("console", true)]
43-
[InlineData("invalid-format", false)]
44-
public void Test_Validation_Format_Option(string format, bool isValid)
40+
[InlineData("markdown")]
41+
[InlineData("jSOn")]
42+
[InlineData("HIT-TXT")]
43+
[InlineData("console")]
44+
public void When_Format_Is_Valid_Shouldnt_Have_Validation_Error(string format)
4545
{
4646
// Arrange
4747
var command = "compare";
@@ -58,6 +58,31 @@ public void Test_Validation_Format_Option(string format, bool isValid)
5858
var firstOptionResult = parseResult.GetResult(formatsOption);
5959

6060
// Assert
61-
firstOptionResult?.Errors.Count().ShouldBe(isValid ? 0 : 1);
61+
firstOptionResult?.Errors.Count().ShouldBe(0);
62+
}
63+
64+
[Theory]
65+
[InlineData("invalid-format")]
66+
[InlineData("csv")]
67+
[InlineData("html")]
68+
public void When_Format_Is_Invalid_Should_Have_Validation_Error(string? format)
69+
{
70+
// Arrange
71+
var command = "compare";
72+
var option = "--format";
73+
74+
var toolCommands = new ToolCommands(_provider);
75+
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == command);
76+
var formatsOption = compareCommand.Options.Single(o => o.Name == option);
77+
var validation = formatsOption.Validators.Single();
78+
79+
80+
// Act
81+
var parseResult = toolCommands.Parse($"{command} {option} {format}");
82+
var firstOptionResult = parseResult.GetResult(formatsOption);
83+
84+
// Assert
85+
firstOptionResult?.Errors.Count().ShouldBe(1);
86+
firstOptionResult?.Errors.ShouldContain(e => e.Message == $"Invalid format '{format}'. Allowed values: console, markdown, json, hit-txt");
6287
}
6388
}

tests/PowerUtils.BenchmarkDotnet.Reporter.Tests/Options/OptionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ public void CompareCommand_ShouldHave_OutputOption()
103103
outputOption.Aliases.Count.ShouldBe(1);
104104
outputOption.Aliases.ShouldContain("-o");
105105
outputOption.Description.ShouldBe("Output directory to export the diff report. Default is current directory.");
106+
(outputOption.GetDefaultValue() as string).ShouldBe("./BenchmarkReporter");
106107
}
107108
}

0 commit comments

Comments
 (0)