forked from dynamicexpresso/DynamicExpresso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionHelperTests.cs
44 lines (35 loc) · 964 Bytes
/
CollectionHelperTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using NUnit.Framework;
using System.Linq;
using System.Collections.Generic;
namespace DynamicExpresso.UnitTest
{
[TestFixture]
public class CollectionHelperTests
{
[Test]
public void Where()
{
var target = new Interpreter();
target.SetVariable("IntCollectionHelper", new CollectionHelper<int>());
var list = new List<int> { 1, 10, 19, 21 };
var results = target.Eval("IntCollectionHelper.Where(list, \"x > 19\")", new Parameter("list", list))
as IEnumerable<int>;
Assert.AreEqual(1, results.Count());
Assert.AreEqual(21, results.First());
}
}
public class CollectionHelper<T>
{
private readonly Interpreter _interpreter;
public CollectionHelper()
{
_interpreter = new Interpreter();
}
public IEnumerable<T> Where(IEnumerable<T> values, string expression)
{
var predicate = _interpreter.ParseAsDelegate<Func<T, bool>>(expression, "x");
return values.Where(predicate);
}
}
}