Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 83 lines (70 sloc) 2.929 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
# coding: utf-8
require 'helper'

describe "Pry#input_stack" do
  it 'should accept :input_stack as a config option' do
    stack = [StringIO.new("test")]
    Pry.new(:input_stack => stack).input_stack.should == stack
  end

  it 'should use defaults from Pry.config' do
    Pry.config.input_stack = [StringIO.new("exit")]
    Pry.new.input_stack.should == Pry.config.input_stack
    Pry.config.input_stack = []
  end

  it 'should read from all input objects on stack and exit session (usingn repl)' do
    stack = [b = StringIO.new(":cloister\nexit\n"), c = StringIO.new(":baron\n")]
    instance = Pry.new(:input => StringIO.new(":alex\n"),
                       :output => str_output = StringIO.new,
                       :input_stack => stack)

    instance.repl
    str_output.string.should =~ /:alex/
    str_output.string.should =~ /:baron/
    str_output.string.should =~ /:cloister/
  end

  it 'input objects should be popped off stack as they are used up' do
    stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
    instance = Pry.new(:input => StringIO.new(":alex\n"),
                       :output => str_output = StringIO.new,
                       :input_stack => stack)

    stack.size.should == 2

    instance.rep
    str_output.string.should =~ /:alex/
    instance.rep
    str_output.string.should =~ /:baron/
    stack.size.should == 1
    instance.rep
    str_output.string.should =~ /:cloister/
    stack.size.should == 0
  end

  it 'should revert to Pry.config.input when it runs out of input objects in input_stack' do
    redirect_pry_io(StringIO.new(":rimbaud\nexit\n"), StringIO.new) do
      stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
      instance = Pry.new(:input => StringIO.new(":alex\n"),
                         :output => str_output = StringIO.new,
                         :input_stack => stack)

      instance.repl
      str_output.string.should =~ /:alex/
      str_output.string.should =~ /:baron/
      str_output.string.should =~ /:cloister/
      str_output.string.should =~ /:rimbaud/
    end
  end

  it 'should display error and throw(:breakout) if at end of input after using up input_stack objects' do
    str_output = StringIO.new
    catch(:breakout) do
      redirect_pry_io(StringIO.new(":rimbaud\n"), str_output) do
        Pry.new(:input_stack => [StringIO.new(":a\n"), StringIO.new(":b\n")]).repl
      end
    end
    str_output.string.should =~ /Error: Pry ran out of things to read/
  end

  if "".respond_to?(:encoding)
    it "should pass strings to Pry in the right encoding" do
      input1 = "'f。。'.encoding.name" # utf-8, see coding declaration
      input2 = input1.encode('Shift_JIS')

      mock_pry(input1, input2).should == %{=> "UTF-8"\n=> "Shift_JIS"\n\n}
    end

    it "should be able to use unicode regexes on a UTF-8 terminal" do
      mock_pry('":-Þ" =~ /þ/i').should == %{=> 2\n\n}
    end
  end
end
Something went wrong with that request. Please try again.