diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/openstruct | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/openstruct')
15 files changed, 276 insertions, 0 deletions
diff --git a/spec/rubyspec/library/openstruct/delete_field_spec.rb b/spec/rubyspec/library/openstruct/delete_field_spec.rb new file mode 100644 index 0000000000..a565f61b92 --- /dev/null +++ b/spec/rubyspec/library/openstruct/delete_field_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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/rubyspec/library/openstruct/element_reference_spec.rb b/spec/rubyspec/library/openstruct/element_reference_spec.rb new file mode 100644 index 0000000000..431843547d --- /dev/null +++ b/spec/rubyspec/library/openstruct/element_reference_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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/rubyspec/library/openstruct/element_set_spec.rb b/spec/rubyspec/library/openstruct/element_set_spec.rb new file mode 100644 index 0000000000..afa65247e4 --- /dev/null +++ b/spec/rubyspec/library/openstruct/element_set_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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/rubyspec/library/openstruct/equal_value_spec.rb b/spec/rubyspec/library/openstruct/equal_value_spec.rb new file mode 100644 index 0000000000..0d2d1d881e --- /dev/null +++ b/spec/rubyspec/library/openstruct/equal_value_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require "ostruct" +require File.expand_path('../fixtures/classes', __FILE__) + +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/rubyspec/library/openstruct/fixtures/classes.rb b/spec/rubyspec/library/openstruct/fixtures/classes.rb new file mode 100644 index 0000000000..da42e8511d --- /dev/null +++ b/spec/rubyspec/library/openstruct/fixtures/classes.rb @@ -0,0 +1,4 @@ +module OpenStructSpecs + class OpenStructSub < OpenStruct + end +end diff --git a/spec/rubyspec/library/openstruct/frozen_spec.rb b/spec/rubyspec/library/openstruct/frozen_spec.rb new file mode 100644 index 0000000000..26dd556e97 --- /dev/null +++ b/spec/rubyspec/library/openstruct/frozen_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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 writeable" do + lambda{ @os.age = 42 }.should raise_error( RuntimeError ) + end + + it "cannot create new fields" do + lambda{ @os.state = :new }.should raise_error( RuntimeError ) + end + + it "creates a frozen clone" do + f = @os.clone + f.age.should == 70 + lambda{ f.age = 0 }.should raise_error( RuntimeError ) + lambda{ f.state = :newer }.should raise_error( RuntimeError ) + end + + it "creates an unfrozen dup" do + d = @os.dup + d.age.should == 70 + d.age = 42 + d.age.should == 42 + end +end diff --git a/spec/rubyspec/library/openstruct/initialize_spec.rb b/spec/rubyspec/library/openstruct/initialize_spec.rb new file mode 100644 index 0000000000..b5edde7618 --- /dev/null +++ b/spec/rubyspec/library/openstruct/initialize_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ostruct' + +describe "OpenStruct#initialize" do + it "is private" do + OpenStruct.should have_private_instance_method(:initialize) + end +end diff --git a/spec/rubyspec/library/openstruct/inspect_spec.rb b/spec/rubyspec/library/openstruct/inspect_spec.rb new file mode 100644 index 0000000000..826437b3c1 --- /dev/null +++ b/spec/rubyspec/library/openstruct/inspect_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ostruct' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/inspect', __FILE__) + +describe "OpenStruct#inspect" do + it_behaves_like :ostruct_inspect, :inspect +end diff --git a/spec/rubyspec/library/openstruct/marshal_dump_spec.rb b/spec/rubyspec/library/openstruct/marshal_dump_spec.rb new file mode 100644 index 0000000000..cdc1564699 --- /dev/null +++ b/spec/rubyspec/library/openstruct/marshal_dump_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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/rubyspec/library/openstruct/marshal_load_spec.rb b/spec/rubyspec/library/openstruct/marshal_load_spec.rb new file mode 100644 index 0000000000..9c89697d8f --- /dev/null +++ b/spec/rubyspec/library/openstruct/marshal_load_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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.age.should eql(20) + os.name.should == "John" + end +end diff --git a/spec/rubyspec/library/openstruct/method_missing_spec.rb b/spec/rubyspec/library/openstruct/method_missing_spec.rb new file mode 100644 index 0000000000..6051fd48d8 --- /dev/null +++ b/spec/rubyspec/library/openstruct/method_missing_spec.rb @@ -0,0 +1,47 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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 + 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" + end +end + +describe "OpenStruct#method_missing when passed additional arguments" do + it "raises a NoMethodError" do + os = OpenStruct.new + lambda { os.method_missing(: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 + end +end diff --git a/spec/rubyspec/library/openstruct/new_spec.rb b/spec/rubyspec/library/openstruct/new_spec.rb new file mode 100644 index 0000000000..ce33634e08 --- /dev/null +++ b/spec/rubyspec/library/openstruct/new_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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/rubyspec/library/openstruct/shared/inspect.rb b/spec/rubyspec/library/openstruct/shared/inspect.rb new file mode 100644 index 0000000000..ffcd690e1f --- /dev/null +++ b/spec/rubyspec/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/rubyspec/library/openstruct/to_h_spec.rb b/spec/rubyspec/library/openstruct/to_h_spec.rb new file mode 100644 index 0000000000..f1bffd6fa6 --- /dev/null +++ b/spec/rubyspec/library/openstruct/to_h_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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 +end diff --git a/spec/rubyspec/library/openstruct/to_s_spec.rb b/spec/rubyspec/library/openstruct/to_s_spec.rb new file mode 100644 index 0000000000..8efa3f5aaf --- /dev/null +++ b/spec/rubyspec/library/openstruct/to_s_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ostruct' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/inspect', __FILE__) + +describe "OpenStruct#to_s" do + it_behaves_like :ostruct_inspect, :to_s +end |