Permalink
Browse files

Add cmd line options error handling

Change-Id: Iab57431531ff4e256c72e0b173539766056e836e
1 parent 4c518fc commit fc1c06fb7e94c461b3091e7f929ca71f0bcce751 @TonyRoussel TonyRoussel committed Aug 2, 2016
@@ -30,6 +30,6 @@
[assembly: AssemblyProduct("NinjaTurtlesMutation")]
[assembly: AssemblyCopyright("Copyright © 2016 Tony Roussel")]
-[assembly: AssemblyVersion("1.1.0.2")]
+[assembly: AssemblyVersion("1.1.0.3")]
[assembly: AssemblyInformationalVersion("BETA 1.1")]
-[assembly: AssemblyFileVersion("1.1.0.2")]
+[assembly: AssemblyFileVersion("1.1.0.3")]
@@ -6,13 +6,17 @@ internal class MaxBusyRunner : Option
{
protected override void TakeArguments(System.Collections.Generic.Queue<string> queue)
{
+ var arg = queue.Dequeue();
try
{
- MaxBusyRunnersValue = int.Parse(queue.Dequeue());
+ MaxBusyRunnersValue = int.Parse(arg);
if (MaxBusyRunnersValue < 1)
- MaxBusyRunnersValue = 8;
+ throw new InvalidArgumentValueException("MaxBusyRunner", "MaxBusyRunner minimum is 1", arg);
+ }
+ catch (FormatException)
+ {
+ throw new OptionArgumentParseException("MaxBusyRunner", arg);
}
- catch (FormatException) { MaxBusyRunnersValue = 8;}
}
public int MaxBusyRunnersValue { get; private set; }
@@ -6,13 +6,17 @@ internal class ParallelLevel : Option
{
protected override void TakeArguments(System.Collections.Generic.Queue<string> queue)
{
+ var arg = queue.Dequeue();
try
{
- ParallelValue = int.Parse(queue.Dequeue());
+ ParallelValue = int.Parse(arg);
if (ParallelValue < 1)
- ParallelValue = 8;
+ throw new InvalidArgumentValueException("ParallelLevel", "ParallelLevel minimum is 1", arg);
+ }
+ catch (FormatException)
+ {
+ throw new OptionArgumentParseException("ParallelLevel", arg);
}
- catch (FormatException) {ParallelValue = 8;}
}
public int ParallelValue { get; private set; }
@@ -6,13 +6,18 @@ internal class SuccessThreshold : Option
{
protected override void TakeArguments(System.Collections.Generic.Queue<string> queue)
{
+ var arg = queue.Dequeue();
try
{
- MinScore = float.Parse(queue.Dequeue().Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);
+ MinScore = float.Parse(arg.Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);
if (MinScore < 0 || MinScore > 1)
- MinScore = 1;
+ throw new InvalidArgumentValueException("SuccessThreshold", "Minimum score must be between 0 and 1",
+ arg);
+ }
+ catch (FormatException)
+ {
+ throw new OptionArgumentParseException("SuccessThreshold", arg);
}
- catch (FormatException) { MinScore = 1; }
}
public float MinScore { get; private set; }
@@ -26,7 +26,7 @@ protected override void TakeArguments(Queue<string> queue)
foreach (char c in arg)
{
if (!_turtlesKeys.ContainsKey(c))
- continue;
+ throw new InvalidArgumentValueException("TurtlesTypes", "Invalid turtle key given (use 'ntm help run' for the valid key list)", c.ToString());
Types.Add(_turtlesKeys[c]);
}
}
@@ -19,17 +19,29 @@
#endregion
+using NinjaTurtlesMutation.Console.Options;
+
namespace NinjaTurtlesMutation.Console
{
class Program
{
private const int SUCCESS = 0;
- private const int VALIDATION_FAILURE = 1;
- private const int EXECUTION_FAILURE = 2;
+ private const int PARSE_FAILURE = 1;
+ private const int VALIDATION_FAILURE = 2;
+ private const int EXECUTION_FAILURE = 3;
static int Main(string[] args)
{
- var options = new CommandWithOptions(args);
+ CommandWithOptions options;
+ try
+ {
+ options = new CommandWithOptions(args);
+ }
+ catch (ArgumentException ae)
+ {
+ System.Console.Error.WriteLine(ae.Message);
+ return PARSE_FAILURE;
+ }
if (!options.Command.Validate())
{
return VALIDATION_FAILURE;
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using NinjaTurtlesMutation.Console.Options;
-using NinjaTurtlesMutation.Turtles;
using NUnit.Framework;
namespace NinjaTurtlesMutation.Tests.ConsoleOptionsTests
@@ -0,0 +1,59 @@
+using System.Collections.Generic;
+using NinjaTurtlesMutation.Console.Options;
+using NUnit.Framework;
+
+namespace NinjaTurtlesMutation.Tests.ConsoleOptionsTests
+{
+ [TestFixture]
+ public class MaxBusyRunnerTest
+ {
+ class MaxBusyRunnerExposed : MaxBusyRunner
+ {
+ public void TakeArgumentsExposed(Queue<string> queue)
+ {
+ TakeArguments(queue);
+ }
+ }
+
+ [TestCase("1", 1)]
+ [TestCase("2", 2)]
+ [TestCase("100", 100)]
+ public void GoodArgumentIsParsed(string arg, int expected)
+ {
+ var qarg = new Queue<string>();
+ var mbre = new MaxBusyRunnerExposed();
+
+ qarg.Enqueue(arg);
+ qarg.Enqueue("Fake");
+ qarg.Enqueue("plouf");
+ qarg.Enqueue("wow, much code");
+ mbre.TakeArgumentsExposed(qarg);
+
+ Assert.AreEqual(mbre.MaxBusyRunnersValue, expected);
+ }
+
+ [TestCase("0")]
+ [TestCase("-1")]
+ public void LessThanOneThrow(string badValue)
+ {
+ var qarg = new Queue<string>();
+ var mbre = new MaxBusyRunnerExposed();
+
+ qarg.Enqueue(badValue);
+ Assert.Throws<InvalidArgumentValueException>(() => mbre.TakeArgumentsExposed(qarg));
+ }
+
+ [TestCase("lolz")]
+ [TestCase("rpz91")]
+ [TestCase("html")]
+ public void NonsenseThrow(string nonsense)
+ {
+ var qarg = new Queue<string>();
+ var mbre = new MaxBusyRunnerExposed();
+
+ qarg.Enqueue(nonsense);
+ qarg.Enqueue("");
+ Assert.Throws<OptionArgumentParseException>(() => mbre.TakeArgumentsExposed(qarg));
+ }
+ }
+}
@@ -0,0 +1,58 @@
+using System.Collections.Generic;
+using NinjaTurtlesMutation.Console.Options;
+using NUnit.Framework;
+
+namespace NinjaTurtlesMutation.Tests.ConsoleOptionsTests
+{
+ class ParallelLevelTest
+ {
+ class ParralelLevelExposed : ParallelLevel
+ {
+ public void TakeArgumentsExposed(Queue<string> queue)
+ {
+ TakeArguments(queue);
+ }
+ }
+
+ [TestCase("1", 1)]
+ [TestCase("2", 2)]
+ [TestCase("100", 100)]
+ public void GoodArgumentIsParsed(string arg, int expected)
+ {
+ var qarg = new Queue<string>();
+ var plvl = new ParralelLevelExposed();
+
+ qarg.Enqueue(arg);
+ qarg.Enqueue("Fake");
+ qarg.Enqueue("plouf");
+ qarg.Enqueue("wow, much code");
+ plvl.TakeArgumentsExposed(qarg);
+
+ Assert.AreEqual(plvl.ParallelValue, expected);
+ }
+
+ [TestCase("0")]
+ [TestCase("-1")]
+ public void LessThanOneThrow(string badValue)
+ {
+ var qarg = new Queue<string>();
+ var plvl = new ParralelLevelExposed();
+
+ qarg.Enqueue(badValue);
+ Assert.Throws<InvalidArgumentValueException>(() => plvl.TakeArgumentsExposed(qarg));
+ }
+
+ [TestCase("lolz")]
+ [TestCase("rpz91")]
+ [TestCase("html")]
+ public void NonsenseThrow(string nonsense)
+ {
+ var qarg = new Queue<string>();
+ var plvl = new ParralelLevelExposed();
+
+ qarg.Enqueue(nonsense);
+ qarg.Enqueue("");
+ Assert.Throws<OptionArgumentParseException>(() => plvl.TakeArgumentsExposed(qarg));
+ }
+ }
+}
@@ -35,19 +35,29 @@ public void AssertArgumentIsParsed(string anArgument, float expected)
Assert.AreEqual(sce.MinScore, expected);
}
+ [TestCase("-1")]
+ [TestCase("2")]
+ public void OutOfRangeValueThrow(string badValue)
+ {
+ var qarg = new Queue<string>();
+ var sce = new SuccessThresholdExposed();
+
+ qarg.Enqueue(badValue);
+ qarg.Enqueue("rededesintox");
+ Assert.Throws<InvalidArgumentValueException>(() => sce.TakeArgumentsExposed(qarg));
+ }
+
[TestCase("lolz")]
[TestCase("rpz91")]
[TestCase("html")]
- public void AssertNonsenseParseAsOne(string nonsence)
+ public void NonsenseThrow(string nonsense)
{
var qarg = new Queue<string>();
var sce = new SuccessThresholdExposed();
- qarg.Enqueue(nonsence);
+ qarg.Enqueue(nonsense);
qarg.Enqueue("");
- sce.TakeArgumentsExposed(qarg);
-
- Assert.AreEqual(sce.MinScore, 1F);
+ Assert.Throws<OptionArgumentParseException>(() => sce.TakeArgumentsExposed(qarg));
}
}
}
@@ -59,5 +59,19 @@ public void AssertLowercaseAddTurtles()
Assert.IsTrue(tte.Types.Contains(typeof(VariableReadTurtle)));
Assert.IsTrue(tte.Types.Contains(typeof(VariableWriteTurtle)));
}
+
+ [TestCase("ACBFW")]
+ [TestCase("CBS3W")]
+ [TestCase("666")]
+ [TestCase("3CBSW")]
+ [TestCase("CBSW3")]
+ public void NonValidKeyThrow(string containBadKey)
+ {
+ var qarg = new Queue<string>();
+ var tte = new TurtleTypesExposed();
+
+ qarg.Enqueue(containBadKey);
+ Assert.Throws<InvalidArgumentValueException>(() => tte.TakeArgumentsExposed(qarg));
+ }
}
}
@@ -74,6 +74,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ConsoleOptionsTests\KillTimeFactorTests.cs" />
+ <Compile Include="ConsoleOptionsTests\MaxBusyRunnerTest.cs" />
+ <Compile Include="ConsoleOptionsTests\ParallelLevelTest.cs" />
<Compile Include="ConsoleOptionsTests\SuccessThresholdTests.cs" />
<Compile Include="MethodDefinitionResolverLoggingTests.cs" />
<Compile Include="ConsoleOptionsTests\TurtlesTypesTests.cs" />

0 comments on commit fc1c06f

Please sign in to comment.