summaryrefslogtreecommitdiff
path: root/spec/ruby/language/defined_spec.rb
blob: ae2bf45bda783fb8cdd0e1d02860f3e279556ede (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
require_relative '../spec_helper'
require_relative 'fixtures/defined'

describe "The defined? keyword for literals" do
  it "returns 'self' for self" do
    ret = defined?(self)
    ret.should == "self"
    ret.frozen?.should == true
  end

  it "returns 'nil' for nil" do
    ret = defined?(nil)
    ret.should == "nil"
    ret.frozen?.should == true
  end

  it "returns 'true' for true" do
    ret = defined?(true)
    ret.should == "true"
    ret.frozen?.should == true
  end

  it "returns 'false' for false" do
    ret = defined?(false)
    ret.should == "false"
    ret.frozen?.should == true
  end

  describe "for a literal Array" do

    it "returns 'expression' if each element is defined" do
      ret = defined?([Object, Array])
      ret.should == "expression"
      ret.frozen?.should == true
    end

    it "returns nil if one element is not defined" do
      ret = defined?([NonExistentConstant, Array])
      ret.should == nil
    end

    it "returns nil if all elements are not defined" do
      ret = defined?([NonExistentConstant, AnotherNonExistentConstant])
      ret.should == nil
    end

  end
end

describe "The defined? keyword when called with a method name" do
  before :each do
    ScratchPad.clear
  end

  it "does not call the method" do
    defined?(DefinedSpecs.side_effects).should == "method"
    ScratchPad.recorded.should != :defined_specs_side_effects
  end

  it "does not execute the arguments" do
    defined?(DefinedSpecs.any_args(DefinedSpecs.side_effects)).should == "method"
    ScratchPad.recorded.should != :defined_specs_side_effects
  end

  describe "without a receiver" do
    it "returns 'method' if the method is defined" do
      ret = defined?(puts)
      ret.should == "method"
      ret.frozen?.should == true
    end

    it "returns nil if the method is not defined" do
      defined?(defined_specs_undefined_method).should be_nil
    end

    it "returns 'method' if the method is defined and private" do
      obj = DefinedSpecs::Basic.new
      obj.private_method_defined.should == "method"
    end

    it "returns 'method' if the predicate method is defined and private" do
      obj = DefinedSpecs::Basic.new
      obj.private_predicate_defined.should == "method"
    end
  end

  describe "having a module as receiver" do
    it "returns 'method' if the method is defined" do
      defined?(Kernel.puts).should == "method"
    end

    it "returns nil if the method is private" do
      defined?(Object.print).should be_nil
    end

    it "returns nil if the method is protected" do
      defined?(DefinedSpecs::Basic.new.protected_method).should be_nil
    end

    it "returns nil if the method is not defined" do
      defined?(Kernel.defined_specs_undefined_method).should be_nil
    end

    it "returns nil if the class is not defined" do
      defined?(DefinedSpecsUndefined.puts).should be_nil
    end

    it "returns nil if the subclass is not defined" do
      defined?(DefinedSpecs::Undefined.puts).should be_nil
    end
  end

  describe "having a local variable as receiver" do
    it "returns 'method' if the method is defined" do
      obj = DefinedSpecs::Basic.new
      defined?(obj.a_defined_method).should == "method"
    end

    it "returns nil if the method is not defined" do
      obj = DefinedSpecs::Basic.new
      defined?(obj.an_undefined_method).should be_nil
    end

    it "returns nil if the variable does not exist" do
      defined?(nonexistent_local_variable.some_method).should be_nil
    end

    it "calls #respond_to_missing?" do
      obj = mock("respond_to_missing object")
      obj.should_receive(:respond_to_missing?).and_return(true)
      defined?(obj.something_undefined).should == "method"
    end
  end

  describe "having an instance variable as receiver" do
    it "returns 'method' if the method is defined" do
      @defined_specs_obj = DefinedSpecs::Basic.new
      defined?(@defined_specs_obj.a_defined_method).should == "method"
    end

    it "returns nil if the method is not defined" do
      @defined_specs_obj = DefinedSpecs::Basic.new
      defined?(@defined_specs_obj.an_undefined_method).should be_nil
    end

    it "returns nil if the variable does not exist" do
      defined?(@nonexistent_instance_variable.some_method).should be_nil
    end
  end

  describe "having a global variable as receiver" do
    it "returns 'method' if the method is defined" do
      $defined_specs_obj = DefinedSpecs::Basic.new
      defined?($defined_specs_obj.a_defined_method).should == "method"
    end

    it "returns nil if the method is not defined" do
      $defined_specs_obj = DefinedSpecs::Basic.new
      defined?($defined_specs_obj.an_undefined_method).should be_nil
    end

    it "returns nil if the variable does not exist" do
      defined?($nonexistent_global_variable.some_method).should be_nil
    end
  end

  describe "having a method call as a receiver" do
    it "returns nil if evaluating the receiver raises an exception" do
      defined?(DefinedSpecs.exception_method / 2).should be_nil
      ScratchPad.recorded.should == :defined_specs_exception
    end

    it "returns nil if the method is not defined on the object the receiver returns" do
      defined?(DefinedSpecs.side_effects / 2).should be_nil
      ScratchPad.recorded.should == :defined_specs_side_effects
    end

    it "returns 'method' if the method is defined on the object the receiver returns" do
      defined?(DefinedSpecs.fixnum_method / 2).should == "method"
      ScratchPad.recorded.should == :defined_specs_fixnum_method
    end
  end
end

describe "The defined? keyword for an expression" do
  before :each do
    ScratchPad.clear
  end

  it "returns 'assignment' for assigning a local variable" do
    ret = defined?(x = 2)
    ret.should == "assignment"
    ret.frozen?.should == true
  end

  it "returns 'assignment' for assigning an instance variable" do
    defined?(@defined_specs_x = 2).should == "assignment"
  end

  it "returns 'assignment' for assigning a global variable" do
    defined?($defined_specs_x = 2).should == "assignment"
  end

  it "returns 'assignment' for assigning a class variable" do
    defined?(@@defined_specs_x = 2).should == "assignment"
  end

  it "returns 'assignment' for assigning multiple variables" do
    defined?((a, b = 1, 2)).should == "assignment"
  end

  it "returns 'assignment' for an expression with '%='" do
    defined?(x %= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '/='" do
    defined?(x /= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '-='" do
    defined?(x -= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '+='" do
    defined?(x += 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '*='" do
    defined?(x *= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '|='" do
    defined?(x |= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '&='" do
    defined?(x &= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '^='" do
    defined?(x ^= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '~='" do
    defined?(x = 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '<<='" do
    defined?(x <<= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '>>='" do
    defined?(x >>= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '||='" do
    defined?(x ||= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '&&='" do
    defined?(x &&= 2).should == "assignment"
  end

  it "returns 'assignment' for an expression with '**='" do
    defined?(x **= 2).should == "assignment"
  end

  it "returns nil for an expression with == and an undefined method" do
    defined?(defined_specs_undefined_method == 2).should be_nil
  end

  it "returns nil for an expression with != and an undefined method" do
    defined?(defined_specs_undefined_method != 2).should be_nil
  end

  it "returns nil for an expression with !~ and an undefined method" do
    defined?(defined_specs_undefined_method !~ 2).should be_nil
  end

  it "returns 'method' for an expression with '=='" do
    x = 42
    defined?(x == 2).should == "method"
  end

  it "returns 'method' for an expression with '!='" do
    x = 42
    defined?(x != 2).should == "method"
  end

  it "returns 'method' for an expression with '!~'" do
    x = 42
    defined?(x !~ 2).should == "method"
  end

  describe "with logical connectives" do
    it "returns nil for an expression with '!' and an undefined method" do
      defined?(!defined_specs_undefined_method).should be_nil
    end

    it "returns nil for an expression with '!' and an unset class variable" do
      @result = eval("class singleton_class::A; defined?(!@@doesnt_exist) end", binding, __FILE__, __LINE__)
      @result.should be_nil
    end

    it "returns nil for an expression with 'not' and an undefined method" do
      defined?(not defined_specs_undefined_method).should be_nil
    end

    it "returns nil for an expression with 'not' and an unset class variable" do
      @result = eval("class singleton_class::A; defined?(not @@doesnt_exist) end", binding, __FILE__, __LINE__)
      @result.should be_nil
    end

    it "does not propagate an exception raised by a method in a 'not' expression" do
      defined?(not DefinedSpecs.exception_method).should be_nil
      ScratchPad.recorded.should == :defined_specs_exception
    end

    it "returns 'expression' for an expression with '&&/and' and an unset global variable" do
      defined?($defined_specs_undefined_global_variable && true).should == "expression"
      defined?(true && $defined_specs_undefined_global_variable).should == "expression"
      defined?($defined_specs_undefined_global_variable and true).should == "expression"
    end

    it "returns 'expression' for an expression with '&&/and' and an unset instance variable" do
      defined?(@defined_specs_undefined_instance_variable && true).should == "expression"
      defined?(true && @defined_specs_undefined_instance_variable).should == "expression"
      defined?(@defined_specs_undefined_instance_variable and true).should == "expression"
    end

    it "returns 'expression' for an expression '&&/and' regardless of its truth value" do
      defined?(true && false).should == "expression"
      defined?(true and false).should == "expression"
    end

    it "returns 'expression' for an expression with '||/or' and an unset global variable" do
      defined?($defined_specs_undefined_global_variable || true).should == "expression"
      defined?(true || $defined_specs_undefined_global_variable).should == "expression"
      defined?($defined_specs_undefined_global_variable or true).should == "expression"
    end

    it "returns 'expression' for an expression with '||/or' and an unset instance variable" do
      defined?(@defined_specs_undefined_instance_variable || true).should == "expression"
      defined?(true || @defined_specs_undefined_instance_variable).should == "expression"
      defined?(@defined_specs_undefined_instance_variable or true).should == "expression"
    end

    it "returns 'expression' for an expression '||/or' regardless of its truth value" do
      defined?(true || false).should == "expression"
      defined?(true or false).should == "expression"
    end

    it "returns nil for an expression with '!' and an unset global variable" do
      defined?(!$defined_specs_undefined_global_variable).should be_nil
    end

    it "returns nil for an expression with '!' and an unset instance variable" do
      defined?(!@defined_specs_undefined_instance_variable).should be_nil
    end

    it "returns 'method' for a 'not' expression with a method" do
      defined?(not DefinedSpecs.side_effects).should == "method"
    end

    it "calls a method in a 'not' expression and returns 'method'" do
      defined?(not DefinedSpecs.side_effects).should == "method"
      ScratchPad.recorded.should == :defined_specs_side_effects
    end

    it "returns nil for an expression with 'not' and an unset global variable" do
      defined?(not $defined_specs_undefined_global_variable).should be_nil
    end

    it "returns nil for an expression with 'not' and an unset instance variable" do
      defined?(not @defined_specs_undefined_instance_variable).should be_nil
    end

    it "returns 'expression' for an expression with '&&/and' and an undefined method" do
      defined?(defined_specs_undefined_method && true).should == "expression"
      defined?(defined_specs_undefined_method and true).should == "expression"
    end

    it "returns 'expression' for an expression with '&&/and' and an unset class variable" do
      defined?(@@defined_specs_undefined_class_variable && true).should == "expression"
      defined?(@@defined_specs_undefined_class_variable and true).should == "expression"
    end

    it "does not call a method in an '&&' expression and returns 'expression'" do
      defined?(DefinedSpecs.side_effects && true).should == "expression"
      ScratchPad.recorded.should be_nil
    end

    it "does not call a method in an 'and' expression and returns 'expression'" do
      defined?(DefinedSpecs.side_effects and true).should == "expression"
      ScratchPad.recorded.should be_nil
    end

    it "returns 'expression' for an expression with '||/or' and an undefined method" do
      defined?(defined_specs_undefined_method || true).should == "expression"
      defined?(defined_specs_undefined_method or true).should == "expression"
    end

    it "returns 'expression' for an expression with '||/or' and an unset class variable" do
      defined?(@@defined_specs_undefined_class_variable || true).should == "expression"
      defined?(@@defined_specs_undefined_class_variable or true).should == "expression"
    end

    it "does not call a method in an '||' expression and returns 'expression'" do
      defined?(DefinedSpecs.side_effects || true).should == "expression"
      ScratchPad.recorded.should be_nil
    end

    it "does not call a method in an 'or' expression and returns 'expression'" do
      defined?(DefinedSpecs.side_effects or true).should == "expression"
      ScratchPad.recorded.should be_nil
    end
  end

  it "returns 'expression' when passed a String" do
    defined?("garble gooble gable").should == "expression"
  end

  describe "with a dynamic String" do
    it "returns 'expression' when the String contains a literal" do
      defined?("garble #{42}").should == "expression"
    end

    it "returns 'expression' when the String contains a call to a defined method" do
      defined?("garble #{DefinedSpecs.side_effects}").should == "expression"
    end

    it "returns 'expression' when the String contains a call to an undefined method" do
      defined?("garble #{DefinedSpecs.undefined_method}").should == "expression"
    end

    it "does not call the method in the String" do
      defined?("garble #{DefinedSpecs.dynamic_string}").should == "expression"
      ScratchPad.recorded.should be_nil
    end
  end

  describe "with a dynamic Regexp" do
    it "returns 'expression' when the Regexp contains a literal" do
      defined?(/garble #{42}/).should == "expression"
    end

    it "returns 'expression' when the Regexp contains a call to a defined method" do
      defined?(/garble #{DefinedSpecs.side_effects}/).should == "expression"
    end

    it "returns 'expression' when the Regexp contains a call to an undefined method" do
      defined?(/garble #{DefinedSpecs.undefined_method}/).should == "expression"
    end

    it "does not call the method in the Regexp" do
      defined?(/garble #{DefinedSpecs.dynamic_string}/).should == "expression"
      ScratchPad.recorded.should be_nil
    end
  end

  it "returns 'expression' when passed a Fixnum literal" do
    defined?(42).should == "expression"
  end

  it "returns 'expression' when passed a Bignum literal" do
    defined?(0xdead_beef_deed_feed).should == "expression"
  end

  it "returns 'expression' when passed a Float literal" do
    defined?(1.5).should == "expression"
  end

  it "returns 'expression' when passed a Range literal" do
    defined?(0..2).should == "expression"
  end

  it "returns 'expression' when passed a Regexp literal" do
    defined?(/undefined/).should == "expression"
  end

  it "returns 'expression' when passed an Array literal" do
    defined?([1, 2]).should == "expression"
  end

  it "returns 'expression' when passed a Hash literal" do
    defined?({a: :b}).should == "expression"
  end

  it "returns 'expression' when passed a Symbol literal" do
    defined?(:defined_specs).should == "expression"
  end
end

describe "The defined? keyword for variables" do
  it "returns 'local-variable' when called with the name of a local variable" do
    ret = DefinedSpecs::Basic.new.local_variable_defined
    ret.should == "local-variable"
    ret.frozen?.should == true
  end

  it "returns 'local-variable' when called with the name of a local variable assigned to nil" do
    DefinedSpecs::Basic.new.local_variable_defined_nil.should == "local-variable"
  end

  it "returns nil for an instance variable that has not been read" do
    DefinedSpecs::Basic.new.instance_variable_undefined.should be_nil
  end

  it "returns nil for an instance variable that has been read but not assigned to" do
    DefinedSpecs::Basic.new.instance_variable_read.should be_nil
  end

  it "returns 'instance-variable' for an instance variable that has been assigned" do
    ret = DefinedSpecs::Basic.new.instance_variable_defined
    ret.should == "instance-variable"
    ret.frozen?.should == true
  end

  it "returns 'instance-variable' for an instance variable that has been assigned to nil" do
    DefinedSpecs::Basic.new.instance_variable_defined_nil.should == "instance-variable"
  end

  it "returns nil for a global variable that has not been read" do
    DefinedSpecs::Basic.new.global_variable_undefined.should be_nil
  end

  it "returns nil for a global variable that has been read but not assigned to" do
    DefinedSpecs::Basic.new.global_variable_read.should be_nil
  end

  it "returns 'global-variable' for a global variable that has been assigned nil" do
    ret = DefinedSpecs::Basic.new.global_variable_defined_as_nil
    ret.should == "global-variable"
    ret.frozen?.should == true
  end

  # MRI appears to special case defined? for $! and $~ in that it returns
  # 'global-variable' even when they are not set (or they are always "set"
  # but the value may be nil). In other words, 'defined?($~)' will return
  # 'global-variable' even if no match has been done.

  it "returns 'global-variable' for $!" do
    defined?($!).should == "global-variable"
  end

  it "returns 'global-variable for $~" do
    defined?($~).should == "global-variable"
  end

  describe "when a String does not match a Regexp" do
    before :each do
      "mis-matched" =~ /z(z)z/
    end

    it "returns 'global-variable' for $~" do
      defined?($~).should == "global-variable"
    end

    it "returns nil for $&" do
      defined?($&).should be_nil
    end

    it "returns nil for $`" do
      defined?($`).should be_nil
    end

    it "returns nil for $'" do
      defined?($').should be_nil
    end

    it "returns nil for $+" do
      defined?($+).should be_nil
    end

    it "returns nil for any last match global" do
      defined?($1).should be_nil
      defined?($4).should be_nil
      defined?($7).should be_nil
      defined?($10).should be_nil
      defined?($200).should be_nil
    end
  end

  describe "when a String matches a Regexp" do
    before :each do
      "mis-matched" =~ /s(-)m(.)/
    end

    it "returns 'global-variable' for $~" do
      defined?($~).should == "global-variable"
    end

    it "returns 'global-variable' for $&" do
      defined?($&).should == "global-variable"
    end

    it "returns 'global-variable' for $`" do
      defined?($`).should == "global-variable"
    end

    it "returns 'global-variable' for $'" do
      defined?($').should == "global-variable"
    end

    it "returns 'global-variable' for $+" do
      defined?($+).should == "global-variable"
    end

    it "returns 'global-variable' for the capture references" do
      defined?($1).should == "global-variable"
      defined?($2).should == "global-variable"
    end

    it "returns nil for non-captures" do
      defined?($4).should be_nil
      defined?($7).should be_nil
      defined?($10).should be_nil
      defined?($200).should be_nil
    end
  end

  describe "when a Regexp does not match a String" do
    before :each do
      /z(z)z/ =~ "mis-matched"
    end

    it "returns 'global-variable' for $~" do
      defined?($~).should == "global-variable"
    end

    it "returns nil for $&" do
      defined?($&).should be_nil
    end

    it "returns nil for $`" do
      defined?($`).should be_nil
    end

    it "returns nil for $'" do
      defined?($').should be_nil
    end

    it "returns nil for $+" do
      defined?($+).should be_nil
    end

    it "returns nil for any last match global" do
      defined?($1).should be_nil
      defined?($4).should be_nil
      defined?($7).should be_nil
      defined?($10).should be_nil
      defined?($200).should be_nil
    end
  end

  describe "when a Regexp matches a String" do
    before :each do
      /s(-)m(.)/ =~ "mis-matched"
    end

    it "returns 'global-variable' for $~" do
      defined?($~).should == "global-variable"
    end

    it "returns 'global-variable' for $&" do
      defined?($&).should == "global-variable"
    end

    it "returns 'global-variable' for $`" do
      defined?($`).should == "global-variable"
    end

    it "returns 'global-variable' for $'" do
      defined?($').should == "global-variable"
    end

    it "returns 'global-variable' for $+" do
      defined?($+).should == "global-variable"
    end

    it "returns 'global-variable' for the capture references" do
      defined?($1).should == "global-variable"
      defined?($2).should == "global-variable"
    end

    it "returns nil for non-captures" do
      defined?($4).should be_nil
      defined?($7).should be_nil
      defined?($10).should be_nil
      defined?($200).should be_nil
    end
  end
  it "returns 'global-variable' for a global variable that has been assigned" do
    DefinedSpecs::Basic.new.global_variable_defined.should == "global-variable"
  end

  it "returns nil for a class variable that has not been read" do
    DefinedSpecs::Basic.new.class_variable_undefined.should be_nil
  end

  # There is no spec for a class variable that is read before being assigned
  # to because setting up the code for this raises a NameError before you
  # get to the defined? call so it really has nothing to do with 'defined?'.

  it "returns 'class variable' when called with the name of a class variable" do
    ret = DefinedSpecs::Basic.new.class_variable_defined
    ret.should == "class variable"
    ret.frozen?.should == true
  end

  it "returns 'local-variable' when called with the name of a block local" do
    block = Proc.new { |xxx| defined?(xxx) }
    block.call(1).should == "local-variable"
  end
end

describe "The defined? keyword for a simple constant" do
  it "returns 'constant' when the constant is defined" do
    ret = defined?(DefinedSpecs)
    ret.should == "constant"
    ret.frozen?.should == true
  end

  it "returns nil when the constant is not defined" do
    defined?(DefinedSpecsUndefined).should be_nil
  end

  it "does not call Object.const_missing if the constant is not defined" do
    Object.should_not_receive(:const_missing)
    defined?(DefinedSpecsUndefined).should be_nil
  end

  it "returns 'constant' for an included module" do
    DefinedSpecs::Child.module_defined.should == "constant"
  end

  it "returns 'constant' for a constant defined in an included module" do
    DefinedSpecs::Child.module_constant_defined.should == "constant"
  end
end

describe "The defined? keyword for a top-level constant" do
  it "returns 'constant' when passed the name of a top-level constant" do
    defined?(::DefinedSpecs).should == "constant"
  end

  it "returns nil if the constant is not defined" do
    defined?(::DefinedSpecsUndefined).should be_nil
  end

  it "does not call Object.const_missing if the constant is not defined" do
    Object.should_not_receive(:const_missing)
    defined?(::DefinedSpecsUndefined).should be_nil
  end
end

describe "The defined? keyword for a scoped constant" do
  it "returns 'constant' when the scoped constant is defined" do
    defined?(DefinedSpecs::Basic).should == "constant"
  end

  it "returns nil when the scoped constant is not defined" do
    defined?(DefinedSpecs::Undefined).should be_nil
  end

  it "does not call .const_missing if the constant is not defined" do
    DefinedSpecs.should_not_receive(:const_missing)
    defined?(DefinedSpecs::UnknownChild).should be_nil
  end

  it "returns nil when an undefined constant is scoped to a defined constant" do
    defined?(DefinedSpecs::Child::Undefined).should be_nil
  end

  it "returns nil when a constant is scoped to an undefined constant" do
    Object.should_not_receive(:const_missing)
    defined?(Undefined::Object).should be_nil
  end

  it "returns nil when the undefined constant is scoped to an undefined constant" do
    defined?(DefinedSpecs::Undefined::Undefined).should be_nil
  end

  it "returns nil when a constant is defined on top-level but not on the module" do
    defined?(DefinedSpecs::String).should be_nil
  end

  it "returns nil when a constant is defined on top-level but not on the class" do
    defined?(DefinedSpecs::Basic::String).should be_nil
  end

  it "returns 'constant' if the scoped-scoped constant is defined" do
    defined?(DefinedSpecs::Child::A).should == "constant"
  end
end

describe "The defined? keyword for a top-level scoped constant" do
  it "returns 'constant' when the scoped constant is defined" do
    defined?(::DefinedSpecs::Basic).should == "constant"
  end

  it "returns nil when the scoped constant is not defined" do
    defined?(::DefinedSpecs::Undefined).should be_nil
  end

  it "returns nil when an undefined constant is scoped to a defined constant" do
    defined?(::DefinedSpecs::Child::Undefined).should be_nil
  end

  it "returns nil when the undefined constant is scoped to an undefined constant" do
    defined?(::DefinedSpecs::Undefined::Undefined).should be_nil
  end

  it "returns 'constant' if the scoped-scoped constant is defined" do
    defined?(::DefinedSpecs::Child::A).should == "constant"
  end
end

describe "The defined? keyword for a self-send method call scoped constant" do
  it "returns nil if the constant is not defined in the scope of the method's value" do
    defined?(defined_specs_method::Undefined).should be_nil
  end

  it "returns 'constant' if the constant is defined in the scope of the method's value" do
    defined?(defined_specs_method::Basic).should == "constant"
  end

  it "returns nil if the last constant is not defined in the scope chain" do
    defined?(defined_specs_method::Basic::Undefined).should be_nil
  end

  it "returns nil if the middle constant is not defined in the scope chain" do
    defined?(defined_specs_method::Undefined::Undefined).should be_nil
  end

  it "returns 'constant' if all the constants in the scope chain are defined" do
    defined?(defined_specs_method::Basic::A).should == "constant"
  end
end

describe "The defined? keyword for a receiver method call scoped constant" do
  it "returns nil if the constant is not defined in the scope of the method's value" do
    defined?(defined_specs_receiver.defined_method::Undefined).should be_nil
  end

  it "returns 'constant' if the constant is defined in the scope of the method's value" do
    defined?(defined_specs_receiver.defined_method::Basic).should == "constant"
  end

  it "returns nil if the last constant is not defined in the scope chain" do
    defined?(defined_specs_receiver.defined_method::Basic::Undefined).should be_nil
  end

  it "returns nil if the middle constant is not defined in the scope chain" do
    defined?(defined_specs_receiver.defined_method::Undefined::Undefined).should be_nil
  end

  it "returns 'constant' if all the constants in the scope chain are defined" do
    defined?(defined_specs_receiver.defined_method::Basic::A).should == "constant"
  end
end

describe "The defined? keyword for a module method call scoped constant" do
  it "returns nil if the constant is not defined in the scope of the method's value" do
    defined?(DefinedSpecs.defined_method::Undefined).should be_nil
  end

  it "returns 'constant' if the constant scoped by the method's value is defined" do
    defined?(DefinedSpecs.defined_method::Basic).should == "constant"
  end

  it "returns nil if the last constant in the scope chain is not defined" do
    defined?(DefinedSpecs.defined_method::Basic::Undefined).should be_nil
  end

  it "returns nil if the middle constant in the scope chain is not defined" do
    defined?(DefinedSpecs.defined_method::Undefined::Undefined).should be_nil
  end

  it "returns 'constant' if all the constants in the scope chain are defined" do
    defined?(DefinedSpecs.defined_method::Basic::A).should == "constant"
  end

  it "returns nil if the outer scope constant in the receiver is not defined" do
    defined?(Undefined::DefinedSpecs.defined_method::Basic).should be_nil
  end

  it "returns nil if the scoped constant in the receiver is not defined" do
    defined?(DefinedSpecs::Undefined.defined_method::Basic).should be_nil
  end

  it "returns 'constant' if all the constants in the receiver are defined" do
    defined?(Object::DefinedSpecs.defined_method::Basic).should == "constant"
  end

  it "returns 'constant' if all the constants in the receiver and scope chain are defined" do
    defined?(Object::DefinedSpecs.defined_method::Basic::A).should == "constant"
  end
end

describe "The defined? keyword for a variable scoped constant" do
  after :all do
    if Object.class_variable_defined? :@@defined_specs_obj
      Object.__send__(:remove_class_variable, :@@defined_specs_obj)
    end
  end

  it "returns nil if the instance scoped constant is not defined" do
    @defined_specs_obj = DefinedSpecs::Basic
    defined?(@defined_specs_obj::Undefined).should be_nil
  end

  it "returns 'constant' if the constant is defined in the scope of the instance variable" do
    @defined_specs_obj = DefinedSpecs::Basic
    defined?(@defined_specs_obj::A).should == "constant"
  end

  it "returns nil if the global scoped constant is not defined" do
    $defined_specs_obj = DefinedSpecs::Basic
    defined?($defined_specs_obj::Undefined).should be_nil
  end

  it "returns 'constant' if the constant is defined in the scope of the global variable" do
    $defined_specs_obj = DefinedSpecs::Basic
    defined?($defined_specs_obj::A).should == "constant"
  end

  it "returns nil if the class scoped constant is not defined" do
    eval(<<-END, binding, __FILE__, __LINE__)
      class singleton_class::A
        @@defined_specs_obj = DefinedSpecs::Basic
        defined?(@@defined_specs_obj::Undefined).should be_nil
      end
    END
  end

  it "returns 'constant' if the constant is defined in the scope of the class variable" do
    eval(<<-END, binding, __FILE__, __LINE__)
      class singleton_class::A
        @@defined_specs_obj = DefinedSpecs::Basic
        defined?(@@defined_specs_obj::A).should == "constant"
      end
    END
  end

  it "returns nil if the local scoped constant is not defined" do
    defined_specs_obj = DefinedSpecs::Basic
    defined?(defined_specs_obj::Undefined).should be_nil
  end

  it "returns 'constant' if the constant is defined in the scope of the local variable" do
    defined_specs_obj = DefinedSpecs::Basic
    defined?(defined_specs_obj::A).should == "constant"
  end
end

describe "The defined? keyword for a self:: scoped constant" do
  it "returns 'constant' for a constant explicitly scoped to self:: when set" do
    defined?(DefinedSpecs::SelfScoped).should == "constant"
  end

  it "returns 'constant' for a constant explicitly scoped to self:: in subclass's metaclass" do
    DefinedSpecs::Child.parent_constant_defined.should == "constant"
  end
end

describe "The defined? keyword for yield" do
  it "returns nil if no block is passed to a method not taking a block parameter" do
    DefinedSpecs::Basic.new.no_yield_block.should be_nil
  end

  it "returns nil if no block is passed to a method taking a block parameter" do
    DefinedSpecs::Basic.new.no_yield_block_parameter.should be_nil
  end

  it "returns 'yield' if a block is passed to a method not taking a block parameter" do
    ret = DefinedSpecs::Basic.new.yield_block
    ret.should == "yield"
    ret.frozen?.should == true
  end

  it "returns 'yield' if a block is passed to a method taking a block parameter" do
    DefinedSpecs::Basic.new.yield_block_parameter.should == "yield"
  end

  it "returns 'yield' when called within a block" do
    def yielder
      yield
    end

    def call_defined
      yielder { defined?(yield) }
    end

    call_defined() { }.should == "yield"
  end
end

describe "The defined? keyword for super" do
  it "returns nil when a superclass undef's the method" do
    DefinedSpecs::ClassWithoutMethod.new.test.should be_nil
  end

  describe "for a method taking no arguments" do
    it "returns nil when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_method_no_args.should be_nil
    end

    it "returns nil from a block when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_method_block_no_args.should be_nil
    end

    it "returns nil from a #define_method when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_define_method_no_args.should be_nil
    end

    it "returns nil from a block in a #define_method when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_define_method_block_no_args.should be_nil
    end

    it "returns 'super' when a superclass method exists" do
      ret = DefinedSpecs::Super.new.method_no_args
      ret.should == "super"
      ret.frozen?.should == true
    end

    it "returns 'super' from a block when a superclass method exists" do
      DefinedSpecs::Super.new.method_block_no_args.should == "super"
    end

    it "returns 'super' from a #define_method when a superclass method exists" do
      DefinedSpecs::Super.new.define_method_no_args.should == "super"
    end

    it "returns 'super' from a block in a #define_method when a superclass method exists" do
      DefinedSpecs::Super.new.define_method_block_no_args.should == "super"
    end

    it "returns 'super' when the method exists in a supermodule" do
      DefinedSpecs::SuperWithIntermediateModules.new.method_no_args.should == "super"
    end
  end

  describe "for a method taking arguments" do
    it "returns nil when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_method_args.should be_nil
    end

    it "returns nil from a block when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_method_block_args.should be_nil
    end

    it "returns nil from a #define_method when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_define_method_args.should be_nil
    end

    it "returns nil from a block in a #define_method when no superclass method exists" do
      DefinedSpecs::Super.new.no_super_define_method_block_args.should be_nil
    end

    it "returns 'super' when a superclass method exists" do
      DefinedSpecs::Super.new.method_args.should == "super"
    end

    it "returns 'super' from a block when a superclass method exists" do
      DefinedSpecs::Super.new.method_block_args.should == "super"
    end

    it "returns 'super' from a #define_method when a superclass method exists" do
      DefinedSpecs::Super.new.define_method_args.should == "super"
    end

    it "returns 'super' from a block in a #define_method when a superclass method exists" do
      DefinedSpecs::Super.new.define_method_block_args.should == "super"
    end
  end

  describe "within an included module's method" do
    it "returns 'super' when a superclass method exists in the including hierarchy" do
      DefinedSpecs::Child.new.defined_super.should == "super"
    end
  end
end


describe "The defined? keyword for instance variables" do
  it "returns 'instance-variable' if assigned" do
    @assigned_ivar = "some value"
    defined?(@assigned_ivar).should == "instance-variable"
  end

  it "returns nil if not assigned" do
    defined?(@unassigned_ivar).should be_nil
  end
end

describe "The defined? keyword for pseudo-variables" do
  it "returns 'expression' for __FILE__" do
    defined?(__FILE__).should == "expression"
  end

  it "returns 'expression' for __LINE__" do
    defined?(__LINE__).should == "expression"
  end

  it "returns 'expression' for __ENCODING__" do
    defined?(__ENCODING__).should == "expression"
  end
end

describe "The defined? keyword for conditional expressions" do
  it "returns 'expression' for an 'if' conditional" do
    defined?(if x then 'x' else '' end).should == "expression"
  end

  it "returns 'expression' for an 'unless' conditional" do
    defined?(unless x then '' else 'x' end).should == "expression"
  end

  it "returns 'expression' for ternary expressions" do
    defined?(x ? 'x' : '').should == "expression"
  end
end

describe "The defined? keyword for case expressions" do
  it "returns 'expression'" do
    defined?(case x; when 'x'; 'y' end).should == "expression"
  end
end

describe "The defined? keyword for loop expressions" do
  it "returns 'expression' for a 'for' expression" do
    defined?(for n in 1..3 do true end).should == "expression"
  end

  it "returns 'expression' for a 'while' expression" do
    defined?(while x do y end).should == "expression"
  end

  it "returns 'expression' for an 'until' expression" do
    defined?(until x do y end).should == "expression"
  end

  it "returns 'expression' for a 'break' expression" do
    defined?(break).should == "expression"
  end

  it "returns 'expression' for a 'next' expression" do
    defined?(next).should == "expression"
  end

  it "returns 'expression' for a 'redo' expression" do
    defined?(redo).should == "expression"
  end

  it "returns 'expression' for a 'retry' expression" do
    defined?(retry).should == "expression"
  end
end

describe "The defined? keyword for return expressions" do
  it "returns 'expression'" do
    defined?(return).should == "expression"
  end
end

describe "The defined? keyword for exception expressions" do
  it "returns 'expression'" do
    defined?(begin 1 end).should == "expression"
  end
end