1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace CmdLine.Tests
{
extern alias migrate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
public class CommandLineTest
{
[Fact]
public void CommandLineTokenizesQuestionMarkSwitches()
{
var args = new[] { "/?" };
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
var tokens = migrate::CmdLine.CommandLine.Tokenize();
Assert.Equal(1, migrate::CmdLine.CommandLine.Args.Length);
Assert.Equal(1, migrate::CmdLine.CommandLine.GetSwitches(tokens).Count);
Assert.Equal(0, migrate::CmdLine.CommandLine.GetParameters(tokens).Count);
Assert.True(tokens[0].IsCommand());
Assert.Equal("?", tokens[0].Command);
}
[Fact]
public void CommandLineTokenizesSwitchesAndParameters()
{
var args = new[]
{
"C:\\Foo And Bar\\Some Long File.txt", "/1:Test", "-2:Some Quoted Arg", "/3=Arg with in it", "/Y-",
"And another",
"Another", "One", "Word", "Args"
};
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
var tokens = migrate::CmdLine.CommandLine.Tokenize();
Assert.Equal(10, migrate::CmdLine.CommandLine.Args.Length);
Assert.Equal(4, migrate::CmdLine.CommandLine.GetSwitches(tokens).Count);
Assert.Equal(6, migrate::CmdLine.CommandLine.GetParameters(tokens).Count);
Assert.True(tokens[0].IsParameter());
Assert.True(tokens[1].IsCommand());
Assert.True(tokens[2].IsCommand());
Assert.True(tokens[3].IsCommand());
Assert.True(tokens[4].IsCommand());
Assert.True(tokens[5].IsParameter());
Assert.True(tokens[6].IsParameter());
Assert.True(tokens[7].IsParameter());
Assert.True(tokens[8].IsParameter());
Assert.True(tokens[9].IsParameter());
for (var i = 0; i < args.Length; i++)
{
Assert.Equal(args[i], tokens[i].Token);
}
}
[Fact]
public void ParseDoesNotThrowWhenNullCommandLine()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
migrate::CmdLine.CommandLine.Parse<object>();
}
[Fact]
public void MissingRequiredSwitchArgShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new migrate::CmdLine.CommandLineRequiredArgumentMissingException(typeof(string), "N", -1).Message,
Assert.Throws<migrate::CmdLine.CommandLineRequiredArgumentMissingException>(
() => migrate::CmdLine.CommandLine.Parse<TestArgs>()).Message);
}
[Fact]
public void AttributeWithNoCommandNameShouldUsePropertyName()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment("/b1-");
var actual = migrate::CmdLine.CommandLine.Parse<PropWithNoCommandName>();
Assert.False(actual.b1);
}
[Fact]
public void MissingRequiredPositionArgShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new migrate::CmdLine.CommandLineRequiredArgumentMissingException(typeof(string), "String 1", 1).Message,
Assert.Throws<migrate::CmdLine.CommandLineRequiredArgumentMissingException>(
() => migrate::CmdLine.CommandLine.Parse<ThreeRequiredPositionArgs>()).Message);
}
[Fact]
public void DuplicateArgsShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment("/N:123 /N:345");
var commandArg = new migrate::CmdLine.CommandArgument("/N:345", 7);
commandArg.Command = "N";
Assert.Equal(
new migrate::CmdLine.CommandLineArgumentInvalidException(typeof(string), commandArg).Message,
Assert.Throws<migrate::CmdLine.CommandLineArgumentInvalidException>(() => migrate::CmdLine.CommandLine.Parse<TestArgs>()).
Message);
}
[Fact]
public void DuplicateArgsWithListShouldNotThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment("/N:123 /N:345");
var actual = migrate::CmdLine.CommandLine.Parse<TestArgsWithList>();
Assert.Equal(2, actual.NList.Count);
Assert.Equal("123", actual.NList[0]);
Assert.Equal("345", actual.NList[1]);
}
[Fact]
public void DefaultArgsAreApplied()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment("/n:123");
var target = migrate::CmdLine.CommandLine.Parse<TestArgs>();
Assert.True(target.BoolT);
Assert.False(target.BoolY);
Assert.Equal(TestArgs.StringArgDefault, target.StringArg);
}
/// <summary>
/// Verifies that you can use alternate seperators
/// </summary>
[Fact]
public void CommandLineTokenizesWithAlternateSwitchChars()
{
var args = new[]
{
"C:\\Foo And Bar\\Some Long File.txt", "~1|Test", "~2|Some Quoted Arg", "~3|Arg with in it", "_Y-",
"And another",
"Another", "One", "Word", "Args"
};
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
// Save the seperators since they are static members
var oldSep = migrate::CmdLine.CommandLine.CommandSeparators;
var oldValueSep = migrate::CmdLine.CommandLine.ValueSeparators;
migrate::CmdLine.CommandLine.CommandSeparators = new List<string>
{
"~",
"_"
};
migrate::CmdLine.CommandLine.ValueSeparators = new List<string>
{
"|"
};
var tokens = migrate::CmdLine.CommandLine.Tokenize();
// Restore the seperators
migrate::CmdLine.CommandLine.CommandSeparators = oldSep;
migrate::CmdLine.CommandLine.ValueSeparators = oldValueSep;
Assert.Equal(10, migrate::CmdLine.CommandLine.Args.Length);
Assert.Equal(4, migrate::CmdLine.CommandLine.GetSwitches(tokens).Count);
Assert.Equal(6, migrate::CmdLine.CommandLine.GetParameters(tokens).Count);
Assert.True(tokens[0].IsParameter());
Assert.True(tokens[1].IsCommand());
Assert.True(tokens[2].IsCommand());
Assert.True(tokens[3].IsCommand());
Assert.True(tokens[4].IsCommand());
Assert.True(tokens[5].IsParameter());
Assert.True(tokens[6].IsParameter());
Assert.True(tokens[7].IsParameter());
Assert.True(tokens[8].IsParameter());
Assert.True(tokens[9].IsParameter());
for (var i = 0; i < args.Length; i++)
{
Assert.Equal(args[i], tokens[i].Token);
}
}
///<summary>
/// Verifies positional args work
///</summary>
[Fact]
public void PositionalArgsAreApplied()
{
var args = new[]
{
@"D:\Documents and Settings\MY.USERNAME\My Documents\*", @"E:\MYBACKUP\My Documents\", "/A",
@"/EXCLUDE:SomeQuoted String"
, "/I", "/D:7-8-2011"
};
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
var xcopyCommand = migrate::CmdLine.CommandLine.Parse<XCopyCommandArgs>();
Assert.NotNull(xcopyCommand);
Assert.Equal(args[0], xcopyCommand.Source);
Assert.Equal(args[1], xcopyCommand.Destination);
Assert.Equal("SomeQuoted String", xcopyCommand.ExcludeFiles);
Assert.True(xcopyCommand.ArchivedBit);
Assert.True(xcopyCommand.InferDirectory);
Assert.Equal(DateTime.Parse("7-8-2011"), xcopyCommand.ChangedAfterDate);
}
[Fact]
public void EmbeddedSeparatorsDoNotCountAsSwitch()
{
var args = new[] { "/S:Value/With:Separators", "/Y-", "/t", "/N:123" };
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
var actual = migrate::CmdLine.CommandLine.Parse<TestArgs>();
Assert.Equal("Value/With:Separators", actual.StringArg);
Assert.True(actual.BoolT);
Assert.False(actual.BoolY);
}
[Fact]
public void CaseSensitiveAllowsUpperAndLowerWithSameSwitch()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment("/t:lower /T:UPPER /y /Y-");
var oldValue = migrate::CmdLine.CommandLine.CaseSensitive;
migrate::CmdLine.CommandLine.CaseSensitive = true;
var actual = migrate::CmdLine.CommandLine.Parse<TypeWithUpperAndLower>();
migrate::CmdLine.CommandLine.CaseSensitive = oldValue;
Assert.Equal("lower", actual.Lower);
Assert.Equal("UPPER", actual.Upper);
Assert.True(actual.YLower);
Assert.False(actual.YUpper);
}
[Fact]
public void NoMatchingPropertyShouldThrow()
{
var args = new[] { "/?" };
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
try
{
migrate::CmdLine.CommandLine.Parse<TestArgs>();
Assert.True(false, "Parse did not throw an exception");
}
catch (migrate::CmdLine.CommandLineArgumentInvalidException exception)
{
Assert.NotNull(exception.ArgumentHelp);
Assert.Equal(4, exception.ArgumentHelp.ValidArguments.Count());
}
}
[Fact]
public void NoMatchingPropertyWithInferredShouldThrow()
{
var args = new[] { "/NoMatch" };
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
var commandArg = new migrate::CmdLine.CommandArgument("/NoMatch", 0);
commandArg.Command = "NoMatch";
Assert.Equal(
new migrate::CmdLine.CommandLineArgumentInvalidException(typeof(string), commandArg).Message,
Assert.Throws<migrate::CmdLine.CommandLineArgumentInvalidException>(
() => migrate::CmdLine.CommandLine.Parse<InferredTestArgs>()).Message);
}
[Fact]
public void TwoPropsWithSameSwitchShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new migrate::CmdLine.CommandLineException("Duplicate Command \"B\"").Message,
Assert.Throws<migrate::CmdLine.CommandLineException>(() => migrate::CmdLine.CommandLine.Parse<TwoPropsWithSameSwitch>()).
Message);
}
[Fact]
public void WhenNoAttributesParseWillUsePropertyNames()
{
var args = new[] { "/StringArg:Value/With:Separators", "/BoolY-", "/BoolT", "/Date:12-1-2011", "/Number:23" };
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment(args);
var actual = migrate::CmdLine.CommandLine.Parse<InferredTestArgs>();
Assert.Equal("Value/With:Separators", actual.StringArg);
Assert.True(actual.BoolT);
Assert.False(actual.BoolY);
Assert.Equal(DateTime.Parse("12-1-2011"), actual.Date);
Assert.Equal(23, actual.Number);
}
[Fact]
public void WhenNoPositionOneShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new migrate::CmdLine.CommandLineException(
"Out of order parameter \"source\" should have be at parameter index 1 but was found at 2").Message,
Assert.Throws<migrate::CmdLine.CommandLineException>(() => migrate::CmdLine.CommandLine.Parse<BadPositionArgNoOne>()).
Message);
}
[Fact]
public void WhenNoPositionTwoShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new migrate::CmdLine.CommandLineException(
"Out of order parameter \"destination\" should have be at parameter index 2 but was found at 3").
Message,
Assert.Throws<migrate::CmdLine.CommandLineException>(() => migrate::CmdLine.CommandLine.Parse<BadPositionArgMissingTwo>()).
Message);
}
[Fact]
public void WhenDuplicatePositionShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new migrate::CmdLine.CommandLineException("Duplicate Parameter Index [1] on Property \"S2\"").Message,
Assert.Throws<migrate::CmdLine.CommandLineException>(
() => migrate::CmdLine.CommandLine.Parse<TypeWithDuplicateParamIndex>()).Message);
}
[Fact]
public void WhenBadParameterIndexShouldThrow()
{
migrate::CmdLine.CommandLine.CommandEnvironment = new TestCommandEnvironment();
Assert.Equal(
new CustomAttributeFormatException("'ParameterIndex' property specified was not found.").Message,
Assert.Throws<CustomAttributeFormatException>(() => migrate::CmdLine.CommandLine.Parse<TypeWithBadParamIndex>()).Message);
}
}
public class TestArgsWithList
{
[migrate::CmdLine.CommandLineParameterAttribute(Command = "N")]
public List<string> NList { get; set; }
}
public class TypeWithUpperAndLower
{
[migrate::CmdLine.CommandLineParameterAttribute("t")]
public string Lower { get; set; }
[migrate::CmdLine.CommandLineParameterAttribute("T")]
public string Upper { get; set; }
[migrate::CmdLine.CommandLineParameterAttribute("y")]
public bool YLower { get; set; }
[migrate::CmdLine.CommandLineParameterAttribute("Y")]
public bool YUpper { get; set; }
}
public class TypeWithBadParamIndex
{
[migrate::CmdLine.CommandLineParameterAttribute(ParameterIndex = -1)]
public string S1 { get; set; }
}
public class TypeWithDuplicateParamIndex
{
[migrate::CmdLine.CommandLineParameterAttribute(ParameterIndex = 1)]
public string S1 { get; set; }
[migrate::CmdLine.CommandLineParameterAttribute(ParameterIndex = 1)]
public string S2 { get; set; }
}
}
|