Skip to content

Commit

Permalink
Fix bug with Kernel#public_methods et al
Browse files Browse the repository at this point in the history
This was missed because there was a bug in our spec runner and shared
behaviors. See previous commit.
  • Loading branch information
seven1m committed Jul 10, 2024
1 parent 7127245 commit 5a84746
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/kernel_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,23 +528,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

0 comments on commit 5a84746

Please sign in to comment.