Best Minitest_ruby code snippet using Assertions.capture_io
test_rake_application.rb
Source:test_rake_application.rb
...5 @app = Rake.application6 @app.options.rakelib = []7 end8 def test_constant_warning9 _, err = capture_io do @app.instance_eval { const_warning("Task") } end10 assert_match(/warning/i, err)11 assert_match(/deprecated/i, err)12 assert_match(/Task/i, err)13 end14 def test_display_tasks15 @app.options.show_tasks = :tasks16 @app.options.show_task_pattern = //17 @app.last_description = "COMMENT"18 @app.define_task(Rake::Task, "t")19 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end20 assert_match(/^rake t/, out)21 assert_match(/# COMMENT/, out)22 end23 def test_display_tasks_with_long_comments24 @app.terminal_columns = 8025 @app.options.show_tasks = :tasks26 @app.options.show_task_pattern = //27 @app.last_description = "1234567890" * 828 @app.define_task(Rake::Task, "t")29 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end30 assert_match(/^rake t/, out)31 assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)32 end33 def test_display_tasks_with_task_name_wider_than_tty_display34 @app.terminal_columns = 8035 @app.options.show_tasks = :tasks36 @app.options.show_task_pattern = //37 task_name = "task name" * 8038 @app.last_description = "something short"39 @app.define_task(Rake::Task, task_name )40 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end41 # Ensure the entire task name is output and we end up showing no description42 assert_match(/rake #{task_name} # .../, out)43 end44 def test_display_tasks_with_very_long_task_name_to_a_non_tty_shows_name_and_comment45 @app.options.show_tasks = :tasks46 @app.options.show_task_pattern = //47 @app.tty_output = false48 description = "something short"49 task_name = "task name" * 8050 @app.last_description = "something short"51 @app.define_task(Rake::Task, task_name )52 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end53 # Ensure the entire task name is output and we end up showing no description54 assert_match(/rake #{task_name} # #{description}/, out)55 end56 def test_display_tasks_with_long_comments_to_a_non_tty_shows_entire_comment57 @app.options.show_tasks = :tasks58 @app.options.show_task_pattern = //59 @app.tty_output = false60 @app.last_description = "1234567890" * 861 @app.define_task(Rake::Task, "t")62 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end63 assert_match(/^rake t/, out)64 assert_match(/# #{@app.last_description}/, out)65 end66 def test_display_tasks_with_long_comments_to_a_non_tty_with_columns_set_truncates_comments67 @app.terminal_columns = 8068 @app.options.show_tasks = :tasks69 @app.options.show_task_pattern = //70 @app.tty_output = false71 @app.last_description = "1234567890" * 872 @app.define_task(Rake::Task, "t")73 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end74 assert_match(/^rake t/, out)75 assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)76 end77 def test_describe_tasks78 @app.options.show_tasks = :describe79 @app.options.show_task_pattern = //80 @app.last_description = "COMMENT"81 @app.define_task(Rake::Task, "t")82 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end83 assert_match(/^rake t$/, out)84 assert_match(/^ {4}COMMENT$/, out)85 end86 def test_show_lines87 @app.options.show_tasks = :lines88 @app.options.show_task_pattern = //89 @app.last_description = "COMMENT"90 @app.define_task(Rake::Task, "t")91 @app['t'].locations << "HERE:1"92 out, = capture_io do @app.instance_eval { display_tasks_and_comments } end93 assert_match(/^rake t +[^:]+:\d+ *$/, out)94 end95 def test_finding_rakefile96 rakefile_default97 assert_match(/Rakefile/i, @app.instance_eval { have_rakefile })98 end99 def test_not_finding_rakefile100 @app.instance_eval { @rakefiles = ['NEVER_FOUND'] }101 assert( ! @app.instance_eval do have_rakefile end )102 assert_nil @app.rakefile103 end104 def test_load_rakefile105 rakefile_unittest106 @app.instance_eval do107 handle_options108 options.silent = true109 load_rakefile110 end111 assert_equal "rakefile", @app.rakefile.downcase112 assert_equal @tempdir, Dir.pwd113 end114 def test_load_rakefile_doesnt_print_rakefile_directory_from_same_dir115 rakefile_unittest116 _, err = capture_io do117 @app.instance_eval do118 # pretend we started from the unittest dir119 @original_dir = File.expand_path(".")120 raw_load_rakefile121 end122 end123 assert_empty err124 end125 def test_load_rakefile_from_subdir126 rakefile_unittest127 Dir.chdir 'subdir'128 @app.instance_eval do129 handle_options130 options.silent = true131 load_rakefile132 end133 assert_equal "rakefile", @app.rakefile.downcase134 assert_equal @tempdir, Dir.pwd135 end136 def test_load_rakefile_prints_rakefile_directory_from_subdir137 rakefile_unittest138 Dir.chdir 'subdir'139 app = Rake::Application.new140 app.options.rakelib = []141 _, err = capture_io do142 app.instance_eval do143 raw_load_rakefile144 end145 end146 assert_equal "(in #{@tempdir}\)\n", err147 end148 def test_load_rakefile_doesnt_print_rakefile_directory_from_subdir_if_silent149 rakefile_unittest150 Dir.chdir 'subdir'151 _, err = capture_io do152 @app.instance_eval do153 handle_options154 options.silent = true155 raw_load_rakefile156 end157 end158 assert_empty err159 end160 def test_load_rakefile_not_found161 Dir.chdir @tempdir162 ENV['RAKE_SYSTEM'] = 'not_exist'163 @app.instance_eval do164 handle_options165 options.silent = true166 end167 ex = assert_raises(RuntimeError) do168 @app.instance_eval do raw_load_rakefile end169 end170 assert_match(/no rakefile found/i, ex.message)171 end172 def test_load_from_system_rakefile173 rake_system_dir174 @app.instance_eval do175 handle_options176 options.silent = true177 options.load_system = true178 options.rakelib = []179 load_rakefile180 end181 assert_equal @system_dir, @app.system_dir182 assert_nil @app.rakefile183 rescue SystemExit184 flunk 'failed to load rakefile'185 end186 def test_load_from_calculated_system_rakefile187 rakefile_default188 def @app.standard_system_dir189 "__STD_SYS_DIR__"190 end191 ENV['RAKE_SYSTEM'] = nil192 @app.instance_eval do193 handle_options194 options.silent = true195 options.load_system = true196 options.rakelib = []197 load_rakefile198 end199 assert_equal "__STD_SYS_DIR__", @app.system_dir200 rescue SystemExit201 flunk 'failed to find system rakefile'202 end203 def test_terminal_columns204 old_RAKE_COLUMNS = ENV['RAKE_COLUMNS']205 ENV['RAKE_COLUMNS'] = '42'206 app = Rake::Application.new207 assert_equal 42, app.terminal_columns208 ensure209 if old_RAKE_COLUMNS then210 ENV['RAKE_COLUMNS'].delete211 else212 ENV['RAKE_COLUMNS'] = old_RAKE_COLUMNS213 end214 end215 def test_windows216 assert ! (@app.windows? && @app.unix?)217 end218 def test_loading_imports219 loader = util_loader220 @app.instance_eval do221 add_loader("dummy", loader)222 add_import("x.dummy")223 load_imports224 end225 # HACK no assertions226 end227 def test_building_imported_files_on_demand228 loader = util_loader229 @app.instance_eval do230 intern(Rake::Task, "x.dummy").enhance do loader.make_dummy end231 add_loader("dummy", loader)232 add_import("x.dummy")233 load_imports234 end235 # HACK no assertions236 end237 def test_handle_options_should_strip_options_from_ARGV238 assert !@app.options.trace239 valid_option = '--trace'240 ARGV.clear241 ARGV << valid_option242 @app.handle_options243 assert !ARGV.include?(valid_option)244 assert @app.options.trace245 end246 def test_good_run247 ran = false248 ARGV << '--rakelib=""'249 @app.options.silent = true250 @app.instance_eval do251 intern(Rake::Task, "default").enhance { ran = true }252 end253 rakefile_default254 out, err = capture_io do255 @app.run256 end257 assert ran258 assert_empty err259 assert_equal "DEFAULT\n", out260 end261 def test_display_task_run262 ran = false263 ARGV.clear264 ARGV << '-f' << '-s' << '--tasks' << '--rakelib=""'265 @app.last_description = "COMMENT"266 @app.define_task(Rake::Task, "default")267 out, = capture_io { @app.run }268 assert @app.options.show_tasks269 assert ! ran270 assert_match(/rake default/, out)271 assert_match(/# COMMENT/, out)272 end273 def test_display_prereqs274 ran = false275 ARGV.clear276 ARGV << '-f' << '-s' << '--prereqs' << '--rakelib=""'277 @app.last_description = "COMMENT"278 t = @app.define_task(Rake::Task, "default")279 t.enhance([:a, :b])280 @app.define_task(Rake::Task, "a")281 @app.define_task(Rake::Task, "b")282 out, = capture_io { @app.run }283 assert @app.options.show_prereqs284 assert ! ran285 assert_match(/rake a$/, out)286 assert_match(/rake b$/, out)287 assert_match(/rake default\n( *(a|b)\n){2}/m, out)288 end289 def test_bad_run290 @app.intern(Rake::Task, "default").enhance { fail }291 ARGV.clear292 ARGV << '-f' << '-s' << '--rakelib=""'293 assert_raises(SystemExit) {294 _, err = capture_io { @app.run }295 assert_match(/see full trace/, err)296 }297 ensure298 ARGV.clear299 end300 def test_bad_run_with_trace301 @app.intern(Rake::Task, "default").enhance { fail }302 ARGV.clear303 ARGV << '-f' << '-s' << '-t'304 assert_raises(SystemExit) {305 _, err = capture_io { @app.run }306 refute_match(/see full trace/, err)307 }308 ensure309 ARGV.clear310 end311 def test_run_with_bad_options312 @app.intern(Rake::Task, "default").enhance { fail }313 ARGV.clear314 ARGV << '-f' << '-s' << '--xyzzy'315 assert_raises(SystemExit) {316 capture_io { @app.run }317 }318 ensure319 ARGV.clear320 end321 def test_deprecation_message322 _, err = capture_io do323 @app.deprecate("a", "b", "c")324 end325 assert_match(/'a' is deprecated/i, err)326 assert_match(/use 'b' instead/i, err)327 assert_match(/at c$/i, err)328 end329 def test_standard_exception_handling_invalid_option330 out, err = capture_io do331 e = assert_raises SystemExit do332 @app.standard_exception_handling do333 raise OptionParser::InvalidOption, 'blah'334 end335 end336 assert_equal 1, e.status337 end338 assert_empty out339 assert_equal "invalid option: blah\n", err340 end341 def test_standard_exception_handling_other342 out, err = capture_io do343 e = assert_raises SystemExit do344 @app.standard_exception_handling do345 raise 'blah'346 end347 end348 assert_equal 1, e.status349 end350 assert_empty out351 assert_match "rake aborted!\n", err352 assert_match "blah\n", err353 end354 def test_standard_exception_handling_system_exit355 out, err = capture_io do356 e = assert_raises SystemExit do357 @app.standard_exception_handling do358 exit 0359 end360 end361 assert_equal 0, e.status362 end363 assert_empty out364 assert_empty err365 end366 def test_standard_exception_handling_system_exit_nonzero367 out, err = capture_io do368 e = assert_raises SystemExit do369 @app.standard_exception_handling do370 exit 5371 end372 end373 assert_equal 5, e.status374 end375 assert_empty out376 assert_empty err377 end378 def util_loader379 loader = Object.new380 loader.instance_variable_set :@load_called, false381 def loader.load arg...
capture_io
Using AI Code Generation
1 assert_output("Hello World2") { puts "Hello World" }3 assert_output(nil, "Hello World4") { warn "Hello World" }5 assert_output("Hello World6") { warn "Hello World"; puts "Hello World" }7 assert_output("Hello World8 assert_output("Hello World9 assert_output("Hello World10 assert_output("Hello World
capture_io
Using AI Code Generation
1 assert_equal("hello2", capture_io { puts "hello" })3 assert_equal("hello4", capture_io { $stderr.puts "hello" })5 assert_equal("hello6", capture_io { print "hello" })7 assert_equal("hello8", capture_io { $stderr.print "hello" })
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!