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

Always close after assert_screen to make test stable. #24

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ You can test the executed result and its rendering on the automatically detected
require 'yamatanooroti'

class MyTest < Yamatanooroti::TestCase
def setup
start_terminal(5, 30, ['irb', '-f', '--multiline'])
end

def test_example
start_terminal(5, 30, ['irb', '-f', '--multiline'])
write(":a\n")
close
assert_screen(['irb(main):001:0> :a', '=> :a', 'irb(main):002:0>', '', ''])
close
end
end
```
Expand Down Expand Up @@ -57,14 +54,11 @@ If you want to specify vterm environment that needs vterm gem, you can use `Yama
require 'yamatanooroti'

class MyTest < Yamatanooroti::VTermTestCase
def setup
start_terminal(5, 30, ['irb', '-f', '--multiline'])
end

def test_example
start_terminal(5, 30, ['irb', '-f', '--multiline'])
write(":a\n")
close
assert_screen(['irb(main):001:0> :a', '=> :a', 'irb(main):002:0>', '', ''])
close
end
end
```
Expand All @@ -82,15 +76,15 @@ Starts terminal internally that is sized `height` and `width` with `command` to
If `startup_message` is given, `start_terminal` waits for the string to be printed and then returns.

```ruby
code = 'sleep 1; puts "aaa"; sleep 10; puts "bbb"'
start_terminal(5, 30, ['ruby', '-e', code], startup_message: 'aaa')
close
code = 'sleep 1; print "prompt>"; s = gets; sleep 1; puts s.upcase'
start_terminal(5, 30, ['ruby', '-e', code], startup_message: 'prompt>')
# Screen is already "prompt>"
write "hello\n"
assert_screen(<<~EOC)
aaa
prompt>hello
HELLO
EOC
# The start_terminal method waits for the output of the "aaa" as specified by
# the startup_message option, the "bbb" after 10 seconds won't come because
# the I/O is closed immediately after it.
close
```

### `write(str)`
Expand Down
11 changes: 4 additions & 7 deletions test/yamatanooroti/test_multiplatform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ def setup
start_terminal(5, 30, ['ruby', 'bin/simple_repl'], startup_message: 'prompt>')
end

def teardown
close
end

def test_example
write(":a\n")
close
assert_screen(['prompt> :a', '=> :a', 'prompt>', '', ''])
assert_screen(<<~EOC)
prompt> :a
Expand All @@ -23,27 +26,23 @@ def test_result_repeatedly
write(":b\n")
assert_screen(/=> :b\nprompt>/)
assert_equal(['prompt> :a', '=> :a', 'prompt> :b', '=> :b', 'prompt>'], result)
close
end

def test_assert_screen_retries
write("sleep 1\n")
assert_screen(/=> 1\nprompt>/)
assert_equal(['prompt> sleep 1', '=> 1', 'prompt>', '', ''], result)
close
end

def test_assert_screen_timeout
write("sleep 3\n")
assert_raise do
assert_screen(/=> 3\nprompt>/)
end
close
end

def test_auto_wrap
write("12345678901234567890123\n")
close
assert_screen(['prompt> 1234567890123456789012', '3', '=> 12345678901234567890123', 'prompt>', ''])
assert_screen(<<~EOC)
prompt> 1234567890123456789012
Expand All @@ -55,14 +54,12 @@ def test_auto_wrap

def test_fullwidth
write(":あ\n")
close
assert_screen(/=> :あ\nprompt>/)
assert_equal(['prompt> :あ', '=> :あ', 'prompt>', '', ''], result)
end

def test_two_fullwidth
write(":あい\n")
close
assert_screen(/=> :あい\nprompt>/)
assert_equal(['prompt> :あい', '=> :あい', 'prompt>', '', ''], result)
end
Expand Down
26 changes: 12 additions & 14 deletions test/yamatanooroti/test_run_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@
class Yamatanooroti::TestRunRuby < Yamatanooroti::TestCase
st0012 marked this conversation as resolved.
Show resolved Hide resolved
def test_winsize
start_terminal(5, 30, ['ruby', '-rio/console', '-e', 'puts(IO.console.winsize.inspect)'])
sleep 0.5
close
assert_screen(<<~EOC)
[5, 30]
EOC
close
end

def test_wait_for_startup_message
code = 'sleep 1; puts "aaa"; sleep 10; puts "bbb"'
start_terminal(5, 30, ['ruby', '-e', code], startup_message: 'aaa')
# The start_terminal method waits 1 sec for "aaa" as specified by
# wait_for_startup_message option and close immediately by the close
# method at the next line. The next "bbb" after waiting 1 sec more doesn't
# be caught because I/O is already closed.
close
code = 'sleep 1; print "prompt>"; s = gets; sleep 1; puts s.upcase'
start_terminal(5, 30, ['ruby', '-e', code], startup_message: 'prompt>')
assert_equal(['prompt>', '', '', '', ''], result)
write "hello\n"
assert_screen(<<~EOC)
aaa
prompt>hello
HELLO
EOC
close
end

def test_move_cursor_and_render
start_terminal(5, 30, ['ruby', '-rio/console', '-e', 'STDOUT.puts(?A);STDOUT.goto(2,2);STDOUT.puts(?B)'])
assert_screen(/^ B/)
close
assert_screen(['A', '', ' B', '', ''])
assert_equal(['A', '', ' B', '', ''], result)
close
end

def test_meta_key
Expand All @@ -37,22 +35,22 @@ def test_meta_key
write('aaa ccc')
write("\M-b")
write('bbb ')
close
assert_screen(<<~EOC)
>>>aaa bbb ccc
EOC
close
ensure
get_out_from_tmpdir
end

def test_assert_screen_takes_a_message_when_failed
start_terminal(5, 30, ['ruby', '-e', 'puts "aaa"'])
close
assert_raise_with_message Test::Unit::AssertionFailedError, /\Amessage when failed/ do
assert_screen(<<~EOC, 'message when failed')
bbb
EOC
end
close
end

private
Expand Down