summaryrefslogtreecommitdiff
path: root/spec/ruby/language
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-01-29 16:08:16 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-01-29 16:08:16 +0000
commit3fa5bd38af50fb3d98de0ea51043d73f8d06a24b (patch)
treed473b71cc6925ee1e17727215e9f9a66e3f24802 /spec/ruby/language
parent1e658d45e1f8dbadab18f9c35b5cfb5a5fec98bf (diff)
Update to ruby/spec@83063a3
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62094 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/language')
-rw-r--r--spec/ruby/language/def_spec.rb37
-rw-r--r--spec/ruby/language/defined_spec.rb16
-rw-r--r--spec/ruby/language/fixtures/def.rb6
-rw-r--r--spec/ruby/language/fixtures/defined.rb5
-rw-r--r--spec/ruby/language/regexp_spec.rb20
5 files changed, 81 insertions, 3 deletions
diff --git a/spec/ruby/language/def_spec.rb b/spec/ruby/language/def_spec.rb
index c31a2afb41..c0b2efbfc7 100644
--- a/spec/ruby/language/def_spec.rb
+++ b/spec/ruby/language/def_spec.rb
@@ -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
diff --git a/spec/ruby/language/defined_spec.rb b/spec/ruby/language/defined_spec.rb
index 08fa2d8fe3..98f4ee8829 100644
--- a/spec/ruby/language/defined_spec.rb
+++ b/spec/ruby/language/defined_spec.rb
@@ -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
diff --git a/spec/ruby/language/fixtures/def.rb b/spec/ruby/language/fixtures/def.rb
index 81bfce73d0..e07060ed74 100644
--- a/spec/ruby/language/fixtures/def.rb
+++ b/spec/ruby/language/fixtures/def.rb
@@ -1,3 +1,9 @@
+def toplevel_define_other_method
+ def nested_method_in_toplevel_method
+ 42
+ end
+end
+
def some_toplevel_method
end
diff --git a/spec/ruby/language/fixtures/defined.rb b/spec/ruby/language/fixtures/defined.rb
index d26e553c4b..8b6004c19f 100644
--- a/spec/ruby/language/fixtures/defined.rb
+++ b/spec/ruby/language/fixtures/defined.rb
@@ -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
diff --git a/spec/ruby/language/regexp_spec.rb b/spec/ruby/language/regexp_spec.rb
index d4b0c81128..684808f5ab 100644
--- a/spec/ruby/language/regexp_spec.rb
+++ b/spec/ruby/language/regexp_spec.rb
@@ -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