|
| 1 | +// Copyright (c) Ubiquity.NET Contributors. All rights reserved. |
| 2 | +// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information. |
| 3 | + |
| 4 | +using System.CommandLine; |
| 5 | + |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | + |
| 8 | +namespace Ubiquity.NET.CommandLine.UT |
| 9 | +{ |
| 10 | + /// <summary>This is mostly for validation.understanding of the underlying RAW API as well as a good place to put "samples" for bug reports</summary> |
| 11 | + [TestClass] |
| 12 | + public class RawApiTests |
| 13 | + { |
| 14 | + [TestMethod] |
| 15 | + [Ignore( "https://github.com/dotnet/command-line-api/issues/2664" )] |
| 16 | + public void RawApi_Version_Error_tests( ) |
| 17 | + { |
| 18 | + var rootCommand = new RootCommand("Test Root") |
| 19 | + { |
| 20 | + new Option<string>("--option1") |
| 21 | + { |
| 22 | + Description = "Test option `", |
| 23 | + Required = true, |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + var result = rootCommand.Parse(["--FooBar", "--version"]); |
| 28 | + Assert.HasCount( 3, result.Errors, "Errors should account for, bogus arg (`--FooBar`), missing required arg (`--option1`), AND that `--version` should be solo" ); |
| 29 | + } |
| 30 | + |
| 31 | + [TestMethod] |
| 32 | + [Ignore( "https://github.com/dotnet/command-line-api/issues/2664" )] |
| 33 | + public void RawApi_Help_Error_tests( ) |
| 34 | + { |
| 35 | + var rootCommand = new RootCommand("Test Root") |
| 36 | + { |
| 37 | + new Option<string>("--option1") |
| 38 | + { |
| 39 | + Description = "Test option `", |
| 40 | + Required = true, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + var result = rootCommand.Parse(["--FooBar", "--help"]); |
| 45 | + Assert.HasCount( 3, result.Errors, "Errors should account for bogus arg (`--FooBar`), missing required arg (`--option1`), AND that `--version` should be solo" ); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + [Ignore( "https://github.com/dotnet/command-line-api/issues/2659" )] |
| 50 | + public void RawApi_Version_Only_with_required_has_no_errors( ) |
| 51 | + { |
| 52 | + var rootCommand = new RootCommand("Test Root") |
| 53 | + { |
| 54 | + new Option<string>("--option1") |
| 55 | + { |
| 56 | + Description = "Test option `", |
| 57 | + Required = true, |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + var result = rootCommand.Parse(["--version"]); |
| 62 | + Assert.HasCount( 0, result.Errors, "Should not be any errors" ); |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + [Ignore("https://github.com/dotnet/command-line-api/issues/2664")] |
| 67 | + public void RawApi_Version_with_required_option_has_errors( ) |
| 68 | + { |
| 69 | + var rootCommand = new RootCommand("Test Root") |
| 70 | + { |
| 71 | + new Option<string>("--option1") |
| 72 | + { |
| 73 | + Description = "Test option `", |
| 74 | + Required = true, |
| 75 | + }, |
| 76 | + }; |
| 77 | + |
| 78 | + var result = rootCommand.Parse(["--version", "--option1"]); |
| 79 | + |
| 80 | + // This assert will fail - result.Errors.Count == 3! |
| 81 | + // result.Errors: |
| 82 | + // [0]{--version option cannot be combined with other arguments.} |
| 83 | + // [1]{Required argument missing for option: '--option1'.} |
| 84 | + // [2]{Required argument missing for option: '--option1'.} // Why is this occurring twice? |
| 85 | + //Assert.HasCount( 2, result.Errors, "Should be two errors (version not used solo, missing arg)" ); |
| 86 | + |
| 87 | + // try with arguments in reversed order (--version is later) |
| 88 | + result = rootCommand.Parse(["--option1", "--version"]); |
| 89 | + |
| 90 | + // result.Action == null! [BUG] |
| 91 | + // result.Errors.Count == 0! [BUG] |
| 92 | + Assert.HasCount( 2, result.Errors, "Should be two errors (version not used solo, missing arg)" ); |
| 93 | + |
| 94 | + result = rootCommand.Parse("--option1 --version"); |
| 95 | + |
| 96 | + // result.Action == null! [BUG] |
| 97 | + // result.Errors.Count == 0! [BUG] |
| 98 | + Assert.HasCount( 2, result.Errors, "Should be two errors (version not used solo, missing arg)" ); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments