diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/module/attr_reader_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) | |
download | ruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/module/attr_reader_spec.rb')
-rw-r--r-- | spec/ruby/core/module/attr_reader_spec.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/spec/ruby/core/module/attr_reader_spec.rb b/spec/ruby/core/module/attr_reader_spec.rb new file mode 100644 index 0000000000..f2b968d3fc --- /dev/null +++ b/spec/ruby/core/module/attr_reader_spec.rb @@ -0,0 +1,64 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Module#attr_reader" do + it "creates a getter for each given attribute name" do + c = Class.new do + attr_reader :a, "b" + + def initialize + @a = "test" + @b = "test2" + end + end + + o = c.new + %w{a b}.each do |x| + o.respond_to?(x).should == true + o.respond_to?("#{x}=").should == false + end + + o.a.should == "test" + o.b.should == "test2" + o.send(:a).should == "test" + o.send(:b).should == "test2" + end + + it "not allows for adding an attr_reader to an immediate" do + class TrueClass + attr_reader :spec_attr_reader + end + + lambda { true.instance_variable_set("@spec_attr_reader", "a") }.should raise_error(RuntimeError) + end + + it "converts non string/symbol/fixnum names to strings using to_str" do + (o = mock('test')).should_receive(:to_str).any_number_of_times.and_return("test") + c = Class.new do + attr_reader o + end + + c.new.respond_to?("test").should == true + c.new.respond_to?("test=").should == false + end + + it "raises a TypeError when the given names can't be converted to strings using to_str" do + o = mock('o') + lambda { Class.new { attr_reader o } }.should raise_error(TypeError) + (o = mock('123')).should_receive(:to_str).and_return(123) + lambda { Class.new { attr_reader o } }.should raise_error(TypeError) + end + + it "applies current visibility to methods created" do + c = Class.new do + protected + attr_reader :foo + end + + lambda { c.new.foo }.should raise_error(NoMethodError) + end + + it "is a private method" do + lambda { Class.new.attr_reader(:foo) }.should raise_error(NoMethodError) + end +end |