-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathpredefined_spec.rb
1381 lines (1159 loc) · 39.4 KB
/
predefined_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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require_relative '../spec_helper'
require 'stringio'
# The following tables are excerpted from Programming Ruby: The Pragmatic Programmer's Guide'
# Second Edition by Dave Thomas, Chad Fowler, and Andy Hunt, page 319-22.
#
# Entries marked [r/o] are read-only and an error will be raised of the program attempts to
# modify them. Entries marked [thread] are thread local.
# Exception Information
# ---------------------------------------------------------------------------------------------------
#
# $! Exception The exception object passed to raise. [thread]
# $@ Array The stack backtrace generated by the last exception. [thread]
# Pattern Matching Variables
# ---------------------------------------------------------------------------------------------------
#
# These variables are set to nil after an unsuccessful pattern match.
#
# $& String The string matched (following a successful pattern match). This variable is
# local to the current scope. [r/o, thread]
# $+ String The contents of the highest-numbered group matched following a successful
# pattern match. Thus, in "cat" =~/(c|a)(t|z)/, $+ will be set to “t”. This
# variable is local to the current scope. [r/o, thread]
# $` String The string preceding the match in a successful pattern match. This variable
# is local to the current scope. [r/o, thread]
# $' String The string following the match in a successful pattern match. This variable
# is local to the current scope. [r/o, thread]
# $1 to $<N> String The contents of successive groups matched in a successful pattern match. In
# "cat" =~/(c|a)(t|z)/, $1 will be set to “a” and $2 to “t”. This variable
# is local to the current scope. [r/o, thread]
# $~ MatchData An object that encapsulates the results of a successful pattern match. The
# variables $&, $`, $', and $1 to $<N> are all derived from $~. Assigning to $~
# changes the values of these derived variables. This variable is local to the
# current scope. [thread]
describe "Predefined global $~" do
it "is set to contain the MatchData object of the last match if successful" do
md = /foo/.match 'foo'
$~.should be_kind_of(MatchData)
$~.should equal md
/bar/ =~ 'bar'
$~.should be_kind_of(MatchData)
$~.should_not equal md
end
it "is set to nil if the last match was unsuccessful" do
/foo/ =~ 'foo'
$~.should_not.nil?
/foo/ =~ 'bar'
$~.should.nil?
end
it "is set at the method-scoped level rather than block-scoped" do
obj = Object.new
def obj.foo; yield; end
def obj.foo2(&proc); proc.call; end
match2 = nil
match3 = nil
match4 = nil
match1 = /foo/.match "foo"
obj.foo { match2 = /bar/.match("bar") }
match2.should_not == nil
$~.should == match2
eval 'match3 = /baz/.match("baz")'
match3.should_not == nil
$~.should == match3
obj.foo2 { match4 = /qux/.match("qux") }
match4.should_not == nil
$~.should == match4
end
it "raises an error if assigned an object not nil or instanceof MatchData" do
$~ = nil
$~.should == nil
$~ = /foo/.match("foo")
$~.should be_an_instance_of(MatchData)
-> { $~ = Object.new }.should raise_error(TypeError)
-> { $~ = 1 }.should raise_error(TypeError)
end
it "changes the value of derived capture globals when assigned" do
"foo" =~ /(f)oo/
foo_match = $~
"bar" =~ /(b)ar/
$~ = foo_match
$1.should == "f"
end
it "changes the value of the derived preceding match global" do
"foo hello" =~ /hello/
foo_match = $~
"bar" =~ /(bar)/
$~ = foo_match
$`.should == "foo "
end
it "changes the value of the derived following match global" do
"foo hello" =~ /foo/
foo_match = $~
"bar" =~ /(bar)/
$~ = foo_match
$'.should == " hello"
end
it "changes the value of the derived full match global" do
"foo hello" =~ /foo/
foo_match = $~
"bar" =~ /(bar)/
$~ = foo_match
$&.should == "foo"
end
end
describe "Predefined global $&" do
it "is equivalent to MatchData#[0] on the last match $~" do
/foo/ =~ 'barfoobaz'
$&.should == $~[0]
$&.should == 'foo'
end
it "sets the encoding to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
$&.encoding.should equal(Encoding::EUC_JP)
end
end
describe "Predefined global $`" do
it "is equivalent to MatchData#pre_match on the last match $~" do
/foo/ =~ 'barfoobaz'
$`.should == $~.pre_match
$`.should == 'bar'
end
it "sets the encoding to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
$`.encoding.should equal(Encoding::EUC_JP)
end
it "sets an empty result to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::ISO_8859_1) =~ /a/
$`.encoding.should equal(Encoding::ISO_8859_1)
end
end
describe "Predefined global $'" do
it "is equivalent to MatchData#post_match on the last match $~" do
/foo/ =~ 'barfoobaz'
$'.should == $~.post_match
$'.should == 'baz'
end
it "sets the encoding to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
$'.encoding.should equal(Encoding::EUC_JP)
end
it "sets an empty result to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::ISO_8859_1) =~ /c/
$'.encoding.should equal(Encoding::ISO_8859_1)
end
end
describe "Predefined global $+" do
it "is equivalent to $~.captures.last" do
/(f(o)o)/ =~ 'barfoobaz'
$+.should == $~.captures.last
$+.should == 'o'
end
it "captures the last non nil capture" do
/(a)|(b)/ =~ 'a'
$+.should == 'a'
end
it "sets the encoding to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /(b)/
$+.encoding.should equal(Encoding::EUC_JP)
end
end
describe "Predefined globals $1..N" do
it "are equivalent to $~[N]" do
/(f)(o)(o)/ =~ 'foo'
$1.should == $~[1]
$2.should == $~[2]
$3.should == $~[3]
$4.should == $~[4]
[$1, $2, $3, $4].should == ['f', 'o', 'o', nil]
end
it "are nil unless a match group occurs" do
def test(arg)
case arg
when /-(.)?/
$1
end
end
test("-").should == nil
end
it "sets the encoding to the encoding of the source String" do
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /(b)/
$1.encoding.should equal(Encoding::EUC_JP)
end
end
describe "Predefined global $stdout" do
before :each do
@old_stdout = $stdout
end
after :each do
$stdout = @old_stdout
end
it "raises TypeError error if assigned to nil" do
-> { $stdout = nil }.should raise_error(TypeError)
end
it "raises TypeError error if assigned to object that doesn't respond to #write" do
obj = mock('object')
-> { $stdout = obj }.should raise_error(TypeError)
obj.stub!(:write)
$stdout = obj
$stdout.should equal(obj)
end
end
describe "Predefined global $!" do
it "is Fiber-local" do
Fiber.new do
raise "hi"
rescue
Fiber.yield
end.resume
$!.should == nil
end
# See http://jira.codehaus.org/browse/JRUBY-5550
it "remains nil after a failed core class \"checked\" coercion against a class that defines method_missing" do
$!.should == nil
obj = Class.new do
def method_missing(*args)
super
end
end.new
[obj, 'foo'].join
$!.should == nil
end
it "should be set to the value of $! before the begin after a successful rescue" do
outer = StandardError.new 'outer'
inner = StandardError.new 'inner'
begin
raise outer
rescue
$!.should == outer
# nested rescue
begin
$!.should == outer
raise inner
rescue
$!.should == inner
ensure
$!.should == outer
end
$!.should == outer
end
$!.should == nil
end
it "should be set to the value of $! before the begin after a rescue which returns" do
def foo
outer = StandardError.new 'outer'
inner = StandardError.new 'inner'
begin
raise outer
rescue
$!.should == outer
# nested rescue
begin
$!.should == outer
raise inner
rescue
$!.should == inner
return
ensure
$!.should == outer
end
$!.should == outer
end
$!.should == nil
end
foo
end
it "should be set to the value of $! before the begin after a successful rescue within an ensure" do
outer = StandardError.new 'outer'
inner = StandardError.new 'inner'
begin
begin
raise outer
ensure
$!.should == outer
# nested rescue
begin
$!.should == outer
raise inner
rescue
$!.should == inner
ensure
$!.should == outer
end
$!.should == outer
end
flunk "outer should be raised after the ensure"
rescue
$!.should == outer
end
$!.should == nil
end
it "should be set to the new exception after a throwing rescue" do
outer = StandardError.new 'outer'
inner = StandardError.new 'inner'
begin
raise outer
rescue
$!.should == outer
begin
# nested rescue
begin
$!.should == outer
raise inner
rescue # the throwing rescue
$!.should == inner
raise inner
ensure
$!.should == inner
end
rescue # do not make the exception fail the example
$!.should == inner
end
$!.should == outer
end
$!.should == nil
end
describe "in bodies without ensure" do
it "should be cleared when an exception is rescued" do
e = StandardError.new 'foo'
begin
raise e
rescue
$!.should == e
end
$!.should == nil
end
it "should be cleared when an exception is rescued even when a non-local return is present" do
def foo(e)
$!.should == e
yield
end
def bar
e = StandardError.new 'foo'
begin
raise e
rescue
$!.should == e
foo(e) { return }
end
end
bar
$!.should == nil
end
it "should be cleared when an exception is rescued even when a non-local return from block" do
def foo
[ 1 ].each do
begin
raise StandardError.new('err')
rescue => e
$!.should == e
return
end
end
end
foo
$!.should == nil
end
it "should not be cleared when an exception is not rescued" do
e = StandardError.new
begin
begin
begin
raise e
rescue TypeError
flunk
end
ensure
$!.should == e
end
rescue
$!.should == e
end
$!.should == nil
end
it "should not be cleared when an exception is rescued and rethrown" do
e = StandardError.new 'foo'
begin
begin
begin
raise e
rescue => e
$!.should == e
raise e
end
ensure
$!.should == e
end
rescue
$!.should == e
end
$!.should == nil
end
end
describe "in ensure-protected bodies" do
it "should be cleared when an exception is rescued" do
e = StandardError.new 'foo'
begin
raise e
rescue
$!.should == e
ensure
$!.should == nil
end
$!.should == nil
end
it "should not be cleared when an exception is not rescued" do
e = StandardError.new
begin
begin
begin
raise e
rescue TypeError
flunk
ensure
$!.should == e
end
ensure
$!.should == e
end
rescue
$!.should == e
end
end
it "should not be cleared when an exception is rescued and rethrown" do
e = StandardError.new
begin
begin
begin
raise e
rescue => e
$!.should == e
raise e
ensure
$!.should == e
end
ensure
$!.should == e
end
rescue
$!.should == e
end
end
end
end
# Input/Output Variables
# ---------------------------------------------------------------------------------------------------
#
# $/ String The input record separator (newline by default). This is the value that rou-
# tines such as Kernel#gets use to determine record boundaries. If set to
# nil, gets will read the entire file.
# $-0 String Synonym for $/.
# $\ String The string appended to the output of every call to methods such as
# Kernel#print and IO#write. The default value is nil.
# $, String The separator string output between the parameters to methods such as
# Kernel#print and Array#join. Defaults to nil, which adds no text.
# $. Integer The number of the last line read from the current input file.
# $; String The default separator pattern used by String#split. May be set from the
# command line using the -F flag.
# $< Object An object that provides access to the concatenation of the contents of all
# the files given as command-line arguments or $stdin (in the case where
# there are no arguments). $< supports methods similar to a File object:
# binmode, close, closed?, each, each_byte, each_line, eof, eof?,
# file, filename, fileno, getc, gets, lineno, lineno=, path, pos, pos=,
# read, readchar, readline, readlines, rewind, seek, skip, tell, to_a,
# to_i, to_io, to_s, along with the methods in Enumerable. The method
# file returns a File object for the file currently being read. This may change
# as $< reads through the files on the command line. [r/o]
# $> IO The destination of output for Kernel#print and Kernel#printf. The
# default value is $stdout.
# $_ String The last line read by Kernel#gets or Kernel#readline. Many string-
# related functions in the Kernel module operate on $_ by default. The vari-
# able is local to the current scope. [thread]
# $-F String Synonym for $;.
# $stderr IO The current standard error output.
# $stdin IO The current standard input.
# $stdout IO The current standard output. Assignment to $stdout is deprecated: use
# $stdout.reopen instead.
describe "Predefined global $/" do
before :each do
@verbose, $VERBOSE = $VERBOSE, nil
@dollar_slash = $/
@dollar_dash_zero = $-0
end
after :each do
$/ = @dollar_slash
$-0 = @dollar_dash_zero
$VERBOSE = @verbose
end
it "can be assigned a String" do
str = "abc"
$/ = str
$/.should equal(str)
end
it "can be assigned nil" do
$/ = nil
$/.should be_nil
end
it "returns the value assigned" do
($/ = "xyz").should == "xyz"
end
it "changes $-0" do
$/ = "xyz"
$-0.should equal($/)
end
it "does not call #to_str to convert the object to a String" do
obj = mock("$/ value")
obj.should_not_receive(:to_str)
-> { $/ = obj }.should raise_error(TypeError)
end
it "raises a TypeError if assigned an Integer" do
-> { $/ = 1 }.should raise_error(TypeError)
end
it "raises a TypeError if assigned a boolean" do
-> { $/ = true }.should raise_error(TypeError)
end
end
describe "Predefined global $-0" do
before :each do
@verbose, $VERBOSE = $VERBOSE, nil
@dollar_slash = $/
@dollar_dash_zero = $-0
end
after :each do
$/ = @dollar_slash
$-0 = @dollar_dash_zero
$VERBOSE = @verbose
end
it "can be assigned a String" do
str = "abc"
$-0 = str
$-0.should equal(str)
end
it "can be assigned nil" do
$-0 = nil
$-0.should be_nil
end
it "returns the value assigned" do
($-0 = "xyz").should == "xyz"
end
it "changes $/" do
$-0 = "xyz"
$/.should equal($-0)
end
it "does not call #to_str to convert the object to a String" do
obj = mock("$-0 value")
obj.should_not_receive(:to_str)
-> { $-0 = obj }.should raise_error(TypeError)
end
it "raises a TypeError if assigned an Integer" do
-> { $-0 = 1 }.should raise_error(TypeError)
end
it "raises a TypeError if assigned a boolean" do
-> { $-0 = true }.should raise_error(TypeError)
end
end
describe "Predefined global $\\" do
before :each do
@verbose, $VERBOSE = $VERBOSE, nil
@dollar_backslash = $\
end
after :each do
$\ = @dollar_backslash
$VERBOSE = @verbose
end
it "can be assigned a String" do
str = "abc"
$\ = str
$\.should equal(str)
end
it "can be assigned nil" do
$\ = nil
$\.should be_nil
end
it "returns the value assigned" do
($\ = "xyz").should == "xyz"
end
it "does not call #to_str to convert the object to a String" do
obj = mock("$\\ value")
obj.should_not_receive(:to_str)
-> { $\ = obj }.should raise_error(TypeError)
end
it "raises a TypeError if assigned not String" do
-> { $\ = 1 }.should raise_error(TypeError)
-> { $\ = true }.should raise_error(TypeError)
end
end
describe "Predefined global $," do
after :each do
$, = nil
end
it "defaults to nil" do
$,.should be_nil
end
it "raises TypeError if assigned a non-String" do
-> { $, = Object.new }.should raise_error(TypeError)
end
it "warns if assigned non-nil" do
-> { $, = "_" }.should complain(/warning: [`']\$,' is deprecated/)
end
end
describe "Predefined global $." do
it "can be assigned an Integer" do
$. = 123
$..should == 123
end
it "can be assigned a Float" do
$. = 123.5
$..should == 123
end
it "should call #to_int to convert the object to an Integer" do
obj = mock("good-value")
obj.should_receive(:to_int).and_return(321)
$. = obj
$..should == 321
end
it "raises TypeError if object can't be converted to an Integer" do
obj = mock("bad-value")
obj.should_receive(:to_int).and_return('abc')
-> { $. = obj }.should raise_error(TypeError)
end
end
describe "Predefined global $;" do
after :each do
$; = nil
end
it "warns if assigned non-nil" do
-> { $; = "_" }.should complain(/warning: [`']\$;' is deprecated/)
end
end
describe "Predefined global $_" do
it "is set to the last line read by e.g. StringIO#gets" do
stdin = StringIO.new("foo\nbar\n", "r")
read = stdin.gets
read.should == "foo\n"
$_.should == read
read = stdin.gets
read.should == "bar\n"
$_.should == read
read = stdin.gets
read.should == nil
$_.should == read
end
it "is set at the method-scoped level rather than block-scoped" do
obj = Object.new
def obj.foo; yield; end
def obj.foo2; yield; end
stdin = StringIO.new("foo\nbar\nbaz\nqux\n", "r")
match = stdin.gets
obj.foo { match = stdin.gets }
match.should == "bar\n"
$_.should == match
eval 'match = stdin.gets'
match.should == "baz\n"
$_.should == match
obj.foo2 { match = stdin.gets }
match.should == "qux\n"
$_.should == match
end
it "is Thread-local" do
$_ = nil
running = false
thr = Thread.new do
$_ = "last line"
running = true
end
Thread.pass until running
$_.should be_nil
thr.join
end
it "can be assigned any value" do
$_ = nil
$_.should == nil
$_ = "foo"
$_.should == "foo"
o = Object.new
$_ = o
$_.should == o
$_ = 1
$_.should == 1
end
end
# Execution Environment Variables
# ---------------------------------------------------------------------------------------------------
#
# $0 String The name of the top-level Ruby program being executed. Typically this will
# be the program’s filename. On some operating systems, assigning to this
# variable will change the name of the process reported (for example) by the
# ps(1) command.
# $* Array An array of strings containing the command-line options from the invoca-
# tion of the program. Options used by the Ruby interpreter will have been
# removed. [r/o]
# $" Array An array containing the filenames of modules loaded by require. [r/o]
# $$ Integer The process number of the program being executed. [r/o]
# $? Process::Status The exit status of the last child process to terminate. [r/o, thread]
# $: Array An array of strings, where each string specifies a directory to be searched for
# Ruby scripts and binary extensions used by the load and require methods.
# The initial value is the value of the arguments passed via the -I command-
# line option, followed by an installation-defined standard library location, fol-
# lowed by the current directory (“.”). This variable may be set from within a
# program to alter the default search path; typically, programs use $: << dir
# to append dir to the path. [r/o]
# $-a Object True if the -a option is specified on the command line. [r/o]
# $-d Object Synonym for $DEBUG.
# $DEBUG Object Set to true if the -d command-line option is specified.
# __FILE__ String The name of the current source file. [r/o]
# $F Array The array that receives the split input line if the -a command-line option is
# used.
# $FILENAME String The name of the current input file. Equivalent to $<.filename. [r/o]
# $-i String If in-place edit mode is enabled (perhaps using the -i command-line
# option), $-i holds the extension used when creating the backup file. If you
# set a value into $-i, enables in-place edit mode.
# $-I Array Synonym for $:. [r/o]
# $-K String Sets the multibyte coding system for strings and regular expressions. Equiv-
# alent to the -K command-line option.
# $-l Object Set to true if the -l option (which enables line-end processing) is present
# on the command line. [r/o]
# __LINE__ String The current line number in the source file. [r/o]
# $LOAD_PATH Array A synonym for $:. [r/o]
# $-p Object Set to true if the -p option (which puts an implicit while gets . . . end
# loop around your program) is present on the command line. [r/o]
# $VERBOSE Object Set to true if the -v, --version, -W, or -w option is specified on the com-
# mand line. Set to false if no option, or -W1 is given. Set to nil if -W0
# was specified. Setting this option to true causes the interpreter and some
# library routines to report additional information. Setting to nil suppresses
# all warnings (including the output of Kernel.warn).
# $-v Object Synonym for $VERBOSE.
# $-w Object Synonym for $VERBOSE.
describe "Execution variable $:" do
it "is initialized to an array of strings" do
$:.is_a?(Array).should == true
($:.length > 0).should == true
end
it "does not include the current directory" do
$:.should_not include(".")
end
it "is the same object as $LOAD_PATH and $-I" do
$:.__id__.should == $LOAD_PATH.__id__
$:.__id__.should == $-I.__id__
end
it "can be changed via <<" do
$: << "foo"
$:.should include("foo")
ensure
$:.delete("foo")
end
it "is read-only" do
-> {
$: = []
}.should raise_error(NameError)
-> {
$LOAD_PATH = []
}.should raise_error(NameError)
-> {
$-I = []
}.should raise_error(NameError)
end
it "default $LOAD_PATH entries until sitelibdir included have @gem_prelude_index set" do
skip "no sense in ruby itself" if MSpecScript.instance_variable_defined?(:@testing_ruby)
$:.should.include?(RbConfig::CONFIG['sitelibdir'])
idx = $:.index(RbConfig::CONFIG['sitelibdir'])
$:[idx..-1].all? { |p| p.instance_variable_defined?(:@gem_prelude_index) }.should be_true
$:[0...idx].all? { |p| !p.instance_variable_defined?(:@gem_prelude_index) }.should be_true
end
end
describe "Global variable $\"" do
it "is an alias for $LOADED_FEATURES" do
$".should equal $LOADED_FEATURES
end
it "is read-only" do
-> {
$" = []
}.should raise_error(NameError)
-> {
$LOADED_FEATURES = []
}.should raise_error(NameError)
end
end
describe "Global variable $<" do
it "is read-only" do
-> {
$< = nil
}.should raise_error(NameError)
end
end
describe "Global variable $FILENAME" do
it "is read-only" do
-> {
$FILENAME = "-"
}.should raise_error(NameError)
end
end
describe "Global variable $?" do
it "is read-only" do
-> {
$? = nil
}.should raise_error(NameError)
end
it "is thread-local" do
system(ruby_cmd('exit 0'))
Thread.new { $?.should be_nil }.join
end
end
describe "Global variable $-a" do
it "is read-only" do
-> { $-a = true }.should raise_error(NameError)
end
end
describe "Global variable $-l" do
it "is read-only" do
-> { $-l = true }.should raise_error(NameError)
end
end
describe "Global variable $-p" do
it "is read-only" do
-> { $-p = true }.should raise_error(NameError)
end
end
describe "Global variable $-d" do
before :each do
@debug = $DEBUG
end
after :each do
$DEBUG = @debug
end
it "is an alias of $DEBUG" do
$DEBUG = true
$-d.should be_true
$-d = false
$DEBUG.should be_false
end
end
describe "Global variable $VERBOSE" do
before :each do
@verbose = $VERBOSE
end
after :each do
$VERBOSE = @verbose
end
it "is false by default" do
$VERBOSE.should be_false
end
it "converts truthy values to true" do
[true, 1, 0, [], ""].each do |true_value|
$VERBOSE = true_value
$VERBOSE.should be_true
end