Skip to content

Commit d44bd1f

Browse files
committed
Add ListOfType Function
1 parent 7f6e2e2 commit d44bd1f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,6 @@ public void TypeTesting(string expression, Type type)
688688
[TestCase("IEEERemainder(6, 3)", ExpectedResult = 0, Category = "Standard Functions,IEEERemainder Function")]
689689
#endregion
690690

691-
//#region if Function
692-
//[TestCase("if(true, \"It's OK\", \"Ho no\")", ExpectedResult = "It's OK", Category = "Standard Functions,if Function")]
693-
//[TestCase("if(false, \"It's OK\", \"Ho no\")", ExpectedResult = "Ho no", Category = "Standard Functions,if Function")]
694-
//[TestCase("if(3<5, \"It's OK\", \"Ho no\")", ExpectedResult = "It's OK", Category = "Standard Functions,if Function")]
695-
//[TestCase("if(3>5, \"It's OK\", \"Ho no\")", ExpectedResult = "Ho no", Category = "Standard Functions,if Function")]
696-
//#endregion
697-
698691
#region in Function
699692
[TestCase("in(8, 4, 2, 8)", ExpectedResult = true, Category = "Standard Functions,in Function")]
700693
[TestCase("in(20, 4, 2, 8)", ExpectedResult = false, Category = "Standard Functions,in Function")]
@@ -708,6 +701,18 @@ public void TypeTesting(string expression, Type type)
708701
[TestCase("List(14, \"A text for test\", 2.5, true)[3]", ExpectedResult = true, Category = "Standard Functions,List Function,Indexing")]
709702
#endregion
710703

704+
#region ListOfType Function
705+
[TestCase("ListOfType(typeof(int), 1,2,3 ).GetType()", ExpectedResult = typeof(List<int>), Category = "Standard Functions,ListOfType Function,Instance Property")]
706+
[TestCase("ListOfType(typeof(int), 1,2,3 ).Count", ExpectedResult = 3, Category = "Standard Functions,ListOfType Function,Instance Property")]
707+
[TestCase("ListOfType(typeof(int), 1,2,3 )[0]", ExpectedResult = 1, Category = "Standard Functions,ListOfType Function,Indexing")]
708+
[TestCase("ListOfType(typeof(int), 1,2,3 )[1]", ExpectedResult = 2, Category = "Standard Functions,ListOfType Function,Indexing")]
709+
[TestCase("ListOfType(typeof(int), 1,2,3 )[2]", ExpectedResult = 3, Category = "Standard Functions,ListOfType Function,Indexing")]
710+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" ).GetType()", ExpectedResult = typeof(List<string>), Category = "Standard Functions,ListOfType Function,Instance Property")]
711+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" ).Count", ExpectedResult = 2, Category = "Standard Functions,ListOfType Function,Instance Property")]
712+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" )[0]", ExpectedResult = "hello", Category = "Standard Functions,ListOfType Function,Indexing")]
713+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" )[1]", ExpectedResult = "Test", Category = "Standard Functions,ListOfType Function,Indexing")]
714+
#endregion
715+
711716
#region Log Function
712717
[TestCase("Log(64d, 2d)", ExpectedResult = 6, Category = "Standard Functions,Log Function")]
713718
[TestCase("Log(100d, 10d)", ExpectedResult = 2, Category = "Standard Functions,Log Function")]

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,22 @@ private enum TryBlockEvaluatedState
380380
//{ "if", (self, args) => (bool)self.Evaluate(args[0]) ? self.Evaluate(args[1]) : self.Evaluate(args[2]) },
381381
{ "in", (self, args) => args.Skip(1).ToList().ConvertAll(arg => self.Evaluate(arg)).Contains(self.Evaluate(args[0])) },
382382
{ "List", (self, args) => args.ConvertAll(arg => self.Evaluate(arg)) },
383+
{ "ListOfType", (self, args) =>
384+
{
385+
Type type = (Type)self.Evaluate(args[0]);
386+
Array sourceArray = args.Skip(1).Select(arg => self.Evaluate(arg)).ToArray();
387+
Array typedArray = Array.CreateInstance(type, sourceArray.Length);
388+
Array.Copy(sourceArray, typedArray, sourceArray.Length);
389+
390+
Type typeOfList = typeof(List<>).MakeGenericType(type);
391+
392+
object list = Activator.CreateInstance(typeOfList);
393+
394+
typeOfList.GetMethod("AddRange").Invoke(list, new object[]{ typedArray });
395+
396+
return list;
397+
}
398+
},
383399
{ "Max", (self, args) => args.ConvertAll(arg => Convert.ToDouble(self.Evaluate(arg))).Max() },
384400
{ "Min", (self, args) => args.ConvertAll(arg => Convert.ToDouble(self.Evaluate(arg))).Min() },
385401
{ "new", (self, args) =>

0 commit comments

Comments
 (0)