示例#1
0
  /** 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;
  }
示例#2
0
 /** Test of setErrorWriter method, of class ScriptingContainer. */
 @Test
 public void testSetErrorWriter() {
   System.out.println("setErrorWriter");
   Writer errorWriter = null;
   ScriptingContainer instance = new ScriptingContainer();
   instance.setErrorWriter(errorWriter);
 }
示例#3
0
  /** 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;
  }
示例#4
0
  /** Test of setWriter method, of class ScriptingContainer. */
  @Test
  public void testSetWriter() {
    System.out.println("setWriter");
    Writer writer = null;
    ScriptingContainer instance = new ScriptingContainer();
    instance.setWriter(writer);

    String filename = System.getProperty("user.dir") + "/test/quiet.rb";
    writer = new StringWriter();
    Writer errorWriter = new StringWriter();
    instance.setWriter(writer);
    instance.setErrorWriter(errorWriter);
    Object result = instance.runScriptlet(PathType.ABSOLUTE, filename);
    String expResult = "foo";
    // This never successes.
    // assertEquals(expResult, result);
  }
示例#5
0
  /** 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;
  }
示例#6
0
  /** 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;
  }