Skip to content

Commit

Permalink
- Fixed Issue VerbalExpressions#22
Browse files Browse the repository at this point in the history
- added test to make sure an ArgumentOutOfRangeException is thrown when the params [] has a length bigger then 3
  • Loading branch information
kjellski committed Sep 7, 2013
1 parent 6da7614 commit 2ab1a0b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 116 deletions.
48 changes: 25 additions & 23 deletions VerbalExpressions/VerbalExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public VerbalExpressions AnythingBut(string value, bool sanitize = true)
return Add(value, false);
}

public VerbalExpressions Something() {
public VerbalExpressions Something()
{
return Add("(.+)", false);
}

Expand Down Expand Up @@ -252,38 +253,39 @@ public VerbalExpressions Range(params object[] arguments)
throw new ArgumentNullException("arguments");
}

if (arguments.Length == 1)
if (arguments.Length == 1 || arguments.Length > 3)
{
throw new ArgumentOutOfRangeException("arguments");
}

string[] sanitizedStrings = arguments.Select(argument =>
{
if (object.ReferenceEquals(argument, null))
{
if (object.ReferenceEquals(argument, null))
{
return string.Empty;
}

string casted = argument.ToString();
if (string.IsNullOrEmpty(casted))
{
return string.Empty;
}
else
{
return Sanitize(casted);
}
})
.Where(sanitizedString => !string.IsNullOrEmpty(sanitizedString))
.OrderBy(s => s)
.ToArray();
return string.Empty;
}

string casted = argument.ToString();
if (string.IsNullOrEmpty(casted))
{
return string.Empty;
}
else
{
return Sanitize(casted);
}
})
.Where(sanitizedString =>
!string.IsNullOrEmpty(sanitizedString))
.OrderBy(s => s)
.ToArray();

if (!sanitizedStrings.Any())
{
return this;
}

bool hasOddNumberOfParams = (sanitizedStrings.Length%2) > 0;
bool hasOddNumberOfParams = (sanitizedStrings.Length % 2) > 0;

StringBuilder sb = new StringBuilder("[");
for (int _from = 0; _from < sanitizedStrings.Length; _from += 2)
Expand Down Expand Up @@ -361,8 +363,8 @@ public VerbalExpressions AddModifier(char modifier)
_modifiers |= RegexOptions.Multiline;
break;
case 's':
_modifiers |= RegexOptions.Singleline;
break;
_modifiers |= RegexOptions.Singleline;
break;
}

return this;
Expand Down
196 changes: 103 additions & 93 deletions VerbalExpressionsUnitTests/RangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,106 @@
using CSharpVerbalExpressions;
using NUnit.Framework;

namespace VerbalExpressionsUnitTests
{
[TestFixture]
public class RangeTests
{
[Test]
public void Range_WhenOddNumberOfItemsInArray_ShouldAppendLastElementWithOrClause()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
string text = "abcd7sdadqascdaswde";
object[] range = new object[3] { 1, 6, 7 };

//Act
verbEx.Range(range);
//Assert
Assert.IsTrue(verbEx.IsMatch(text));
}

[Test]
public void Range_WhenOddNumberOfItemsInArray_ShouldAppendWithPipe()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] range = new object[3] { 1, 6, 7 };
string expectedExpression = "[1-6]|7";

//Act
verbEx.Range(range);

//Assert
Assert.AreEqual(expectedExpression, verbEx.ToString());
}

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Range_WhenNullParameterPassed_ShouldThrowArgumentNullException()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] value = null;

//Act
//Assert
verbEx.Range(value);
}

[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Range_WhenArrayParameterHasOnlyOneValue_ShouldThrowArgumentOutOfRangeException()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] value = new object[1] { 0 };

//Act
//Assert
verbEx.Range(value);
}

[Test]
public void Range_WhenArrayParameterHasValuesInReverseOrder_ReturnsCorrectResultForCorrectOrder()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] inversedOrderArray = new object[2] { 9, 2 };
verbEx.Range(inversedOrderArray);
string lookupString = "testing 8 another test";

//Act
bool isMatch = verbEx.IsMatch(lookupString);

//Assert
Assert.IsTrue(isMatch);
}

[Test]
public void Range_WhenArrayContainsNullParameter_ItIsIgnoredAndRemovedFromList()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] inversedOrderArray = new object[4] { 1, null, null, 7 };
verbEx.Range(inversedOrderArray);
string lookupString = "testing 5 testing";

//Act
bool isMatch = verbEx.IsMatch(lookupString);

//Assert
Assert.IsTrue(isMatch);
}
}
}
namespace VerbalExpressionsUnitTests
{
[TestFixture]
public class RangeTests
{
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Range_WhenTooManyItemsInArray_ShouldThrowArgumentOutOfRangeException()
{
var verbEx = VerbalExpressions.DefaultExpression;
object[] range = new object[4] { 1, 6, 7, 12 };

verbEx.Range(range);
}

[Test]
public void Range_WhenOddNumberOfItemsInArray_ShouldAppendLastElementWithOrClause()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
string text = "abcd7sdadqascdaswde";
object[] range = new object[3] { 1, 6, 7 };

//Act
verbEx.Range(range);
//Assert
Assert.IsTrue(verbEx.IsMatch(text));
}

[Test]
public void Range_WhenOddNumberOfItemsInArray_ShouldAppendWithPipe()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] range = new object[3] { 1, 6, 7 };
string expectedExpression = "[1-6]|7";

//Act
verbEx.Range(range);

//Assert
Assert.AreEqual(expectedExpression, verbEx.ToString());
}

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Range_WhenNullParameterPassed_ShouldThrowArgumentNullException()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] value = null;

//Act
//Assert
verbEx.Range(value);
}

[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Range_WhenArrayParameterHasOnlyOneValue_ShouldThrowArgumentOutOfRangeException()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] value = new object[1] { 0 };

//Act
//Assert
verbEx.Range(value);
}

[Test]
public void Range_WhenArrayParameterHasValuesInReverseOrder_ReturnsCorrectResultForCorrectOrder()
{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] inversedOrderArray = new object[2] { 9, 2 };
verbEx.Range(inversedOrderArray);
string lookupString = "testing 8 another test";

//Act
bool isMatch = verbEx.IsMatch(lookupString);

//Assert
Assert.IsTrue(isMatch);
}

[Test]
public void Range_WhenArrayContainsNullParameter_ItIsIgnoredAndRemovedFromList()

This comment has been minimized.

Copy link
@psoholt

psoholt Sep 10, 2013

This test doesn't seem to pass...

{
//Arrange
var verbEx = VerbalExpressions.DefaultExpression;
object[] inversedOrderArray = new object[4] { 1, null, null, 7 };
verbEx.Range(inversedOrderArray);
string lookupString = "testing 5 testing";

//Act
bool isMatch = verbEx.IsMatch(lookupString);

//Assert
Assert.IsTrue(isMatch);
}
}
}

0 comments on commit 2ab1a0b

Please sign in to comment.