Skip to content

Commit

Permalink
Bug fix regex options
Browse files Browse the repository at this point in the history
  • Loading branch information
mstama committed Jun 29, 2017
1 parent fc9d739 commit 4910445
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Merchant/Services/CommandRegexParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class CommandRegexParser : ICommandParser
private const string _mapPattern = @"^ *(?<From>\w+) +is +(?<To>\w+) *$";
private const string _muchPattern = @"^ *how +much +is +(?<AlienValue>.*\b) *\? *$";
private const string _ratePattern = @"^ *(?<AlienValue>.*) +(?<Commodity>\w+) +is +(?<TargetValue>\d+) +(?<Target>\w+) *$";
private static readonly Regex _manyExpression = new Regex(_manyPattern, RegexOptions.CultureInvariant & RegexOptions.IgnoreCase);
private static readonly Regex _mapExpression = new Regex(_mapPattern, RegexOptions.CultureInvariant & RegexOptions.IgnoreCase);
private static readonly Regex _muchExpression = new Regex(_muchPattern, RegexOptions.CultureInvariant & RegexOptions.IgnoreCase);
private static readonly Regex _rateExpression = new Regex(_ratePattern, RegexOptions.CultureInvariant & RegexOptions.IgnoreCase);
private static readonly Regex _manyExpression = new Regex(_manyPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
private static readonly Regex _mapExpression = new Regex(_mapPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
private static readonly Regex _muchExpression = new Regex(_muchPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
private static readonly Regex _rateExpression = new Regex(_ratePattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

/// <summary>
/// Parse the text and returns a command
Expand Down
60 changes: 60 additions & 0 deletions UnitTests/CommandParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ public void TestManyQueryCommand()
Assert.Equal<string>("Iron", final.Commodity);
}

[Fact]
[Trait("Category", _category)]
public void TestManyQueryCommandUpper()
{
// Arrange
string input = "HOW MANY CREDITS IS GLOB PROK IRON ?";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<ManyQueryCommand>(output);
var final = output as ManyQueryCommand;
Assert.Equal<string>("GLOB PROK", final.AlienValue);
Assert.Equal<string>("IRON", final.Commodity);
}

[Fact]
[Trait("Category", _category)]
public void TestMapCommand()
Expand All @@ -41,6 +56,21 @@ public void TestMapCommand()
Assert.Equal<string>("I", final.To);
}

[Fact]
[Trait("Category", _category)]
public void TestMapCommandUpper()
{
// Arrange
string input = "GLOB IS I";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<MapCommand>(output);
var final = output as MapCommand;
Assert.Equal<string>("GLOB", final.From);
Assert.Equal<string>("I", final.To);
}

[Fact]
[Trait("Category", _category)]
public void TestMuchQueryCommand()
Expand All @@ -55,6 +85,20 @@ public void TestMuchQueryCommand()
Assert.Equal<string>("pish tegj glob glob", final.AlienValue);
}

[Fact]
[Trait("Category", _category)]
public void TestMuchQueryCommandUpper()
{
// Arrange
string input = "HOW MUCH IS PISH TEGJ GLOB GLOB ?";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<MuchQueryCommand>(output);
var final = output as MuchQueryCommand;
Assert.Equal<string>("PISH TEGJ GLOB GLOB", final.AlienValue);
}

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down Expand Up @@ -84,6 +128,22 @@ public void TestRateCommand()
Assert.Equal<int>(34, final.CreditValue);
}

[Fact]
[Trait("Category", _category)]
public void TestRateCommandUpper()
{
// Arrange
string input = "GLOB GLOB SILVER IS 34 CREDITS";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<RateCommand>(output);
var final = output as RateCommand;
Assert.Equal<string>("GLOB GLOB", final.Amount);
Assert.Equal<String>("SILVER", final.Commodity);
Assert.Equal<int>(34, final.CreditValue);
}

[Fact]
[Trait("Category", _category)]
public void TestUnknownCommand()
Expand Down
60 changes: 60 additions & 0 deletions UnitTests/CommandRegexParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ public void TestManyQueryCommand()
Assert.Equal<string>("Iron", final.Commodity);
}

[Fact]
[Trait("Category", _category)]
public void TestManyQueryCommandUpper()
{
// Arrange
string input = "HOW MANY CREDITS IS GLOB PROK IRON ?";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<ManyQueryCommand>(output);
var final = output as ManyQueryCommand;
Assert.Equal<string>("GLOB PROK", final.AlienValue);
Assert.Equal<string>("IRON", final.Commodity);
}

[Fact]
[Trait("Category", _category)]
public void TestMapCommand()
Expand All @@ -41,6 +56,21 @@ public void TestMapCommand()
Assert.Equal<string>("I", final.To);
}

[Fact]
[Trait("Category", _category)]
public void TestMapCommandUpper()
{
// Arrange
string input = "GLOB IS I";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<MapCommand>(output);
var final = output as MapCommand;
Assert.Equal<string>("GLOB", final.From);
Assert.Equal<string>("I", final.To);
}

[Fact]
[Trait("Category", _category)]
public void TestMuchQueryCommand()
Expand All @@ -55,6 +85,20 @@ public void TestMuchQueryCommand()
Assert.Equal<string>("pish tegj glob glob", final.AlienValue);
}

[Fact]
[Trait("Category", _category)]
public void TestMuchQueryCommandUpper()
{
// Arrange
string input = "HOW MUCH IS PISH TEGJ GLOB GLOB ?";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<MuchQueryCommand>(output);
var final = output as MuchQueryCommand;
Assert.Equal<string>("PISH TEGJ GLOB GLOB", final.AlienValue);
}

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down Expand Up @@ -84,6 +128,22 @@ public void TestRateCommand()
Assert.Equal<int>(34, final.CreditValue);
}

[Fact]
[Trait("Category", _category)]
public void TestRateCommandUpper()
{
// Arrange
string input = "GLOB GLOB SILVER IS 34 CREDITS";
// Act
var output = _target.Parse(input);
// Assert
Assert.IsType<RateCommand>(output);
var final = output as RateCommand;
Assert.Equal<string>("GLOB GLOB", final.Amount);
Assert.Equal<String>("SILVER", final.Commodity);
Assert.Equal<int>(34, final.CreditValue);
}

[Fact]
[Trait("Category", _category)]
public void TestUnknownCommand()
Expand Down

0 comments on commit 4910445

Please sign in to comment.