Skip to content

Commit

Permalink
fixed 163 issue (#164)
Browse files Browse the repository at this point in the history
* Added ability to skip operators and '\' with \ in filtering.
* Added unit tests for filtering '\|' and skipping operators
  • Loading branch information
AViktorovGRSE authored Dec 16, 2021
1 parent 5ef8843 commit 7b6f3c7
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 33 deletions.
51 changes: 22 additions & 29 deletions Sieve/Models/FilterTerm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,36 @@ namespace Sieve.Models
{
public class FilterTerm : IFilterTerm, IEquatable<FilterTerm>
{
public FilterTerm() { }

private const string EscapedPipePattern = @"(?<!($|[^\\])(\\\\)*?\\)\|";
private const string EscapedPipePattern = @"(?<!($|[^\\]|^)(\\\\)*?\\)\|";
private const string PipeToEscape = @"\|";

private static readonly string[] Operators = new string[] {
"!@=*",
"!_=*",
"!=*",
"!@=",
"!_=",
"==*",
"@=*",
"_=*",
"==",
"!=",
">=",
"<=",
">",
"<",
"@=",
"_="
};
private const string BackslashToEscape = @"\\";
private const string OperatorsRegEx = @"(!@=\*|!_=\*|!=\*|!@=|!_=|==\*|@=\*|_=\*|==|!=|>=|<=|>|<|@=|_=)";
private const string EscapeNegPatternForOper = @"(?<!\\)" + OperatorsRegEx;
private const string EscapePosPatternForOper = @"(?<=\\)" + OperatorsRegEx;

public string Filter
{
set
{
var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim()).ToArray();
var filterSplits = Regex.Split(value,EscapeNegPatternForOper).Select(t => t.Trim()).ToArray();

Names = Regex.Split(filterSplits[0], EscapedPipePattern).Select(t => t.Trim()).ToArray();
Values = filterSplits.Length > 1
? Regex.Split(filterSplits[1], EscapedPipePattern)

if (filterSplits.Length > 2)
{
foreach (var match in Regex.Matches(filterSplits[2],EscapePosPatternForOper))
{
var matchStr = match.ToString();
filterSplits[2] = filterSplits[2].Replace('\\' + matchStr, matchStr);
}

Values = Regex.Split(filterSplits[2], EscapedPipePattern)
.Select(t => t.Replace(PipeToEscape, "|").Trim())
.ToArray()
: null;
Operator = Array.Find(Operators, o => value.Contains(o)) ?? "==";
.Select(t => t.Replace(BackslashToEscape, "\\").Trim())
.ToArray();
}

Operator = Regex.Match(value,EscapeNegPatternForOper).Value;
OperatorParsed = GetOperatorParsed(Operator);
OperatorIsCaseInsensitive = Operator.EndsWith("*");
OperatorIsNegated = OperatorParsed != FilterOperator.NotEquals && Operator.StartsWith("!");
Expand Down
109 changes: 105 additions & 4 deletions SieveUnitTests/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public General(ITestOutputHelper testOutputHelper)
CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "D1" },
FeaturedComment = new Comment { Id = 7, Text = "D2" }
},
}
}.AsQueryable();

_comments = new List<Comment>
Expand Down Expand Up @@ -684,15 +684,116 @@ public void OrEscapedPipeValueFilteringWorks()
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "Here is | another comment"
},
new Comment
{
Id = 2,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = @"Here is \| another comment(1)"
}
}.AsQueryable();

var model = new SieveModel()
var model = new SieveModel
{
Filters = "Text==Here is \\| a comment|Here is \\| another comment",
Filters = @"Text==Here is \| a comment|Here is \| another comment|Here is \\\| another comment(1)",
};

var result = _processor.Apply(model, comments);
Assert.Equal(2, result.Count());
Assert.Equal(3, result.Count());
}

[Theory]
[InlineData("CategoryId==1,(CategoryId|LikeCount)==50")]
[InlineData("(CategoryId|LikeCount)==50,CategoryId==1")]
public void CanFilterWithEscape(string filter)
{
var model = new SieveModel
{
Filters = filter
};

var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();

Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}

[Theory]
[InlineData(@"Title@=\\")]
public void CanFilterWithEscapedBackSlash(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = "E\\",
LikeCount = 4,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "E1" },
FeaturedComment = new Comment { Id = 7, Text = "E2" }
}
}.AsQueryable();

var model = new SieveModel
{
Filters = filter
};

var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();

Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}

[Theory]
[InlineData(@"Title@=\== ")]
[InlineData(@"Title@=\!= ")]
[InlineData(@"Title@=\> ")]
[InlineData(@"Title@=\< ")]
[InlineData(@"Title@=\<= ")]
[InlineData(@"Title@=\>= ")]
[InlineData(@"Title@=\@= ")]
[InlineData(@"Title@=\_= ")]
[InlineData(@"Title@=!\@= ")]
[InlineData(@"Title@=!\_= ")]
[InlineData(@"Title@=\@=* ")]
[InlineData(@"Title@=\_=* ")]
[InlineData(@"Title@=\==* ")]
[InlineData(@"Title@=\!=* ")]
[InlineData(@"Title@=!\@=* ")]
public void CanFilterWithEscapedOperators(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = @"Operators: == != > < >= <= @= _= !@= !_= @=* _=* ==* !=* !@=* !_=* ",
LikeCount = 1,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "F1" },
FeaturedComment = new Comment { Id = 7, Text = "F2" }
}
}.AsQueryable();

var model = new SieveModel
{
Filters = filter,
};

var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();

Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}

}
}

0 comments on commit 7b6f3c7

Please sign in to comment.