-
-
Notifications
You must be signed in to change notification settings - Fork 389
Add specs for reserved keywords #1187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
80f1817
2cc6546
0f54929
fde3d4b
62a023a
a1d7726
1e727ee
d6dec78
8cc2c02
618b860
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,155 @@ | ||||||
require_relative '../spec_helper' | ||||||
|
||||||
describe "Ruby's reserved keywords" do | ||||||
# Copied from Prism::Translation::Ripper | ||||||
keywords = [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: It would be nice to have one canonical list that can be used in all these places:
Where should such a canonical list live? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no way to avoid duplication there, since it's files in different repositories. FWIW in ruby/ruby there is also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, done: |
||||||
"alias", | ||||||
"and", | ||||||
"begin", | ||||||
"BEGIN", | ||||||
"break", | ||||||
"case", | ||||||
"class", | ||||||
"def", | ||||||
"defined?", | ||||||
"do", | ||||||
"else", | ||||||
"elsif", | ||||||
"end", | ||||||
"END", | ||||||
"ensure", | ||||||
"false", | ||||||
"for", | ||||||
"if", | ||||||
"in", | ||||||
"module", | ||||||
"next", | ||||||
"nil", | ||||||
"not", | ||||||
"or", | ||||||
"redo", | ||||||
"rescue", | ||||||
"retry", | ||||||
"return", | ||||||
"self", | ||||||
"super", | ||||||
"then", | ||||||
"true", | ||||||
"undef", | ||||||
"unless", | ||||||
"until", | ||||||
"when", | ||||||
"while", | ||||||
"yield", | ||||||
"__ENCODING__", | ||||||
"__FILE__", | ||||||
"__LINE__" | ||||||
] | ||||||
|
||||||
keywords.each do |kw| | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmmmm I disagree, in a spec focused on describing the behaviour of keywords, I would expect "keyword" to a be prominent term. It's an overloaded term, but that's just how Ruby is already. Perhaps Anyway, I renamed to |
||||||
describe "keyword '#{kw}'" do | ||||||
it "can't be used as local variable name" do | ||||||
expect_syntax_error <<~RUBY | ||||||
#{kw} = "a local variable named '#{kw}'" | ||||||
amomchilov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
RUBY | ||||||
end | ||||||
|
||||||
invalid_ivar_names = ["defined?"] | ||||||
|
||||||
if invalid_ivar_names.include?(kw) | ||||||
eregon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
it "can't be used as an instance variable name" do | ||||||
expect_syntax_error <<~RUBY | ||||||
@#{kw} = "an instance variable named '#{kw}'" | ||||||
RUBY | ||||||
end | ||||||
else | ||||||
it "can be used as an instance variable name" do | ||||||
result = sandboxed_eval <<~RUBY | ||||||
@#{kw} = "an instance variable named '#{kw}'" | ||||||
@#{kw} | ||||||
RUBY | ||||||
|
||||||
result.should == "an instance variable named '#{kw}'" | ||||||
end | ||||||
end | ||||||
|
||||||
invalid_class_var_names = ["defined?"] | ||||||
|
||||||
if invalid_class_var_names.include?(kw) | ||||||
it "can't be used as a class variable name" do | ||||||
expect_syntax_error <<~RUBY | ||||||
@@#{kw} = "a class variable named '#{kw}'" | ||||||
RUBY | ||||||
end | ||||||
else | ||||||
it "can be used as a class variable name" do | ||||||
result = sandboxed_eval <<~RUBY | ||||||
@@#{kw} = "a class variable named '#{kw}'" | ||||||
@@#{kw} | ||||||
RUBY | ||||||
|
||||||
result.should == "a class variable named '#{kw}'" | ||||||
end | ||||||
end | ||||||
|
||||||
invalid_global_var_names = ["defined?"] | ||||||
amomchilov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
if invalid_global_var_names.include?(kw) | ||||||
it "can't be used as a global variable name" do | ||||||
expect_syntax_error <<~RUBY | ||||||
$#{kw} = "a global variable named '#{kw}'" | ||||||
RUBY | ||||||
end | ||||||
else | ||||||
it "can be used as a global variable name" do | ||||||
result = sandboxed_eval <<~RUBY | ||||||
$#{kw} = "a global variable named '#{kw}'" | ||||||
$#{kw} | ||||||
RUBY | ||||||
|
||||||
result.should == "a global variable named '#{kw}'" | ||||||
end | ||||||
end | ||||||
|
||||||
it "can't be used as a positional parameter name" do | ||||||
expect_syntax_error <<~RUBY | ||||||
def x(#{kw}); end | ||||||
RUBY | ||||||
end | ||||||
|
||||||
invalid_kw_param_names = ["BEGIN","END","defined?"] | ||||||
|
||||||
if invalid_kw_param_names.include?(kw) | ||||||
it "can't be used a keyword parameter name" do | ||||||
expect_syntax_error <<~RUBY | ||||||
def m(#{kw}:); end | ||||||
RUBY | ||||||
end | ||||||
else | ||||||
it "can be used a keyword parameter name" do | ||||||
result = sandboxed_eval <<~RUBY | ||||||
def m(#{kw}:) | ||||||
binding.local_variable_get(:#{kw}) | ||||||
end | ||||||
|
||||||
m(#{kw}: "an argument to '#{kw}'") | ||||||
RUBY | ||||||
|
||||||
result.should == "an argument to '#{kw}'" | ||||||
end | ||||||
end | ||||||
|
||||||
it "can be used as a method name" do | ||||||
result = sandboxed_eval <<~RUBY | ||||||
def #{kw} | ||||||
"a method named '#{kw}'" | ||||||
end | ||||||
|
||||||
send(:#{kw}) | ||||||
RUBY | ||||||
|
||||||
result.should == "a method named '#{kw}'" | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
Uh oh!
There was an error while loading. Please reload this page.