1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62094 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2018-01-29 16:08:16 +00:00
parent 1e658d45e1
commit 3fa5bd38af
494 changed files with 4133 additions and 3109 deletions

View file

@ -488,7 +488,7 @@ describe "A nested method definition" do
DefSpecNested.should_not have_instance_method :body_method
end
it "defines methods as public by default" do
it "creates an instance method inside Class.new" do
cls = Class.new do
def do_def
def new_def
@ -500,6 +500,41 @@ describe "A nested method definition" do
obj = cls.new
obj.do_def
obj.new_def.should == 1
cls.new.new_def.should == 1
-> { Object.new.new_def }.should raise_error(NoMethodError)
end
end
describe "A method definition always resets the visibility to public for nested definitions" do
it "in Class.new" do
cls = Class.new do
private
def do_def
def new_def
1
end
end
end
obj = cls.new
-> { obj.do_def }.should raise_error(NoMethodError, /private/)
obj.send :do_def
obj.new_def.should == 1
cls.new.new_def.should == 1
-> { Object.new.new_def }.should raise_error(NoMethodError)
end
it "at the toplevel" do
obj = Object.new
-> { obj.toplevel_define_other_method }.should raise_error(NoMethodError, /private/)
toplevel_define_other_method
nested_method_in_toplevel_method.should == 42
Object.new.nested_method_in_toplevel_method.should == 42
end
end

View file

@ -505,6 +505,10 @@ describe "The defined? keyword for variables" 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
DefinedSpecs::Basic.new.global_variable_defined_as_nil.should == "global-variable"
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
@ -752,8 +756,16 @@ describe "The defined? keyword for a scoped constant" 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
ruby_version_is ""..."2.6" do
it "returns 'constant' when a constant is defined on top-level but not on the class" do
defined?(DefinedSpecs::Basic::String).should == 'constant'
end
end
ruby_version_is "2.6" do
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
end
it "returns 'constant' if the scoped-scoped constant is defined" do

View file

@ -1,3 +1,9 @@
def toplevel_define_other_method
def nested_method_in_toplevel_method
42
end
end
def some_toplevel_method
end

View file

@ -94,6 +94,11 @@ module DefinedSpecs
defined? $defined_specs_global_variable_defined
end
def global_variable_defined_as_nil
$defined_specs_global_variable_defined_as_nil = nil
defined? $defined_specs_global_variable_defined_as_nil
end
def class_variable_undefined
defined? @@class_variable_undefined
end

View file

@ -147,4 +147,24 @@ describe "Literal Regexps" do
pattern.should_not =~ 'fooF'
pattern.should_not =~ 'T'
end
escapable_terminators = ['!', '"', '#', '%', '&', "'", ',', '-', ':', ';', '@', '_', '`']
it "supports escaping characters when used as a terminator" do
escapable_terminators.each do |c|
ref = "(?-mix:#{c})"
pattern = eval("%r" + c + "\\" + c + c)
pattern.to_s.should == ref
end
end
it "treats an escaped non-escapable character normally when used as a terminator" do
all_terminators = [*("!".."/"), *(":".."@"), *("[".."`"), *("{".."~")]
special_cases = ['(', '{', '[', '<', '\\', '=', '~']
(all_terminators - special_cases - escapable_terminators).each do |c|
ref = "(?-mix:\\#{c})"
pattern = eval("%r" + c + "\\" + c + c)
pattern.to_s.should == ref
end
end
end