Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 4 additions & 134 deletions core/array/collect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,143 +1,13 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../enumerable/shared/enumeratorized'
require_relative 'shared/iterable_and_tolerating_size_increasing'

describe "Array#collect" do
it "returns a copy of array with each element replaced by the value returned by block" do
a = ['a', 'b', 'c', 'd']
b = a.collect { |i| i + '!' }
b.should == ["a!", "b!", "c!", "d!"]
b.should_not.equal? a
it "is an alias of Array#map" do
Array.instance_method(:collect).should == Array.instance_method(:map)
end

it "does not return subclass instance" do
ArraySpecs::MyArray[1, 2, 3].collect { |x| x + 1 }.should.instance_of?(Array)
end

it "does not change self" do
a = ['a', 'b', 'c', 'd']
a.collect { |i| i + '!' }
a.should == ['a', 'b', 'c', 'd']
end

it "returns the evaluated value of block if it broke in the block" do
a = ['a', 'b', 'c', 'd']
b = a.collect {|i|
if i == 'c'
break 0
else
i + '!'
end
}
b.should == 0
end

it "returns an Enumerator when no block given" do
a = [1, 2, 3]
a.collect.should.instance_of?(Enumerator)
end

it "raises an ArgumentError when no block and with arguments" do
a = [1, 2, 3]
-> {
a.collect(:foo)
}.should.raise(ArgumentError)
end

before :each do
@object = [1, 2, 3, 4]
end
it_behaves_like :enumeratorized_with_origin_size, :collect

it_behaves_like :array_iterable_and_tolerating_size_increasing, :collect
end

describe "Array#collect!" do
it "replaces each element with the value returned by block" do
a = [7, 9, 3, 5]
a.collect! { |i| i - 1 }.should.equal?(a)
a.should == [6, 8, 2, 4]
end

it "returns self" do
a = [1, 2, 3, 4, 5]
b = a.collect! {|i| i+1 }
a.should.equal? b
end

it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do
a = ['a', 'b', 'c', 'd']
b = a.collect! {|i|
if i == 'c'
break 0
else
i + '!'
end
}
b.should == 0
a.should == ['a!', 'b!', 'c', 'd']
it "is an alias of Array#map!" do
Array.instance_method(:collect!).should == Array.instance_method(:map!)
end

it "returns an Enumerator when no block given, and the enumerator can modify the original array" do
a = [1, 2, 3]
enum = a.collect!
enum.should.instance_of?(Enumerator)
enum.each{|i| "#{i}!" }
a.should == ["1!", "2!", "3!"]
end

describe "when frozen" do
it "raises a FrozenError" do
-> { ArraySpecs.frozen_array.collect! {} }.should.raise(FrozenError)
end

it "raises a FrozenError when empty" do
-> { ArraySpecs.empty_frozen_array.collect! {} }.should.raise(FrozenError)
end

it "raises a FrozenError when calling #each on the returned Enumerator" do
enumerator = ArraySpecs.frozen_array.collect!
-> { enumerator.each {|x| x } }.should.raise(FrozenError)
end

it "raises a FrozenError when calling #each on the returned Enumerator when empty" do
enumerator = ArraySpecs.empty_frozen_array.collect!
-> { enumerator.each {|x| x } }.should.raise(FrozenError)
end
end

it "does not truncate the array is the block raises an exception" do
a = [1, 2, 3]
begin
a.collect! { raise StandardError, 'Oops' }
rescue
end

a.should == [1, 2, 3]
end

it "only changes elements before error is raised, keeping the element which raised an error." do
a = [1, 2, 3, 4]
begin
a.collect! do |e|
case e
when 1 then -1
when 2 then -2
when 3 then raise StandardError, 'Oops'
else 0
end
end
rescue StandardError
end

a.should == [-1, -2, 3, 4]
end

before :each do
@object = [1, 2, 3, 4]
end
it_behaves_like :enumeratorized_with_origin_size, :collect!

it_behaves_like :array_iterable_and_tolerating_size_increasing, :collect!
end
11 changes: 2 additions & 9 deletions core/array/length_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#length" do
it "returns the number of elements" do
[].length.should == 0
[1, 2, 3].length.should == 3
end

it "properly handles recursive arrays" do
ArraySpecs.empty_recursive_array.length.should == 1
ArraySpecs.recursive_array.length.should == 8
it "is an alias of Array#size" do
Array.instance_method(:length).should == Array.instance_method(:size)
end
end
138 changes: 134 additions & 4 deletions core/array/map_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,143 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../enumerable/shared/enumeratorized'
require_relative 'shared/iterable_and_tolerating_size_increasing'

describe "Array#map" do
it "is an alias of Array#collect" do
Array.instance_method(:map).should == Array.instance_method(:collect)
it "returns a copy of array with each element replaced by the value returned by block" do
a = ['a', 'b', 'c', 'd']
b = a.map { |i| i + '!' }
b.should == ["a!", "b!", "c!", "d!"]
b.should_not.equal? a
end

it "does not return subclass instance" do
ArraySpecs::MyArray[1, 2, 3].map { |x| x + 1 }.should.instance_of?(Array)
end

it "does not change self" do
a = ['a', 'b', 'c', 'd']
a.map { |i| i + '!' }
a.should == ['a', 'b', 'c', 'd']
end

it "returns the evaluated value of block if it broke in the block" do
a = ['a', 'b', 'c', 'd']
b = a.map {|i|
if i == 'c'
break 0
else
i + '!'
end
}
b.should == 0
end

it "returns an Enumerator when no block given" do
a = [1, 2, 3]
a.map.should.instance_of?(Enumerator)
end

it "raises an ArgumentError when no block and with arguments" do
a = [1, 2, 3]
-> {
a.map(:foo)
}.should.raise(ArgumentError)
end

before :each do
@object = [1, 2, 3, 4]
end
it_behaves_like :enumeratorized_with_origin_size, :map

it_behaves_like :array_iterable_and_tolerating_size_increasing, :map
end

describe "Array#map!" do
it "is an alias of Array#collect!" do
Array.instance_method(:map!).should == Array.instance_method(:collect!)
it "replaces each element with the value returned by block" do
a = [7, 9, 3, 5]
a.map! { |i| i - 1 }.should.equal?(a)
a.should == [6, 8, 2, 4]
end

it "returns self" do
a = [1, 2, 3, 4, 5]
b = a.map! {|i| i+1 }
a.should.equal? b
end

it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do
a = ['a', 'b', 'c', 'd']
b = a.map! {|i|
if i == 'c'
break 0
else
i + '!'
end
}
b.should == 0
a.should == ['a!', 'b!', 'c', 'd']
end

it "returns an Enumerator when no block given, and the enumerator can modify the original array" do
a = [1, 2, 3]
enum = a.map!
enum.should.instance_of?(Enumerator)
enum.each{|i| "#{i}!" }
a.should == ["1!", "2!", "3!"]
end

describe "when frozen" do
it "raises a FrozenError" do
-> { ArraySpecs.frozen_array.map! {} }.should.raise(FrozenError)
end

it "raises a FrozenError when empty" do
-> { ArraySpecs.empty_frozen_array.map! {} }.should.raise(FrozenError)
end

it "raises a FrozenError when calling #each on the returned Enumerator" do
enumerator = ArraySpecs.frozen_array.map!
-> { enumerator.each {|x| x } }.should.raise(FrozenError)
end

it "raises a FrozenError when calling #each on the returned Enumerator when empty" do
enumerator = ArraySpecs.empty_frozen_array.map!
-> { enumerator.each {|x| x } }.should.raise(FrozenError)
end
end

it "does not truncate the array is the block raises an exception" do
a = [1, 2, 3]
begin
a.map! { raise StandardError, 'Oops' }
rescue
end

a.should == [1, 2, 3]
end

it "only changes elements before error is raised, keeping the element which raised an error." do
a = [1, 2, 3, 4]
begin
a.map! do |e|
case e
when 1 then -1
when 2 then -2
when 3 then raise StandardError, 'Oops'
else 0
end
end
rescue StandardError
end

a.should == [-1, -2, 3, 4]
end

before :each do
@object = [1, 2, 3, 4]
end
it_behaves_like :enumeratorized_with_origin_size, :map!

it_behaves_like :array_iterable_and_tolerating_size_increasing, :map!
end
11 changes: 9 additions & 2 deletions core/array/size_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#size" do
it "is an alias of Array#length" do
Array.instance_method(:size).should == Array.instance_method(:length)
it "returns the number of elements" do
[].size.should == 0
[1, 2, 3].size.should == 3
end

it "properly handles recursive arrays" do
ArraySpecs.empty_recursive_array.size.should == 1
ArraySpecs.recursive_array.size.should == 8
end
end
59 changes: 2 additions & 57 deletions core/data/inspect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,7 @@
require_relative 'fixtures/classes'

describe "Data#inspect" do
it "returns a string representation showing members and values" do
a = DataSpecs::Measure.new(42, "km")
a.inspect.should == '#<data DataSpecs::Measure amount=42, unit="km">'
end

it "returns a string representation without the class name for anonymous structs" do
Data.define(:a).new("").inspect.should == '#<data a="">'
end

it "returns a string representation without the class name for structs nested in anonymous classes" do
c = Class.new
c.class_eval <<~DOC
Foo = Data.define(:a)
DOC

c::Foo.new("").inspect.should == '#<data a="">'
end

it "returns a string representation without the class name for structs nested in anonymous modules" do
m = Module.new
m.class_eval <<~DOC
Foo = Data.define(:a)
DOC

m::Foo.new("").inspect.should == '#<data a="">'
end

it "does not call #name method" do
struct = DataSpecs::MeasureWithOverriddenName.new(42, "km")
struct.inspect.should == '#<data DataSpecs::MeasureWithOverriddenName amount=42, unit="km">'
end

it "does not call #name method when struct is anonymous" do
klass = Class.new(DataSpecs::Measure) do
def self.name
"A"
end
end
struct = klass.new(42, "km")
struct.inspect.should == '#<data amount=42, unit="km">'
end

context "recursive structure" do
it "returns string representation with recursive attribute replaced with ..." do
a = DataSpecs::Measure.allocate
a.send(:initialize, amount: 42, unit: a)

a.inspect.should == "#<data DataSpecs::Measure amount=42, unit=#<data DataSpecs::Measure:...>>"
end

it "returns string representation with recursive attribute replaced with ... when an anonymous class" do
klass = Class.new(DataSpecs::Measure)
a = klass.allocate
a.send(:initialize, amount: 42, unit: a)

a.inspect.should =~ /#<data amount=42, unit=#<data #<Class:0x.+?>:\.\.\.>>/
end
it "is an alias of Data#to_s" do
DataSpecs::Measure.instance_method(:inspect).should == DataSpecs::Measure.instance_method(:to_s)
end
end
Loading
Loading