diff options
Diffstat (limited to 'spec/ruby/core/kernel/fixtures/classes.rb')
| -rw-r--r-- | spec/ruby/core/kernel/fixtures/classes.rb | 154 |
1 files changed, 133 insertions, 21 deletions
diff --git a/spec/ruby/core/kernel/fixtures/classes.rb b/spec/ruby/core/kernel/fixtures/classes.rb index 1f45bbb083..ad82f3cef5 100644 --- a/spec/ruby/core/kernel/fixtures/classes.rb +++ b/spec/ruby/core/kernel/fixtures/classes.rb @@ -49,7 +49,7 @@ module KernelSpecs def self.chomp(str, method, sep="\n") code = "$_ = #{str.inspect}; $/ = #{sep.inspect}; #{method}; print $_" - IO.popen([*ruby_exe, "-n", "-e", code], "r+") do |io| + IO.popen([*ruby_exe, "-W0", "-n", "-e", code], "r+") do |io| io.puts io.close_write io.read @@ -180,6 +180,25 @@ module KernelSpecs alias aliased_pub_method pub_method end + class BasicA < BasicObject + define_method(:respond_to?, ::Kernel.instance_method(:respond_to?)) + + def pub_method; :public_method; end + + def undefed_method; :undefed_method; end + undef_method :undefed_method + + protected + def protected_method; :protected_method; end + + private + def private_method; :private_method; end + end + + class MissingA < A + undef :respond_to_missing? + end + class VisibilityChange class << self private :new @@ -219,10 +238,28 @@ module KernelSpecs block_given? end + def self.accept_block_inside_block() + yield_self { + block_given? + } + end + + def self.accept_block_as_argument_inside_block(&block) + yield_self { + block_given? + } + end + class << self define_method(:defined_block) do block_given? end + + define_method(:defined_block_inside_block) do + yield_self { + block_given? + } + end end end @@ -235,10 +272,28 @@ module KernelSpecs self.send(:block_given?) end + def self.accept_block_inside_block + yield_self { + self.send(:block_given?) + } + end + + def self.accept_block_as_argument_inside_block(&block) + yield_self { + self.send(:block_given?) + } + end + class << self define_method(:defined_block) do self.send(:block_given?) end + + define_method(:defined_block_inside_block) do + yield_self { + self.send(:block_given?) + } + end end end @@ -251,10 +306,28 @@ module KernelSpecs Kernel.block_given? end + def self.accept_block_inside_block + yield_self { + Kernel.block_given? + } + end + + def self.accept_block_as_argument_inside_block(&block) + yield_self { + Kernel.block_given? + } + end + class << self define_method(:defined_block) do Kernel.block_given? end + + define_method(:defined_block_inside_block) do + yield_self { + Kernel.block_given? + } + end end end @@ -281,14 +354,25 @@ module KernelSpecs @two = two end - def initialize_copy(other) + def initialize_copy(other, **kw) ScratchPad.record object_id end + + # define to support calling #clone with optional :freeze keyword argument + def initialize_clone(other, **kw) + super(other) # to call #initialize_copy + end end class Clone def initialize_clone(other) - ScratchPad.record other.object_id + ScratchPad.record other + end + end + + class CloneFreeze + def initialize_clone(other, **kwargs) + ScratchPad.record([other, kwargs]) end end @@ -328,7 +412,7 @@ module KernelSpecs def inner b = mp { return :good } - pr = lambda { |x| x.call } + pr = -> x { x.call } pr.call(b) @@ -337,21 +421,44 @@ module KernelSpecs end end + module LambdaSpecs + module ZSuper + def lambda + super + end + end + + class ForwardBlockWithZSuper + prepend(ZSuper) + end + + module Ampersand + def lambda(&block) + suppress_warning {super(&block)} + end + end + + class SuperAmpersand + prepend(Ampersand) + end + end + class RespondViaMissing def respond_to_missing?(method, priv=false) case method - when :handled_publicly - true - when :handled_privately - priv - when :not_handled - false - else - raise "Typo in method name" + when :handled_publicly + true + when :handled_privately + priv + when :not_handled + false + else + raise "Typo in method name: #{method.inspect}" end end def method_missing(method, *args) + raise "the method name should be a Symbol" unless Symbol === method "Done #{method}(#{args})" end end @@ -424,6 +531,20 @@ module KernelSpecs def f2_call_lineno; method(:f3).source_location[1] + 1; end def f3_call_lineno; method(:f4).source_location[1] + 1; end end + + CustomRangeInteger = Struct.new(:value) do + def to_int; value; end + def <=>(other); to_int <=> other.to_int; end + def -(other); self.class.new(to_int - other.to_int); end + def +(other); self.class.new(to_int + other.to_int); end + end + + CustomRangeFloat = Struct.new(:value) do + def to_f; value; end + def <=>(other); to_f <=> other.to_f; end + def -(other); to_f - other.to_f; end + def +(other); self.class.new(to_f + other.to_f); end + end end class EvalSpecs @@ -454,12 +575,3 @@ class EvalSpecs return f end end - -# for Kernel#sleep to have Channel in it's specs -# TODO: switch directly to queue for both Kernel#sleep and Thread specs? -unless defined? Channel - require 'thread' - class Channel < Queue - alias receive shift - end -end |
