Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The input stream is exhausted #28

Closed
dyba opened this issue Dec 14, 2011 · 2 comments
Closed

The input stream is exhausted #28

dyba opened this issue Dec 14, 2011 · 2 comments

Comments

@dyba
Copy link

dyba commented Dec 14, 2011

I am trying to test the output from the Terminal and wrote this snippet that narrows down what I am trying to do. https://gist.github.com/1474931. Would you be able to tell me if I am testing HighLine incorrectly?

I see that calling Readline.readline(question, true) results in a nil return value. At that point of execution, question is defined as @output.string which is equal to the empty string "". Now, if I'm testing the output text, why am I getting an error relating to the input?

I am running Ruby 1.9.2-p290 with the latest version of highline on the latest version of Mac OS X.

Incidentally, I tried the same test on a Windows 7 machine with Ruby 1.9.3-p0 and got the following results:

C:\Users\ddyba\Scripts\exhausted_input>ruby highline_test.rb
Run options:

# Running tests:

E

Finished tests in 0.005000s, 200.0000 tests/s, 0.0000 assertions/s.

  1) Error:
test_ask_question_returns_prompt(HighLineTest):
NoMethodError: private method `puts' called for nil:NilClass
    C:/Users/ddyba/.pik/rubies/Ruby-193-p0/lib/ruby/gems/1.9.1/gems/highline-1.6.8/lib/highline.rb:608:in `say'
    C:/Users/ddyba/.pik/rubies/Ruby-193-p0/lib/ruby/gems/1.9.1/gems/highline-1.6.8/lib/highline.rb:245:in `ask'
    highline_test.rb:7:in `ask_question'
    highline_test.rb:15:in `setup'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

C:\Users\ddyba\Scripts\exhausted_input>
@JEG2
Copy link
Owner

JEG2 commented Dec 14, 2011

This isn't a bug in HighLine and it isn't related to Readline (which isn't used in this example).

The issue is that you are passing an empty StringIO for HighLine to read content from. That returns nil, just as an exhausted IO object would:

>> StringIO.new.gets
=> nil

We just need to provide some answer for HighLine to read. I realize you aren't using it in the test, but HighLine reads for input before we ever get to your assertion.

Here's how I got your test to run:

require 'test/unit'
require 'stringio'
require 'highline'

def ask_question(input = $stdin, output = $stdout)
  highline = HighLine.new(input, output)
  highline.ask("How are you?")
end

class HighlineTest < Test::Unit::TestCase
  def setup
    @input = StringIO.new("some answer")
    @output = StringIO.new
    ask_question(@input, @output)
  end

  def test_ask_question_returns_prompt
    assert_equal "How are you?\n", @output.string
  end
end

Hope that helps

@JEG2 JEG2 closed this as completed Dec 14, 2011
@dyba
Copy link
Author

dyba commented Dec 14, 2011

@JEG2 Thanks for your speedy response! It's clear to me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants