-
Notifications
You must be signed in to change notification settings - Fork 302
[Test Improver] Add unit tests for CommandLineParseResult #8249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Evangelink
merged 2 commits into
main
from
test-assist/commandline-parse-result-tests-a3720d0efbb63e1f
May 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletions
230
...UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/CommandLineParseResultTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Testing.Platform.CommandLine; | ||
|
|
||
| namespace Microsoft.Testing.Platform.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public sealed class CommandLineParseResultTests | ||
| { | ||
| [TestMethod] | ||
| public void Empty_HasNoTool_NoOptions_NoErrors() | ||
| { | ||
| CommandLineParseResult empty = CommandLineParseResult.Empty; | ||
|
|
||
| Assert.IsNull(empty.ToolName); | ||
| Assert.IsFalse(empty.HasTool); | ||
| Assert.IsFalse(empty.HasError); | ||
| Assert.IsEmpty(empty.Options); | ||
| Assert.IsEmpty(empty.Errors); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void HasTool_TrueWhenToolNameProvided() | ||
| { | ||
| var result = new CommandLineParseResult("mytool", [], []); | ||
|
|
||
| Assert.IsTrue(result.HasTool); | ||
| Assert.AreEqual("mytool", result.ToolName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void HasTool_FalseWhenToolNameIsNull() | ||
| { | ||
| var result = new CommandLineParseResult(null, [], []); | ||
|
|
||
| Assert.IsFalse(result.HasTool); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void HasError_TrueWhenErrorsPresent() | ||
| { | ||
| var result = new CommandLineParseResult(null, [], ["some error"]); | ||
|
|
||
| Assert.IsTrue(result.HasError); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void HasError_FalseWhenNoErrors() | ||
| { | ||
| var result = new CommandLineParseResult(null, [], []); | ||
|
|
||
| Assert.IsFalse(result.HasError); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOptionSet_ReturnsTrueForKnownOption() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("verbose", [])], []); | ||
|
|
||
| Assert.IsTrue(result.IsOptionSet("verbose")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOptionSet_IsCaseInsensitive() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("verbose", [])], []); | ||
|
|
||
| Assert.IsTrue(result.IsOptionSet("VERBOSE")); | ||
| Assert.IsTrue(result.IsOptionSet("Verbose")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOptionSet_StripsLeadingDashes() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("verbose", [])], []); | ||
|
|
||
| Assert.IsTrue(result.IsOptionSet("--verbose")); | ||
| Assert.IsTrue(result.IsOptionSet("-verbose")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void IsOptionSet_ReturnsFalseForUnknownOption() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("verbose", [])], []); | ||
|
|
||
| Assert.IsFalse(result.IsOptionSet("quiet")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGetOptionArgumentList_ReturnsTrueAndArgumentsForKnownOption() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("output", ["file.txt"])], []); | ||
|
|
||
| bool found = result.TryGetOptionArgumentList("output", out string[]? args); | ||
|
|
||
| Assert.IsTrue(found); | ||
| Assert.IsNotNull(args); | ||
| Assert.HasCount(1, args); | ||
| Assert.AreEqual("file.txt", args[0]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGetOptionArgumentList_ReturnsFalseForUnknownOption() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("output", ["file.txt"])], []); | ||
|
|
||
| bool found = result.TryGetOptionArgumentList("missing", out string[]? args); | ||
|
|
||
| Assert.IsFalse(found); | ||
| Assert.IsNull(args); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGetOptionArgumentList_IsCaseSensitive() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("output", ["file.txt"])], []); | ||
|
|
||
| bool found = result.TryGetOptionArgumentList("OUTPUT", out string[]? args); | ||
|
|
||
| Assert.IsFalse(found); | ||
| Assert.IsNull(args); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGetOptionArgumentList_StripsLeadingDashes() | ||
| { | ||
| var result = new CommandLineParseResult(null, [new CommandLineParseOption("output", ["file.txt"])], []); | ||
|
|
||
| bool found = result.TryGetOptionArgumentList("--output", out string[]? args); | ||
|
|
||
| Assert.IsTrue(found); | ||
| Assert.IsNotNull(args); | ||
| Assert.HasCount(1, args); | ||
| Assert.AreEqual("file.txt", args[0]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGetOptionArgumentList_CombinesArgumentsFromMultipleOccurrences() | ||
| { | ||
| var result = new CommandLineParseResult( | ||
| null, | ||
| [ | ||
| new CommandLineParseOption("filter", ["Class1"]), | ||
| new CommandLineParseOption("filter", ["Class2"]), | ||
| ], | ||
| []); | ||
|
|
||
| bool found = result.TryGetOptionArgumentList("filter", out string[]? args); | ||
|
|
||
| Assert.IsTrue(found); | ||
| Assert.IsNotNull(args); | ||
| Assert.HasCount(2, args); | ||
| Assert.Contains("Class1", args); | ||
| Assert.Contains("Class2", args); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_ReturnsTrueForIdenticalResults() | ||
| { | ||
| var a = new CommandLineParseResult("tool", [new CommandLineParseOption("opt", ["val"])], ["err"]); | ||
| var b = new CommandLineParseResult("tool", [new CommandLineParseOption("opt", ["val"])], ["err"]); | ||
|
|
||
| Assert.AreEqual(a, b); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_ReturnsFalseWhenToolNameDiffers() | ||
| { | ||
| var a = new CommandLineParseResult("tool1", [], []); | ||
| var b = new CommandLineParseResult("tool2", [], []); | ||
|
|
||
| Assert.AreNotEqual(a, b); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_ReturnsFalseWhenErrorsDiffer() | ||
| { | ||
| var a = new CommandLineParseResult(null, [], ["error1"]); | ||
| var b = new CommandLineParseResult(null, [], ["error2"]); | ||
|
|
||
| Assert.AreNotEqual(a, b); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_ReturnsFalseWhenOptionsDiffer() | ||
| { | ||
| var a = new CommandLineParseResult(null, [new CommandLineParseOption("opt1", [])], []); | ||
| var b = new CommandLineParseResult(null, [new CommandLineParseOption("opt2", [])], []); | ||
|
|
||
| Assert.AreNotEqual(a, b); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_ReturnsFalseForNull() | ||
| { | ||
| var a = new CommandLineParseResult(null, [], []); | ||
|
|
||
| Assert.IsFalse(a.Equals(null)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_ReturnsTrueForSameReference() | ||
| { | ||
| var a = new CommandLineParseResult("tool", [], []); | ||
|
|
||
| Assert.AreEqual(a, a); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ToString_ContainsToolNameAndOptions() | ||
| { | ||
| var result = new CommandLineParseResult("mytool", [new CommandLineParseOption("opt", ["val"])], ["an error"]); | ||
|
|
||
| string text = result.ToString(); | ||
|
|
||
| Assert.IsTrue(text.Contains("mytool", StringComparison.Ordinal)); | ||
| Assert.IsTrue(text.Contains("opt", StringComparison.Ordinal)); | ||
| Assert.IsTrue(text.Contains("an error", StringComparison.Ordinal)); | ||
| } | ||
|
Evangelink marked this conversation as resolved.
|
||
|
|
||
| [TestMethod] | ||
| public void ToString_EmptyResult_ContainsNone() | ||
| { | ||
| string text = CommandLineParseResult.Empty.ToString(); | ||
|
|
||
| Assert.IsTrue(text.Contains($"Errors:{Environment.NewLine} None", StringComparison.Ordinal)); | ||
| Assert.IsTrue(text.Contains($"Options:{Environment.NewLine} None", StringComparison.Ordinal)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.