Skip to content

Commit

Permalink
fixup parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf committed Aug 25, 2021
1 parent 47ed79d commit 2b74021
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
21 changes: 21 additions & 0 deletions src/CommandLineExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -35,12 +36,32 @@ internal static T GetValueForArgument<T>(this ParseResult result, string alias)
return GetValueForArgument<T>(result.CommandResult, alias);
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this ParseResult result, Argument<T> argument)
{
return GetValueForArgument<T>(result.CommandResult, argument);
}

[return: MaybeNull]
internal static T GetValueForOption<T>(this ParseResult result, string alias)
{
return GetValueForOption<T>(result.CommandResult, alias);
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this CommandResult result, Argument<T> argumentDefinition)
{
var arguments = result.Children.Where(x => x.Symbol.Name == argumentDefinition.Name).ToArray();
if (arguments.Length == 1 &&
arguments.SingleOrDefault() is ArgumentResult argument &&
argument.GetValueOrDefault<T>() is T t)
{
return t;
}

return default;
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this CommandResult result, string alias)
{
Expand Down
34 changes: 12 additions & 22 deletions tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Tools.Commands;
using Xunit;
Expand Down Expand Up @@ -48,10 +49,11 @@ public void CommandLine_OptionsAreParsedCorrectly()

// Act
var result = sut.Parse(new[] {
"--folder",
"--no-restore",
"--include", "include1", "include2",
"--exclude", "exclude1", "exclude2",
"--check",
"--verify-no-changes",
"--binarylog", "binary-log-path",
"--report", "report",
"--verbosity", "detailed",
"--include-generated"});
Expand All @@ -60,14 +62,15 @@ public void CommandLine_OptionsAreParsedCorrectly()
Assert.Equal(0, result.Errors.Count);
Assert.Equal(0, result.UnmatchedTokens.Count);
Assert.Equal(0, result.UnparsedTokens.Count);
Assert.True(result.GetValueForOption<bool>("--folder"));
result.GetValueForOption<bool>("--no-restore");
Assert.Collection(result.GetValueForOption<IEnumerable<string>>("--include"),
i0 => Assert.Equal("include1", i0),
i1 => Assert.Equal("include2", i1));
Assert.Collection(result.GetValueForOption<IEnumerable<string>>("--exclude"),
i0 => Assert.Equal("exclude1", i0),
i1 => Assert.Equal("exclude2", i1));
Assert.True(result.GetValueForOption<bool>("--check"));
Assert.True(result.GetValueForOption<bool>("--verify-no-changes"));
Assert.Equal("binary-log-path", result.GetValueForOption<string>("--binarylog"));
Assert.Equal("report", result.GetValueForOption<string>("--report"));
Assert.Equal("detailed", result.GetValueForOption<string>("--verbosity"));
Assert.True(result.GetValueForOption<bool>("--include-generated"));
Expand All @@ -84,7 +87,7 @@ public void CommandLine_ProjectArgument_Simple()

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal("workspaceValue", result.GetValueForArgument<string>("workspace"));
Assert.Equal("workspaceValue", result.GetValueForArgument<string>(FormatCommandCommon.SlnOrProjectArgument));
}

[Fact]
Expand All @@ -98,7 +101,7 @@ public void CommandLine_ProjectArgument_WithOption_AfterArgument()

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal("workspaceValue", result.GetValueForArgument<string>("workspace"));
Assert.Equal("workspaceValue", result.GetValueForArgument<string>(FormatCommandCommon.SlnOrProjectArgument));
Assert.Equal("detailed", result.GetValueForOption<string>("--verbosity"));
}

Expand All @@ -113,7 +116,7 @@ public void CommandLine_ProjectArgument_WithOption_BeforeArgument()

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal("workspaceValue", result.GetValueForArgument<string>("workspace"));
Assert.Equal("workspaceValue", result.GetValueForArgument<string>(FormatCommandCommon.SlnOrProjectArgument));
Assert.Equal("detailed", result.GetValueForOption<string>("--verbosity"));
}

Expand Down Expand Up @@ -163,25 +166,12 @@ public void CommandLine_FolderValidation_FailsIfNoRestoreSpecified()
var sut = RootFormatCommand.GetCommand();

// Act
var result = sut.Parse(new[] { "--folder", "--no-restore" });
var result = sut.Parse(new[] { "whitespace", "--folder", "--no-restore" });

// Assert
Assert.Equal(1, result.Errors.Count);
}

[Fact]
public void CommandLine_AnalyzerOptions_CanSpecifyBothWithDefaults()
{
// Arrange
var sut = RootFormatCommand.GetCommand();

// Act
var result = sut.Parse(new[] { "--fix-analyzers", "--fix-style" });

// Assert
Assert.Equal(0, result.Errors.Count);
}

[Fact]
public void CommandLine_BinaryLog_DoesNotFailIfPathNotSpecified()
{
Expand Down Expand Up @@ -217,7 +207,7 @@ public void CommandLine_BinaryLog_FailsIfFolderIsSpecified()
var sut = RootFormatCommand.GetCommand();

// Act
var result = sut.Parse(new[] { "--folder", "--binarylog" });
var result = sut.Parse(new[] { "whitespace", "--folder", "--binarylog" });

// Assert
Assert.Equal(1, result.Errors.Count);
Expand Down

0 comments on commit 2b74021

Please sign in to comment.