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/delete_field_spec.rb19
-rw-r--r--spec/ruby/library/openstruct/element_reference_spec.rb13
-rw-r--r--spec/ruby/library/openstruct/element_set_spec.rb13
-rw-r--r--spec/ruby/library/openstruct/equal_value_spec.rb28
-rw-r--r--spec/ruby/library/openstruct/fixtures/classes.rb4
-rw-r--r--spec/ruby/library/openstruct/frozen_spec.rb40
-rw-r--r--spec/ruby/library/openstruct/initialize_spec.rb8
-rw-r--r--spec/ruby/library/openstruct/inspect_spec.rb8
-rw-r--r--spec/ruby/library/openstruct/marshal_dump_spec.rb9
-rw-r--r--spec/ruby/library/openstruct/marshal_load_spec.rb12
-rw-r--r--spec/ruby/library/openstruct/method_missing_spec.rb24
-rw-r--r--spec/ruby/library/openstruct/new_spec.rb20
-rw-r--r--spec/ruby/library/openstruct/shared/inspect.rb20
-rw-r--r--spec/ruby/library/openstruct/to_h_spec.rb68
-rw-r--r--spec/ruby/library/openstruct/to_s_spec.rb8
15 files changed, 294 insertions, 0 deletions
diff --git a/spec/ruby/library/openstruct/delete_field_spec.rb b/spec/ruby/library/openstruct/delete_field_spec.rb
new file mode 100644
index 0000000000..9ac80196cc
--- /dev/null
+++ b/spec/ruby/library/openstruct/delete_field_spec.rb
@@ -0,0 +1,19 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+
+describe "OpenStruct#delete_field" do
+ before :each do
+ @os = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
+ end
+
+ it "removes the named field from self's method/value table" do
+ @os.delete_field(:name)
+ @os[:name].should be_nil
+ end
+
+ it "does remove the accessor methods" do
+ @os.delete_field(:name)
+ @os.respond_to?(:name).should be_false
+ @os.respond_to?(:name=).should be_false
+ end
+end
diff --git a/spec/ruby/library/openstruct/element_reference_spec.rb b/spec/ruby/library/openstruct/element_reference_spec.rb
new file mode 100644
index 0000000000..c425991b0f
--- /dev/null
+++ b/spec/ruby/library/openstruct/element_reference_spec.rb
@@ -0,0 +1,13 @@
+require_relative '../../spec_helper'
+require "ostruct"
+
+describe "OpenStruct#[]" do
+ before :each do
+ @os = OpenStruct.new
+ end
+
+ it "returns the associated value" do
+ @os.foo = 42
+ @os[:foo].should == 42
+ end
+end
diff --git a/spec/ruby/library/openstruct/element_set_spec.rb b/spec/ruby/library/openstruct/element_set_spec.rb
new file mode 100644
index 0000000000..eeb5a8b318
--- /dev/null
+++ b/spec/ruby/library/openstruct/element_set_spec.rb
@@ -0,0 +1,13 @@
+require_relative '../../spec_helper'
+require "ostruct"
+
+describe "OpenStruct#[]=" do
+ before :each do
+ @os = OpenStruct.new
+ end
+
+ it "sets the associated value" do
+ @os[:foo] = 42
+ @os.foo.should == 42
+ end
+end
diff --git a/spec/ruby/library/openstruct/equal_value_spec.rb b/spec/ruby/library/openstruct/equal_value_spec.rb
new file mode 100644
index 0000000000..103ac13588
--- /dev/null
+++ b/spec/ruby/library/openstruct/equal_value_spec.rb
@@ -0,0 +1,28 @@
+require_relative '../../spec_helper'
+require "ostruct"
+require_relative 'fixtures/classes'
+
+describe "OpenStruct#==" do
+ before :each do
+ @os = OpenStruct.new(name: "John")
+ end
+
+ it "returns false when the passed argument is no OpenStruct" do
+ (@os == Object.new).should be_false
+ (@os == "Test").should be_false
+ (@os == 10).should be_false
+ (@os == :sym).should be_false
+ end
+
+ it "returns true when self and other are equal method/value wise" do
+ (@os == @os).should be_true
+ (@os == OpenStruct.new(name: "John")).should be_true
+ (@os == OpenStructSpecs::OpenStructSub.new(name: "John")).should be_true
+
+ (@os == OpenStruct.new(name: "Jonny")).should be_false
+ (@os == OpenStructSpecs::OpenStructSub.new(name: "Jonny")).should be_false
+
+ (@os == OpenStruct.new(name: "John", age: 20)).should be_false
+ (@os == OpenStructSpecs::OpenStructSub.new(name: "John", age: 20)).should be_false
+ end
+end
diff --git a/spec/ruby/library/openstruct/fixtures/classes.rb b/spec/ruby/library/openstruct/fixtures/classes.rb
new file mode 100644
index 0000000000..da42e8511d
--- /dev/null
+++ b/spec/ruby/library/openstruct/fixtures/classes.rb
@@ -0,0 +1,4 @@
+module OpenStructSpecs
+ class OpenStructSub < OpenStruct
+ end
+end
diff --git a/spec/ruby/library/openstruct/frozen_spec.rb b/spec/ruby/library/openstruct/frozen_spec.rb
new file mode 100644
index 0000000000..c14a4bac55
--- /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_error( RuntimeError )
+ end
+
+ it "cannot create new fields" do
+ ->{ @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
+ ->{ 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
+ end
+end
diff --git a/spec/ruby/library/openstruct/initialize_spec.rb b/spec/ruby/library/openstruct/initialize_spec.rb
new file mode 100644
index 0000000000..dee5de48c6
--- /dev/null
+++ b/spec/ruby/library/openstruct/initialize_spec.rb
@@ -0,0 +1,8 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+
+describe "OpenStruct#initialize" do
+ it "is private" do
+ OpenStruct.should have_private_instance_method(:initialize)
+ end
+end
diff --git a/spec/ruby/library/openstruct/inspect_spec.rb b/spec/ruby/library/openstruct/inspect_spec.rb
new file mode 100644
index 0000000000..e2fed41528
--- /dev/null
+++ b/spec/ruby/library/openstruct/inspect_spec.rb
@@ -0,0 +1,8 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+require_relative 'fixtures/classes'
+require_relative 'shared/inspect'
+
+describe "OpenStruct#inspect" do
+ it_behaves_like :ostruct_inspect, :inspect
+end
diff --git a/spec/ruby/library/openstruct/marshal_dump_spec.rb b/spec/ruby/library/openstruct/marshal_dump_spec.rb
new file mode 100644
index 0000000000..5c38fd959e
--- /dev/null
+++ b/spec/ruby/library/openstruct/marshal_dump_spec.rb
@@ -0,0 +1,9 @@
+require_relative '../../spec_helper'
+require "ostruct"
+
+describe "OpenStruct#marshal_dump" do
+ it "returns the method/value table" do
+ os = OpenStruct.new("age" => 20, "name" => "John")
+ os.marshal_dump.should == { age: 20, name: "John" }
+ end
+end
diff --git a/spec/ruby/library/openstruct/marshal_load_spec.rb b/spec/ruby/library/openstruct/marshal_load_spec.rb
new file mode 100644
index 0000000000..342e5e68cd
--- /dev/null
+++ b/spec/ruby/library/openstruct/marshal_load_spec.rb
@@ -0,0 +1,12 @@
+require_relative '../../spec_helper'
+require "ostruct"
+
+describe "OpenStruct#marshal_load when passed [Hash]" do
+ it "defines methods based on the passed Hash" do
+ os = OpenStruct.new
+ os.send :marshal_load, age: 20, name: "John"
+
+ os.age.should eql(20)
+ os.name.should == "John"
+ end
+end
diff --git a/spec/ruby/library/openstruct/method_missing_spec.rb b/spec/ruby/library/openstruct/method_missing_spec.rb
new file mode 100644
index 0000000000..89f83d07b3
--- /dev/null
+++ b/spec/ruby/library/openstruct/method_missing_spec.rb
@@ -0,0 +1,24 @@
+require_relative '../../spec_helper'
+require "ostruct"
+
+describe "OpenStruct#method_missing when called with a method name ending in '='" do
+ before :each do
+ @os = OpenStruct.new
+ end
+
+ it "raises an ArgumentError when not passed any additional arguments" do
+ -> { @os.send(:test=) }.should raise_error(ArgumentError)
+ end
+end
+
+describe "OpenStruct#method_missing when passed additional arguments" do
+ it "raises a NoMethodError when the key does not exist" do
+ os = OpenStruct.new
+ -> { os.test(1, 2, 3) }.should raise_error(NoMethodError)
+ end
+
+ 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/new_spec.rb b/spec/ruby/library/openstruct/new_spec.rb
new file mode 100644
index 0000000000..5d2cacea40
--- /dev/null
+++ b/spec/ruby/library/openstruct/new_spec.rb
@@ -0,0 +1,20 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+
+describe "OpenStruct.new when passed [Hash]" do
+ before :each do
+ @os = OpenStruct.new(name: "John Smith", age: 70, pension: 300)
+ end
+
+ it "creates an attribute for each key of the passed Hash" do
+ @os.age.should eql(70)
+ @os.pension.should eql(300)
+ @os.name.should == "John Smith"
+ end
+end
+
+describe "OpenStruct.new when passed no arguments" do
+ it "returns a new OpenStruct Object without any attributes" do
+ OpenStruct.new.to_s.should == "#<OpenStruct>"
+ end
+end
diff --git a/spec/ruby/library/openstruct/shared/inspect.rb b/spec/ruby/library/openstruct/shared/inspect.rb
new file mode 100644
index 0000000000..ffcd690e1f
--- /dev/null
+++ b/spec/ruby/library/openstruct/shared/inspect.rb
@@ -0,0 +1,20 @@
+describe :ostruct_inspect, shared: true do
+ it "returns a String representation of self" do
+ os = OpenStruct.new(name: "John Smith")
+ os.send(@method).should == "#<OpenStruct name=\"John Smith\">"
+
+ os = OpenStruct.new(age: 20, name: "John Smith")
+ os.send(@method).should be_kind_of(String)
+ end
+
+ it "correctly handles self-referential OpenStructs" do
+ os = OpenStruct.new
+ os.self = os
+ os.send(@method).should == "#<OpenStruct self=#<OpenStruct ...>>"
+ end
+
+ it "correctly handles OpenStruct subclasses" do
+ os = OpenStructSpecs::OpenStructSub.new(name: "John Smith")
+ os.send(@method).should == "#<OpenStructSpecs::OpenStructSub name=\"John Smith\">"
+ end
+end
diff --git a/spec/ruby/library/openstruct/to_h_spec.rb b/spec/ruby/library/openstruct/to_h_spec.rb
new file mode 100644
index 0000000000..6c272bcc71
--- /dev/null
+++ b/spec/ruby/library/openstruct/to_h_spec.rb
@@ -0,0 +1,68 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+
+describe "OpenStruct#to_h" do
+ before :each do
+ @h = {name: "John Smith", age: 70, pension: 300}
+ @os = OpenStruct.new(@h)
+ @to_h = @os.to_h
+ end
+
+ it "returns a Hash with members as keys" do
+ @to_h.should == @h
+ end
+
+ it "returns a Hash with keys as symbols" do
+ os = OpenStruct.new("name" => "John Smith", "age" => 70)
+ os.pension = 300
+ os.to_h.should == @h
+ end
+
+ it "does not return the hash used as initializer" do
+ @to_h.should_not equal(@h)
+ end
+
+ it "returns a Hash that is independent from the struct" 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
diff --git a/spec/ruby/library/openstruct/to_s_spec.rb b/spec/ruby/library/openstruct/to_s_spec.rb
new file mode 100644
index 0000000000..73d91bf981
--- /dev/null
+++ b/spec/ruby/library/openstruct/to_s_spec.rb
@@ -0,0 +1,8 @@
+require_relative '../../spec_helper'
+require 'ostruct'
+require_relative 'fixtures/classes'
+require_relative 'shared/inspect'
+
+describe "OpenStruct#to_s" do
+ it_behaves_like :ostruct_inspect, :to_s
+end