4848
4949import java .io .ByteArrayOutputStream ;
5050import java .io .IOException ;
51- import java .io .UnsupportedEncodingException ;
5251import java .math .BigInteger ;
5352import java .nio .charset .StandardCharsets ;
5453import java .util .ArrayList ;
@@ -107,6 +106,14 @@ public void tearDown() {
107106 context .getEngine ().close ();
108107 }
109108
109+ private String getOutString () {
110+ return out .toString (StandardCharsets .UTF_8 ).replaceAll ("\r \n " , "\n " );
111+ }
112+
113+ private String getErrString () {
114+ return err .toString (StandardCharsets .UTF_8 ).replaceAll ("\r \n " , "\n " );
115+ }
116+
110117 @ Test
111118 public void evalFailsOnError () {
112119 boolean didFail = false ;
@@ -217,8 +224,8 @@ public void testHostException() {
217224 Assert .assertTrue (e .getMessage (), e .getMessage ().contains ("divide by zero" ));
218225 }
219226
220- String outString = out . toString ( StandardCharsets . UTF_8 );
221- String errString = err . toString ( StandardCharsets . UTF_8 );
227+ String outString = getOutString ( );
228+ String errString = getErrString ( );
222229 Assert .assertTrue (outString , outString .isEmpty ());
223230 Assert .assertTrue (errString , errString .isEmpty ());
224231 }
@@ -257,7 +264,7 @@ def is_none(x):
257264 }
258265
259266 @ Test
260- public void testPassingFloats () throws UnsupportedEncodingException {
267+ public void testPassingFloats () {
261268 String source = "import polyglot\n " +
262269 "@polyglot.export_value\n " +
263270 "def foo(x, y):\n " +
@@ -266,11 +273,11 @@ public void testPassingFloats() throws UnsupportedEncodingException {
266273 context .eval (script );
267274 Value main = context .getPolyglotBindings ().getMember ("foo" );
268275 main .execute ((float ) 1.0 , (float ) 2.0 );
269- assertEquals ("2.0\n " , out . toString ( "UTF-8" ));
276+ assertEquals ("2.0\n " , getOutString ( ));
270277 }
271278
272279 @ Test
273- public void testPassingBigIntegers () throws UnsupportedEncodingException {
280+ public void testPassingBigIntegers () {
274281 String source = "import polyglot\n " +
275282 "@polyglot.export_value\n " +
276283 "def foo(x, y):\n " +
@@ -279,20 +286,20 @@ public void testPassingBigIntegers() throws UnsupportedEncodingException {
279286 context .eval (script );
280287 Value main = context .getPolyglotBindings ().getMember ("foo" );
281288 main .execute (BigInteger .valueOf (Long .MAX_VALUE ).multiply (BigInteger .TWO ), BigInteger .valueOf (7 ));
282- assertEquals ("129127208515966861298\n " , out . toString ( "UTF-8" ));
289+ assertEquals ("129127208515966861298\n " , getOutString ( ));
283290 out .reset ();
284291 main .execute (Long .MAX_VALUE , 14 );
285- assertEquals ("129127208515966861298\n " , out . toString ( "UTF-8" ));
292+ assertEquals ("129127208515966861298\n " , getOutString ( ));
286293 out .reset ();
287294 main .execute (6 , 7 );
288- assertEquals ("42\n " , out . toString ( "UTF-8" ));
295+ assertEquals ("42\n " , getOutString ( ));
289296 out .reset ();
290297 main .execute (true , true );
291- assertEquals ("1\n " , out . toString ( "UTF-8" ));
298+ assertEquals ("1\n " , getOutString ( ));
292299 }
293300
294301 @ Test
295- public void testBigIntegersAdd () throws UnsupportedEncodingException {
302+ public void testBigIntegersAdd () {
296303 String source = "import polyglot\n " +
297304 "@polyglot.export_value\n " +
298305 "def foo(x, y):\n " +
@@ -301,20 +308,20 @@ public void testBigIntegersAdd() throws UnsupportedEncodingException {
301308 context .eval (script );
302309 Value main = context .getPolyglotBindings ().getMember ("foo" );
303310 main .execute (BigInteger .valueOf (24 ).shiftLeft (101 ), BigInteger .valueOf (7 ));
304- assertEquals ("60847228810955011271841753858055\n " , out . toString ( "UTF-8" ));
311+ assertEquals ("60847228810955011271841753858055\n " , getOutString ( ));
305312 out .reset ();
306313 main .execute (BigInteger .valueOf (24 ).shiftLeft (101 ), BigInteger .valueOf (24 ).shiftLeft (101 ));
307- assertEquals ("121694457621910022543683507716096\n " , out . toString ( "UTF-8" ));
314+ assertEquals ("121694457621910022543683507716096\n " , getOutString ( ));
308315 out .reset ();
309316 main .execute (6 , 7 );
310- assertEquals ("13\n " , out . toString ( "UTF-8" ));
317+ assertEquals ("13\n " , getOutString ( ));
311318 out .reset ();
312319 main .execute (true , true );
313- assertEquals ("2\n " , out . toString ( "UTF-8" ));
320+ assertEquals ("2\n " , getOutString ( ));
314321 }
315322
316323 @ Test
317- public void testBigIntegersEg () throws UnsupportedEncodingException {
324+ public void testBigIntegersEg () {
318325 String source = "import polyglot\n " +
319326 "@polyglot.export_value\n " +
320327 "def foo(x, y):\n " +
@@ -323,26 +330,26 @@ public void testBigIntegersEg() throws UnsupportedEncodingException {
323330 context .eval (script );
324331 Value main = context .getPolyglotBindings ().getMember ("foo" );
325332 main .execute (BigInteger .valueOf (24 ).shiftLeft (101 ), BigInteger .valueOf (7 ));
326- assertEquals ("False\n " , out . toString ( "UTF-8" ));
333+ assertEquals ("False\n " , getOutString ( ));
327334 out .reset ();
328335 main .execute (BigInteger .valueOf (24 ).shiftLeft (101 ), BigInteger .valueOf (24 ).shiftLeft (101 ));
329- assertEquals ("True\n " , out . toString ( "UTF-8" ));
336+ assertEquals ("True\n " , getOutString ( ));
330337 out .reset ();
331338 main .execute (6 , 7 );
332- assertEquals ("False\n " , out . toString ( "UTF-8" ));
339+ assertEquals ("False\n " , getOutString ( ));
333340 out .reset ();
334341 main .execute (6 , 6 );
335- assertEquals ("True\n " , out . toString ( "UTF-8" ));
342+ assertEquals ("True\n " , getOutString ( ));
336343 out .reset ();
337344 main .execute (true , BigInteger .ONE );
338- assertEquals ("True\n " , out . toString ( "UTF-8" ));
345+ assertEquals ("True\n " , getOutString ( ));
339346 out .reset ();
340347 main .execute (true , BigInteger .ZERO );
341- assertEquals ("False\n " , out . toString ( "UTF-8" ));
348+ assertEquals ("False\n " , getOutString ( ));
342349 }
343350
344351 @ Test
345- public void testAsFunction () throws UnsupportedEncodingException {
352+ public void testAsFunction () {
346353 String source = "import polyglot\n " +
347354 "@polyglot.export_value\n " +
348355 "def foo():\n " +
@@ -351,11 +358,11 @@ public void testAsFunction() throws UnsupportedEncodingException {
351358 context .eval (script );
352359 Value main = context .getPolyglotBindings ().getMember ("foo" );
353360 main .execute ();
354- assertEquals ("Called\n " , out . toString ( "UTF-8" ));
361+ assertEquals ("Called\n " , getOutString ( ));
355362 }
356363
357364 @ Test
358- public void testAsFunctionVarArgs () throws UnsupportedEncodingException {
365+ public void testAsFunctionVarArgs () {
359366 String source = "import polyglot\n " +
360367 "@polyglot.export_value\n " +
361368 "def foo(a, b):\n " +
@@ -364,28 +371,28 @@ public void testAsFunctionVarArgs() throws UnsupportedEncodingException {
364371 context .eval (script );
365372 Value main = context .getPolyglotBindings ().getMember ("foo" );
366373 main .execute ("Hello" , "World" );
367- assertEquals ("Hello World\n " , out . toString ( "UTF-8" ));
374+ assertEquals ("Hello World\n " , getOutString ( ));
368375 }
369376
370377 @ Test
371- public void mainFunctionsAreImplicitlyImporteable () throws UnsupportedEncodingException {
378+ public void mainFunctionsAreImplicitlyImporteable () {
372379 String source = "def foo(a, b):\n " +
373380 " print(a, b)\n \n " ;
374381 Source script = Source .create ("python" , source );
375382 context .eval (script );
376383 Value main = context .getBindings ("python" ).getMember ("foo" );
377384 main .execute ("Hello" , "World" );
378- assertEquals ("Hello World\n " , out . toString ( "UTF-8" ));
385+ assertEquals ("Hello World\n " , getOutString ( ));
379386 }
380387
381388 @ Test
382- public void builtinFunctionsAreImporteable () throws UnsupportedEncodingException {
389+ public void builtinFunctionsAreImporteable () {
383390 String source = "pass" ;
384391 Source script = Source .create ("python" , source );
385392 context .eval (script );
386393 Value main = context .getBindings ("python" ).getMember ("__builtins__" ).getMember ("print" );
387394 main .execute ("Hello" , "World" );
388- assertEquals ("Hello World\n " , out . toString ( "UTF-8" ));
395+ assertEquals ("Hello World\n " , getOutString ( ));
389396 }
390397
391398 @ Test
@@ -394,22 +401,22 @@ public void enumeratingMainBindingsWorks() throws Exception {
394401 }
395402
396403 @ Test
397- public void testMultipleInvocationsAreInSameScope () throws UnsupportedEncodingException {
404+ public void testMultipleInvocationsAreInSameScope () {
398405 String source = "def foo(a, b):\n " +
399406 " print(a, b)\n " +
400407 "foo" ;
401408 Source script = Source .create ("python" , source );
402409 Value foo = context .eval (script );
403410 foo .execute ("Hello" , "World" );
404- assertEquals ("Hello World\n " , out . toString ( "UTF-8" ));
411+ assertEquals ("Hello World\n " , getOutString ( ));
405412
406413 source = "def bar(a, b):\n " +
407414 " foo(a, b)\n " +
408415 "bar" ;
409416 script = Source .create ("python" , source );
410417 Value bar = context .eval (script );
411418 bar .execute ("Hello" , "World" );
412- assertEquals ("Hello World\n Hello World\n " , out . toString ( "UTF-8" ));
419+ assertEquals ("Hello World\n Hello World\n " , getOutString ( ));
413420
414421 source = "invalid syntax" ;
415422 script = Source .create ("python" , source );
@@ -418,9 +425,9 @@ public void testMultipleInvocationsAreInSameScope() throws UnsupportedEncodingEx
418425 } catch (Throwable t ) {
419426 }
420427 bar .execute ("Hello" , "World" );
421- assertEquals ("Hello World\n Hello World\n Hello World\n " , out . toString ( "UTF-8" ));
428+ assertEquals ("Hello World\n Hello World\n Hello World\n " , getOutString ( ));
422429 foo .execute ("Hello" , "World" );
423- assertEquals ("Hello World\n Hello World\n Hello World\n Hello World\n " , out . toString ( "UTF-8" ));
430+ assertEquals ("Hello World\n Hello World\n Hello World\n Hello World\n " , getOutString ( ));
424431 }
425432
426433 @ Test
@@ -568,7 +575,7 @@ public void accessJavaObjectFields() throws IOException {
568575 "5.0 <class 'float'>\n " +
569576 "6.0 <class 'float'>\n " +
570577 "True <class 'bool'>\n " +
571- "c <class 'polyglot.ForeignString'>\n " , out . toString ( "UTF-8" ));
578+ "c <class 'polyglot.ForeignString'>\n " , getOutString ( ));
572579 }
573580
574581 @ Test
@@ -595,7 +602,7 @@ public void accessJavaObjectGetters() throws IOException {
595602 "5.0 <class 'float'>\n " +
596603 "6.0 <class 'float'>\n " +
597604 "True <class 'bool'>\n " +
598- "c <class 'polyglot.ForeignString'>\n " , out . toString ( "UTF-8" ));
605+ "c <class 'polyglot.ForeignString'>\n " , getOutString ( ));
599606 }
600607
601608 @ Test
@@ -612,7 +619,7 @@ public void javaStringsAndPythonStrings() throws IOException {
612619 assertEquals ("" +
613620 "<class 'polyglot.ForeignDict'>\n " +
614621 "True\n " +
615- "True\n " , out . toString ( "UTF-8" ));
622+ "True\n " , getOutString ( ));
616623 }
617624
618625 @ Test
0 commit comments