|
| 1 | +require "iruby" |
| 2 | +require "test/unit" |
| 3 | +require "test/unit/rr" |
| 4 | +require "tmpdir" |
| 5 | + |
| 6 | +module IRubyTest |
| 7 | + class TestBase < Test::Unit::TestCase |
| 8 | + def assert_output(stdout=nil, stderr=nil) |
| 9 | + flunk "assert_output requires a block to capture output." unless block_given? |
| 10 | + |
| 11 | + out, err = capture_io do |
| 12 | + yield |
| 13 | + end |
| 14 | + |
| 15 | + y = check_assert_output_result(stderr, err, "stderr") |
| 16 | + x = check_assert_output_result(stdout, out, "stdout") |
| 17 | + |
| 18 | + (!stdout || x) && (!stderr || y) |
| 19 | + end |
| 20 | + |
| 21 | + private |
| 22 | + |
| 23 | + def capture_io |
| 24 | + captured_stdout = StringIO.new |
| 25 | + captured_stderr = StringIO.new |
| 26 | + |
| 27 | + orig_stdout, $stdout = $stdout, captured_stdout |
| 28 | + orig_stderr, $stderr = $stderr, captured_stderr |
| 29 | + |
| 30 | + yield |
| 31 | + |
| 32 | + return captured_stdout.string, captured_stderr.string |
| 33 | + ensure |
| 34 | + $stdout = orig_stdout |
| 35 | + $stderr = orig_stderr |
| 36 | + end |
| 37 | + |
| 38 | + def check_assert_output_result(expected, actual, name) |
| 39 | + if expected |
| 40 | + message = "In #{name}" |
| 41 | + case expected |
| 42 | + when Regexp |
| 43 | + assert_match(expected, actual, message) |
| 44 | + else |
| 45 | + assert_equal(expected, actual, message) |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def ignore_warning |
| 51 | + saved, $VERBOSE = $VERBOSE , nil |
| 52 | + yield |
| 53 | + ensure |
| 54 | + $VERBOSE = saved |
| 55 | + end |
| 56 | + |
| 57 | + def with_env(env) |
| 58 | + keys = env.keys |
| 59 | + saved_values = ENV.values_at(*keys) |
| 60 | + ENV.update(env) |
| 61 | + yield |
| 62 | + ensure |
| 63 | + if keys && saved_values |
| 64 | + keys.zip(saved_values) do |k, v| |
| 65 | + ENV[k] = v |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def windows_only |
| 71 | + omit('windows only test') unless windows? |
| 72 | + end |
| 73 | + |
| 74 | + def apple_only |
| 75 | + omit('apple only test') unless windows? |
| 76 | + end |
| 77 | + |
| 78 | + def unix_only |
| 79 | + omit('unix only test') if windows? || apple? |
| 80 | + end |
| 81 | + |
| 82 | + def windows? |
| 83 | + /mingw|mswin/ =~ RUBY_PLATFORM |
| 84 | + end |
| 85 | + |
| 86 | + def apple? |
| 87 | + /darwin/ =~ RUBY_PLATFORM |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments