Skip to content

Commit 70eed6f

Browse files
committed
Fix null not being regarded as a valid filter
1 parent f99bc72 commit 70eed6f

2 files changed

Lines changed: 31 additions & 22 deletions

File tree

src/Core/NameFilter.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,29 @@ public static bool IsValidExpression(string expression)
9797
/// <returns>True if the expression is valid, false otherwise.</returns>
9898
public static bool IsValidFilterExpression(string toTest)
9999
{
100-
if ( toTest == null ) {
101-
throw new ArgumentNullException("toTest");
102-
}
103-
104100
bool result = true;
105101

106102
try {
107-
string[] items = SplitQuoted(toTest);
108-
for (int i = 0; i < items.Length; ++i) {
109-
if ((items[i] != null) && (items[i].Length > 0)) {
110-
string toCompile;
103+
if (toTest != null) {
104+
string[] items = SplitQuoted(toTest);
105+
for (int i = 0; i < items.Length; ++i) {
106+
if ((items[i] != null) && (items[i].Length > 0)) {
107+
string toCompile;
111108

112-
if (items[i][0] == '+') {
113-
toCompile = items[i].Substring(1, items[i].Length - 1);
114-
}
115-
else if (items[i][0] == '-') {
116-
toCompile = items[i].Substring(1, items[i].Length - 1);
117-
}
118-
else {
119-
toCompile = items[i];
120-
}
109+
if (items[i][0] == '+') {
110+
toCompile = items[i].Substring(1, items[i].Length - 1);
111+
}
112+
else if (items[i][0] == '-') {
113+
toCompile = items[i].Substring(1, items[i].Length - 1);
114+
}
115+
else {
116+
toCompile = items[i];
117+
}
121118

122-
Regex testRegex = new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Singleline);
123-
}
124-
}
119+
Regex testRegex = new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Singleline);
120+
}
121+
}
122+
}
125123
}
126124
catch (ArgumentException) {
127125
result = false;

tests/Core/Core.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,22 @@ public void FilterQuoting()
3333
}
3434
}
3535

36+
[Test]
37+
public void NullFilter()
38+
{
39+
NameFilter nf = new NameFilter(null);
40+
Assert.IsTrue(nf.IsIncluded("o78i6bgv5rvu\\kj//&*"));
41+
}
42+
3643
[Test]
3744
public void ValidFilter()
3845
{
39-
Assert.IsTrue(NameFilter.IsValidFilterExpression("a"));
40-
Assert.IsFalse(NameFilter.IsValidFilterExpression(@"\,)"));
46+
Assert.IsTrue(NameFilter.IsValidFilterExpression(null));
47+
Assert.IsTrue(NameFilter.IsValidFilterExpression(string.Empty));
48+
Assert.IsTrue(NameFilter.IsValidFilterExpression("a"));
49+
50+
Assert.IsFalse(NameFilter.IsValidFilterExpression(@"\,)"));
51+
Assert.IsFalse(NameFilter.IsValidFilterExpression(@"[]"));
4152
}
4253
}
4354
}

0 commit comments

Comments
 (0)