Command Line
Command Line
using
using
using
using
System.Collections.Generic;
System.IO;
System.Linq;
System.Reflection;
CommandLineParser.Arguments;
namespace CodingChallange1_800
{
public class Arguments : IArgumentsConfig<IArgumentsValues>, IArgumentsValue
s
{
private const char InputFileShortName = 'I';
private const char DictionaryShortName = 'D';
internal const string InputFileLongName = "input-file";
internal const string DictionaryLongName = "dictionary-file";
private readonly FileArgument _inputFile;
private readonly FileArgument _dictionaryFile;
public Arguments()
{
_inputFile = new FileArgument(InputFileShortName, InputFileLongName,
Help.InputFileArgument);
_dictionaryFile = new FileArgument(DictionaryShortName, DictionaryLo
ngName,
Help.DictionaryFileArgument)
{
DefaultValue = new FileInfo("defaultDictionary")
};
}
public IEnumerable<Argument> All
{
get
{
return GetType().GetFields(BindingFlags.Instance | BindingFlags.
NonPublic)
.Select(field => field.GetValue(this)).OfType<Argument>().To
List();
}
}
public FileInfo InputFile
{
get { return _inputFile.Value; }
}
public FileInfo DictionaryFile
{
get { return _dictionaryFile.Value; }
}
public IArgumentsValues AsOutput()
{
return this;
}
}
[TestFixture]
public class ArgumentsTest
{
private Arguments _arguments;
[SetUp]
public void SetUp()
{
_arguments = new Arguments();
}
[Test]
public void AllCollectsAllArguments()
{
AssertArgument<FileArgument>(Arguments.InputFileLongName);
AssertArgument<FileArgument>(Arguments.DictionaryLongName);
}
private void AssertArgument<T>(string longName) where T : Argument
{
var argument = FindArgument(longName);
Assert.IsNotNull(argument, "there should be an argument " + longName
);
Assert.IsInstanceOf(typeof(T), argument, "argument " + longName);
}
private Argument FindArgument(string longName)
{
return _arguments.All.First(x => x.LongName == longName);
}
[Test]
public void AllArgumentsShouldHaveDifferentShortNames()
{
var shortNames = _arguments.All.Select(x => x.ShortName).Where(x =>
x != ' ');
Assert.AreEqual(shortNames, shortNames.Distinct(), "distinct short n
ames");
}
[Test]
public void AllArgumentsShouldHaveDifferentLongNames()
{
var longNames = _arguments.All.Select(x => x.LongName);
Assert.AreEqual(longNames, longNames.Distinct(), "distinct long name
s");
}
[Test]
public void PropertiesHaveTheValueOfTheCorrespondingArguments()
{
InputFileArgument.Value = new FileInfo("inputFileName");
Assert.AreEqual(InputFileArgument.Value, _arguments.InputFile, "Argu
ments.InputFileName");
DictionaryFileArgument.Value = new FileInfo("dictionaryFileName");
Assert.AreEqual(DictionaryFileArgument.Value, _arguments.DictionaryF
ile,
"Arguments.DictionaryFileName");
}
[Test]
public void DictionaryArgumentIsDefaultDictionaryByDedfault()
{
Assert.AreEqual(new FileInfo("defaultDictionary").FullName, Dictiona
ryFileArgument.DefaultValue.FullName,
"DictionaryFileArgument default value");
}
[Test]
public void ReturnsItselfAsOutput()
{
_parser.ReadArgs(new [] { "--help"});
Assert.IsTrue(_parser.UsageShouldBeDisplayed, "Parser.UsageShouldBeD
isplayed");
}
[Test]
public void DoesNotShowUsageWithValidArgs()
{
ParseArgumentsWithUser();
Assert.IsFalse(_parser.UsageShouldBeDisplayed, "Parser.UsageShouldBe
Displayed");
}
[Test]
public void ByDefaultCommandLineIsEmpty()
{
GivenANewParser();
Assert.AreEqual("", _parser.CommandLine, "Command Line");
}
[Test]
public void KnowsTheActualCommandLineTextAfterAParsing()
{
var args = new[] {TestArguments.UserShortArg, "spoke", TestArguments
.NumberShortArg, "42"};
ParseArguments(args);
Assert.AreEqual(string.Join(" ", args), _parser.CommandLine, "Comman
d line");
}
[Test]
public void ActuallyParsesTheCommandLine()
{
const string User = "Bob";
const int Number = 12;
ParseArguments(TestArguments.UserShortArg, User, TestArguments.Numbe
rShortArg, Number.ToString());
Assert.AreEqual(User, _arguments.User.Value, "Parsed user argument")
;
Assert.AreEqual(Number, _arguments.Number.Value, "Parsed number argu
ment");
}
[Test, ExpectedException(typeof(InvalidConversionException))]
public void ThrowsWhenAParsingErrorOccurs()
{
ParseArgumentsWithUser(TestArguments.NumberShortArg, "malformed numb
er ...");
}
[Test]
public void CreatesTheOutputOnSuccessfulParsing()
{
Assert.AreEqual(_arguments.Output, ParseArgumentsWithUser(), "Output
built by the parser");
}
[Test]
public void HandlesUperCaseArguments()
{
const string User = "Client";
ParseArguments("--" + TestArguments.UserLongName.ToUpper(), User);
Assert.AreEqual(User, _arguments.User.Value, "User specified with up
case argument name");
}
private object ParseArgumentsWithUser(params string[] extraArgs)
{