Skip to content

Commit

Permalink
Add spec for Module#attr* calling method_added hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 19, 2024
1 parent 1305397 commit c498a46
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions spec/ruby/core/module/attr_accessor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/attr_added'

describe "Module#attr_accessor" do
it "creates a getter and setter for each given attribute name" do
Expand Down Expand Up @@ -106,4 +107,6 @@ class Integer
1.foobar.should be_nil
end
end

it_behaves_like :module_attr_added, :attr_accessor
end
3 changes: 3 additions & 0 deletions spec/ruby/core/module/attr_reader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/attr_added'

describe "Module#attr_reader" do
it "creates a getter for each given attribute name" do
Expand Down Expand Up @@ -67,4 +68,6 @@ class TrueClass
(attr_reader :foo, 'bar').should == [:foo, :bar]
end
end

it_behaves_like :module_attr_added, :attr_reader
end
3 changes: 3 additions & 0 deletions spec/ruby/core/module/attr_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/attr_added'

describe "Module#attr" do
before :each do
Expand Down Expand Up @@ -153,4 +154,6 @@ def initialize
(attr :qux, true).should == [:qux, :qux=]
end
end

it_behaves_like :module_attr_added, :attr
end
3 changes: 3 additions & 0 deletions spec/ruby/core/module/attr_writer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/attr_added'

describe "Module#attr_writer" do
it "creates a setter for each given attribute name" do
Expand Down Expand Up @@ -77,4 +78,6 @@ class TrueClass
(attr_writer :foo, 'bar').should == [:foo=, :bar=]
end
end

it_behaves_like :module_attr_added, :attr_writer
end
34 changes: 34 additions & 0 deletions spec/ruby/core/module/shared/attr_added.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
describe :module_attr_added, shared: true do
it "calls method_added for normal classes" do
ScratchPad.record []

cls = Class.new do
class << self
def method_added(name)
ScratchPad.recorded << name
end
end
end

cls.send(@method, :foo)

ScratchPad.recorded.each {|name| name.to_s.should =~ /foo[=]?/}
end

it "calls singleton_method_added for singleton classes" do
ScratchPad.record []
cls = Class.new do
class << self
def singleton_method_added(name)
# called for this def so ignore it
return if name == :singleton_method_added
ScratchPad.recorded << name
end
end
end

cls.singleton_class.send(@method, :foo)

ScratchPad.recorded.each {|name| name.to_s.should =~ /foo[=]?/}
end
end

0 comments on commit c498a46

Please sign in to comment.