From f8f252c2a3d1bf64e7b26a899358c6c07d74b284 Mon Sep 17 00:00:00 2001 From: tompng Date: Sat, 28 Sep 2024 21:16:27 +0900 Subject: [PATCH] Always close after assert_screen to make test stable. --- README.md | 18 ++++++------ test/yamatanooroti/test_multiplatform.rb | 8 ++--- test/yamatanooroti/test_run_ruby.rb | 37 +++++++++++++++--------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 99fa145..840d887 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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 ``` @@ -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)` diff --git a/test/yamatanooroti/test_multiplatform.rb b/test/yamatanooroti/test_multiplatform.rb index dbcd1af..09c89b7 100644 --- a/test/yamatanooroti/test_multiplatform.rb +++ b/test/yamatanooroti/test_multiplatform.rb @@ -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 @@ -43,7 +43,6 @@ 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 @@ -51,19 +50,20 @@ def test_auto_wrap => 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 diff --git a/test/yamatanooroti/test_run_ruby.rb b/test/yamatanooroti/test_run_ruby.rb index 28d7b7d..f90acc1 100644 --- a/test/yamatanooroti/test_run_ruby.rb +++ b/test/yamatanooroti/test_run_ruby.rb @@ -4,31 +4,40 @@ 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. + def test_foo + code = 'sleep 1; print "prompt>"; s = gets; sleep 1; puts s.upcase' + start_terminal(5, 30, ['ruby', '-e', code], startup_message: 'prompt>') + write "hello\n" + assert_screen(<<~EOC) + prompt>hello + HELLO + EOC close + end + + def test_wait_for_startup_message + 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 @@ -37,22 +46,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