Skip to content

Commit d85eb2a

Browse files
committed
Allow variables
allowing variables in filter
1 parent cab3314 commit d85eb2a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

RedditSharp/Search/DefaultSearchFormatter.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ string ISearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> sear
3838
}
3939
}
4040

41-
string searchQuery = string.Empty;
4241
Stack<string> compoundSearchStack = new Stack<string>();
4342
while(formatInfoStack.Count >0)
4443
{
@@ -114,10 +113,23 @@ private void MemberExpressionHelper(MemberExpression expression, Stack<string> s
114113

115114
}
116115
}
116+
else
117+
{
118+
searchStack.Push(InvokeGetExpression(expression).ToString());
119+
}
117120
}
118121

122+
private static object InvokeGetExpression(Expression expression)
123+
{
124+
var objectMember = Expression.Convert(expression, typeof(object));
125+
126+
var getterLambda = Expression.Lambda<Func<object>>(objectMember);
127+
128+
var getter = getterLambda.Compile();
129+
130+
return getter();
131+
}
119132

120-
121133

122134

123135

UnitTesting/AdvancedSearchTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ public void AndNotOrElseTest()
199199
Assert.AreEqual(expected, actual);
200200
}
201201

202+
[TestCategory("AdvancedSearch"), TestMethod]
203+
public void StringVariablePropertyTest()
204+
{
205+
//Arrange
206+
string title = "meirl";
207+
Expression<Func<AdvancedSearchFilter, bool>>
208+
expression = x => x.Title == title;
209+
string expected = "title:meirl";
210+
211+
ISearchFormatter searchFormatter = new DefaultSearchFormatter();
212+
213+
//Act
214+
string actual = searchFormatter.Format(expression);
215+
216+
//Assert
217+
Assert.AreEqual(expected, actual);
218+
}
219+
202220
//[TestMethod]
203221
//[TestCategory("AdvancedSearch")]
204222
//public void ValueComparison_BoolPropertyTest()

0 commit comments

Comments
 (0)