|
1 | 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
| 4 | +using System.Collections.Generic; |
| 5 | +using System.CommandLine.Parsing; |
| 6 | +using System.IO; |
4 | 7 | using FluentAssertions; |
| 8 | +using System.Linq; |
5 | 9 | using Xunit; |
6 | 10 |
|
7 | 11 | namespace System.CommandLine.Tests |
@@ -50,6 +54,142 @@ public void When_there_is_no_default_value_then_GetDefaultValue_throws() |
50 | 54 | .Be("Argument \"the-arg\" does not have a default value"); |
51 | 55 | } |
52 | 56 |
|
| 57 | + public class CustomParsing |
| 58 | + { |
| 59 | + [Fact] |
| 60 | + public void HasDefaultValue_can_be_set_to_true() |
| 61 | + { |
| 62 | + var argument = new Argument<FileSystemInfo>(result => null, true); |
| 63 | + |
| 64 | + argument.HasDefaultValue |
| 65 | + .Should() |
| 66 | + .BeTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void HasDefaultValue_can_be_set_to_false() |
| 71 | + { |
| 72 | + var argument = new Argument<FileSystemInfo>(result => null, false); |
| 73 | + |
| 74 | + argument.HasDefaultValue |
| 75 | + .Should() |
| 76 | + .BeFalse(); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void GetDefaultValue_returns_specified_value() |
| 81 | + { |
| 82 | + var argument = new Argument<string>(result => "the-default", isDefault: true); |
| 83 | + |
| 84 | + argument.GetDefaultValue() |
| 85 | + .Should() |
| 86 | + .Be("the-default"); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_without_setting_a_value() |
| 91 | + { |
| 92 | + var argument = new Argument<string>(result => null, isDefault: true); |
| 93 | + |
| 94 | + argument.GetDefaultValue() |
| 95 | + .Should() |
| 96 | + .BeNull(); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_and_sets_value_to_null() |
| 101 | + { |
| 102 | + var argument = new Argument<string>(result => null, isDefault: true); |
| 103 | + |
| 104 | + argument.GetDefaultValue() |
| 105 | + .Should() |
| 106 | + .BeNull(); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void GetDefaultValue_can_return_null() |
| 111 | + { |
| 112 | + var argument = new Argument<string>(result => null, isDefault: true); |
| 113 | + |
| 114 | + argument.GetDefaultValue() |
| 115 | + .Should() |
| 116 | + .BeNull(); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void validation_failure_message() |
| 121 | + { |
| 122 | + var argument = new Argument<FileSystemInfo>(result => |
| 123 | + { |
| 124 | + result.ErrorMessage = "oops!"; |
| 125 | + return null; |
| 126 | + }); |
| 127 | + |
| 128 | + argument.Parse("x") |
| 129 | + .Errors |
| 130 | + .Should() |
| 131 | + .ContainSingle(e => e.SymbolResult.Symbol == argument) |
| 132 | + .Which |
| 133 | + .Message |
| 134 | + .Should() |
| 135 | + .Be("oops!"); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void custom_parsing_of_scalar_value_from_an_argument_with_one_token() |
| 140 | + { |
| 141 | + var argument = new Argument<int>(result => int.Parse(result.Tokens.Single().Value)); |
| 142 | + |
| 143 | + argument.Parse("123") |
| 144 | + .FindResultFor(argument) |
| 145 | + .GetValueOrDefault() |
| 146 | + .Should() |
| 147 | + .Be(123); |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public void custom_parsing_of_sequence_value_from_an_argument_with_one_token() |
| 152 | + { |
| 153 | + var argument = new Argument<IEnumerable<int>>(result => result.Tokens.Single().Value.Split(',').Select(int.Parse)); |
| 154 | + |
| 155 | + argument.Parse("1,2,3") |
| 156 | + .FindResultFor(argument) |
| 157 | + .GetValueOrDefault() |
| 158 | + .Should() |
| 159 | + .BeEquivalentTo(new[] { 1, 2, 3 }); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_tokens() |
| 164 | + { |
| 165 | + var argument = new Argument<IEnumerable<int>>(result => |
| 166 | + { |
| 167 | + return result.Tokens.Select(t => int.Parse(t.Value)).ToArray(); |
| 168 | + }); |
| 169 | + |
| 170 | + argument.Parse("1 2 3") |
| 171 | + .FindResultFor(argument) |
| 172 | + .GetValueOrDefault() |
| 173 | + .Should() |
| 174 | + .BeEquivalentTo(new[] { 1, 2, 3 }); |
| 175 | + } |
| 176 | + |
| 177 | + [Fact] |
| 178 | + public void custom_parsing_of_scalar_value_from_an_argument_with_multiple_tokens() |
| 179 | + { |
| 180 | + var argument = new Argument<int>(result => result.Tokens.Select(t => int.Parse(t.Value)).Sum()) |
| 181 | + { |
| 182 | + Arity = ArgumentArity.ZeroOrMore |
| 183 | + }; |
| 184 | + |
| 185 | + argument.Parse("1 2 3") |
| 186 | + .FindResultFor(argument) |
| 187 | + .GetValueOrDefault() |
| 188 | + .Should() |
| 189 | + .Be(6); |
| 190 | + } |
| 191 | + } |
| 192 | + |
53 | 193 | protected override Symbol CreateSymbol(string name) |
54 | 194 | { |
55 | 195 | return new Argument(name); |
|
0 commit comments