forked from jruby/jruby
/
test_globals.rb
135 lines (114 loc) · 2.69 KB
/
test_globals.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'test/unit'
class TestGlobals < Test::Unit::TestCase
def check_global_variable
assert_equal "a global variable.", $output
end
def test_global_scope
$output = "a global variable."
check_global_variable
end
def test_global_alias
$bar = 5
alias $foo $bar
assert_equal 5, $foo
$bar = 10
assert_equal 10, $foo
$foo = 5
assert_equal 5, $bar
end
# Make sure $@ == nil if $! is not nil and $!.backtrace is an array
class MyWobblyError < StandardError
def initialize(backtrace) ; @backtrace = backtrace ; end
def backtrace ; @backtrace ; end
end
def test_global_error_vars
begin
raise MyWobblyError.new(nil)
rescue
assert_equal nil, $@
end
begin
raise MyWobblyError.new("abc")
rescue
assert_equal nil, $@
end
#inconsistent with set_backtrace but it's what MRI does
begin
raise MyWobblyError.new(["abc", 123])
rescue
assert $@ != nil
end
begin
raise MyWobblyError.new(["abc", "123"])
rescue
assert $@ != nil
end
begin
raise MyWobblyError.new([])
rescue
assert $@ != nil
end
end
def test_program_name
assert_equal $0, $PROGRAM_NAME
old, $0 = $0, "abc"
$0 = old
end
def test_locally_scoped_globals
assert_nothing_raised { print }
assert_nothing_raised { $_.to_s }
assert_nothing_raised { $~.to_s }
$_ = 'one'
'one' =~ /one/
second_call
assert_equal('one', $_)
assert_equal('one', $~[0])
end
def second_call
$_ = 'two'
'two' =~ /two/
end
def test_aliases_backref_globals
alias $POSTMATCH $'
alias $PREMATCH $`
alias $MATCH $&
alias $LAST_MATCH_INFO $~
/^is/ =~ "isFubared"
assert_not_nil($LAST_MATCH_INFO)
assert_equal($~, $LAST_MATCH_INFO)
assert_equal "Fubared", $'
assert_equal $', $POSTMATCH
assert_equal "", $`
assert_equal $`, $PREMATCH
assert_equal "is", $&
assert_equal $&, $MATCH
end
def test_english_ignore_case
alias $IGNORECASE $=
assert_not_nil($IGNORECASE)
assert_equal($=, $IGNORECASE)
assert_nil("fOo" =~ /foo/)
assert("fOo" =~ /foo/i)
$= = true
assert("fOo" =~ /foo/i)
$= = false
end
# JRUBY-1396, $? was returning Java null instead of nil when uninitialized
def test_last_exit_status_as_param
assert_nothing_raised {'foo' == $?}
end
def test_that_last_exit_status_is_nil
assert_nil $?
end
def test_backref_set_checks_for_matchdata
assert_raises(TypeError){$~ = 1}
end
def test_backref_assignment
"ac" =~ /a/
m = $~
"ab" =~ /a/
assert_equal(m.post_match, "c")
$~ = m
assert_equal($~.post_match, "c")
end
end