Package com.squareup.rack.jruby

Source Code of com.squareup.rack.jruby.JRubyRackBodyIteratorTest

package com.squareup.rack.jruby;

import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Before;
import org.junit.Test;

import static org.fest.assertions.api.Assertions.assertThat;

public class JRubyRackBodyIteratorTest {
  private ScriptingContainer scriptingContainer;

  @Before public void setUp() {
    scriptingContainer = new ScriptingContainer();
    scriptingContainer.runScriptlet(PathType.CLASSPATH, "enumerable_with_close.rb");
  }

  @Test public void iteratingOverAThingThatRespondsToClose_shouldCloseTheThing() {
    IRubyObject body = scriptingContainer.parse("EnumerableWithClose.new(%w(first second third))").run();
    JRubyRackBodyIterator subject = new JRubyRackBodyIterator(body);

    assertThat(isOpen(body)).isEqualTo(true);

    while (subject.hasNext()) {
      subject.next();
    }

    assertThat(isOpen(body)).isEqualTo(false);
  }

  @Test public void iteratingOverAThingThatDoesNotRespondToClose_shouldNotBlowUp() {
    JRubyRackBodyIterator subject = new JRubyRackBodyIterator(
        scriptingContainer.parse("%w(first second third)").run());

    while (subject.hasNext()) {
      subject.next();
    }
  }

  private Boolean isOpen(IRubyObject body) {
    return (Boolean) body.callMethod(body.getRuntime().getCurrentContext(), "open").toJava(Boolean.class);
  }
}
TOP

Related Classes of com.squareup.rack.jruby.JRubyRackBodyIteratorTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.