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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using DynamicExpresso.Exceptions;
using NUnit.Framework;
 
namespace DynamicExpresso.UnitTest
{
    [TestFixture]
    public class InvalidExpressionTest
    {
        [Test]
        public void Not_existing_variable()
        {
            var target = new Interpreter();
 
            Assert.Throws<UnknownIdentifierException>(() => target.Eval("not_existing"));
        }
 
        [Test]
        public void Invalid_equal_assignment_operator_left()
        {
            var target = new Interpreter();
 
            Assert.Throws<ParseException>(() => target.Eval("=234"));
        }
 
        [Test]
        public void Invalid_equal_assignment_operator_left_is_literal()
        {
            var target = new Interpreter();
 
            Assert.Throws<ParseException>(() => target.Eval("352=234"));
        }
 
        [Test]
        public void Unkonwn_operator_triple_equal()
        {
            var target = new Interpreter();
 
            Assert.Throws<ParseException>(() => target.Eval("352===234"));
        }
 
        [Test]
        public void Not_existing_function()
        {
            var target = new Interpreter();
 
            Assert.Throws<UnknownIdentifierException>(() => target.Eval("pippo()"));
        }
 
        [Test]
        public void Not_valid_function()
        {
            var target = new Interpreter();
 
            
            Assert.Throws<ParseException>(() => target.Eval("2()"));
        }
 
        [Test]
        public void Not_valid_expression()
        {
            var target = new Interpreter();
 
            Assert.Throws<UnknownIdentifierException>(() => target.Eval("'5' + 3 /(asda"));
        }
 
        [Test]
        public void TryParse_an_invalid_expression_unknown_identifier_x()
        {
            var target = new Interpreter();
 
            var ex = Assert.Throws<UnknownIdentifierException>(() => target.Parse("x + y * Math.Pow(x, 2)", typeof(void)));
 
            Assert.That(ex.Message, Is.EqualTo("Unknown identifier 'x' (at index 0)."));
            Assert.That(ex.Identifier, Is.EqualTo("x"));
            Assert.That(ex.Position, Is.EqualTo(0));
        }
 
        [Test]
        public void Parse_an_invalid_expression_unknown_identifier_y()
        {
            var target = new Interpreter()
                                    .SetVariable("x", 10.0);
 
            var ex = Assert.Throws<UnknownIdentifierException>(() => target.Parse("x + y * Math.Pow(x, 2)", typeof(void)));
 
            Assert.That(ex.Identifier, Is.EqualTo("y"));
            Assert.That(ex.Position, Is.EqualTo(4));
        }
 
        [Test]
        public void Parse_an_invalid_expression_unknown_method()
        {
            var target = new Interpreter()
                                    .SetVariable("x", 10.0);
 
            var ex = Assert.Throws<NoApplicableMethodException>(() => target.Parse("Math.NotExistingMethod(x, 2)", typeof(void)));
 
            Assert.That(ex.MethodName, Is.EqualTo("NotExistingMethod"));
            Assert.That(ex.MethodTypeName, Is.EqualTo("Math"));
            Assert.That(ex.Position, Is.EqualTo(5));
        }
 
        [Test]
        public void SystemExceptions_are_preserved_using_delegate_variable()
        {
            var target = new Interpreter();
 
            Func<string> testException = () =>
            {
                throw new InvalidOperationException("Test");
            };
 
            target.SetVariable("testException", testException);
 
            Assert.Throws<InvalidOperationException>(() => target.Eval("testException()"));
        }
 
        [Test]
        public void CustomExceptions_WithoutSerializationConstructor_are_preserved()
        {
            var target = new Interpreter();
 
            Func<string> testException = () =>
            {
                throw new MyException("Test");
            };
 
            target.SetVariable("testException", testException);
 
            Assert.Throws<MyException>(() => target.Eval("testException()"));
        }
 
        [Test]
        public void SystemExceptions_are_preserved_using_method_invocation()
        {
            var target = new Interpreter();
            target.SetVariable("a", new MyTestService());
 
            Assert.Throws<NotImplementedException>(() => target.Eval("a.ThrowException()"));
        }
 
        public class MyException : Exception
        {
            public MyException(string message) : base(message) { }
        }
 
        // ReSharper disable once UnusedMember.Local
        private class MyTestService
        {
            // ReSharper disable once UnusedMember.Local
            public string ThrowException()
            {
                throw new NotImplementedException("AppException");
            }
        }
    }
}