summaryrefslogtreecommitdiff
path: root/spec/ruby/library/openstruct
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/openstruct')
-rw-r--r--spec/ruby/library/openstruct/frozen_spec.rb12
-rw-r--r--spec/ruby/library/openstruct/marshal_load_spec.rb2
-rw-r--r--spec/ruby/library/openstruct/method_missing_spec.rb35
-rw-r--r--spec/ruby/library/openstruct/to_h_spec.rb39
4 files changed, 53 insertions, 35 deletions
diff --git a/spec/ruby/library/openstruct/frozen_spec.rb b/spec/ruby/library/openstruct/frozen_spec.rb
index 27931fe5ac..c14a4bac55 100644
--- a/spec/ruby/library/openstruct/frozen_spec.rb
+++ b/spec/ruby/library/openstruct/frozen_spec.rb
@@ -14,23 +14,25 @@ describe "OpenStruct.new when frozen" do
@os.name.should == "John Smith"
end
- it "is not writeable" do
- lambda{ @os.age = 42 }.should raise_error( RuntimeError )
+ it "is not writable" do
+ ->{ @os.age = 42 }.should raise_error( RuntimeError )
end
it "cannot create new fields" do
- lambda{ @os.state = :new }.should raise_error( RuntimeError )
+ ->{ @os.state = :new }.should raise_error( RuntimeError )
end
it "creates a frozen clone" do
f = @os.clone
+ f.frozen?.should == true
f.age.should == 70
- lambda{ f.age = 0 }.should raise_error( RuntimeError )
- lambda{ f.state = :newer }.should raise_error( RuntimeError )
+ ->{ f.age = 0 }.should raise_error( RuntimeError )
+ ->{ f.state = :newer }.should raise_error( RuntimeError )
end
it "creates an unfrozen dup" do
d = @os.dup
+ d.frozen?.should == false
d.age.should == 70
d.age = 42
d.age.should == 42
diff --git a/spec/ruby/library/openstruct/marshal_load_spec.rb b/spec/ruby/library/openstruct/marshal_load_spec.rb
index e07c4cef05..342e5e68cd 100644
--- a/spec/ruby/library/openstruct/marshal_load_spec.rb
+++ b/spec/ruby/library/openstruct/marshal_load_spec.rb
@@ -4,7 +4,7 @@ require "ostruct"
describe "OpenStruct#marshal_load when passed [Hash]" do
it "defines methods based on the passed Hash" do
os = OpenStruct.new
- os.marshal_load(age: 20, name: "John")
+ os.send :marshal_load, age: 20, name: "John"
os.age.should eql(20)
os.name.should == "John"
diff --git a/spec/ruby/library/openstruct/method_missing_spec.rb b/spec/ruby/library/openstruct/method_missing_spec.rb
index eefe30661a..89f83d07b3 100644
--- a/spec/ruby/library/openstruct/method_missing_spec.rb
+++ b/spec/ruby/library/openstruct/method_missing_spec.rb
@@ -7,41 +7,18 @@ describe "OpenStruct#method_missing when called with a method name ending in '='
end
it "raises an ArgumentError when not passed any additional arguments" do
- lambda { @os.method_missing(:test=) }.should raise_error(ArgumentError)
- end
-
- it "raises a TypeError when self is frozen" do
- @os.freeze
- lambda { @os.method_missing(:test=, "test") }.should raise_error(RuntimeError)
- end
-
- it "creates accessor methods" do
- @os.method_missing(:test=, "test")
- @os.respond_to?(:test=).should be_true
- @os.respond_to?(:test).should be_true
-
- @os.test.should == "test"
- @os.test = "changed"
- @os.test.should == "changed"
- end
-
- it "updates the method/value table with the passed method/value" do
- @os.method_missing(:test=, "test")
- @os.send(:table)[:test].should == "test"
+ -> { @os.send(:test=) }.should raise_error(ArgumentError)
end
end
describe "OpenStruct#method_missing when passed additional arguments" do
- it "raises a NoMethodError" do
+ it "raises a NoMethodError when the key does not exist" do
os = OpenStruct.new
- lambda { os.method_missing(:test, 1, 2, 3) }.should raise_error(NoMethodError)
+ -> { os.test(1, 2, 3) }.should raise_error(NoMethodError)
end
-end
-describe "OpenStruct#method_missing when not passed any additional arguments" do
- it "returns the value for the passed method from the method/value table" do
- os = OpenStruct.new(age: 20)
- os.method_missing(:age).should eql(20)
- os.method_missing(:name).should be_nil
+ it "raises an ArgumentError when the key exists" do
+ os = OpenStruct.new(test: 20)
+ -> { os.test(1, 2, 3) }.should raise_error(ArgumentError)
end
end
diff --git a/spec/ruby/library/openstruct/to_h_spec.rb b/spec/ruby/library/openstruct/to_h_spec.rb
index f3e157816b..6c272bcc71 100644
--- a/spec/ruby/library/openstruct/to_h_spec.rb
+++ b/spec/ruby/library/openstruct/to_h_spec.rb
@@ -26,4 +26,43 @@ describe "OpenStruct#to_h" do
@to_h[:age] = 71
@os.age.should == 70
end
+
+ context "with block" do
+ it "converts [key, value] pairs returned by the block to a hash" do
+ h = @os.to_h { |k, v| [k.to_s, v*2] }
+ h.should == { "name" => "John SmithJohn Smith", "age" => 140, "pension" => 600 }
+ end
+
+ it "raises ArgumentError if block returns longer or shorter array" do
+ -> do
+ @os.to_h { |k, v| [k.to_s, v*2, 1] }
+ end.should raise_error(ArgumentError, /element has wrong array length/)
+
+ -> do
+ @os.to_h { |k, v| [k] }
+ end.should raise_error(ArgumentError, /element has wrong array length/)
+ end
+
+ it "raises TypeError if block returns something other than Array" do
+ -> do
+ @os.to_h { |k, v| "not-array" }
+ end.should raise_error(TypeError, /wrong element type String/)
+ end
+
+ it "coerces returned pair to Array with #to_ary" do
+ x = mock('x')
+ x.stub!(:to_ary).and_return([:b, 'b'])
+
+ @os.to_h { |k| x }.should == { :b => 'b' }
+ end
+
+ it "does not coerce returned pair to Array with #to_a" do
+ x = mock('x')
+ x.stub!(:to_a).and_return([:b, 'b'])
+
+ -> do
+ @os.to_h { |k| x }
+ end.should raise_error(TypeError, /wrong element type MockObject/)
+ end
+ end
end