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.rb2
-rw-r--r--spec/ruby/library/openstruct/element_reference_spec.rb2
-rw-r--r--spec/ruby/library/openstruct/element_set_spec.rb2
-rw-r--r--spec/ruby/library/openstruct/equal_value_spec.rb4
-rw-r--r--spec/ruby/library/openstruct/frozen_spec.rb14
-rw-r--r--spec/ruby/library/openstruct/initialize_spec.rb2
-rw-r--r--spec/ruby/library/openstruct/inspect_spec.rb6
-rw-r--r--spec/ruby/library/openstruct/marshal_dump_spec.rb2
-rw-r--r--spec/ruby/library/openstruct/marshal_load_spec.rb4
-rw-r--r--spec/ruby/library/openstruct/method_missing_spec.rb37
-rw-r--r--spec/ruby/library/openstruct/new_spec.rb2
-rw-r--r--spec/ruby/library/openstruct/to_h_spec.rb41
-rw-r--r--spec/ruby/library/openstruct/to_s_spec.rb6
13 files changed, 71 insertions, 53 deletions
diff --git a/spec/ruby/library/openstruct/delete_field_spec.rb b/spec/ruby/library/openstruct/delete_field_spec.rb
index a565f61b92..9ac80196cc 100644
--- a/spec/ruby/library/openstruct/delete_field_spec.rb
+++ b/spec/ruby/library/openstruct/delete_field_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
describe "OpenStruct#delete_field" do
diff --git a/spec/ruby/library/openstruct/element_reference_spec.rb b/spec/ruby/library/openstruct/element_reference_spec.rb
index 431843547d..c425991b0f 100644
--- a/spec/ruby/library/openstruct/element_reference_spec.rb
+++ b/spec/ruby/library/openstruct/element_reference_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require "ostruct"
describe "OpenStruct#[]" do
diff --git a/spec/ruby/library/openstruct/element_set_spec.rb b/spec/ruby/library/openstruct/element_set_spec.rb
index afa65247e4..eeb5a8b318 100644
--- a/spec/ruby/library/openstruct/element_set_spec.rb
+++ b/spec/ruby/library/openstruct/element_set_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require "ostruct"
describe "OpenStruct#[]=" do
diff --git a/spec/ruby/library/openstruct/equal_value_spec.rb b/spec/ruby/library/openstruct/equal_value_spec.rb
index 0d2d1d881e..103ac13588 100644
--- a/spec/ruby/library/openstruct/equal_value_spec.rb
+++ b/spec/ruby/library/openstruct/equal_value_spec.rb
@@ -1,6 +1,6 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require "ostruct"
-require File.expand_path('../fixtures/classes', __FILE__)
+require_relative 'fixtures/classes'
describe "OpenStruct#==" do
before :each do
diff --git a/spec/ruby/library/openstruct/frozen_spec.rb b/spec/ruby/library/openstruct/frozen_spec.rb
index 26dd556e97..c14a4bac55 100644
--- a/spec/ruby/library/openstruct/frozen_spec.rb
+++ b/spec/ruby/library/openstruct/frozen_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
describe "OpenStruct.new when frozen" do
@@ -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/initialize_spec.rb b/spec/ruby/library/openstruct/initialize_spec.rb
index b5edde7618..dee5de48c6 100644
--- a/spec/ruby/library/openstruct/initialize_spec.rb
+++ b/spec/ruby/library/openstruct/initialize_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
describe "OpenStruct#initialize" do
diff --git a/spec/ruby/library/openstruct/inspect_spec.rb b/spec/ruby/library/openstruct/inspect_spec.rb
index 826437b3c1..e2fed41528 100644
--- a/spec/ruby/library/openstruct/inspect_spec.rb
+++ b/spec/ruby/library/openstruct/inspect_spec.rb
@@ -1,7 +1,7 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
-require File.expand_path('../fixtures/classes', __FILE__)
-require File.expand_path('../shared/inspect', __FILE__)
+require_relative 'fixtures/classes'
+require_relative 'shared/inspect'
describe "OpenStruct#inspect" do
it_behaves_like :ostruct_inspect, :inspect
diff --git a/spec/ruby/library/openstruct/marshal_dump_spec.rb b/spec/ruby/library/openstruct/marshal_dump_spec.rb
index cdc1564699..5c38fd959e 100644
--- a/spec/ruby/library/openstruct/marshal_dump_spec.rb
+++ b/spec/ruby/library/openstruct/marshal_dump_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require "ostruct"
describe "OpenStruct#marshal_dump" do
diff --git a/spec/ruby/library/openstruct/marshal_load_spec.rb b/spec/ruby/library/openstruct/marshal_load_spec.rb
index 9c89697d8f..342e5e68cd 100644
--- a/spec/ruby/library/openstruct/marshal_load_spec.rb
+++ b/spec/ruby/library/openstruct/marshal_load_spec.rb
@@ -1,10 +1,10 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+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.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 6051fd48d8..89f83d07b3 100644
--- a/spec/ruby/library/openstruct/method_missing_spec.rb
+++ b/spec/ruby/library/openstruct/method_missing_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require "ostruct"
describe "OpenStruct#method_missing when called with a method name ending in '='" do
@@ -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/new_spec.rb b/spec/ruby/library/openstruct/new_spec.rb
index ce33634e08..5d2cacea40 100644
--- a/spec/ruby/library/openstruct/new_spec.rb
+++ b/spec/ruby/library/openstruct/new_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
describe "OpenStruct.new when passed [Hash]" do
diff --git a/spec/ruby/library/openstruct/to_h_spec.rb b/spec/ruby/library/openstruct/to_h_spec.rb
index f1bffd6fa6..6c272bcc71 100644
--- a/spec/ruby/library/openstruct/to_h_spec.rb
+++ b/spec/ruby/library/openstruct/to_h_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
describe "OpenStruct#to_h" do
@@ -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
diff --git a/spec/ruby/library/openstruct/to_s_spec.rb b/spec/ruby/library/openstruct/to_s_spec.rb
index 8efa3f5aaf..73d91bf981 100644
--- a/spec/ruby/library/openstruct/to_s_spec.rb
+++ b/spec/ruby/library/openstruct/to_s_spec.rb
@@ -1,7 +1,7 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
require 'ostruct'
-require File.expand_path('../fixtures/classes', __FILE__)
-require File.expand_path('../shared/inspect', __FILE__)
+require_relative 'fixtures/classes'
+require_relative 'shared/inspect'
describe "OpenStruct#to_s" do
it_behaves_like :ostruct_inspect, :to_s