1
+ using Dynamo ;
2
+ using NUnit . Framework ;
3
+ using System . Collections ;
4
+
5
+
6
+ namespace IronPython3Tests
7
+ {
8
+ public class PythonEvalTests : UnitTestBase
9
+ {
10
+ public delegate object PythonEvaluatorDelegate ( string code , IList bindingNames , IList bindingValues ) ;
11
+
12
+ public IEnumerable < PythonEvaluatorDelegate > Evaluators = new List < PythonEvaluatorDelegate > {
13
+ IronPython3 . Evaluator . IronPython3Evaluator . EvaluateIronPythonScript ,
14
+ } ;
15
+
16
+ [ Test ]
17
+ [ Category ( "UnitTests" ) ]
18
+ public void EvaluatorWorks ( )
19
+ {
20
+ foreach ( var pythonEvaluator in Evaluators )
21
+ {
22
+ var empty = new ArrayList ( ) ;
23
+ var output = pythonEvaluator ( "OUT = 0" , empty , empty ) ;
24
+ Assert . AreEqual ( 0 , output ) ;
25
+ }
26
+ }
27
+
28
+ [ Test ]
29
+ [ Category ( "UnitTests" ) ]
30
+ public void BindingsWork ( )
31
+ {
32
+ const string expected = "Hi!" ;
33
+
34
+ var names = new ArrayList { "test" } ;
35
+ var vals = new ArrayList { expected } ;
36
+
37
+ foreach ( var pythonEvaluator in Evaluators )
38
+ {
39
+ var output = pythonEvaluator (
40
+ "OUT = test" ,
41
+ names ,
42
+ vals
43
+ ) ;
44
+
45
+ Assert . AreEqual ( expected , output ) ;
46
+ }
47
+ }
48
+
49
+ [ Test ]
50
+ [ Category ( "UnitTests" ) ]
51
+ public void DataMarshaling_Output ( )
52
+ {
53
+ var marshaler = IronPython3 . Evaluator . IronPython3Evaluator . OutputMarshaler ;
54
+ marshaler . RegisterMarshaler ( ( string s ) => s . Length ) ;
55
+
56
+ const string script = "OUT = ['', ' ', ' ']" ;
57
+
58
+ object output = IronPython3 . Evaluator . IronPython3Evaluator . EvaluateIronPythonScript (
59
+ script ,
60
+ new ArrayList ( ) ,
61
+ new ArrayList ( ) ) ;
62
+
63
+ Assert . AreEqual ( new [ ] { 0 , 1 , 2 } , output ) ;
64
+
65
+ marshaler . UnregisterMarshalerOfType < string > ( ) ;
66
+ }
67
+
68
+ [ Test ]
69
+ [ Category ( "UnitTests" ) ]
70
+ public void DataMarshaling_Input ( )
71
+ {
72
+ var marshaler = IronPython3 . Evaluator . IronPython3Evaluator . InputMarshaler ;
73
+ marshaler . RegisterMarshaler ( ( string s ) => s . Length ) ;
74
+
75
+ const string script = "OUT = sum(IN)" ;
76
+
77
+ object output = IronPython3 . Evaluator . IronPython3Evaluator . EvaluateIronPythonScript (
78
+ script ,
79
+ new ArrayList { "IN" } ,
80
+ new ArrayList { new ArrayList { " " , " " } } ) ;
81
+
82
+ Assert . AreEqual ( 3 , output ) ;
83
+
84
+ marshaler . UnregisterMarshalerOfType < string > ( ) ;
85
+ }
86
+
87
+
88
+ [ Test ]
89
+ public void SliceOperator_Output ( )
90
+ {
91
+ var names = new ArrayList { "indx" } ;
92
+ var vals = new ArrayList { 3 } ;
93
+
94
+ foreach ( var pythonEvaluator in Evaluators )
95
+ {
96
+ var output = pythonEvaluator (
97
+ "OUT = [1,2,3,4,5,6,7][indx:indx+2]" ,
98
+ names ,
99
+ vals ) ;
100
+
101
+ var expected = new ArrayList { 4 , 5 } ;
102
+
103
+ Assert . AreEqual ( expected , output ) ;
104
+ }
105
+ }
106
+
107
+ [ Test ]
108
+ public void IronPythonGivesCorrectErrorLineNumberAndLoadsStdLib ( )
109
+ {
110
+ var code = @"
111
+ from xml.dom.minidom import parseString
112
+ my_xml = parseString('invalid XML!')
113
+ " ;
114
+ try
115
+ {
116
+ IronPython3 . Evaluator . IronPython3Evaluator . EvaluateIronPythonScript ( code , new ArrayList ( ) , new ArrayList ( ) ) ;
117
+ Assert . Fail ( "An exception was expected" ) ;
118
+ }
119
+ catch ( Exception exc )
120
+ {
121
+ StringAssert . StartsWith ( @"Traceback (most recent call last):
122
+ File ""<string>"", line 3, in <module>" , exc . Message ) ;
123
+ StringAssert . EndsWith ( "Data at the root level is invalid. Line 1, position 1." , exc . Message ) ;
124
+ }
125
+ }
126
+
127
+ [ Test ]
128
+ public void NonListIterablesCanBeOutput ( )
129
+ {
130
+ var code = @"
131
+ s = { 'hello' }
132
+ fs = frozenset({ 'world' })
133
+ d = { 'one': 1 }
134
+ dk = d.keys()
135
+ dv = d.values()
136
+ di = d.items()
137
+
138
+ OUT = s,fs,dk,dv,di
139
+ " ;
140
+ var expected = new ArrayList
141
+ {
142
+ new ArrayList { "hello" } ,
143
+ new ArrayList { "world" } ,
144
+ new ArrayList { "one" } ,
145
+ new ArrayList { 1 } ,
146
+ new ArrayList { new ArrayList { "one" , 1 } }
147
+ } ;
148
+ var empty = new ArrayList ( ) ;
149
+ foreach ( var pythonEvaluator in Evaluators )
150
+ {
151
+ var output = pythonEvaluator ( code , empty , empty ) ;
152
+ Assert . AreEqual ( expected , output ) ;
153
+ }
154
+ }
155
+ }
156
+ }
0 commit comments