Skip to content

Commit 2c80ae5

Browse files
authored
Add specs for reserved keywords (#1187)
1 parent 03650c2 commit 2c80ae5

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

language/reserved_keywords.rb

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
require_relative '../spec_helper'
2+
3+
describe "Ruby's reserved keywords" do
4+
# Copied from https://github.com/ruby/ruby/blob/master/defs/keywords
5+
keywords = %w[
6+
alias
7+
and
8+
begin
9+
BEGIN
10+
break
11+
case
12+
class
13+
def
14+
defined?
15+
do
16+
else
17+
elsif
18+
end
19+
END
20+
ensure
21+
false
22+
for
23+
if
24+
in
25+
module
26+
next
27+
nil
28+
not
29+
or
30+
redo
31+
rescue
32+
retry
33+
return
34+
self
35+
super
36+
then
37+
true
38+
undef
39+
unless
40+
until
41+
when
42+
while
43+
yield
44+
__ENCODING__
45+
__FILE__
46+
__LINE__
47+
]
48+
49+
keywords.each do |name|
50+
describe "keyword '#{name}'" do
51+
it "can't be used as local variable name" do
52+
-> { eval(<<~RUBY) }.should raise_error(SyntaxError)
53+
#{name} = :local_variable
54+
RUBY
55+
end
56+
57+
if name == "defined?"
58+
it "can't be used as an instance variable name" do
59+
-> { eval(<<~RUBY) }.should raise_error(SyntaxError)
60+
@#{name} = :instance_variable
61+
RUBY
62+
end
63+
64+
it "can't be used as a class variable name" do
65+
-> { eval(<<~RUBY) }.should raise_error(SyntaxError)
66+
class C
67+
@@#{name} = :class_variable
68+
end
69+
RUBY
70+
end
71+
72+
it "can't be used as a global variable name" do
73+
-> { eval(<<~RUBY) }.should raise_error(SyntaxError)
74+
$#{name} = :global_variable
75+
RUBY
76+
end
77+
else
78+
it "can be used as an instance variable name" do
79+
result = eval <<~RUBY
80+
@#{name} = :instance_variable
81+
@#{name}
82+
RUBY
83+
84+
result.should == :instance_variable
85+
end
86+
87+
it "can be used as a class variable name" do
88+
result = eval <<~RUBY
89+
class C
90+
@@#{name} = :class_variable
91+
@@#{name}
92+
end
93+
RUBY
94+
95+
result.should == :class_variable
96+
end
97+
98+
it "can be used as a global variable name" do
99+
result = eval <<~RUBY
100+
$#{name} = :global_variable
101+
$#{name}
102+
RUBY
103+
104+
result.should == :global_variable
105+
end
106+
end
107+
108+
it "can't be used as a positional parameter name" do
109+
-> { eval(<<~RUBY) }.should raise_error(SyntaxError)
110+
def x(#{name}); end
111+
RUBY
112+
end
113+
114+
invalid_kw_param_names = ["BEGIN","END","defined?"]
115+
116+
if invalid_kw_param_names.include?(name)
117+
it "can't be used a keyword parameter name" do
118+
-> { eval(<<~RUBY) }.should raise_error(SyntaxError)
119+
def m(#{name}:); end
120+
RUBY
121+
end
122+
else
123+
it "can be used a keyword parameter name" do
124+
result = instance_eval <<~RUBY
125+
def m(#{name}:)
126+
binding.local_variable_get(:#{name})
127+
end
128+
129+
m(#{name}: :argument)
130+
RUBY
131+
132+
result.should == :argument
133+
end
134+
end
135+
136+
it "can be used as a method name" do
137+
result = instance_eval <<~RUBY
138+
def #{name}
139+
:method_return_value
140+
end
141+
142+
send(:#{name})
143+
RUBY
144+
145+
result.should == :method_return_value
146+
end
147+
end
148+
end
149+
end

0 commit comments

Comments
 (0)