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