@@ -88,8 +88,8 @@ def obj.foo2(&proc); proc.call; end
88
88
$~ = /foo/ . match ( "foo" )
89
89
$~. should be_an_instance_of ( MatchData )
90
90
91
- -> { $~ = Object . new } . should raise_error ( TypeError )
92
- -> { $~ = 1 } . should raise_error ( TypeError )
91
+ -> { $~ = Object . new } . should raise_error ( TypeError , 'wrong argument type Object (expected MatchData)' )
92
+ -> { $~ = 1 } . should raise_error ( TypeError , 'wrong argument type Integer (expected MatchData)' )
93
93
end
94
94
95
95
it "changes the value of derived capture globals when assigned" do
@@ -136,6 +136,12 @@ def obj.foo2(&proc); proc.call; end
136
136
"abc" . dup . force_encoding ( Encoding ::EUC_JP ) =~ /b/
137
137
$&. encoding . should equal ( Encoding ::EUC_JP )
138
138
end
139
+
140
+ it "is read-only" do
141
+ -> {
142
+ eval %q{$& = ""}
143
+ } . should raise_error ( SyntaxError , /Can't set variable \$ &/ )
144
+ end
139
145
end
140
146
141
147
describe "Predefined global $`" do
@@ -154,6 +160,12 @@ def obj.foo2(&proc); proc.call; end
154
160
"abc" . dup . force_encoding ( Encoding ::ISO_8859_1 ) =~ /a/
155
161
$`. encoding . should equal ( Encoding ::ISO_8859_1 )
156
162
end
163
+
164
+ it "is read-only" do
165
+ -> {
166
+ eval %q{$` = ""}
167
+ } . should raise_error ( SyntaxError , /Can't set variable \$ `/ )
168
+ end
157
169
end
158
170
159
171
describe "Predefined global $'" do
@@ -172,6 +184,12 @@ def obj.foo2(&proc); proc.call; end
172
184
"abc" . dup . force_encoding ( Encoding ::ISO_8859_1 ) =~ /c/
173
185
$'. encoding . should equal ( Encoding ::ISO_8859_1 )
174
186
end
187
+
188
+ it "is read-only" do
189
+ -> {
190
+ eval %q{$' = ""}
191
+ } . should raise_error ( SyntaxError , /Can't set variable \$ '/ )
192
+ end
175
193
end
176
194
177
195
describe "Predefined global $+" do
@@ -190,6 +208,12 @@ def obj.foo2(&proc); proc.call; end
190
208
"abc" . dup . force_encoding ( Encoding ::EUC_JP ) =~ /(b)/
191
209
$+. encoding . should equal ( Encoding ::EUC_JP )
192
210
end
211
+
212
+ it "is read-only" do
213
+ -> {
214
+ eval %q{$+ = ""}
215
+ } . should raise_error ( SyntaxError , /Can't set variable \$ \+ / )
216
+ end
193
217
end
194
218
195
219
describe "Predefined globals $1..N" do
@@ -229,7 +253,7 @@ def test(arg)
229
253
end
230
254
231
255
it "raises TypeError error if assigned to nil" do
232
- -> { $stdout = nil } . should raise_error ( TypeError )
256
+ -> { $stdout = nil } . should raise_error ( TypeError , '$stdout must have write method, NilClass given' )
233
257
end
234
258
235
259
it "raises TypeError error if assigned to object that doesn't respond to #write" do
@@ -253,6 +277,12 @@ def test(arg)
253
277
$!. should == nil
254
278
end
255
279
280
+ it "is read-only" do
281
+ -> {
282
+ $! = [ ]
283
+ } . should raise_error ( NameError , '$! is a read-only variable' )
284
+ end
285
+
256
286
# See http://jira.codehaus.org/browse/JRUBY-5550
257
287
it "remains nil after a failed core class \" checked\" coercion against a class that defines method_missing" do
258
288
$!. should == nil
@@ -512,6 +542,64 @@ def foo
512
542
end
513
543
end
514
544
545
+ describe "Predefined global $@" do
546
+ it "is Fiber-local" do
547
+ Fiber . new do
548
+ raise "hi"
549
+ rescue
550
+ Fiber . yield
551
+ end . resume
552
+
553
+ $@. should == nil
554
+ end
555
+
556
+ it "is set to a backtrace of a rescued exception" do
557
+ begin
558
+ raise
559
+ rescue
560
+ $@. should be_an_instance_of ( Array )
561
+ $@. should == $!. backtrace
562
+ end
563
+ end
564
+
565
+ it "is cleared when an exception is rescued" do
566
+ begin
567
+ raise
568
+ rescue
569
+ end
570
+
571
+ $@. should == nil
572
+ end
573
+
574
+ it "is not set when there is no current exception" do
575
+ $@. should == nil
576
+ end
577
+
578
+ it "is set to a backtrace of a rescued exception" do
579
+ begin
580
+ raise
581
+ rescue
582
+ $@. should be_an_instance_of ( Array )
583
+ $@. should == $!. backtrace
584
+ end
585
+ end
586
+
587
+ it "is not read-only" do
588
+ begin
589
+ raise
590
+ rescue
591
+ $@ = [ ]
592
+ $@. should == [ ]
593
+ end
594
+ end
595
+
596
+ it "cannot be assigned when there is no a rescued exception" do
597
+ -> {
598
+ $@ = [ ]
599
+ } . should raise_error ( ArgumentError , '$! not set' )
600
+ end
601
+ end
602
+
515
603
# Input/Output Variables
516
604
# ---------------------------------------------------------------------------------------------------
517
605
#
@@ -583,15 +671,15 @@ def foo
583
671
obj = mock ( "$/ value" )
584
672
obj . should_not_receive ( :to_str )
585
673
586
- -> { $/ = obj } . should raise_error ( TypeError )
674
+ -> { $/ = obj } . should raise_error ( TypeError , 'value of $/ must be String' )
587
675
end
588
676
589
677
it "raises a TypeError if assigned an Integer" do
590
- -> { $/ = 1 } . should raise_error ( TypeError )
678
+ -> { $/ = 1 } . should raise_error ( TypeError , 'value of $/ must be String' )
591
679
end
592
680
593
681
it "raises a TypeError if assigned a boolean" do
594
- -> { $/ = true } . should raise_error ( TypeError )
682
+ -> { $/ = true } . should raise_error ( TypeError , 'value of $/ must be String' )
595
683
end
596
684
end
597
685
@@ -632,15 +720,15 @@ def foo
632
720
obj = mock ( "$-0 value" )
633
721
obj . should_not_receive ( :to_str )
634
722
635
- -> { $-0 = obj } . should raise_error ( TypeError )
723
+ -> { $-0 = obj } . should raise_error ( TypeError , 'value of $-0 must be String' )
636
724
end
637
725
638
726
it "raises a TypeError if assigned an Integer" do
639
- -> { $-0 = 1 } . should raise_error ( TypeError )
727
+ -> { $-0 = 1 } . should raise_error ( TypeError , 'value of $-0 must be String' )
640
728
end
641
729
642
730
it "raises a TypeError if assigned a boolean" do
643
- -> { $-0 = true } . should raise_error ( TypeError )
731
+ -> { $-0 = true } . should raise_error ( TypeError , 'value of $-0 must be String' )
644
732
end
645
733
end
646
734
@@ -674,12 +762,12 @@ def foo
674
762
obj = mock ( "$\\ value" )
675
763
obj . should_not_receive ( :to_str )
676
764
677
- -> { $\ = obj } . should raise_error ( TypeError )
765
+ -> { $\ = obj } . should raise_error ( TypeError , 'value of $\ must be String' )
678
766
end
679
767
680
768
it "raises a TypeError if assigned not String" do
681
- -> { $\ = 1 } . should raise_error ( TypeError )
682
- -> { $\ = true } . should raise_error ( TypeError )
769
+ -> { $\ = 1 } . should raise_error ( TypeError , 'value of $\ must be String' )
770
+ -> { $\ = true } . should raise_error ( TypeError , 'value of $\ must be String' )
683
771
end
684
772
end
685
773
@@ -693,7 +781,7 @@ def foo
693
781
end
694
782
695
783
it "raises TypeError if assigned a non-String" do
696
- -> { $, = Object . new } . should raise_error ( TypeError )
784
+ -> { $, = Object . new } . should raise_error ( TypeError , 'value of $, must be String' )
697
785
end
698
786
699
787
it "warns if assigned non-nil" do
@@ -878,15 +966,15 @@ def obj.foo2; yield; end
878
966
it "is read-only" do
879
967
-> {
880
968
$: = [ ]
881
- } . should raise_error ( NameError )
969
+ } . should raise_error ( NameError , '$: is a read-only variable' )
882
970
883
971
-> {
884
972
$LOAD_PATH = [ ]
885
- } . should raise_error ( NameError )
973
+ } . should raise_error ( NameError , '$LOAD_PATH is a read-only variable' )
886
974
887
975
-> {
888
976
$-I = [ ]
889
- } . should raise_error ( NameError )
977
+ } . should raise_error ( NameError , '$-I is a read-only variable' )
890
978
end
891
979
892
980
it "default $LOAD_PATH entries until sitelibdir included have @gem_prelude_index set" do
@@ -908,35 +996,35 @@ def obj.foo2; yield; end
908
996
it "is read-only" do
909
997
-> {
910
998
$" = [ ]
911
- } . should raise_error ( NameError )
999
+ } . should raise_error ( NameError , '$" is a read-only variable' )
912
1000
913
1001
-> {
914
1002
$LOADED_FEATURES = [ ]
915
- } . should raise_error ( NameError )
1003
+ } . should raise_error ( NameError , '$LOADED_FEATURES is a read-only variable' )
916
1004
end
917
1005
end
918
1006
919
1007
describe "Global variable $<" do
920
1008
it "is read-only" do
921
1009
-> {
922
1010
$< = nil
923
- } . should raise_error ( NameError )
1011
+ } . should raise_error ( NameError , '$< is a read-only variable' )
924
1012
end
925
1013
end
926
1014
927
1015
describe "Global variable $FILENAME" do
928
1016
it "is read-only" do
929
1017
-> {
930
1018
$FILENAME = "-"
931
- } . should raise_error ( NameError )
1019
+ } . should raise_error ( NameError , '$FILENAME is a read-only variable' )
932
1020
end
933
1021
end
934
1022
935
1023
describe "Global variable $?" do
936
1024
it "is read-only" do
937
1025
-> {
938
1026
$? = nil
939
- } . should raise_error ( NameError )
1027
+ } . should raise_error ( NameError , '$? is a read-only variable' )
940
1028
end
941
1029
942
1030
it "is thread-local" do
@@ -947,19 +1035,19 @@ def obj.foo2; yield; end
947
1035
948
1036
describe "Global variable $-a" do
949
1037
it "is read-only" do
950
- -> { $-a = true } . should raise_error ( NameError )
1038
+ -> { $-a = true } . should raise_error ( NameError , '$-a is a read-only variable' )
951
1039
end
952
1040
end
953
1041
954
1042
describe "Global variable $-l" do
955
1043
it "is read-only" do
956
- -> { $-l = true } . should raise_error ( NameError )
1044
+ -> { $-l = true } . should raise_error ( NameError , '$-l is a read-only variable' )
957
1045
end
958
1046
end
959
1047
960
1048
describe "Global variable $-p" do
961
1049
it "is read-only" do
962
- -> { $-p = true } . should raise_error ( NameError )
1050
+ -> { $-p = true } . should raise_error ( NameError , '$-p is a read-only variable' )
963
1051
end
964
1052
end
965
1053
0 commit comments