diff --git a/VerbalExpressions/VerbalExpressions.cs b/VerbalExpressions/VerbalExpressions.cs index fd07b7d..46b2ff8 100644 --- a/VerbalExpressions/VerbalExpressions.cs +++ b/VerbalExpressions/VerbalExpressions.cs @@ -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); } @@ -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) @@ -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; diff --git a/VerbalExpressionsUnitTests/RangeTests.cs b/VerbalExpressionsUnitTests/RangeTests.cs index 3e84bcb..5a74f94 100644 --- a/VerbalExpressionsUnitTests/RangeTests.cs +++ b/VerbalExpressionsUnitTests/RangeTests.cs @@ -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() + { + //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); + } + } +}