alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using NUnit.Framework;
using System.Linq.Expressions;
 
namespace DynamicExpresso.UnitTest
{
    [TestFixture]
    public class GenerateLambdaTest
    {
        [Test]
        public void Parse_as_LambdaExpression()
        {
            var target = new Interpreter();
 
            Expression<Func<double, double>> lambdaExpression = target.ParseAsExpression<Func<double, double>>("arg + 5");
 
            Assert.That(lambdaExpression.Compile()(10), Is.EqualTo(15));
        }
 
        [Test]
        public void Parse_as_LambdaExpression_with_parameter()
        {
            var target = new Interpreter();
 
            Expression<Func<double, double>> lambdaExpression = target.ParseAsExpression<Func<double, double>>("arg + 5");
 
            Assert.That(lambdaExpression.Compile()(10), Is.EqualTo(15));
 
            lambdaExpression = target.ParseAsExpression<Func<double, double>>("arg + .5");
            Assert.That(lambdaExpression.Compile()(10), Is.EqualTo(10.5));
        }
 
        [Test]
        public void Parse_To_a_Delegate_With_Two_Parameters()
        {
            var target = new Interpreter();
 
            var lambdaExpression = target.ParseAsExpression<Func<double, double, double>>("arg1 * arg2");
 
            Assert.That(lambdaExpression.Compile()(3, 2), Is.EqualTo(6));
            Assert.That(lambdaExpression.Compile()(5, 10), Is.EqualTo(50));
        }
 
        [Test]
        public void Parse_To_a_Delegate_With_Two_Parameters_With_Custom_Name()
        {
            var target = new Interpreter();
 
            var argumentNames = new string[] { "x", "y" };
            var lambdaExpression = target.ParseAsExpression<Func<double, double, double>>("x * y", argumentNames);
 
            Assert.That(lambdaExpression.Compile()(3, 2), Is.EqualTo(6));
            Assert.That(lambdaExpression.Compile()(5, 10), Is.EqualTo(50));
        }
 
        [Test]
        public void Generate_a_LambdaExpression_From_Lambda()
        {
            var target = new Interpreter();
 
            var lambda = target.Parse("Math.Pow(x, y) + 5",
                new Parameter("x", typeof(double)),
                new Parameter("y", typeof(double))
            );
 
            Expression<Func<double, double, double>> lambdaExpression = lambda.LambdaExpression<Func<double, double, double>>();
 
            Assert.That(lambdaExpression.Compile()(10, 2), Is.EqualTo(Math.Pow(10, 2) + 5));
        }
 
        [Test]
        public void Cannot_Generate_a_LambdaExpression_From_Lambda_with_parameters_count_mismatch()
        {
            var target = new Interpreter();
 
            // Func delegate has 2 inputs, I just use one
 
            var lambda = target.Parse("x + 5",
                new Parameter("x", typeof(double))
            );
 
            Assert.Throws<ArgumentException>(() => lambda.LambdaExpression<Func<double, double, double>>());
        }
 
        [Test]
        public void Cannot_Generate_a_LambdaExpression_From_Lambda_with_parameters_type_mismatch()
        {
            var target = new Interpreter();
 
            // Func delegate takes a string, I pass a double
 
            var lambda = target.Parse("x + 5",
                new Parameter("x", typeof(double))
            );
 
            Assert.Throws<ArgumentException>(() => lambda.LambdaExpression<Func<string, double>>());
        }
 
        [Test]
        public void Cannot_generate_a_Lambda_with_return_type_mismatch()
        {
            var target = new Interpreter();
 
            // Func delegate returns a string, I return a double
 
            var lambda = target.Parse("x + 5",
                new Parameter("x", typeof(double))
            );
 
            Assert.Throws<ArgumentException>(() => lambda.LambdaExpression<Func<double, string>>());
        }
 
    }
}