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 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
|
require File.expand_path('../../spec_helper', __FILE__)
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.
=begin
Exception Information
---------------------------------------------------------------------------------------------------
$! Exception The exception object passed to raise. [thread]
$@ Array The stack backtrace generated by the last exception. [thread]
=end
=begin
Pattern Matching Variables
---------------------------------------------------------------------------------------------------
These variables (except $=) 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]
$= Object Deprecated.1.8 If set to any value apart from nil or false, all pattern matches
will be case insensitive, string comparisons will ignore case, and string hash
values will be case insensitive.
$1 to $9 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 $9 are all derived from $~. Assigning to $~
changes the values of these derived variables. This variable is local to the
current scope. [thread]
=end
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)
$~.object_id.should == md.object_id
/bar/ =~ 'bar'
$~.should be_kind_of(MatchData)
$~.object_id.should_not == md.object_id
end
it "is set to nil if the last match was unsuccessful" do
/foo/ =~ 'foo'
$~.nil?.should == false
/foo/ =~ 'bar'
$~.nil?.should == true
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
match = /foo/.match "foo"
obj.foo { match = /bar/.match("bar") }
$~.should == match
eval 'match = /baz/.match("baz")'
$~.should == match
obj.foo2 { match = /qux/.match("qux") }
$~.should == match
end
it "raises an error if assigned an object not nil or instanceof MatchData" do
lambda { $~ = nil }.should_not raise_error
lambda { $~ = /foo/.match("foo") }.should_not raise_error
lambda { $~ = Object.new }.should raise_error(TypeError)
lambda { $~ = 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
with_feature :encoding do
it "sets the encoding to the encoding of the source String" do
"abc".force_encoding(Encoding::EUC_JP) =~ /b/
$&.encoding.should equal(Encoding::EUC_JP)
end
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
with_feature :encoding do
it "sets the encoding to the encoding of the source String" do
"abc".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".force_encoding(Encoding::ISO_8859_1) =~ /a/
$`.encoding.should equal(Encoding::ISO_8859_1)
end
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
with_feature :encoding do
it "sets the encoding to the encoding of the source String" do
"abc".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".force_encoding(Encoding::ISO_8859_1) =~ /c/
$'.encoding.should equal(Encoding::ISO_8859_1)
end
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
with_feature :encoding do
it "sets the encoding to the encoding of the source String" do
"abc".force_encoding(Encoding::EUC_JP) =~ /(b)/
$+.encoding.should equal(Encoding::EUC_JP)
end
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
with_feature :encoding do
it "sets the encoding to the encoding of the source String" do
"abc".force_encoding(Encoding::EUC_JP) =~ /(b)/
$1.encoding.should equal(Encoding::EUC_JP)
end
end
end
describe "Predefined global $stdout" do
before(:each) do
@old_stdout = $stdout
end
after(:each) do
$stdout = @old_stdout
end
ruby_version_is "" ... "1.9" do
it "is the same as $defout" do
$stdout.should == $defout
$stdout = IOStub.new
$stdout.should == $defout
end
end
it "is the same as $DEFAULT_OUTPUT from 'English' library" do
require 'English'
$stdout.should == $DEFAULT_OUTPUT
$stdout = IOStub.new
$stdout.should == $DEFAULT_OUTPUT
end
it "raises TypeError error if assigned to nil" do
lambda { $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')
lambda { $stdout = obj }.should raise_error(TypeError)
obj.stub!(:write)
lambda { $stdout = obj }.should_not raise_error()
end
end
describe "Predefined global $!" do
it "needs to be reviewed for spec completeness"
ruby_version_is "1.9" do
# 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
end
end
=begin
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.
$. Fixnum 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.
=end
describe "Predefined global $/" do
before :each do
@dollar_slash = $/
@dollar_dash_zero = $-0
end
after :each do
$/ = @dollar_slash
$-0 = @dollar_dash_zero
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)
lambda { $/ = obj }.should raise_error(TypeError)
end
it "raises a TypeError if assigned a Fixnum" do
lambda { $/ = 1 }.should raise_error(TypeError)
end
it "raises a TypeError if assigned a boolean" do
lambda { $/ = true }.should raise_error(TypeError)
end
end
describe "Predefined global $-0" do
before :each do
@dollar_slash = $/
@dollar_dash_zero = $-0
end
after :each do
$/ = @dollar_slash
$-0 = @dollar_dash_zero
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)
lambda { $-0 = obj }.should raise_error(TypeError)
end
it "raises a TypeError if assigned a Fixnum" do
lambda { $-0 = 1 }.should raise_error(TypeError)
end
it "raises a TypeError if assigned a boolean" do
lambda { $-0 = true }.should raise_error(TypeError)
end
end
describe "Predefined global $," do
it "defaults to nil" do
$,.should be_nil
end
it "raises TypeError if assigned a non-String" do
lambda { $, = Object.new }.should raise_error(TypeError)
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
lambda { $_ = nil }.should_not raise_error
lambda { $_ = "foo" }.should_not raise_error
lambda { $_ = Object.new }.should_not raise_error
lambda { $_ = 1 }.should_not raise_error
end
end
=begin
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]
$$ Fixnum 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]
$SAFE Fixnum The current safe level. This variable’s value may never be
reduced by assignment. [thread] (Not implemented in Rubinius)
$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.
=end
describe "Execution variable $:" do
it "is initialized to an array of strings" do
$:.is_a?(Array).should == true
($:.length > 0).should == true
end
ruby_version_is ""..."1.9" do
it "includes the current directory" do
$:.should include(".")
end
end
ruby_version_is "1.9" do
it "does not include the current directory" do
$:.should_not include(".")
end
end
not_compliant_on :rubinius do
it "does not include '.' when the taint check level > 1" do
begin
orig_opts, ENV['RUBYOPT'] = ENV['RUBYOPT'], '-T'
`#{RUBY_EXE} -e 'p $:.include?(".")'`.should == "false\n"
ensure
ENV['RUBYOPT'] = orig_opts
end
end
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")
end
it "is read-only" do
lambda {
$: = []
}.should raise_error(NameError)
lambda {
$LOAD_PATH = []
}.should raise_error(NameError)
lambda {
$-I = []
}.should raise_error(NameError)
end
end
describe "Global variable $\"" do
it "is an alias for $LOADED_FEATURES" do
$".object_id.should == $LOADED_FEATURES.object_id
end
it "is read-only" do
lambda {
$" = []
}.should raise_error(NameError)
lambda {
$LOADED_FEATURES = []
}.should raise_error(NameError)
end
end
describe "Global variable $<" do
it "is read-only" do
lambda {
$< = nil
}.should raise_error(NameError)
end
end
describe "Global variable $FILENAME" do
it "is read-only" do
lambda {
$FILENAME = "-"
}.should raise_error(NameError)
end
end
describe "Global variable $?" do
it "is read-only" do
lambda {
$? = nil
}.should raise_error(NameError)
end
ruby_version_is ""..."1.9" do
it "is shared across threads" do
system("true")
pid = $?.pid
Thread.new { $?.pid.should == pid }.join
end
end
ruby_version_is "1.9" do
it "is thread-local" do
system("true")
Thread.new { $?.should be_nil }.join
end
end
end
describe "Global variable $-a" do
it "is read-only" do
lambda { $-a = true }.should raise_error(NameError)
end
end
describe "Global variable $-l" do
it "is read-only" do
lambda { $-l = true }.should raise_error(NameError)
end
end
describe "Global variable $-p" do
it "is read-only" do
lambda { $-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 :verbose_global_alias, :shared => true do
before :each do
@verbose = $VERBOSE
end
after :each do
$VERBOSE = @verbose
end
it "is an alias of $VERBOSE" do
$VERBOSE = true
eval(@method).should be_true
eval("#{@method} = false")
$VERBOSE.should be_false
end
end
describe "Global variable $-v" do
it_behaves_like :verbose_global_alias, '$-v'
end
describe "Global variable $-w" do
it_behaves_like :verbose_global_alias, '$-w'
end
describe "Global variable $0" do
before :each do
@orig_program_name = $0
end
after :each do
$0 = @orig_program_name
end
it "returns the program name" do
$0 = "rbx"
$0.should == "rbx"
end
it "returns the given value when set" do
($0 = "rbx").should == "rbx"
end
it "raises a TypeError when not given an object that can be coerced to a String" do
lambda { $0 = nil }.should raise_error(TypeError)
end
end
=begin
Standard Objects
---------------------------------------------------------------------------------------------------
ARGF Object A synonym for $<.
ARGV Array A synonym for $*.
ENV Object A hash-like object containing the program’s environment variables. An
instance of class Object, ENV implements the full set of Hash methods. Used
to query and set the value of an environment variable, as in ENV["PATH"]
and ENV["term"]="ansi".
false FalseClass Singleton instance of class FalseClass. [r/o]
nil NilClass The singleton instance of class NilClass. The value of uninitialized
instance and global variables. [r/o]
self Object The receiver (object) of the current method. [r/o]
true TrueClass Singleton instance of class TrueClass. [r/o]
=end
describe "The predefined standard objects" do
it "includes ARGF" do
Object.const_defined?(:ARGF).should == true
end
it "includes ARGV" do
Object.const_defined?(:ARGV).should == true
end
it "includes a hash-like object ENV" do
Object.const_defined?(:ENV).should == true
ENV.respond_to?(:[]).should == true
end
end
describe "The predefined standard object nil" do
it "is an instance of NilClass" do
nil.should be_kind_of(NilClass)
end
it "raises a SyntaxError if assigned to" do
lambda { eval("nil = true") }.should raise_error(SyntaxError)
end
end
describe "The predefined standard object true" do
it "is an instance of TrueClass" do
true.should be_kind_of(TrueClass)
end
it "raises a SyntaxError if assigned to" do
lambda { eval("true = false") }.should raise_error(SyntaxError)
end
end
describe "The predefined standard object false" do
it "is an instance of FalseClass" do
false.should be_kind_of(FalseClass)
end
it "raises a SyntaxError if assigned to" do
lambda { eval("false = nil") }.should raise_error(SyntaxError)
end
end
describe "The self pseudo-variable" do
it "raises a SyntaxError if assigned to" do
lambda { eval("self = 1") }.should raise_error(SyntaxError)
end
end
=begin
Global Constants
---------------------------------------------------------------------------------------------------
The following constants are defined by the Ruby interpreter.
DATA IO If the main program file contains the directive __END__, then
the constant DATA will be initialized so that reading from it will
return lines following __END__ from the source file.
FALSE FalseClass Synonym for false.
NIL NilClass Synonym for nil.
RUBY_PLATFORM String The identifier of the platform running this program. This string
is in the same form as the platform identifier used by the GNU
configure utility (which is not a coincidence).
PLATFORM String Same as RUBY_PLATFORM (only in 1.8).
RUBY_RELEASE_DATE String The date of this release.
RUBY_VERSION String The version number of the interpreter.
STDERR IO The actual standard error stream for the program. The initial
value of $stderr.
STDIN IO The actual standard input stream for the program. The initial
value of $stdin.
STDOUT IO The actual standard output stream for the program. The initial
value of $stdout.
SCRIPT_LINES__ Hash If a constant SCRIPT_LINES__ is defined and references a Hash,
Ruby will store an entry containing the contents of each file it
parses, with the file’s name as the key and an array of strings as
the value.
TOPLEVEL_BINDING Binding A Binding object representing the binding at Ruby’s top level—
the level where programs are initially executed.
TRUE TrueClass Synonym for true.
=end
describe "The predefined global constants" do
it "includes TRUE" do
Object.const_defined?(:TRUE).should == true
TRUE.should equal(true)
end
it "includes FALSE" do
Object.const_defined?(:FALSE).should == true
FALSE.should equal(false)
end
it "includes NIL" do
Object.const_defined?(:NIL).should == true
NIL.should equal(nil)
end
it "includes STDIN" do
Object.const_defined?(:STDIN).should == true
end
it "includes STDOUT" do
Object.const_defined?(:STDOUT).should == true
end
it "includes STDERR" do
Object.const_defined?(:STDERR).should == true
end
it "includes RUBY_VERSION" do
Object.const_defined?(:RUBY_VERSION).should == true
end
it "includes RUBY_RELEASE_DATE" do
Object.const_defined?(:RUBY_RELEASE_DATE).should == true
end
it "includes RUBY_PLATFORM" do
Object.const_defined?(:RUBY_PLATFORM).should == true
end
ruby_version_is "" ... "1.9" do
it "includes PLATFORM" do
Object.const_defined?(:PLATFORM).should == true
RUBY_PLATFORM == PLATFORM
end
end
it "includes TOPLEVEL_BINDING" do
Object.const_defined?(:TOPLEVEL_BINDING).should == true
end
end
with_feature :encoding do
describe "The predefined global constant" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
end
after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end
describe "STDIN" do
it "has the same external encoding as Encoding.default_external" do
STDIN.external_encoding.should equal(Encoding.default_external)
end
it "has the same external encoding as Encoding.default_external when that encoding is changed" do
Encoding.default_external = Encoding::ISO_8859_16
STDIN.external_encoding.should equal(Encoding::ISO_8859_16)
end
it "has the encodings set by #set_encoding"do
code = "STDIN.set_encoding Encoding::IBM775, Encoding::IBM866; " \
"p [STDIN.external_encoding.name, STDIN.internal_encoding.name]"
ruby_exe(code).chomp.should == %{["IBM775", "IBM866"]}
end
it "retains the encoding set by #set_encoding when Encoding.default_external is changed" do
code = "STDIN.set_encoding Encoding::IBM775, Encoding::IBM866; " \
"Encoding.default_external = Encoding::ISO_8859_16;" \
"p [STDIN.external_encoding.name, STDIN.internal_encoding.name]"
ruby_exe(code).chomp.should == %{["IBM775", "IBM866"]}
end
it "has nil for the internal encoding" do
STDIN.internal_encoding.should be_nil
end
it "has nil for the internal encoding despite Encoding.default_internal being changed" do
Encoding.default_internal = Encoding::IBM437
STDIN.internal_encoding.should be_nil
end
end
describe "STDOUT" do
it "has nil for the external encoding" do
STDOUT.external_encoding.should be_nil
end
it "has nil for the external encoding despite Encoding.default_external being changed" do
Encoding.default_external = Encoding::ISO_8859_1
STDOUT.external_encoding.should be_nil
end
it "has the encodings set by #set_encoding"do
code = "STDOUT.set_encoding Encoding::IBM775, Encoding::IBM866; " \
"p [STDOUT.external_encoding.name, STDOUT.internal_encoding.name]"
ruby_exe(code).chomp.should == %{["IBM775", "IBM866"]}
end
it "has nil for the internal encoding" do
STDOUT.internal_encoding.should be_nil
end
it "has nil for the internal encoding despite Encoding.default_internal being changed" do
Encoding.default_internal = Encoding::IBM437
STDOUT.internal_encoding.should be_nil
end
end
describe "STDERR" do
it "has nil for the external encoding" do
STDERR.external_encoding.should be_nil
end
it "has nil for the external encoding despite Encoding.default_external being changed" do
Encoding.default_external = Encoding::ISO_8859_1
STDERR.external_encoding.should be_nil
end
it "has the encodings set by #set_encoding"do
code = "STDERR.set_encoding Encoding::IBM775, Encoding::IBM866; " \
"p [STDERR.external_encoding.name, STDERR.internal_encoding.name]"
ruby_exe(code).chomp.should == %{["IBM775", "IBM866"]}
end
it "has nil for the internal encoding" do
STDERR.internal_encoding.should be_nil
end
it "has nil for the internal encoding despite Encoding.default_internal being changed" do
Encoding.default_internal = Encoding::IBM437
STDERR.internal_encoding.should be_nil
end
end
describe "ARGV" do
it "contains Strings encoded in locale Encoding" do
code = fixture __FILE__, "argv_encoding.rb"
result = ruby_exe(code, :args => "a b")
encoding = Encoding.default_external
result.chomp.should == %{["#{encoding}", "#{encoding}"]}
end
end
end
end
describe "Processing RUBYOPT" do
before (:each) do
@rubyopt, ENV["RUBYOPT"] = ENV["RUBYOPT"], nil
@ruby_flags, ENV["RUBY_FLAGS"] = ENV["RUBY_FLAGS"], nil
end
after (:each) do
ENV["RUBYOPT"] = @rubyopt
ENV["RUBY_FLAGS"] = @ruby_flags
end
it "adds the -I path to $LOAD_PATH" do
ENV["RUBYOPT"] = "-Ioptrubyspecincl"
result = ruby_exe("puts $LOAD_PATH.grep(/byspecin/)", :escape => true)
result.chomp[-15..-1].should == "optrubyspecincl"
end
it "sets $DEBUG to true for '-d'" do
ENV["RUBYOPT"] = '-d'
command = %[puts "value of $DEBUG is \#{$DEBUG}"]
result = ruby_exe(command, :escape => true, :args => "2>&1")
result.should =~ /value of \$DEBUG is true/
end
ruby_version_is "1.9" do
it "prints the version number for '-v'" do
ENV["RBXOPT"] = '-X19'
ENV["RUBYOPT"] = '-v'
ruby_exe("").chomp.should == RUBY_DESCRIPTION
end
end
it "sets $VERBOSE to true for '-w'" do
ENV["RUBYOPT"] = '-w'
ruby_exe("p $VERBOSE", :escape => true).chomp.should == "true"
end
it "sets $VERBOSE to true for '-W'" do
ENV["RUBYOPT"] = '-W'
ruby_exe("p $VERBOSE", :escape => true).chomp.should == "true"
end
it "sets $VERBOSE to nil for '-W0'" do
ENV["RUBYOPT"] = '-W0'
ruby_exe("p $VERBOSE", :escape => true).chomp.should == "nil"
end
it "sets $VERBOSE to false for '-W1'" do
ENV["RUBYOPT"] = '-W1'
ruby_exe("p $VERBOSE", :escape => true).chomp.should == "false"
end
it "sets $VERBOSE to true for '-W2'" do
ENV["RUBYOPT"] = '-W2'
ruby_exe("p $VERBOSE", :escape => true).chomp.should == "true"
end
it "requires the file for '-r'" do
f = fixture __FILE__, "rubyopt"
ENV["RUBYOPT"] = "-r#{f}"
ruby_exe("").should =~ /^rubyopt.rb required/
end
ruby_version_is ""..."1.9" do
it "sets $KCODE to 'NONE' with '-K'" do
ENV["RUBYOPT"] = '-K'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "NONE"
end
it "sets $KCODE to 'NONE' with '-Ka'" do
ENV["RUBYOPT"] = '-Ka'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "NONE"
end
it "sets $KCODE to 'NONE' with '-KA'" do
ENV["RUBYOPT"] = '-KA'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "NONE"
end
it "sets $KCODE to 'NONE' with '-Kn'" do
ENV["RUBYOPT"] = '-Kn'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "NONE"
end
it "sets $KCODE to 'NONE' with '-KN'" do
ENV["RUBYOPT"] = '-KN'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "NONE"
end
it "sets $KCODE to 'EUC' with '-Ke'" do
ENV["RUBYOPT"] = '-Ke'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "EUC"
end
it "sets $KCODE to 'EUC' with '-KE'" do
ENV["RUBYOPT"] = '-KE'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "EUC"
end
it "sets $KCODE to 'UTF8' with '-Ku'" do
ENV["RUBYOPT"] = '-Ku'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "UTF8"
end
it "sets $KCODE to 'UTF8' with '-KU'" do
ENV["RUBYOPT"] = '-KU'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "UTF8"
end
it "sets $KCODE to 'SJIS' with '-Ks'" do
ENV["RUBYOPT"] = '-Ks'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "SJIS"
end
it "sets $KCODE to 'SJIS' with '-KS'" do
ENV["RUBYOPT"] = '-KS'
ruby_exe("puts $KCODE", :escape => true).chomp.should == "SJIS"
end
end
it "raises a RuntimeError for '-a'" do
ENV["RUBYOPT"] = '-a'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-p'" do
ENV["RUBYOPT"] = '-p'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-n'" do
ENV["RUBYOPT"] = '-n'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-y'" do
ENV["RUBYOPT"] = '-y'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-c'" do
ENV["RUBYOPT"] = '-c'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-s'" do
ENV["RUBYOPT"] = '-s'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-h'" do
ENV["RUBYOPT"] = '-h'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--help'" do
ENV["RUBYOPT"] = '--help'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-l'" do
ENV["RUBYOPT"] = '-l'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-S'" do
ENV["RUBYOPT"] = '-S irb'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-e'" do
ENV["RUBYOPT"] = '-e0'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-i'" do
ENV["RUBYOPT"] = '-i.bak'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-x'" do
ENV["RUBYOPT"] = '-x'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-C'" do
ENV["RUBYOPT"] = '-C'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-X'" do
ENV["RUBYOPT"] = '-X.'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-F'" do
ENV["RUBYOPT"] = '-F'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-0'" do
ENV["RUBYOPT"] = '-0'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--copyright'" do
ENV["RUBYOPT"] = '--copyright'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--version'" do
ENV["RUBYOPT"] = '--version'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--yydebug'" do
ENV["RUBYOPT"] = '--yydebug'
ruby_exe("", :args => '2>&1').should =~ /RuntimeError/
end
end
|