Skip to content

Commit

Permalink
Always close after assert_screen to make test stable.
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Sep 28, 2024
1 parent 92e188a commit 516bc48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class MyTest < Yamatanooroti::TestCase

def test_example
write(":a\n")
close
assert_screen(['irb(main):001:0> :a', '=> :a', 'irb(main):002:0>', '', ''])
close
end
end
```
Expand Down Expand Up @@ -63,8 +63,8 @@ class MyTest < Yamatanooroti::VTermTestCase

def test_example
write(":a\n")
close
assert_screen(['irb(main):001:0> :a', '=> :a', 'irb(main):002:0>', '', ''])
close
end
end
```
Expand All @@ -82,15 +82,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
8 changes: 4 additions & 4 deletions test/yamatanooroti/test_multiplatform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def setup

def test_example
write(":a\n")
close
assert_screen(['prompt> :a', '=> :a', 'prompt>', '', ''])
assert_screen(<<~EOC)
prompt> :a
=> :a
prompt>
EOC
close
end

def test_result_repeatedly
Expand Down Expand Up @@ -43,27 +43,27 @@ def test_assert_screen_timeout

def test_auto_wrap
write("12345678901234567890123\n")
close
assert_screen(['prompt> 1234567890123456789012', '3', '=> 12345678901234567890123', 'prompt>', ''])
assert_screen(<<~EOC)
prompt> 1234567890123456789012
3
=> 12345678901234567890123
prompt>
EOC
close
end

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

def test_two_fullwidth
write(":あい\n")
close
assert_screen(/=> :あい\nprompt>/)
assert_equal(['prompt> :あい', '=> :あい', 'prompt>', '', ''], result)
close
end
end
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
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

0 comments on commit 516bc48

Please sign in to comment.