Highline をテストする方法を見つける最善の方法は、作成者がパッケージをどのようにテストするかを確認することです。
class TestHighLine < Test::Unit::TestCase
def setup
@input = StringIO.new
@output = StringIO.new
@terminal = HighLine.new(@input, @output)..
end
..
def test_agree
@input << "y\nyes\nYES\nHell no!\nNo\n"
@input.rewind
assert_equal(true, @terminal.agree("Yes or no? "))
assert_equal(true, @terminal.agree("Yes or no? "))
assert_equal(true, @terminal.agree("Yes or no? "))
assert_equal(false, @terminal.agree("Yes or no? "))
....
@input.truncate(@input.rewind)
@input << "yellow"
@input.rewind
assert_equal(true, @terminal.agree("Yes or no? ", :getc))
end
def test_ask
name = "James Edward Gray II"
@input << name << "\n"
@input.rewind
assert_equal(name, @terminal.ask("What is your name? "))
....
assert_raise(EOFError) { @terminal.ask("Any input left? ") }
end
など、彼のコードに示されているように。この情報は、リンクで強調したセットアップに細心の注意を払ってハイライン ソースで見つけることができます。
キーボードでキーを入力する代わりに、彼が STDIN IO パイプを使用する方法に注目してください。
highline
これが実際に示しているのは、そのようなことをテストするために を使用する必要がないということです。ここでは、彼のテストのセットアップが本当に重要です。StringIO
オブジェクトとしての彼の使用とともに 。