Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created September 26, 2009 22:04
Show Gist options
  • Save thinkerbot/194470 to your computer and use it in GitHub Desktop.
Save thinkerbot/194470 to your computer and use it in GitHub Desktop.
Failed models for a prompt test
def prompt_test(cmd, script, prompt)
cmd, leader = cmd.lstrip.split(/^/, 2)
inputs = []
expected = [leader]
script.lstrip.split(/^#{prompt}/).each do |lines|
next if lines.empty?
input, output = lines.split(/^/, 2)
inputs << input
expected << output
end
IO.popen(cmd, "r+") do |io|
io.write inputs.join
io.close_write
assert_equal expected.join, io.read
end
end
def prompt_test(cmd, script, prompt)
cmd, leader = cmd.lstrip.split(/^/, 2)
prompt_regexp = /^#{prompt}/
Open3.popen3(cmd) do |i, o, s|
if leader
assert_equal leader, o.read_nonblock(leader.length)
end
n = 0
script.lstrip.split(prompt_regexp).each do |lines|
n += 1
next if lines.empty?
input, expected = lines.split(/^/, 2)
i.write(input)
assert_equal expected, o.read(expected.length), "at input #{n}: #{prompt}#{input}"
assert_equal prompt, o.read(prompt.length), "after input: #{n} (incorrect prompt)"
end
end
end
def test_irb_prompt_test
prompt_test "irb", %Q{
>> 1 + 2
=> 3
>> puts "goodnight moon"
goodnight moon
=> nil
>> exit
}, ">> "
end
def test_python_prompt_test
prompt_test "python", %Q{
>>> 1 + 2
3
>>> exit()
}, ">>> "
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment