Skip to content

Commit

Permalink
Merge pull request #47 from mehmetuken/develop
Browse files Browse the repository at this point in the history
Adding StringFilter NotContains, NotStartsWith and NotEndsWith options.
  • Loading branch information
enisn authored Jan 28, 2022
2 parents 3415aad + 75da85d commit 2b3ba5d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/AutoFilterer/Types/StringFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,34 @@ public class StringFilter : IFilterableType
public virtual new string Equals { get; set; }

/// <summary>
/// Provides parameter to String.Conains method query.
/// Provides parameter to String.Contains method query.
/// </summary>
public virtual string Contains { get; set; }

/// <summary>
/// Provides parameter to !String.Contains method query.
/// </summary>
public virtual string NotContains { get; set; }

/// <summary>
/// Provides parameter to String.StartsWith method query.
/// </summary>
public virtual string StartsWith { get; set; }

/// <summary>
/// Provides parameter to !String.StartsWith method query.
/// </summary>
public virtual string NotStartsWith { get; set; }

/// <summary>
/// Provides parameter to String.EndsWith method query.
/// </summary>
public virtual string EndsWith { get; set; }

/// <summary>
/// Provides parameter to !String.EndsWith method query.
/// </summary>
public virtual string NotEndsWith { get; set; }

/// <summary>
/// <inheritdoc />
Expand Down Expand Up @@ -68,11 +83,20 @@ public virtual Expression BuildExpression(Expression expressionBody, PropertyInf
if (Contains != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.Contains) { Comparison = Compare }.BuildExpression(expressionBody, targetProperty, filterProperty, Contains), CombineWith);

if (NotContains != null)
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.Contains) { Comparison = Compare }.BuildExpression(expressionBody, targetProperty, filterProperty, NotContains)), CombineWith);

if (StartsWith != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.StartsWith) { Comparison = Compare }.BuildExpression(expressionBody, targetProperty, filterProperty, StartsWith), CombineWith);

if (NotStartsWith != null)
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.StartsWith) { Comparison = Compare }.BuildExpression(expressionBody, targetProperty, filterProperty, NotStartsWith)), CombineWith);

if (EndsWith != null)
expression = expression.Combine(new StringFilterOptionsAttribute(StringFilterOption.EndsWith) { Comparison = Compare }.BuildExpression(expressionBody, targetProperty, filterProperty, EndsWith), CombineWith);

if (NotEndsWith != null)
expression = expression.Combine(Expression.Not(new StringFilterOptionsAttribute(StringFilterOption.EndsWith) { Comparison = Compare }.BuildExpression(expressionBody, targetProperty, filterProperty, NotEndsWith)), CombineWith);

return expression;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/AutoFilterer.Tests/Types/StringFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,53 @@ public void BuildExpression_TitleWithContainsCaseInsensitive_ShouldMatchCount(Li
foreach (var item in actualResult)
Assert.Contains(item, result);
}

[Theory, AutoMoqData(count: 64)]
public void BuildExpression_TitleWithNotContains_ShouldMatchCount(List<Book> data)
{
// Arrange
var filter = new BookFilter_StringFilter_Title
{
Title = new StringFilter
{
NotContains = "ab"
}
};

// Act
var query = data.AsQueryable().ApplyFilter(filter);
var result = query.ToList();

// Assert
var actualResult = data.AsQueryable().Where(x => !x.Title.Contains(filter.Title.NotContains)).ToList();

Assert.Equal(actualResult.Count, result.Count);
foreach (var item in actualResult)
Assert.Contains(item, result);
}

[Theory, AutoMoqData(count: 64)]
public void BuildExpression_TitleWithNotContainsCaseInsensitive_ShouldMatchCount(List<Book> data)
{
// Arrange
var filter = new BookFilter_StringFilter_Title
{
Title = new StringFilter
{
NotContains = "Ab",
Compare = StringComparison.InvariantCultureIgnoreCase
}
};

// Act
var query = data.AsQueryable().ApplyFilter(filter);
var result = query.ToList();

// Assert
var actualResult = data.AsQueryable().Where(x => !x.Title.Contains(filter.Title.NotContains, StringComparison.InvariantCultureIgnoreCase)).ToList();

Assert.Equal(actualResult.Count, result.Count);
foreach (var item in actualResult)
Assert.Contains(item, result);
}
}

0 comments on commit 2b3ba5d

Please sign in to comment.