diff options
Diffstat (limited to 'spec/ruby/core/data')
-rw-r--r-- | spec/ruby/core/data/constants_spec.rb | 22 | ||||
-rw-r--r-- | spec/ruby/core/data/define_spec.rb | 36 | ||||
-rw-r--r-- | spec/ruby/core/data/fixtures/classes.rb | 5 | ||||
-rw-r--r-- | spec/ruby/core/data/initialize_spec.rb | 65 | ||||
-rw-r--r-- | spec/ruby/core/data/with_spec.rb | 35 |
5 files changed, 151 insertions, 12 deletions
diff --git a/spec/ruby/core/data/constants_spec.rb b/spec/ruby/core/data/constants_spec.rb index 1d469f9237..2eb43d501e 100644 --- a/spec/ruby/core/data/constants_spec.rb +++ b/spec/ruby/core/data/constants_spec.rb @@ -1,23 +1,21 @@ require_relative '../../spec_helper' -ruby_version_is ''...'3.0' do +ruby_version_is ''...'3.2' do describe "Data" do - it "is a subclass of Object" do - suppress_warning do - Data.superclass.should == Object - end - end - - it "is deprecated" do - -> { Data }.should complain(/constant ::Data is deprecated/) + it "does not exist anymore" do + Object.should_not have_constant(:Data) end end end -ruby_version_is '3.0' do +ruby_version_is '3.2' do describe "Data" do - it "does not exist anymore" do - Object.should_not have_constant(:Data) + it "is a new constant" do + Data.superclass.should == Object + end + + it "is not deprecated" do + -> { Data }.should_not complain end end end diff --git a/spec/ruby/core/data/define_spec.rb b/spec/ruby/core/data/define_spec.rb new file mode 100644 index 0000000000..2aa2c50d4c --- /dev/null +++ b/spec/ruby/core/data/define_spec.rb @@ -0,0 +1,36 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +ruby_version_is "3.2" do + describe "Data.define" do + it "accepts no arguments" do + empty_data = Data.define + empty_data.members.should == [] + end + + it "accepts symbols" do + movie = Data.define(:title, :year) + movie.members.should == [:title, :year] + end + + it "accepts strings" do + movie = Data.define("title", "year") + movie.members.should == [:title, :year] + end + + it "accepts a mix of strings and symbols" do + movie = Data.define("title", :year, "genre") + movie.members.should == [:title, :year, :genre] + end + + it "accepts a block" do + movie = Data.define(:title, :year) do + def title_with_year + "#{title} (#{year})" + end + end + movie.members.should == [:title, :year] + movie.new("Matrix", 1999).title_with_year.should == "Matrix (1999)" + end + end +end diff --git a/spec/ruby/core/data/fixtures/classes.rb b/spec/ruby/core/data/fixtures/classes.rb new file mode 100644 index 0000000000..46a6b48bb2 --- /dev/null +++ b/spec/ruby/core/data/fixtures/classes.rb @@ -0,0 +1,5 @@ +module DataSpecs + guard -> { ruby_version_is "3.2" and Data.respond_to?(:define) } do + Measure = Data.define(:amount, :unit) + end +end diff --git a/spec/ruby/core/data/initialize_spec.rb b/spec/ruby/core/data/initialize_spec.rb new file mode 100644 index 0000000000..2c36bd3ac4 --- /dev/null +++ b/spec/ruby/core/data/initialize_spec.rb @@ -0,0 +1,65 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +ruby_version_is "3.2" do + describe "Data#initialize" do + it "accepts positional arguments" do + data = DataSpecs::Measure.new(42, "km") + + data.amount.should == 42 + data.unit.should == "km" + end + + it "accepts alternative positional arguments" do + data = DataSpecs::Measure[42, "km"] + + data.amount.should == 42 + data.unit.should == "km" + end + + it "accepts keyword arguments" do + data = DataSpecs::Measure.new(amount: 42, unit: "km") + + data.amount.should == 42 + data.unit.should == "km" + end + + it "accepts alternative keyword arguments" do + data = DataSpecs::Measure[amount: 42, unit: "km"] + + data.amount.should == 42 + data.unit.should == "km" + end + + it "accepts String keyword arguments" do + data = DataSpecs::Measure.new("amount" => 42, "unit" => "km") + + data.amount.should == 42 + data.unit.should == "km" + end + + it "raises ArgumentError if no arguments are given" do + -> { + DataSpecs::Measure.new + }.should raise_error(ArgumentError) { |e| + e.message.should.include?("missing keywords: :amount, :unit") + } + end + + it "raises ArgumentError if at least one argument is missing" do + -> { + DataSpecs::Measure.new(unit: "km") + }.should raise_error(ArgumentError) { |e| + e.message.should.include?("missing keyword: :amount") + } + end + + it "raises ArgumentError if unknown keyword is given" do + -> { + DataSpecs::Measure.new(amount: 42, unit: "km", system: "metric") + }.should raise_error(ArgumentError) { |e| + e.message.should.include?("unknown keyword: :system") + } + end + end +end diff --git a/spec/ruby/core/data/with_spec.rb b/spec/ruby/core/data/with_spec.rb new file mode 100644 index 0000000000..97e34c951f --- /dev/null +++ b/spec/ruby/core/data/with_spec.rb @@ -0,0 +1,35 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +ruby_version_is "3.2" do + describe "Data#with" do + it "returns self if given no arguments" do + data = DataSpecs::Measure.new(amount: 42, unit: "km") + data = data.with.should.equal?(data) + end + + it "accepts keyword arguments" do + data = DataSpecs::Measure.new(amount: 42, unit: "km") + data = data.with(amount: 4, unit: "m") + + data.amount.should == 4 + data.unit.should == "m" + end + + it "accepts String keyword arguments" do + data = DataSpecs::Measure.new(amount: 42, unit: "km") + data = data.with("amount" => 4, "unit" => "m") + + data.amount.should == 4 + data.unit.should == "m" + end + + it "raises ArgumentError if no keyword arguments are given" do + data = DataSpecs::Measure.new(amount: 42, unit: "km") + + -> { + data.with(4, "m") + }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)") + end + end +end |