summaryrefslogtreecommitdiff
path: root/spec/ruby/library/openstruct/frozen_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/openstruct/frozen_spec.rb')
-rw-r--r--spec/ruby/library/openstruct/frozen_spec.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/ruby/library/openstruct/frozen_spec.rb b/spec/ruby/library/openstruct/frozen_spec.rb
new file mode 100644
index 0000000000..c37fd18c8c
--- /dev/null
+++ b/spec/ruby/library/openstruct/frozen_spec.rb
@@ -0,0 +1,40 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+
+describe "OpenStruct.new when frozen" do
+ before :each do
+ @os = OpenStruct.new(name: "John Smith", age: 70, pension: 300).freeze
+ end
+ #
+ # method_missing case handled in method_missing_spec.rb
+ #
+ it "is still readable" do
+ @os.age.should.eql?(70)
+ @os.pension.should.eql?(300)
+ @os.name.should == "John Smith"
+ end
+
+ it "is not writable" do
+ ->{ @os.age = 42 }.should.raise( RuntimeError )
+ end
+
+ it "cannot create new fields" do
+ ->{ @os.state = :new }.should.raise( RuntimeError )
+ end
+
+ it "creates a frozen clone" do
+ f = @os.clone
+ f.frozen?.should == true
+ f.age.should == 70
+ ->{ f.age = 0 }.should.raise( RuntimeError )
+ ->{ f.state = :newer }.should.raise( 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
+ end
+end