Skip to content

Commit

Permalink
Merge pull request #2202 from natalie-lang/public_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Jul 10, 2024
2 parents 8952221 + 5a84746 commit d07ca36
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/kernel_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,23 +520,23 @@ Value KernelModule::print(Env *env, Args args) {

Value KernelModule::private_methods(Env *env, Value recur) {
if (singleton_class())
return singleton_class()->private_instance_methods(env, recur);
return singleton_class()->private_instance_methods(env, TrueObject::the());
else
return klass()->private_instance_methods(env, FalseObject::the());
return klass()->private_instance_methods(env, recur);
}

Value KernelModule::protected_methods(Env *env, Value recur) {
if (singleton_class())
return singleton_class()->protected_instance_methods(env, recur);
return singleton_class()->protected_instance_methods(env, TrueObject::the());
else
return klass()->protected_instance_methods(env, FalseObject::the());
return klass()->protected_instance_methods(env, recur);
}

Value KernelModule::public_methods(Env *env, Value recur) {
if (singleton_class())
return singleton_class()->public_instance_methods(env, recur);
return singleton_class()->public_instance_methods(env, TrueObject::the());
else
return klass()->public_instance_methods(env, FalseObject::the());
return klass()->public_instance_methods(env, recur);
}

Value KernelModule::proc(Env *env, Block *block) {
Expand Down
49 changes: 49 additions & 0 deletions test/natalie/module_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,53 @@ class C2 < C1
M3.constants.sort.should == [:A, :M3A]
end
end

describe '#private_methods' do
mod = Module.new do
private
def m_mod = :m
end
klass = Class.new do
include mod
private
def m_klass = :k
end
klass.new.private_methods.grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.private_methods(true).grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.private_methods(1).grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.private_methods(false).grep(/^m_/).sort.should == %i[m_klass]
klass.new.private_methods(nil).grep(/^m_/).sort.should == %i[m_klass]
end

describe '#protected_methods' do
mod = Module.new do
protected
def m_mod = :m
end
klass = Class.new do
include mod
protected
def m_klass = :k
end
klass.new.protected_methods.grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.protected_methods(true).grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.protected_methods(1).grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.protected_methods(false).grep(/^m_/).sort.should == %i[m_klass]
klass.new.protected_methods(nil).grep(/^m_/).sort.should == %i[m_klass]
end

describe '#public_methods' do
mod = Module.new do
def m_mod = :m
end
klass = Class.new do
include mod
def m_klass = :k
end
klass.new.public_methods.grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.public_methods(true).grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.public_methods(1).grep(/^m_/).sort.should == %i[m_klass m_mod]
klass.new.public_methods(false).grep(/^m_/).sort.should == %i[m_klass]
klass.new.public_methods(nil).grep(/^m_/).sort.should == %i[m_klass]
end
end
13 changes: 10 additions & 3 deletions test/support/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ def skip(test = nil, &block)
xit(test, &block)
end

def it_behaves_like(behavior, method, obj = nil)
def it_behaves_like(behavior, method, obj = :zzzz_not_given)
before :all do
@method = method if method
@object = obj if obj
@object = obj unless obj == :zzzz_not_given
end

block = @shared[behavior]
Expand All @@ -172,7 +172,14 @@ def it_behaves_like(behavior, method, obj = nil)
end

def it_should_behave_like(*shared_groups)
shared_groups.each { |behavior| it_behaves_like behavior, @method, @object }
shared_groups.each do |behavior|
block = @shared[behavior]
if block
block.call
else
raise "Cannot find shared behavior: #{behavior.inspect} (available: #{@shared.keys.inspect})"
end
end
end

def specify(test = nil, &block)
Expand Down

0 comments on commit d07ca36

Please sign in to comment.