|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | +using ebuild.cli; |
| 4 | + |
| 5 | +namespace ebuild.Tests.Unit |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class CliParserTests |
| 9 | + { |
| 10 | + class TestRoot : Command |
| 11 | + { |
| 12 | + [Option("verbose", ShortName = "v")] |
| 13 | + public bool Verbose; |
| 14 | + |
| 15 | + [Option("define", ShortName = "D")] |
| 16 | + public string? Define; |
| 17 | + [Argument(0, Name = "input")] |
| 18 | + public string? Input; |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void Parses_long_option_with_equals() |
| 23 | + { |
| 24 | + var parser = new CliParser(typeof(TestRoot)); |
| 25 | + parser.Parse(new[] { "cmd", "--define=Z=1" }); |
| 26 | + parser.ApplyParsedToCommands(); |
| 27 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 28 | + Assert.That(root.Define, Is.EqualTo("Z=1")); |
| 29 | + } |
| 30 | + |
| 31 | + [Test] |
| 32 | + public void Negative_number_is_argument_when_no_option() |
| 33 | + { |
| 34 | + var parser = new CliParser(typeof(TestRoot)); |
| 35 | + parser.Parse(new[] { "cmd", "-42", "foo" }); |
| 36 | + parser.ApplyParsedToCommands(); |
| 37 | + Assert.That(parser.ParsedOptions.Count, Is.EqualTo(0)); |
| 38 | + Assert.That(parser.ParsedArguments.Count, Is.EqualTo(2)); |
| 39 | + Assert.That(parser.ParsedArguments[0].Value, Is.EqualTo("-42")); |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void Positional_argument_maps_to_field() |
| 44 | + { |
| 45 | + var parser = new CliParser(typeof(TestRoot)); |
| 46 | + parser.Parse(new[] { "cmd", "input.txt" }); |
| 47 | + parser.ApplyParsedToCommands(); |
| 48 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 49 | + Assert.That(root.Input, Is.EqualTo("input.txt")); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void Duplicate_option_policy_error_throws() |
| 54 | + { |
| 55 | + var parser = new CliParser(typeof(TestRoot), DuplicateOptionPolicy.Error); |
| 56 | + parser.Parse(new[] { "cmd", "--define=one", "--define=two" }); |
| 57 | + Assert.Throws<InvalidOperationException>(() => parser.ApplyParsedToCommands()); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void Duplicate_option_policy_warn_last_wins() |
| 62 | + { |
| 63 | + var parser = new CliParser(typeof(TestRoot), DuplicateOptionPolicy.Warn); |
| 64 | + parser.Parse(new[] { "cmd", "--define=one", "--define=two" }); |
| 65 | + parser.ApplyParsedToCommands(); |
| 66 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 67 | + Assert.That(root.Define, Is.EqualTo("two")); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void Duplicate_option_policy_ignore_last_wins() |
| 72 | + { |
| 73 | + var parser = new CliParser(typeof(TestRoot), DuplicateOptionPolicy.Ignore); |
| 74 | + parser.Parse(new[] { "cmd", "--define=one", "--define=two" }); |
| 75 | + parser.ApplyParsedToCommands(); |
| 76 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 77 | + Assert.That(root.Define, Is.EqualTo("two")); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void Disallow_combined_short_flags() |
| 82 | + { |
| 83 | + var parser = new CliParser(typeof(TestRoot)); |
| 84 | + parser.Parse(new[] { "cmd", "-abc" }); |
| 85 | + parser.ApplyParsedToCommands(); |
| 86 | + // Should treat as a single option name 'abc' |
| 87 | + Assert.That(parser.ParsedOptions.Count, Is.EqualTo(1)); |
| 88 | + Assert.That(parser.ParsedOptions[0].Name, Is.EqualTo("abc")); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public void Short_option_attached_value_with_equals_parses() |
| 93 | + { |
| 94 | + var parser = new CliParser(typeof(TestRoot)); |
| 95 | + parser.Parse(new[] { "cmd", "-DZLIB=1.2.11" }); |
| 96 | + parser.ApplyParsedToCommands(); |
| 97 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 98 | + Assert.That(root.Define, Is.EqualTo("ZLIB=1.2.11")); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void Short_option_attached_quoted_value_parses() |
| 103 | + { |
| 104 | + var parser = new CliParser(typeof(TestRoot)); |
| 105 | + parser.Parse(new[] { "cmd", "-D\"USE_ZLIB\"" }); |
| 106 | + parser.ApplyParsedToCommands(); |
| 107 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 108 | + Assert.That(root.Define, Is.EqualTo("USE_ZLIB")); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void Short_option_attached_quoted_value_with_spaces_parses() |
| 113 | + { |
| 114 | + var parser = new CliParser(typeof(TestRoot)); |
| 115 | + parser.Parse(new[] { "cmd", "-D\"Use ZLIB Data\"" }); |
| 116 | + parser.ApplyParsedToCommands(); |
| 117 | + var root = (TestRoot)parser.currentCommandChain.First!.Value; |
| 118 | + Assert.That(root.Define, Is.EqualTo("Use ZLIB Data")); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments