public void TestParse() { // Arrange var parser = new ParameterParserBuilder() .AddFlag("Foo", shortName: "f", longName: "foo") .AddFlag("Bar", longName: "bar") .AddValue("OptionalValue", longName: "optional") .AddValue("MandatoryValue", shortNames: new[] { "m", "A" }, optional: false) .AddValue("SecondMandatory", longNames: new[] { "second", "mandatory" }, optional: false) .Build(); var cliParams = "--optional ValueOne -f --mandatory ValueTwo ArgOne ArgTwo".Split(' '); // Act var result = parser.Parse(cliParams); // Assert Assert.NotNull(result, "Result nullability"); Assert.IsTrue(result.HasParameter("Foo"), "Parameter Foo present"); Assert.IsFalse(result.HasParameter("Bar"), "Parameter Bar present"); Assert.IsTrue(result.HasParameter("OptionalValue"), "Parameter OptionalValue present"); Assert.IsFalse(result.HasParameter("MandatoryValue"), "Parameter MandatoryValue present"); Assert.IsTrue(result.HasParameter("SecondMandatory"), "Parameter SecondMandatory present"); Assert.AreEqual("ValueOne", result.GetParameterValue("OptionalValue"), "OptionalValue value"); Assert.AreEqual("ValueTwo", result.GetParameterValue("SecondMandatory"), "SecondMandatory value"); Assert.AreEqual(new[] { "ArgOne", "ArgTwo" }, result.GetRemainingArguments(), "Remaining args"); }
public void TestParseEmptyOptionalValue() { // Arrange var parser = new ParameterParserBuilder <SampleClass>() .AddFlag(x => x.Foo, shortName: "f", longName: "foo") .AddFlag(x => x.Bar, longName: "bar") .AddValue(x => x.OptionalValue, longName: "optional") .AddValue(x => x.MandatoryValue, shortNames: new[] { "m", "A" }, optional: false) .AddValue(x => x.SecondMandatory, longNames: new[] { "second", "mandatory" }, optional: false) .RemainingArguments(x => x.RemainingArgs) .Build(); var cliParams = "--optional -f --mandatory ValueTwo ArgOne ArgTwo".Split(' '); // Act var result = parser.Parse(cliParams); // Assert Assert.NotNull(result, "Result nullability"); Assert.IsTrue(result.Foo, "Parameter Foo present"); Assert.IsFalse(result.Bar, "Parameter Bar present"); Assert.NotNull(result.OptionalValue, "Parameter OptionalValue present"); Assert.IsNull(result.MandatoryValue, "Parameter MandatoryValue present"); Assert.NotNull(result.SecondMandatory, "Parameter SecondMandatory present"); Assert.AreEqual(String.Empty, result.OptionalValue, "OptionalValue value"); Assert.AreEqual("ValueTwo", result.SecondMandatory, "SecondMandatory value"); Assert.AreEqual(new[] { "ArgOne", "ArgTwo" }, result.RemainingArgs, "Remaining args"); }
public void TestAddOptionalValue() { // Arrange var sut = new ParameterParserBuilder(); // Act sut.AddValue("Foo", shortName: "f", longName: "foo", optional: true); var result = sut.Build(); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual(1, result.GetRegisteredParameters().Length, "Count of parameters"); var param = result.GetRegisteredParameters().First(); Assert.AreEqual("Foo", param.Identifier, "Param identifier"); Assert.AreEqual("f", param.ShortNames.First(), "Param short name"); Assert.AreEqual("foo", param.LongNames.First(), "Param long name"); Assert.AreEqual(ParameterBehaviour.ValueOptional, param.Behaviour, "Param behaviour"); }
public void TestAddValue() { // Arrange var sut = new ParameterParserBuilder <SampleClass>(); // Act sut.AddValue(x => x.ValueProperty, shortName: "f", longName: "foo", optional: false); var result = sut.Build(); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual(1, result.GetRegisteredParameters().Length, "Count of parameters"); var param = result.GetRegisteredParameters().First(); Assert.AreEqual(Reflect.Property <SampleClass>(x => x.ValueProperty), param.Identifier, "Param identifier"); Assert.AreEqual("f", param.ShortNames.First(), "Param short name"); Assert.AreEqual("foo", param.LongNames.First(), "Param long name"); Assert.AreEqual(ParameterBehaviour.ValueRequired, param.Behaviour, "Param behaviour"); }