@@ -90,13 +90,15 @@ def sum(*args):
90
90
91
91
assertEquals (0 , module .invokeMember ("sum" ).asInt ());
92
92
assertEquals (22 , module .invokeMember ("sum" , 22 ).asInt ());
93
- assertEquals (60 , module .invokeMember ("sum" ,10 , 20 , 30 ).asInt ());
94
- assertEquals (6 , module .invokeMember ("sum" , PositionalArguments .of (1 ,2 , 3 )).asInt ());
93
+ assertEquals (60 , module .invokeMember ("sum" , 10 , 20 , 30 ).asInt ());
94
+ assertEquals (6 , module .invokeMember ("sum" , PositionalArguments .of (1 , 2 , 3 )).asInt ());
95
95
96
96
assertEquals (0 , module .invokeMember ("sum" , PositionalArguments .of ()).asInt ());
97
97
assertEquals (0 , module .invokeMember ("sum" , KeywordArguments .from (Map .of ())).asInt ());
98
98
99
- PolyglotException pe = assertThrows (PolyglotException .class , () -> {assertEquals (0 , module .invokeMember ("sum" , KeywordArguments .of ("one" , 1 )).asInt ());});
99
+ PolyglotException pe = assertThrows (PolyglotException .class , () -> {
100
+ assertEquals (0 , module .invokeMember ("sum" , KeywordArguments .of ("one" , 1 )).asInt ());
101
+ });
100
102
assertEquals ("TypeError: sum() got an unexpected keyword argument 'one'" , pe .getMessage ());
101
103
102
104
}
@@ -116,22 +118,24 @@ def text(a, *args):
116
118
);
117
119
118
120
assertEquals ("a=0," , module .invokeMember ("text" , 0 ).asString ());
119
- assertEquals ("a=22,args[0]=33," , module .invokeMember ("text" , 22 ,33 ).asString ());
120
- assertEquals ("a='hello',args[0]=ahoj,args[1]=cau," , module .invokeMember ("text" ,"hello" , "ahoj" , "cau" ).asString ());
121
- assertEquals ("a='6',args[0]=1,args[1]=2,args[2]=3," , module .invokeMember ("text" , "6" , PositionalArguments .of (1 ,2 , 3 )).asString ());
122
- assertEquals ("a=1,args[0]=2,args[1]=3," , module .invokeMember ("text" , PositionalArguments .of (1 ,2 , 3 )).asString ());
121
+ assertEquals ("a=22,args[0]=33," , module .invokeMember ("text" , 22 , 33 ).asString ());
122
+ assertEquals ("a='hello',args[0]=ahoj,args[1]=cau," , module .invokeMember ("text" , "hello" , "ahoj" , "cau" ).asString ());
123
+ assertEquals ("a='6',args[0]=1,args[1]=2,args[2]=3," , module .invokeMember ("text" , "6" , PositionalArguments .of (1 , 2 , 3 )).asString ());
124
+ assertEquals ("a=1,args[0]=2,args[1]=3," , module .invokeMember ("text" , PositionalArguments .of (1 , 2 , 3 )).asString ());
123
125
assertEquals ("a=1," , module .invokeMember ("text" , PositionalArguments .of (1 )).asString ());
124
126
125
127
assertEquals ("a=1," , module .invokeMember ("text" , KeywordArguments .of ("a" , 1 )).asString ());
126
128
127
- PolyglotException pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" , KeywordArguments .of ("a" , 1 , "b" , 2 ));});
129
+ PolyglotException pe = assertThrows (PolyglotException .class , () -> {
130
+ module .invokeMember ("text" , KeywordArguments .of ("a" , 1 , "b" , 2 ));
131
+ });
128
132
assertEquals ("TypeError: text() got an unexpected keyword argument 'b'" , pe .getMessage ());
129
133
}
130
134
131
135
@ Test
132
136
public void testPositionalArgs03 () {
133
137
// @formatter:off
134
- Value module = run ( """
138
+ Value module = run ("""
135
139
def text(a,b=44, *args):
136
140
result = f'{a=},{b=},'
137
141
index = 0
@@ -143,22 +147,22 @@ def text(a,b=44, *args):
143
147
);
144
148
145
149
assertEquals ("a=0,b=44," , module .invokeMember ("text" , 0 ).asString ());
146
- assertEquals ("a=22,b=33," , module .invokeMember ("text" , 22 ,33 ).asString ());
147
- assertEquals ("a='hello',b='ahoj',args[0]=cau," , module .invokeMember ("text" ,"hello" , "ahoj" , "cau" ).asString ());
148
- assertEquals ("a='6',b=1,args[0]=2,args[1]=3," , module .invokeMember ("text" , "6" , PositionalArguments .of (1 ,2 , 3 )).asString ());
150
+ assertEquals ("a=22,b=33," , module .invokeMember ("text" , 22 , 33 ).asString ());
151
+ assertEquals ("a='hello',b='ahoj',args[0]=cau," , module .invokeMember ("text" , "hello" , "ahoj" , "cau" ).asString ());
152
+ assertEquals ("a='6',b=1,args[0]=2,args[1]=3," , module .invokeMember ("text" , "6" , PositionalArguments .of (1 , 2 , 3 )).asString ());
149
153
assertEquals ("a=1,b=44," , module .invokeMember ("text" , PositionalArguments .of (1 )).asString ());
150
- assertEquals ("a=1,b=2," , module .invokeMember ("text" , PositionalArguments .of (1 ,2 )).asString ());
151
- assertEquals ("a=1,b=2,args[0]=3," , module .invokeMember ("text" , PositionalArguments .of (1 ,2 , 3 )).asString ());
152
- assertEquals ("a='a',b='b',args[0]=1,args[1]=2,args[2]=3," , module .invokeMember ("text" ,"a" , "b" , PositionalArguments .of (1 ,2 , 3 )).asString ());
154
+ assertEquals ("a=1,b=2," , module .invokeMember ("text" , PositionalArguments .of (1 , 2 )).asString ());
155
+ assertEquals ("a=1,b=2,args[0]=3," , module .invokeMember ("text" , PositionalArguments .of (1 , 2 , 3 )).asString ());
156
+ assertEquals ("a='a',b='b',args[0]=1,args[1]=2,args[2]=3," , module .invokeMember ("text" , "a" , "b" , PositionalArguments .of (1 , 2 , 3 )).asString ());
153
157
}
154
158
155
159
private static String assertAllKeysInText (String text , Map <String , Object > kwArgs ) {
156
160
String rest = text ;
157
161
for (Map .Entry <String , Object > entry : kwArgs .entrySet ()) {
158
162
String key = entry .getKey ();
159
163
Object val = entry .getValue ();
160
- String keyVal = "[" + key + ":" + val .toString () +"]," ;
161
- assertTrue ("The string \" " + keyVal + "\" was not found in \" " + text + "\" " , text .contains (keyVal ));
164
+ String keyVal = "[" + key + ":" + val .toString () + "]," ;
165
+ assertTrue ("The string \" " + keyVal + "\" was not found in \" " + text + "\" " , text .contains (keyVal ));
162
166
rest = rest .replace (keyVal , "" );
163
167
}
164
168
return rest ;
@@ -167,7 +171,7 @@ private static String assertAllKeysInText(String text, Map<String, Object> kwArg
167
171
@ Test
168
172
public void testKwArgs01 () {
169
173
// @formatter:off
170
- Value module = run ( """
174
+ Value module = run ("""
171
175
def text(**kwArgs):
172
176
result = ''
173
177
for key, value in kwArgs.items():
@@ -189,14 +193,16 @@ def text(**kwArgs):
189
193
190
194
assertTrue (module .invokeMember ("text" , PositionalArguments .of ()).asString ().isEmpty ());
191
195
192
- PolyglotException pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" , PositionalArguments .of (44 )).asString ();});
196
+ PolyglotException pe = assertThrows (PolyglotException .class , () -> {
197
+ module .invokeMember ("text" , PositionalArguments .of (44 )).asString ();
198
+ });
193
199
assertEquals ("TypeError: text() takes 0 positional arguments but 1 was given" , pe .getMessage ());
194
200
}
195
201
196
202
@ Test
197
203
public void testKwArgs02 () {
198
204
// @formatter:off
199
- Value module = run ( """
205
+ Value module = run ("""
200
206
def text(*,named1, **kwArgs):
201
207
result = f'[named1:{str(named1)}],'
202
208
for key, value in kwArgs.items():
@@ -213,26 +219,36 @@ def text(*,named1, **kwArgs):
213
219
remaining = assertAllKeysInText (module .invokeMember ("text" , KeywordArguments .from (kwargsMap )).asString (), kwargsMap );
214
220
assertTrue (remaining .isEmpty ());
215
221
216
- PolyglotException pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" ).asString ();});
222
+ PolyglotException pe = assertThrows (PolyglotException .class , () -> {
223
+ module .invokeMember ("text" ).asString ();
224
+ });
217
225
assertEquals ("TypeError: text() missing 1 required keyword-only argument: 'named1'" , pe .getMessage ());
218
226
219
- pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" , KeywordArguments .from (Map .of ())).asString ();});
227
+ pe = assertThrows (PolyglotException .class , () -> {
228
+ module .invokeMember ("text" , KeywordArguments .from (Map .of ())).asString ();
229
+ });
220
230
assertEquals ("TypeError: text() missing 1 required keyword-only argument: 'named1'" , pe .getMessage ());
221
231
222
- pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" , KeywordArguments .of ("named2" , 10 )).asString ();});
232
+ pe = assertThrows (PolyglotException .class , () -> {
233
+ module .invokeMember ("text" , KeywordArguments .of ("named2" , 10 )).asString ();
234
+ });
223
235
assertEquals ("TypeError: text() missing 1 required keyword-only argument: 'named1'" , pe .getMessage ());
224
236
225
- pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" , PositionalArguments .of ()).asString ();});
237
+ pe = assertThrows (PolyglotException .class , () -> {
238
+ module .invokeMember ("text" , PositionalArguments .of ()).asString ();
239
+ });
226
240
assertEquals ("TypeError: text() missing 1 required keyword-only argument: 'named1'" , pe .getMessage ());
227
241
228
- pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" , PositionalArguments .of (10 )).asString ();});
242
+ pe = assertThrows (PolyglotException .class , () -> {
243
+ module .invokeMember ("text" , PositionalArguments .of (10 )).asString ();
244
+ });
229
245
assertEquals ("TypeError: text() takes 0 positional arguments but 1 was given" , pe .getMessage ());
230
246
}
231
247
232
248
@ Test
233
249
public void testKwArgs03 () {
234
250
// @formatter:off
235
- Value module = run ( """
251
+ Value module = run ("""
236
252
def text(*,named1, named2=44, **kwArgs):
237
253
result = f'[named1:{str(named1)}],[named2:{str(named2)}],'
238
254
for key, value in kwArgs.items():
@@ -243,7 +259,7 @@ def text(*,named1, named2=44, **kwArgs):
243
259
244
260
Map <String , Object > kwargsMap = Map .of ("named1" , 1 );
245
261
String remaining = assertAllKeysInText (module .invokeMember ("text" , KeywordArguments .from (kwargsMap )).asString (), kwargsMap );
246
- assertEquals ("[named2:44]," ,remaining );
262
+ assertEquals ("[named2:44]," , remaining );
247
263
248
264
kwargsMap = Map .of ("named1" , 1 , "named2" , 2 );
249
265
remaining = assertAllKeysInText (module .invokeMember ("text" , KeywordArguments .from (kwargsMap )).asString (), kwargsMap );
@@ -253,7 +269,7 @@ def text(*,named1, named2=44, **kwArgs):
253
269
@ Test
254
270
public void testKwArgs04 () {
255
271
// @formatter:off
256
- Value module = run ( """
272
+ Value module = run ("""
257
273
def text(*,named1, named2=44):
258
274
result = f'[named1:{str(named1)}],[named2:{str(named2)}],'
259
275
return result
@@ -262,14 +278,16 @@ def text(*,named1, named2=44):
262
278
263
279
Map <String , Object > kwargsMap = Map .of ("named1" , 1 );
264
280
String remaining = module .invokeMember ("text" , KeywordArguments .from (kwargsMap )).asString ();
265
- assertEquals ("[named1:1],[named2:44]," ,remaining );
281
+ assertEquals ("[named1:1],[named2:44]," , remaining );
266
282
267
283
kwargsMap = Map .of ("named1" , 1 , "named2" , 2 );
268
284
remaining = module .invokeMember ("text" , KeywordArguments .from (kwargsMap )).asString ();
269
- assertEquals ("[named1:1],[named2:2]," ,remaining );
285
+ assertEquals ("[named1:1],[named2:2]," , remaining );
270
286
271
- PolyglotException pe = assertThrows (PolyglotException .class , () -> {module .invokeMember ("text" ,
272
- module .invokeMember ("text" , KeywordArguments .of ("named1" , 1 , "named2" , 2 , "named3" , 3 ))).asString ();});
287
+ PolyglotException pe = assertThrows (PolyglotException .class , () -> {
288
+ module .invokeMember ("text" ,
289
+ module .invokeMember ("text" , KeywordArguments .of ("named1" , 1 , "named2" , 2 , "named3" , 3 ))).asString ();
290
+ });
273
291
assertEquals ("TypeError: text() got an unexpected keyword argument 'named3'" , pe .getMessage ());
274
292
}
275
293
}
0 commit comments