/** Test of parse method, of class ScriptingContainer. */ @Test public void testParse_3args_2() { System.out.println("parse(type, filename, lines)"); PathType type = null; String filename = ""; int[] lines = null; String basedir = System.getProperty("user.dir"); String[] paths = {basedir + "/lib/ruby/1.8"}; ScriptingContainer instance = new ScriptingContainer(); instance.getProvider().setLoadPaths(Arrays.asList(paths)); EmbedEvalUnit result; try { result = instance.parse(type, filename, lines); } catch (RuntimeException e) { assertTrue(e.getCause() instanceof FileNotFoundException); } filename = basedir + "/test/org/jruby/embed/ruby/next_year.rb"; result = instance.parse(PathType.ABSOLUTE, filename); IRubyObject ret = result.run(); assertEquals(getNextYear(), ret.toJava(Integer.class)); StringWriter writer = new StringWriter(); instance.setWriter(writer); String[] planets = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" }; instance.put("@list", Arrays.asList(planets)); filename = "/test/org/jruby/embed/ruby/list_printer.rb"; result = instance.parse(PathType.RELATIVE, filename); ret = result.run(); String expResult = "Mercury >> Venus >> Earth >> Mars >> Jupiter >> Saturn >> Uranus >> Neptune: 8 in total"; assertEquals(expResult, writer.toString().trim()); writer = new StringWriter(); instance.setWriter(writer); planets = new String[] {"水星", "金星", "地球", "火星", "木星", "土星", "天王星", "海王星"}; instance.put("@list", Arrays.asList(planets)); filename = "org/jruby/embed/ruby/list_printer.rb"; result = instance.parse(PathType.CLASSPATH, filename); ret = result.run(); expResult = "水星 >> 金星 >> 地球 >> 火星 >> 木星 >> 土星 >> 天王星 >> 海王星: 8 in total"; assertEquals(expResult, writer.toString().trim()); filename = "org/jruby/embed/ruby/raises_parse_error.rb"; writer = new StringWriter(); instance.setErrorWriter(writer); try { instance.parse(PathType.CLASSPATH, filename, 2); } catch (Exception e) { System.out.println(writer.toString()); assertTrue(writer.toString().contains(filename + ":7:")); } instance.getVarMap().clear(); instance = null; }
/** Test of parse method, of class ScriptingContainer. */ @Test public void testParse_3args_1() throws FileNotFoundException { System.out.println("parse(reader, filename, lines)"); Reader reader = null; String filename = ""; int[] lines = null; ScriptingContainer instance = new ScriptingContainer(); EmbedEvalUnit expResult = null; EmbedEvalUnit result = instance.parse(reader, filename, lines); assertEquals(expResult, result); String basedir = System.getProperty("user.dir"); filename = basedir + "/test/org/jruby/embed/ruby/iteration.rb"; reader = new FileReader(filename); instance.put("@t", 2); result = instance.parse(reader, filename); IRubyObject ret = result.run(); String expStringResult = "Trick or Treat!\nTrick or Treat!\n\nHmmm...I'd like trick."; assertEquals(expStringResult, ret.toJava(String.class)); // line number test filename = basedir + "/test/org/jruby/embed/ruby/raises_parse_error.rb"; reader = new FileReader(filename); StringWriter writer = new StringWriter(); instance.setErrorWriter(writer); try { instance.parse(reader, filename, 2); } catch (Exception e) { System.out.println(writer.toString()); assertTrue(writer.toString().contains(filename + ":7:")); } instance.getVarMap().clear(); instance = null; }
/** Test of parse method, of class ScriptingContainer. */ @Test public void testParse_String_intArr() { System.out.println("parse"); String script = null; int[] lines = null; ScriptingContainer instance = new ScriptingContainer(); EmbedEvalUnit expResult = null; EmbedEvalUnit result = instance.parse(script, lines); assertEquals(expResult, result); script = ""; Ruby runtime = JavaEmbedUtils.initialize(new ArrayList()); Node node = runtime.parseEval(script, "<script>", null, 0); IRubyObject expRet = runtime.runInterpreter(node); result = instance.parse(script); IRubyObject ret = result.run(); assertEquals(expRet.toJava(String.class), ret.toJava(String.class)); // Maybe bug. This returns RubyNil, but it should be "" // assertEquals("", ret.toJava(String.class)); script = "def say_something()" + "\"はろ〜、わぁ〜るど!\"\n" + "end\n" + "say_something"; expRet = runtime.runInterpreter(runtime.parseEval(script, "<script>", null, 0)); ret = instance.parse(script).run(); assertEquals(expRet.toJava(String.class), ret.toJava(String.class)); // sharing variables instance.put("what", "Trick or Treat."); script = "\"Did you say, #{what}?\""; result = instance.parse(script); ret = result.run(); assertEquals("Did you say, Trick or Treat.?", ret.toJava(String.class)); // line number test script = "puts \"Hello World!!!\"\nputs \"Have a nice day!"; StringWriter writer = new StringWriter(); instance.setErrorWriter(writer); try { instance.parse(script, 1); } catch (Exception e) { assertTrue(writer.toString().contains("<script>:3:")); } instance.getVarMap().clear(); instance = null; }
/** Test of parse method, of class ScriptingContainer. */ @Test public void testParse_3args_3() throws FileNotFoundException { System.out.println("parse(istream, filename, lines)"); InputStream istream = null; String filename = ""; int[] lines = null; ScriptingContainer instance = new ScriptingContainer(); EmbedEvalUnit expResult = null; EmbedEvalUnit result = instance.parse(istream, filename, lines); assertEquals(expResult, result); String basedir = System.getProperty("user.dir"); filename = basedir + "/test/org/jruby/embed/ruby/law_of_cosines.rb"; istream = new FileInputStream(filename); result = instance.parse(istream, filename); instance.put("@a", 1); instance.put("@b", 1); instance.put("@c", 1); IRubyObject ret = result.run(); List<Double> angles = (List) ret.toJava(List.class); // this result goes to 60.00000000000001,60.00000000000001,59.99999999999999. // these should be 60.0, 60.0, 60.0. conversion precision error? for (double angle : angles) { assertEquals(60.0, angle, 0.00001); } filename = basedir + "/test/org/jruby/embed/ruby/raises_parse_error.rb"; StringWriter writer = new StringWriter(); instance.setErrorWriter(writer); istream = new FileInputStream(filename); try { instance.parse(istream, filename, 2); } catch (Exception e) { System.out.println(writer.toString()); assertTrue(writer.toString().contains(filename + ":7:")); } instance.getVarMap().clear(); instance = null; }
/** Test of callMethod method, of class ScriptingContainer. */ @Test public void testCallMethod_4args_3() { // Sharing local variables over method call doesn't work. // Should delete methods with unit argument? System.out.println("callMethod(receiver, methodName, returnType, unit)"); Object receiver = null; String methodName = ""; Class<Object> returnType = null; EmbedEvalUnit unit = null; ScriptingContainer instance = new ScriptingContainer(LocalVariableBehavior.PERSISTENT); instance.getProvider().getRubyInstanceConfig().setJRubyHome(System.getProperty("user.dir")); Object expResult = null; Object result = instance.callMethod(receiver, methodName, returnType, unit); assertEquals(expResult, result); String text = "songs:\n" + "- Hey Soul Sister\n" + "- Who Says\n" + "- Apologize\n" + "podcasts:\n" + "- Java Posse\n" + "- Stack Overflow"; String filename = "org/jruby/embed/ruby/yaml_dump.rb"; StringWriter writer = new StringWriter(); instance.setWriter(writer); // local variable doesn't work in this case, so instance variable is used. instance.put("@text", text); unit = instance.parse(PathType.CLASSPATH, filename); receiver = unit.run(); methodName = "dump"; result = instance.callMethod(receiver, methodName, null, unit); expResult = "songs: Hey Soul Sister, Who Says, Apologize\npodcasts: Java Posse, Stack Overflow\n"; assertEquals(expResult, writer.toString()); instance.getVarMap().clear(); instance = null; }
/** Test of newRuntimeAdapter method, of class ScriptingContainer. */ @Test public void testNewRuntimeAdapter() { System.out.println("newRuntimeAdapter"); ScriptingContainer instance = new ScriptingContainer(); EmbedRubyRuntimeAdapter result = instance.newRuntimeAdapter(); String script = "def volume\n" + " (Math::PI * (@r ** 2.0) * @h)/3.0\n" + "end\n" + "def surface_area\n" + " Math::PI * @r * Math.sqrt((@r ** 2.0) + (@h ** 2.0)) + Math::PI * (@r ** 2.0)\n" + "end\n" + "return volume, surface_area"; instance.put("@r", 1.0); instance.put("@h", Math.sqrt(3.0)); EmbedEvalUnit unit = result.parse(script); IRubyObject ret = unit.run(); List<Double> rightCircularCone = (List<Double>) ret.toJava(List.class); assertEquals(1.813799, rightCircularCone.get(0), 0.000001); assertEquals(9.424778, rightCircularCone.get(1), 0.000001); instance.getVarMap().clear(); instance = null; }