Skip to content

Commit b7b969a

Browse files
committed
Add more tests
clean up and more tests
1 parent d85eb2a commit b7b969a

File tree

5 files changed

+126
-108
lines changed

5 files changed

+126
-108
lines changed

RedditSharp/Reddit.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Linq;
66
using System.Security.Authentication;
77
using System.Threading.Tasks;
8+
using RedditSharp.Search;
89
using DefaultWebAgent = RedditSharp.WebAgent;
10+
using System.Linq.Expressions;
911

1012
namespace RedditSharp
1113
{
@@ -37,6 +39,19 @@ public class Reddit
3739
private const string GetLiveEventUrl = "https://www.reddit.com/live/{0}/about";
3840

3941
#endregion
42+
private IAdvancedSearchFormatter _searchFormatter;
43+
private IAdvancedSearchFormatter SearchFormatter
44+
{
45+
get
46+
{
47+
if(_searchFormatter == null)
48+
{
49+
_searchFormatter = new DefaultSearchFormatter();
50+
}
51+
return _searchFormatter;
52+
}
53+
set => _searchFormatter = value;
54+
}
4055

4156
#region Properties
4257
internal IWebAgent WebAgent { get; set; }
@@ -382,6 +397,15 @@ public Listing<T> Search<T>(string query, Sorting sortE = Sorting.Relevance, Tim
382397
return Listing<T>.Create(this.WebAgent, string.Format(SearchUrl, query, sort, time), max, 100);
383398
}
384399

400+
public Listing<Post> AdvancedSearch(Expression<Func<AdvancedSearchFilter, bool>> searchFilter, Sorting sortE = Sorting.Relevance, TimeSorting timeE = TimeSorting.All)
401+
{
402+
string query = SearchFormatter.Format(searchFilter);
403+
string sort = sortE.ToString().ToLower();
404+
string time = timeE.ToString().ToLower();
405+
string final = string.Format(SearchUrl, query, sort, time);
406+
return new Listing<Post>(this,final,WebAgent);
407+
}
408+
385409
/// <summary>
386410
/// Return a <see cref="Listing{T}"/> of items matching search with a given time period.
387411
/// </summary>

RedditSharp/Search/DefaultSearchFormatter.cs

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88

99
namespace RedditSharp.Search
1010
{
11-
public class DefaultSearchFormatter : ISearchFormatter
11+
public class DefaultSearchFormatter : IAdvancedSearchFormatter
1212
{
13-
string ISearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> search)
13+
#region Constants
14+
private const string BOOL_PROPERTY_PREFIX = "Is";
15+
#endregion Constants
16+
17+
18+
string IAdvancedSearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> search)
1419
{
1520
Expression expression = null;
1621
Stack<Expression> expressionStack = new Stack<Expression>();
@@ -33,30 +38,39 @@ string ISearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> sear
3338
case ConstantExpression constantExpresssion:
3439
searchStack.Push(ConstantExpressionHelper(constantExpresssion));
3540
break;
41+
case MethodCallExpression methodCallExpression:
42+
searchStack.Push(MethodCallExpressionHelper(methodCallExpression));
43+
break;
3644
default:
3745
throw new NotImplementedException(expression.ToString());
3846
}
3947
}
4048

4149
Stack<string> compoundSearchStack = new Stack<string>();
42-
while(formatInfoStack.Count >0)
50+
while (formatInfoStack.Count > 0)
4351
{
4452
FormatInfo current = formatInfoStack.Pop();
4553
string[] formatParameters = new string[current.ParameterCount];
4654
int currentCount = current.ParameterCount;
47-
while(currentCount > 0)
55+
while (currentCount > 0)
4856
{
49-
formatParameters[formatParameters.Length - currentCount] = current.IsCompound ? compoundSearchStack.Pop() : searchStack.Pop();
57+
formatParameters[formatParameters.Length - currentCount] = current.IsCompound ? compoundSearchStack.Pop() : searchStack.Pop();
5058
currentCount--;
5159
}
52-
60+
5361
compoundSearchStack.Push(string.Format(current.Pattern, formatParameters));
54-
62+
5563
}
5664

5765
return compoundSearchStack.Pop();
5866
}
5967

68+
private string MethodCallExpressionHelper(MethodCallExpression expression)
69+
{
70+
var o = InvokeGetExpression(expression);
71+
return o.ToString();
72+
}
73+
6074
private string ConstantExpressionHelper(ConstantExpression constantExpresssion)
6175
{
6276
return constantExpresssion.ToString().Replace("\"","");
@@ -79,29 +93,23 @@ private void BinaryExpressionHelper(BinaryExpression expression, Stack<Expressio
7993
expressionStack.Push(expression.Left);
8094
}
8195

82-
8396
if (expression.NodeType != ExpressionType.Equal)
8497
{
8598
formatInfoStack.Push(expression.ToFormatInfo());
86-
//searchStack.Push("NOT(+{0}+)");
8799
}
88-
89-
90100
}
91101

92102

93103
private void UnaryExpressionHelper(UnaryExpression expression, Stack<Expression> expressionStack,Stack<FormatInfo> formatInfoStack)
94104
{
95-
formatInfoStack.Push( expression.ToFormatInfo());
105+
formatInfoStack.Push(expression.ToFormatInfo());
96106
expressionStack.Push(expression.Operand);
97-
//return expressionOperator;
98107
}
99108

100109
private void MemberExpressionHelper(MemberExpression expression, Stack<string> searchStack, Stack<FormatInfo> formatInfoStack)
101110
{
102111
MemberInfo member = expression.Member;
103112

104-
105113
if (member.DeclaringType == typeof(AdvancedSearchFilter))
106114
{
107115
string result = member.Name.Replace(BOOL_PROPERTY_PREFIX, string.Empty).ToLower();
@@ -110,7 +118,6 @@ private void MemberExpressionHelper(MemberExpression expression, Stack<string> s
110118
if (expression.Type == typeof(bool))
111119
{
112120
searchStack.Push("1");
113-
114121
}
115122
}
116123
else
@@ -130,30 +137,6 @@ private static object InvokeGetExpression(Expression expression)
130137
return getter();
131138
}
132139

133-
134-
135-
136-
private const string BOOL_PROPERTY_PREFIX = "Is";
137-
138-
139-
private static readonly List<ExpressionType> conditionalTypes = new List<ExpressionType>()
140-
{
141-
ExpressionType.AndAlso,
142-
ExpressionType.And,
143-
ExpressionType.OrElse,
144-
ExpressionType.Or
145-
};
146-
147-
private static readonly List<ExpressionType> evaluateExpressions = new List<ExpressionType>()
148-
{
149-
ExpressionType.Add,
150-
ExpressionType.Subtract,
151-
ExpressionType.Multiply,
152-
ExpressionType.Divide,
153-
ExpressionType.Coalesce,
154-
ExpressionType.Conditional
155-
};
156-
157140
private static bool IsAdvancedSearchMemberExpression(Expression expression)
158141
{
159142
MemberExpression memberExpression = expression as MemberExpression;
@@ -180,28 +163,4 @@ public FormatInfo(string pattern, int parameterCount = 0, bool isCompound = fals
180163
internal static FormatInfo MemberAccess = new FormatInfo("{1}:{0}", 2);
181164
}
182165
}
183-
184-
public static class Extensions
185-
{
186-
internal static DefaultSearchFormatter.FormatInfo ToFormatInfo(this Expression expression)
187-
{
188-
ExpressionType? type = expression?.NodeType;
189-
switch (type)
190-
{
191-
case ExpressionType.Not:
192-
case ExpressionType.NotEqual:
193-
return DefaultSearchFormatter.FormatInfo.Not;
194-
case ExpressionType.Equal:
195-
throw new NotImplementedException("Currently not supporting Equal expression.");
196-
case ExpressionType.AndAlso:
197-
return DefaultSearchFormatter.FormatInfo.AndAlso;
198-
case ExpressionType.MemberAccess:
199-
return DefaultSearchFormatter.FormatInfo.MemberAccess;
200-
case ExpressionType.OrElse:
201-
return DefaultSearchFormatter.FormatInfo.OrElse;
202-
}
203-
throw new NotImplementedException($"{type.ToString()} is not implemented.");
204-
}
205-
206-
}
207166
}

RedditSharp/Search/ISearchFormatter.cs renamed to RedditSharp/Search/IAdvancedSearchFormatter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
namespace RedditSharp.Search
77
{
8-
public interface ISearchFormatter
8+
public interface IAdvancedSearchFormatter
99
{
1010

1111
/// <summary>
12-
///
12+
/// use an expression to create a search for reddit
1313
/// </summary>
1414
/// <param name="search"></param>
15-
/// <returns></returns>
15+
/// <returns>the string representing the search expression in reddits search format</returns>
1616
/// <remarks>
1717
/// has to be Expression Func https://michael-mckenna.com/func-t-vs-expression-func-t-in-linq/
1818
/// </remarks>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
4+
namespace RedditSharp.Search
5+
{
6+
public static class SearchExtensions
7+
{
8+
internal static DefaultSearchFormatter.FormatInfo ToFormatInfo(this Expression expression)
9+
{
10+
ExpressionType? type = expression?.NodeType;
11+
switch (type)
12+
{
13+
case ExpressionType.Not:
14+
case ExpressionType.NotEqual:
15+
return DefaultSearchFormatter.FormatInfo.Not;
16+
case ExpressionType.Equal:
17+
throw new NotImplementedException("Currently not supporting Equal expression.");
18+
case ExpressionType.AndAlso:
19+
return DefaultSearchFormatter.FormatInfo.AndAlso;
20+
case ExpressionType.MemberAccess:
21+
return DefaultSearchFormatter.FormatInfo.MemberAccess;
22+
case ExpressionType.OrElse:
23+
return DefaultSearchFormatter.FormatInfo.OrElse;
24+
}
25+
throw new NotImplementedException($"{type.ToString()} is not implemented.");
26+
}
27+
28+
}
29+
}

0 commit comments

Comments
 (0)