From 7127245d084e38c888d5d268efbcf9c086522016 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Tue, 9 Jul 2024 22:23:54 -0500 Subject: [PATCH 1/2] Fix behavior in spec runner Two problems: 1. It was not possible to pass `nil` as the third argument. 2. `it_behaves_like` was kinda working by accident... the `@object` value was not set and was nil. --- test/support/spec.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/support/spec.rb b/test/support/spec.rb index 0f567629f..47dd74255 100644 --- a/test/support/spec.rb +++ b/test/support/spec.rb @@ -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] @@ -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) From 5a8474692c77a6d1c8811d54049e743bd09a3d3d Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Sun, 7 Jul 2024 07:29:23 -0500 Subject: [PATCH 2/2] Fix bug with Kernel#public_methods et al This was missed because there was a bug in our spec runner and shared behaviors. See previous commit. --- src/kernel_module.cpp | 12 ++++----- test/natalie/module_test.rb | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/kernel_module.cpp b/src/kernel_module.cpp index ed7af638c..3183a4a3a 100644 --- a/src/kernel_module.cpp +++ b/src/kernel_module.cpp @@ -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) { diff --git a/test/natalie/module_test.rb b/test/natalie/module_test.rb index 98ef700da..6d473b105 100644 --- a/test/natalie/module_test.rb +++ b/test/natalie/module_test.rb @@ -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