From 82c329da9ee3b0e7dd2f1bc2064836b15a67b5f4 Mon Sep 17 00:00:00 2001 From: Johan Holmberg <holmberg556@gmail.com> Date: Thu, 12 Apr 2018 17:09:44 +0200 Subject: [PATCH] add stdout_check/stderr_check methods --- lib/cmdtest/testcase.rb | 18 ++++++++++ t/CMDTEST_stdxxx_check.rb | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 t/CMDTEST_stdxxx_check.rb diff --git a/lib/cmdtest/testcase.rb b/lib/cmdtest/testcase.rb index 831e549..f40efaf 100644 --- a/lib/cmdtest/testcase.rb +++ b/lib/cmdtest/testcase.rb @@ -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 diff --git a/t/CMDTEST_stdxxx_check.rb b/t/CMDTEST_stdxxx_check.rb new file mode 100644 index 0000000..314dc8a --- /dev/null +++ b/t/CMDTEST_stdxxx_check.rb @@ -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 +