-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
globals_spec.rb
293 lines (237 loc) · 6.79 KB
/
globals_spec.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require_relative 'spec_helper'
require "stringio"
load_extension("globals")
describe "CApiGlobalSpecs" do
before :each do
@f = CApiGlobalSpecs.new
end
it "correctly gets global values" do
suppress_warning { @f.sb_gv_get("$BLAH") }.should == nil
@f.sb_gv_get("$\\").should == nil
@f.sb_gv_get("\\").should == nil # rb_gv_get should change \ to $\
end
it "returns $~" do
'a' =~ /a/
@f.sb_gv_get("$~").to_a.should == ['a']
@f.sb_gv_get("~").to_a.should == ['a']
end
it "correctly sets global values" do
suppress_warning { @f.sb_gv_get("$BLAH") }.should == nil
@f.sb_gv_set("$BLAH", 10)
begin
@f.sb_gv_get("$BLAH").should == 10
ensure
@f.sb_gv_set("$BLAH", nil)
end
end
it "lists all global variables" do
@f.rb_f_global_variables.should == Kernel.global_variables
end
it "rb_define_variable should define a new global variable" do
@f.rb_define_variable("my_gvar", "ABC")
$my_gvar.should == "ABC"
$my_gvar = "XYZ"
@f.sb_get_global_value.should == "XYZ"
end
it "rb_define_readonly_variable should define a new readonly global variable" do
# Check the gvar doesn't exist and ensure rb_gv_get doesn't implicitly declare the gvar,
# otherwise the rb_define_readonly_variable call will conflict.
suppress_warning { @f.sb_gv_get("ro_gvar") } .should == nil
@f.rb_define_readonly_variable("ro_gvar", 15)
$ro_gvar.should == 15
-> { $ro_gvar = 10 }.should raise_error(NameError)
end
it "rb_define_hooked_variable should define a C hooked global variable" do
@f.rb_define_hooked_variable_2x("$hooked_gvar")
$hooked_gvar = 2
$hooked_gvar.should == 4
end
it "rb_define_hooked_variable should use default accessors if NULL ones are supplied" do
@f.rb_define_hooked_variable_default_accessors("$hooked_gvar_default_accessors")
$hooked_gvar_default_accessors = 10
$hooked_gvar_default_accessors.should == 10
end
it "rb_define_hooked_variable with default accessors should return nil for NULL variables" do
@f.rb_define_hooked_variable_null_var("$hooked_gvar_null_value")
$hooked_gvar_null_value.should == nil
end
describe "rb_define_virtual_variable" do
describe "with default accessors" do
before :all do
@f.rb_define_virtual_variable_default_accessors("$virtual_variable_default_accessors")
end
it "is read-only" do
-> { $virtual_variable_default_accessors = 10 }.should raise_error(NameError, /read-only/)
end
it "returns false with the default getter" do
$virtual_variable_default_accessors.should == false
$virtual_variable_default_accessors.should == false
end
end
describe "with supplied accessors" do
before :all do
@f.rb_define_virtual_variable_incrementing_accessors("$virtual_variable_incrementing_accessors")
end
it "returns a dynamically changing value" do
$virtual_variable_incrementing_accessors = 20
$virtual_variable_incrementing_accessors.should == 20
$virtual_variable_incrementing_accessors.should == 21
$virtual_variable_incrementing_accessors.should == 22
$virtual_variable_incrementing_accessors = 100
$virtual_variable_incrementing_accessors.should == 100
$virtual_variable_incrementing_accessors.should == 101
$virtual_variable_incrementing_accessors.should == 102
end
end
end
describe "rb_fs" do
before :each do
@field_separator = $;
end
after :each do
suppress_warning { $; = @field_separator }
end
it "returns nil by default" do
@f.rb_fs.should == nil
end
it "returns the value of $;" do
suppress_warning { $; = "foo" }
@f.rb_fs.should == "foo"
end
end
describe "rb_rs" do
before :each do
@dollar_slash = $/
end
after :each do
suppress_warning { $/ = @dollar_slash }
end
it "returns \\n by default" do
@f.rb_rs.should == "\n"
end
it "returns the value of $/" do
suppress_warning { $/ = "foo" }
@f.rb_rs.should == "foo"
end
end
context "rb_std streams" do
before :each do
@name = tmp("rb_std_streams")
@stream = new_io @name
end
after :each do
@stream.close
rm_r @name
end
describe "rb_stdin" do
after :each do
$stdin = STDIN
end
it "returns $stdin" do
$stdin = @stream
@f.rb_stdin.should equal($stdin)
end
end
describe "rb_stdout" do
after :each do
$stdout = STDOUT
end
it "returns $stdout" do
$stdout = @stream
@f.rb_stdout.should equal($stdout)
end
end
describe "rb_stderr" do
after :each do
$stderr = STDERR
end
it "returns $stderr" do
$stderr = @stream
@f.rb_stderr.should equal($stderr)
end
end
describe "rb_defout" do
after :each do
$stdout = STDOUT
end
it "is an alias of rb_stdout" do
$stdout = @stream
@f.rb_defout.should equal($stdout)
end
end
end
describe "rb_default_rs" do
it "returns \\n" do
@f.rb_default_rs.should == "\n"
end
end
describe "rb_output_rs" do
before :each do
@dollar_backslash = $\
end
after :each do
suppress_warning {$\ = @dollar_backslash}
end
it "returns nil by default" do
@f.rb_output_rs.should be_nil
end
it "returns the value of $\\" do
suppress_warning {$\ = "foo"}
@f.rb_output_rs.should == "foo"
end
end
describe "rb_output_fs" do
before :each do
@dollar_comma = $,
end
after :each do
suppress_warning {$, = @dollar_comma}
end
it "returns nil by default" do
@f.rb_output_fs.should be_nil
end
it "returns the value of $\\" do
suppress_warning {$, = "foo"}
@f.rb_output_fs.should == "foo"
end
end
describe "rb_lastline_set" do
it "sets the value of $_" do
@f.rb_lastline_set("last line")
$_.should == "last line"
end
it "sets a Thread-local value" do
$_ = nil
running = false
thr = Thread.new do
@f.rb_lastline_set("last line")
$_.should == "last line"
running = true
end
Thread.pass while thr.status and !running
$_.should be_nil
thr.join
end
end
describe "rb_lastline_get" do
before do
@io = StringIO.new("last line")
end
it "gets the value of $_" do
@io.gets
@f.rb_lastline_get.should == "last line"
end
it "gets a Thread-local value" do
$_ = nil
running = false
thr = Thread.new do
@io.gets
@f.rb_lastline_get.should == "last line"
running = true
end
Thread.pass while thr.status and !running
$_.should be_nil
thr.join
end
end
end