add stdout_check/stderr_check methods

This commit is contained in:
Johan Holmberg 2018-04-12 17:09:44 +02:00
parent f59ee18820
commit 82c329da9e
2 changed files with 87 additions and 0 deletions

@ -606,8 +606,26 @@ module Cmdtest
_stdxxx_equal_aux("stderr", false, expected)
end
# Yield stdout to user, expecting 'assert' call.
def stdout_check(&block)
_stdxxx_check_aux("stdout", &block)
end
# Yield stderr to user, expecting 'assert' call.
def stderr_check(&block)
_stdxxx_check_aux("stderr", &block)
end
# helper methods
def _stdxxx_check_aux(stdxxx)
_process_after do
@_checked[stdxxx] = true
str = @_effects.send(stdxxx).text(@_output_encoding, @_output_newline)
yield _str_as_lines(str)
end
end
def _stdxxx_contain_aux(stdxxx, positive, expected)
_process_after do
@_checked[stdxxx] = true

69
t/CMDTEST_stdxxx_check.rb Normal file

@ -0,0 +1,69 @@
require "selftest_utils"
class CMDTEST_stdxxx_check < Cmdtest::Testcase
include SelftestUtils
#========================================
# Using "define_method" to avoid duplicating definitions of
# stderr/stdout methods.
def self._define_stdxxx_methods(stdxxx)
#----------------------------------------
# stdxxx_check
#----------------------------------------
define_method("test_#{stdxxx}_check_CORRECT") do
create_CMDTEST_foo [
"cmd 'echo_#{stdxxx}.rb --lines A B C A' do",
" #{stdxxx}_check do |lines|",
" n = 0",
" for line in lines",
" n +=1 if line == 'A'",
" end",
" assert n == 2, \"A occurs \#{n} times (not 2)\"",
" end",
"end",
]
cmd_cmdtest do
stdout_equal [
"### echo_#{stdxxx}.rb --lines A B C A",
]
end
end
define_method("test_#{stdxxx}_check_INCORRECT") do
create_CMDTEST_foo [
"cmd 'echo_#{stdxxx}.rb --lines A B C D' do",
" #{stdxxx}_check do |lines|",
" n = 0",
" for line in lines",
" n +=1 if line == 'A'",
" end",
" assert n == 2, \"A occurs \#{n} times (not 2)\"",
" end",
"end",
]
cmd_cmdtest do
stdout_equal [
"### echo_#{stdxxx}.rb --lines A B C D",
"--- ERROR: assertion: A occurs 1 times (not 2)",
]
exit_nonzero
end
end
end # _define_stdxxx_methods
#----------------------------------------
for stdxxx in ["stderr", "stdout"]
_define_stdxxx_methods(stdxxx)
end
end