Solution restructured to use multiple test projects
This commit is contained in:
305
test/AMWD.Common.Tests/Cli/CommandLineParserTest.cs
Normal file
305
test/AMWD.Common.Tests/Cli/CommandLineParserTest.cs
Normal file
@@ -0,0 +1,305 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using AMWD.Common.Cli;
|
||||
|
||||
namespace AMWD.Common.Tests.Cli
|
||||
{
|
||||
[TestClass]
|
||||
public class CommandLineParserTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void ShouldParseStringToArgs()
|
||||
{
|
||||
// arrange
|
||||
|
||||
// act
|
||||
string[] result = CommandLineParser.ParseArgsString("Option1 \"Option 2\" \"Some \"\" Option\" Foo=Bar \\ /help \\\\backslash \\\"escapedquote \\test");
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(9, result.Length);
|
||||
|
||||
Assert.AreEqual("Option1", result[0]);
|
||||
Assert.AreEqual("Option 2", result[1]);
|
||||
Assert.AreEqual("Some \" Option", result[2]);
|
||||
Assert.AreEqual("Foo=Bar", result[3]);
|
||||
Assert.AreEqual("\\", result[4]);
|
||||
Assert.AreEqual("/help", result[5]);
|
||||
Assert.AreEqual("\\backslash", result[6]);
|
||||
Assert.AreEqual("\"escapedquote", result[7]);
|
||||
Assert.AreEqual("\\test", result[8]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldReadArgs()
|
||||
{
|
||||
// arrange
|
||||
var parser = new CommandLineParser();
|
||||
|
||||
// act
|
||||
parser.ReadArgs("Option1 \"Option 2\"");
|
||||
string[] args = parser.GetType().GetField("_args", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(parser) as string[];
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(args);
|
||||
Assert.AreEqual(2, args.Length);
|
||||
|
||||
Assert.AreEqual("Option1", args[0]);
|
||||
Assert.AreEqual("Option 2", args[1]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldRegisterOptions()
|
||||
{
|
||||
// arrange
|
||||
var parser = new CommandLineParser();
|
||||
|
||||
// act
|
||||
parser.RegisterOption("opt1");
|
||||
parser.RegisterOption("opt2", 1);
|
||||
parser.RegisterOption("opt3", 2).Required().Single();
|
||||
parser.RegisterOption("opt4").Alias("option4");
|
||||
|
||||
var options = parser.GetType().GetField("_options", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(parser) as List<Option>;
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(options);
|
||||
Assert.AreEqual(4, options.Count);
|
||||
|
||||
Assert.AreEqual(1, options.ElementAt(0).Names.Count);
|
||||
Assert.AreEqual("opt1", options.ElementAt(0).Names.First());
|
||||
Assert.AreEqual(0, options.ElementAt(0).ParameterCount);
|
||||
Assert.IsFalse(options.ElementAt(0).IsSingle);
|
||||
Assert.IsFalse(options.ElementAt(0).IsRequired);
|
||||
|
||||
Assert.AreEqual(1, options.ElementAt(1).Names.Count);
|
||||
Assert.AreEqual("opt2", options.ElementAt(1).Names.First());
|
||||
Assert.AreEqual(1, options.ElementAt(1).ParameterCount);
|
||||
Assert.IsFalse(options.ElementAt(1).IsSingle);
|
||||
Assert.IsFalse(options.ElementAt(1).IsRequired);
|
||||
|
||||
Assert.AreEqual(1, options.ElementAt(2).Names.Count);
|
||||
Assert.AreEqual("opt3", options.ElementAt(2).Names.First());
|
||||
Assert.AreEqual(2, options.ElementAt(2).ParameterCount);
|
||||
Assert.IsTrue(options.ElementAt(2).IsSingle);
|
||||
Assert.IsTrue(options.ElementAt(2).IsRequired);
|
||||
|
||||
Assert.AreEqual(2, options.ElementAt(3).Names.Count);
|
||||
Assert.AreEqual("opt4", options.ElementAt(3).Names.First());
|
||||
Assert.AreEqual("option4", options.ElementAt(3).Names.Last());
|
||||
Assert.AreEqual(0, options.ElementAt(3).ParameterCount);
|
||||
Assert.IsFalse(options.ElementAt(3).IsSingle);
|
||||
Assert.IsFalse(options.ElementAt(3).IsRequired);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldParse()
|
||||
{
|
||||
// arrange
|
||||
string argString = "/opt1 /opt2:two -opt3=three1 three2 --opt4:four /test:done -- foo bar";
|
||||
|
||||
var parser = new CommandLineParser();
|
||||
parser.RegisterOption("opt1");
|
||||
parser.RegisterOption("opt2", 1);
|
||||
parser.RegisterOption("opt3", 2);
|
||||
parser.RegisterOption("opt4", 1);
|
||||
var opt = parser.RegisterOption("notUsed");
|
||||
parser.RegisterOption("testing", 1);
|
||||
|
||||
parser.ReadArgs(argString);
|
||||
|
||||
// act
|
||||
parser.Parse();
|
||||
|
||||
// assert
|
||||
Assert.IsFalse(opt.IsSet);
|
||||
Assert.AreEqual(7, parser.Arguments.Length);
|
||||
Assert.AreEqual(2, parser.FreeArguments.Length);
|
||||
|
||||
Assert.AreEqual("foo", parser.FreeArguments.First());
|
||||
Assert.AreEqual("bar", parser.FreeArguments.Last());
|
||||
|
||||
Assert.IsTrue(parser.Arguments.ElementAt(0).Option.IsSet);
|
||||
Assert.IsNull(parser.Arguments.ElementAt(0).Option.Value);
|
||||
Assert.AreEqual(0, parser.Arguments.ElementAt(0).Values.Length);
|
||||
|
||||
Assert.IsTrue(parser.Arguments.ElementAt(1).Option.IsSet);
|
||||
Assert.AreEqual("two", parser.Arguments.ElementAt(1).Option.Value);
|
||||
Assert.AreEqual(1, parser.Arguments.ElementAt(1).Values.Length);
|
||||
Assert.AreEqual("two", parser.Arguments.ElementAt(1).Values.First());
|
||||
|
||||
Assert.IsTrue(parser.Arguments.ElementAt(2).Option.IsSet);
|
||||
Assert.AreEqual("three1", parser.Arguments.ElementAt(2).Option.Value);
|
||||
Assert.AreEqual(2, parser.Arguments.ElementAt(2).Values.Length);
|
||||
Assert.AreEqual("three1", parser.Arguments.ElementAt(2).Values.First());
|
||||
Assert.AreEqual("three2", parser.Arguments.ElementAt(2).Values.Last());
|
||||
|
||||
Assert.IsTrue(parser.Arguments.ElementAt(3).Option.IsSet);
|
||||
Assert.AreEqual("four", parser.Arguments.ElementAt(3).Option.Value);
|
||||
Assert.AreEqual(1, parser.Arguments.ElementAt(3).Values.Length);
|
||||
Assert.AreEqual("four", parser.Arguments.ElementAt(3).Values.First());
|
||||
|
||||
Assert.IsTrue(parser.Arguments.ElementAt(4).Option.IsSet);
|
||||
Assert.AreEqual("testing", parser.Arguments.ElementAt(4).Option.Names.First());
|
||||
Assert.AreEqual("done", parser.Arguments.ElementAt(4).Option.Value);
|
||||
Assert.AreEqual(1, parser.Arguments.ElementAt(4).Values.Length);
|
||||
Assert.AreEqual("done", parser.Arguments.ElementAt(4).Values.First());
|
||||
|
||||
Assert.IsNull(parser.Arguments.ElementAt(5).Option);
|
||||
Assert.AreEqual("foo", parser.Arguments.ElementAt(5).Value);
|
||||
Assert.AreEqual(1, parser.Arguments.ElementAt(5).Values.Length);
|
||||
|
||||
Assert.IsNull(parser.Arguments.ElementAt(6).Option);
|
||||
Assert.AreEqual("bar", parser.Arguments.ElementAt(6).Value);
|
||||
Assert.AreEqual(1, parser.Arguments.ElementAt(6).Values.Length);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldExecuteOptionActionOnParse()
|
||||
{
|
||||
// arrange
|
||||
Argument actionArgument = null;
|
||||
|
||||
string[] args = ["/run", "--opt"];
|
||||
var parser = new CommandLineParser();
|
||||
parser.RegisterOption("opt").Required();
|
||||
parser.RegisterOption("run").Do(arg => actionArgument = arg);
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(actionArgument);
|
||||
Assert.IsNotNull(actionArgument.Option);
|
||||
Assert.AreEqual("run", actionArgument.Option.Names.First());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldReturnSetOptions()
|
||||
{
|
||||
// arrange
|
||||
string argString = "/Opt1 --opt3";
|
||||
|
||||
var parser = new CommandLineParser();
|
||||
parser.ReadArgs(argString);
|
||||
|
||||
parser.RegisterOption("opt1");
|
||||
parser.RegisterOption("opt2");
|
||||
parser.RegisterOption("opt3");
|
||||
|
||||
// act
|
||||
var opts = parser.SetOptions;
|
||||
|
||||
// assert
|
||||
Assert.AreEqual(2, opts.Length);
|
||||
|
||||
Assert.AreEqual("opt1", opts.First().Names.First());
|
||||
Assert.AreEqual("opt3", opts.Last().Names.First());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void ShouldThrowExceptionOnNullArgs()
|
||||
{
|
||||
string[] args = null;
|
||||
var parser = new CommandLineParser();
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert - ArgumentNullException
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(Exception))]
|
||||
public void ShouldThrowExceptionOnMultipleAutocomplete()
|
||||
{
|
||||
// arrange
|
||||
string[] args = ["/Opt:on"];
|
||||
var parser = new CommandLineParser
|
||||
{
|
||||
IsCaseSensitive = true
|
||||
};
|
||||
parser.RegisterOption("Option1", 1);
|
||||
parser.RegisterOption("Option2", 1);
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert - Exception
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(Exception))]
|
||||
public void ShouldThrowExceptionOnMissingOption()
|
||||
{
|
||||
// arrange
|
||||
string[] args = ["/Option:on"];
|
||||
var parser = new CommandLineParser
|
||||
{
|
||||
AutoCompleteOptions = false
|
||||
};
|
||||
parser.RegisterOption("Opt1", 1);
|
||||
parser.RegisterOption("Opt2", 1);
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert - Exception
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(Exception))]
|
||||
public void ShouldTrhowExceptionOnDuplicateOption()
|
||||
{
|
||||
// arrange
|
||||
string[] args = ["/Opt:on", "--opt=off"];
|
||||
var parser = new CommandLineParser();
|
||||
parser.RegisterOption("opt", 1).Single();
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert - Exception
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(Exception))]
|
||||
public void ShouldThrowExceptionOnMissingArgument()
|
||||
{
|
||||
// arrange
|
||||
string[] args = ["/Option"];
|
||||
var parser = new CommandLineParser();
|
||||
parser.RegisterOption("option", 1);
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert - Exception
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(Exception))]
|
||||
public void ShouldThrowExceptionForMissingRequiredOption()
|
||||
{
|
||||
// arrange
|
||||
string[] args = ["/opt"];
|
||||
var parser = new CommandLineParser();
|
||||
parser.RegisterOption("opt").Required();
|
||||
parser.RegisterOption("foo").Required();
|
||||
|
||||
// act
|
||||
parser.Parse(args);
|
||||
|
||||
// assert - Exception
|
||||
Assert.Fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
89
test/AMWD.Common.Tests/Cli/EnumerableWalkerTest.cs
Normal file
89
test/AMWD.Common.Tests/Cli/EnumerableWalkerTest.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AMWD.Common.Cli;
|
||||
|
||||
namespace AMWD.Common.Tests.Cli
|
||||
{
|
||||
[TestClass]
|
||||
public class EnumerableWalkerTest
|
||||
{
|
||||
private List<string> _list;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
_list = ["one", "two", "three", "four"];
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void ShouldThrowExceptionOnNullReference()
|
||||
{
|
||||
// arrange
|
||||
|
||||
// act
|
||||
_ = new EnumerableWalker<object>(null);
|
||||
|
||||
// assert - ArgumentNullException
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldReturnEnumerator()
|
||||
{
|
||||
// arrange
|
||||
var walker = new EnumerableWalker<string>(_list);
|
||||
|
||||
// act
|
||||
var enumerator = walker.GetEnumerator();
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(enumerator);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldReturnGenericEnumerator()
|
||||
{
|
||||
// arrange
|
||||
var walker = new EnumerableWalker<string>(_list);
|
||||
|
||||
// act
|
||||
var enumerator = ((IEnumerable<string>)walker).GetEnumerator();
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(enumerator);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldReturnItems()
|
||||
{
|
||||
// arrange
|
||||
var walker = new EnumerableWalker<string>(_list);
|
||||
_ = walker.GetEnumerator();
|
||||
|
||||
string[] items = new string[_list.Count];
|
||||
|
||||
// act
|
||||
for (int i = 0; i < _list.Count; i++)
|
||||
items[i] = walker.GetNext();
|
||||
|
||||
// assert
|
||||
for (int i = 0; i < _list.Count; i++)
|
||||
Assert.AreEqual(_list[i], items[i], $"Position {i} failed");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldReturnDefaultWhenNothingLeft()
|
||||
{
|
||||
// arrange
|
||||
var walker = new EnumerableWalker<string>(Array.Empty<string>());
|
||||
_ = walker.GetEnumerator();
|
||||
|
||||
// act
|
||||
string item = walker.GetNext();
|
||||
|
||||
// assert
|
||||
Assert.AreEqual(default, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user