diff --git a/core/array/collect_spec.rb b/core/array/collect_spec.rb index 43a539f80..bdee5c240 100644 --- a/core/array/collect_spec.rb +++ b/core/array/collect_spec.rb @@ -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 diff --git a/core/array/length_spec.rb b/core/array/length_spec.rb index 74b2eb3a0..e45abb213 100644 --- a/core/array/length_spec.rb +++ b/core/array/length_spec.rb @@ -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 diff --git a/core/array/map_spec.rb b/core/array/map_spec.rb index f5e88c862..0cc394663 100644 --- a/core/array/map_spec.rb +++ b/core/array/map_spec.rb @@ -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 diff --git a/core/array/size_spec.rb b/core/array/size_spec.rb index 83e896901..710754ed6 100644 --- a/core/array/size_spec.rb +++ b/core/array/size_spec.rb @@ -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 diff --git a/core/data/inspect_spec.rb b/core/data/inspect_spec.rb index 6c97a719e..e5b9689ca 100644 --- a/core/data/inspect_spec.rb +++ b/core/data/inspect_spec.rb @@ -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 == '#' - end - - it "returns a string representation without the class name for anonymous structs" do - Data.define(:a).new("").inspect.should == '#' - 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 == '#' - 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 == '#' - end - - it "does not call #name method" do - struct = DataSpecs::MeasureWithOverriddenName.new(42, "km") - struct.inspect.should == '#' - 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 == '#' - 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 == "#>" - 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 =~ /#:\.\.\.>>/ - end + it "is an alias of Data#to_s" do + DataSpecs::Measure.instance_method(:inspect).should == DataSpecs::Measure.instance_method(:to_s) end end diff --git a/core/data/to_s_spec.rb b/core/data/to_s_spec.rb index a552e4659..e436c2110 100644 --- a/core/data/to_s_spec.rb +++ b/core/data/to_s_spec.rb @@ -2,8 +2,62 @@ require_relative 'fixtures/classes' describe "Data#to_s" do - it "is an alias of Data#inspect" do + it "returns a string representation showing members and values" do a = DataSpecs::Measure.new(42, "km") - a.method(:to_s).should == a.method(:inspect) + a.to_s.should == '#' + end + + it "returns a string representation without the class name for anonymous structs" do + Data.define(:a).new("").to_s.should == '#' + 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("").to_s.should == '#' + 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("").to_s.should == '#' + end + + it "does not call #name method" do + struct = DataSpecs::MeasureWithOverriddenName.new(42, "km") + struct.to_s.should == '#' + 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.to_s.should == '#' + 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.to_s.should == "#>" + 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.to_s.should =~ /#:\.\.\.>>/ + end end end diff --git a/core/dir/path_spec.rb b/core/dir/path_spec.rb index 02ddd2cd4..e506db122 100644 --- a/core/dir/path_spec.rb +++ b/core/dir/path_spec.rb @@ -1,37 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/common' describe "Dir#path" do - before :all do - DirSpecs.create_mock_dirs - end - - after :all do - DirSpecs.delete_mock_dirs - end - - it "returns the path that was supplied to .new or .open" do - dir = Dir.open DirSpecs.mock_dir - begin - dir.path.should == DirSpecs.mock_dir - ensure - dir.close rescue nil - end - end - - it "returns the path even when called on a closed Dir instance" do - dir = Dir.open DirSpecs.mock_dir - dir.close - dir.path.should == DirSpecs.mock_dir - end - - it "returns a String with the same encoding as the argument to .open" do - path = DirSpecs.mock_dir.force_encoding Encoding::IBM866 - dir = Dir.open path - begin - dir.path.encoding.should.equal?(Encoding::IBM866) - ensure - dir.close - end + it "is an alias of Dir#to_path" do + Dir.instance_method(:path).should == Dir.instance_method(:to_path) end end diff --git a/core/dir/pos_spec.rb b/core/dir/pos_spec.rb index f8ca06d77..1e364fbe3 100644 --- a/core/dir/pos_spec.rb +++ b/core/dir/pos_spec.rb @@ -1,10 +1,42 @@ require_relative '../../spec_helper' require_relative 'fixtures/common' +require_relative 'shared/closed' require_relative 'shared/pos' describe "Dir#pos" do - it "is an alias of Dir#tell" do - Dir.instance_method(:pos).should == Dir.instance_method(:tell) + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_closed, :pos + + before :each do + @dir = Dir.open DirSpecs.mock_dir + end + + after :each do + @dir.close rescue nil + end + + it "returns an Integer representing the current position in the directory" do + @dir.pos.should.is_a?(Integer) + @dir.pos.should.is_a?(Integer) + @dir.pos.should.is_a?(Integer) + end + + it "returns a different Integer if moved from previous position" do + a = @dir.pos + @dir.read + b = @dir.pos + + a.should.is_a?(Integer) + b.should.is_a?(Integer) + + a.should_not == b end end diff --git a/core/dir/tell_spec.rb b/core/dir/tell_spec.rb index dcbb40438..04f92a8ad 100644 --- a/core/dir/tell_spec.rb +++ b/core/dir/tell_spec.rb @@ -1,41 +1,9 @@ require_relative '../../spec_helper' require_relative 'fixtures/common' -require_relative 'shared/closed' require_relative 'shared/pos' describe "Dir#tell" do - before :all do - DirSpecs.create_mock_dirs - end - - after :all do - DirSpecs.delete_mock_dirs - end - - it_behaves_like :dir_closed, :tell - - before :each do - @dir = Dir.open DirSpecs.mock_dir - end - - after :each do - @dir.close rescue nil - end - - it "returns an Integer representing the current position in the directory" do - @dir.tell.should.is_a?(Integer) - @dir.tell.should.is_a?(Integer) - @dir.tell.should.is_a?(Integer) - end - - it "returns a different Integer if moved from previous position" do - a = @dir.tell - @dir.read - b = @dir.tell - - a.should.is_a?(Integer) - b.should.is_a?(Integer) - - a.should_not == b + it "is an alias of Dir#pos" do + Dir.instance_method(:tell).should == Dir.instance_method(:pos) end end diff --git a/core/dir/to_path_spec.rb b/core/dir/to_path_spec.rb index 2ed533e75..43e349c50 100644 --- a/core/dir/to_path_spec.rb +++ b/core/dir/to_path_spec.rb @@ -1,7 +1,37 @@ require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir#to_path" do - it "is an alias of Dir#path" do - Dir.instance_method(:to_path).should == Dir.instance_method(:path) + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it "returns the to_path that was supplied to .new or .open" do + dir = Dir.open DirSpecs.mock_dir + begin + dir.to_path.should == DirSpecs.mock_dir + ensure + dir.close rescue nil + end + end + + it "returns the to_path even when called on a closed Dir instance" do + dir = Dir.open DirSpecs.mock_dir + dir.close + dir.to_path.should == DirSpecs.mock_dir + end + + it "returns a String with the same encoding as the argument to .open" do + to_path = DirSpecs.mock_dir.force_encoding Encoding::IBM866 + dir = Dir.open to_path + begin + dir.to_path.encoding.should.equal?(Encoding::IBM866) + ensure + dir.close + end end end diff --git a/core/hash/has_value_spec.rb b/core/hash/has_value_spec.rb index d40e52ebf..95b8390a6 100644 --- a/core/hash/has_value_spec.rb +++ b/core/hash/has_value_spec.rb @@ -1,16 +1,7 @@ require_relative '../../spec_helper' describe "Hash#has_value?" do - it "returns true if the value exists in the hash" do - { a: :b }.has_value?(:a).should == false - { 1 => 2 }.has_value?(2).should == true - h = Hash.new(5) - h.has_value?(5).should == false - h = Hash.new { 5 } - h.has_value?(5).should == false - end - - it "uses == semantics for comparing values" do - { 5 => 2.0 }.has_value?(2).should == true + it "is an alias of Hash#value?" do + Hash.instance_method(:has_value?).should == Hash.instance_method(:value?) end end diff --git a/core/hash/merge_spec.rb b/core/hash/merge_spec.rb index 9e566fcee..7a444f7f2 100644 --- a/core/hash/merge_spec.rb +++ b/core/hash/merge_spec.rb @@ -117,7 +117,78 @@ end describe "Hash#merge!" do - it "is an alias of Hash#update" do - Hash.instance_method(:merge!).should == Hash.instance_method(:update) + it "adds the entries from other, overwriting duplicate keys. Returns self" do + h = { _1: 'a', _2: '3' } + h.merge!(_1: '9', _9: 2).should.equal?(h) + h.should == { _1: "9", _2: "3", _9: 2 } + end + + it "sets any duplicate key to the value of block if passed a block" do + h1 = { a: 2, b: -1 } + h2 = { a: -2, c: 1 } + h1.merge!(h2) { |k,x,y| 3.14 }.should.equal?(h1) + h1.should == { c: 1, b: -1, a: 3.14 } + + h1.merge!(h1) { nil } + h1.should == { a: nil, b: nil, c: nil } + end + + it "tries to convert the passed argument to a hash using #to_hash" do + obj = mock('{1=>2}') + obj.should_receive(:to_hash).and_return({ 1 => 2 }) + { 3 => 4 }.merge!(obj).should == { 1 => 2, 3 => 4 } + end + + it "does not call to_hash on hash subclasses" do + { 3 => 4 }.merge!(HashSpecs::ToHashHash[1 => 2]).should == { 1 => 2, 3 => 4 } + end + + it "processes entries with same order as merge()" do + h = { 1 => 2, 3 => 4, 5 => 6, "x" => nil, nil => 5, [] => [] } + merge_bang_pairs = [] + merge_pairs = [] + h.merge(h) { |*arg| merge_pairs << arg } + h.merge!(h) { |*arg| merge_bang_pairs << arg } + merge_bang_pairs.should == merge_pairs + end + + it "raises a FrozenError on a frozen instance that is modified" do + -> do + HashSpecs.frozen_hash.merge!(1 => 2) + end.should.raise(FrozenError) + end + + it "checks frozen status before coercing an object with #to_hash" do + obj = mock("to_hash frozen") + # This is necessary because mock cleanup code cannot run on the frozen + # object. + def obj.to_hash() raise Exception, "should not receive #to_hash" end + obj.freeze + + -> { HashSpecs.frozen_hash.merge!(obj) }.should.raise(FrozenError) + end + + # see redmine #1571 + it "raises a FrozenError on a frozen instance that would not be modified" do + -> do + HashSpecs.frozen_hash.merge!(HashSpecs.empty_frozen_hash) + end.should.raise(FrozenError) + end + + it "does not raise an exception if changing the value of an existing key during iteration" do + hash = {1 => 2, 3 => 4, 5 => 6} + hash2 = {1 => :foo, 3 => :bar} + hash.each { hash.merge!(hash2) } + hash.should == {1 => :foo, 3 => :bar, 5 => 6} + end + + it "accepts multiple hashes" do + result = { a: 1 }.merge!({ b: 2 }, { c: 3 }, { d: 4 }) + result.should == { a: 1, b: 2, c: 3, d: 4 } + end + + it "accepts zero arguments" do + hash = { a: 1 } + hash.merge!.should.eql?(hash) end end diff --git a/core/hash/update_spec.rb b/core/hash/update_spec.rb index f3a3e6b4d..04070baad 100644 --- a/core/hash/update_spec.rb +++ b/core/hash/update_spec.rb @@ -1,79 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' describe "Hash#update" do - it "adds the entries from other, overwriting duplicate keys. Returns self" do - h = { _1: 'a', _2: '3' } - h.update(_1: '9', _9: 2).should.equal?(h) - h.should == { _1: "9", _2: "3", _9: 2 } - end - - it "sets any duplicate key to the value of block if passed a block" do - h1 = { a: 2, b: -1 } - h2 = { a: -2, c: 1 } - h1.update(h2) { |k,x,y| 3.14 }.should.equal?(h1) - h1.should == { c: 1, b: -1, a: 3.14 } - - h1.update(h1) { nil } - h1.should == { a: nil, b: nil, c: nil } - end - - it "tries to convert the passed argument to a hash using #to_hash" do - obj = mock('{1=>2}') - obj.should_receive(:to_hash).and_return({ 1 => 2 }) - { 3 => 4 }.update(obj).should == { 1 => 2, 3 => 4 } - end - - it "does not call to_hash on hash subclasses" do - { 3 => 4 }.update(HashSpecs::ToHashHash[1 => 2]).should == { 1 => 2, 3 => 4 } - end - - it "processes entries with same order as merge()" do - h = { 1 => 2, 3 => 4, 5 => 6, "x" => nil, nil => 5, [] => [] } - merge_bang_pairs = [] - merge_pairs = [] - h.merge(h) { |*arg| merge_pairs << arg } - h.update(h) { |*arg| merge_bang_pairs << arg } - merge_bang_pairs.should == merge_pairs - end - - it "raises a FrozenError on a frozen instance that is modified" do - -> do - HashSpecs.frozen_hash.update(1 => 2) - end.should.raise(FrozenError) - end - - it "checks frozen status before coercing an object with #to_hash" do - obj = mock("to_hash frozen") - # This is necessary because mock cleanup code cannot run on the frozen - # object. - def obj.to_hash() raise Exception, "should not receive #to_hash" end - obj.freeze - - -> { HashSpecs.frozen_hash.update(obj) }.should.raise(FrozenError) - end - - # see redmine #1571 - it "raises a FrozenError on a frozen instance that would not be modified" do - -> do - HashSpecs.frozen_hash.update(HashSpecs.empty_frozen_hash) - end.should.raise(FrozenError) - end - - it "does not raise an exception if changing the value of an existing key during iteration" do - hash = {1 => 2, 3 => 4, 5 => 6} - hash2 = {1 => :foo, 3 => :bar} - hash.each { hash.update(hash2) } - hash.should == {1 => :foo, 3 => :bar, 5 => 6} - end - - it "accepts multiple hashes" do - result = { a: 1 }.update({ b: 2 }, { c: 3 }, { d: 4 }) - result.should == { a: 1, b: 2, c: 3, d: 4 } - end - - it "accepts zero arguments" do - hash = { a: 1 } - hash.update.should.eql?(hash) + it "is an alias of Hash#merge!" do + Hash.instance_method(:update).should == Hash.instance_method(:merge!) end end diff --git a/core/hash/value_spec.rb b/core/hash/value_spec.rb index 9cfbe576d..8e4732480 100644 --- a/core/hash/value_spec.rb +++ b/core/hash/value_spec.rb @@ -1,7 +1,16 @@ require_relative '../../spec_helper' describe "Hash#value?" do - it "is an alias of Hash#has_value?" do - Hash.instance_method(:value?).should == Hash.instance_method(:has_value?) + it "returns true if the value exists in the hash" do + { a: :b }.value?(:a).should == false + { 1 => 2 }.value?(2).should == true + h = Hash.new(5) + h.value?(5).should == false + h = Hash.new { 5 } + h.value?(5).should == false + end + + it "uses == semantics for comparing values" do + { 5 => 2.0 }.value?(2).should == true end end diff --git a/core/matchdata/eql_spec.rb b/core/matchdata/eql_spec.rb index bc7123b05..937c4424a 100644 --- a/core/matchdata/eql_spec.rb +++ b/core/matchdata/eql_spec.rb @@ -1,26 +1,7 @@ require_relative '../../spec_helper' describe "MatchData#eql?" do - it "returns true if both operands have equal target strings, patterns, and match positions" do - a = 'haystack'.match(/hay/) - b = 'haystack'.match(/hay/) - a.eql?(b).should == true - end - - it "returns false if the operands have different target strings" do - a = 'hay'.match(/hay/) - b = 'haystack'.match(/hay/) - a.eql?(b).should == false - end - - it "returns false if the operands have different patterns" do - a = 'haystack'.match(/h.y/) - b = 'haystack'.match(/hay/) - a.eql?(b).should == false - end - - it "returns false if the argument is not a MatchData object" do - a = 'haystack'.match(/hay/) - a.eql?(Object.new).should == false + it "is an alias of MatchData#==" do + MatchData.instance_method(:eql?).should == MatchData.instance_method(:==) end end diff --git a/core/matchdata/equal_value_spec.rb b/core/matchdata/equal_value_spec.rb index e3abb59a1..74d3a5adc 100644 --- a/core/matchdata/equal_value_spec.rb +++ b/core/matchdata/equal_value_spec.rb @@ -1,7 +1,26 @@ require_relative '../../spec_helper' describe "MatchData#==" do - it "is an alias of MatchData#eql?" do - MatchData.instance_method(:==).should == MatchData.instance_method(:eql?) + it "returns true if both operands have equal target strings, patterns, and match positions" do + a = 'haystack'.match(/hay/) + b = 'haystack'.match(/hay/) + a.==(b).should == true + end + + it "returns false if the operands have different target strings" do + a = 'hay'.match(/hay/) + b = 'haystack'.match(/hay/) + a.==(b).should == false + end + + it "returns false if the operands have different patterns" do + a = 'haystack'.match(/h.y/) + b = 'haystack'.match(/hay/) + a.==(b).should == false + end + + it "returns false if the argument is not a MatchData object" do + a = 'haystack'.match(/hay/) + a.==(Object.new).should == false end end diff --git a/core/time/asctime_spec.rb b/core/time/asctime_spec.rb index d62565c98..17e155787 100644 --- a/core/time/asctime_spec.rb +++ b/core/time/asctime_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' describe "Time#asctime" do - it "is an alias of Time#ctime" do - Time.instance_method(:asctime).should == Time.instance_method(:ctime) + it "returns a canonical string representation of time" do + t = Time.now + t.asctime.should == t.strftime("%a %b %e %H:%M:%S %Y") end end diff --git a/core/time/ctime_spec.rb b/core/time/ctime_spec.rb index a99dbcc3d..b609b0397 100644 --- a/core/time/ctime_spec.rb +++ b/core/time/ctime_spec.rb @@ -1,8 +1,7 @@ require_relative '../../spec_helper' describe "Time#ctime" do - it "returns a canonical string representation of time" do - t = Time.now - t.ctime.should == t.strftime("%a %b %e %H:%M:%S %Y") + it "is an alias of Time#asctime" do + Time.instance_method(:ctime).should == Time.instance_method(:asctime) end end diff --git a/core/time/iso8601_spec.rb b/core/time/iso8601_spec.rb index cfd44a24d..a6efc57b2 100644 --- a/core/time/iso8601_spec.rb +++ b/core/time/iso8601_spec.rb @@ -2,8 +2,32 @@ describe "Time#iso8601" do ruby_version_is "3.4" do - it "is an alias of Time#xmlschema" do - Time.instance_method(:iso8601).should == Time.instance_method(:xmlschema) + it "generates ISO-8601 strings in Z for UTC times" do + t = Time.utc(1985, 4, 12, 23, 20, 50, 521245) + t.iso8601.should == "1985-04-12T23:20:50Z" + t.iso8601(2).should == "1985-04-12T23:20:50.52Z" + t.iso8601(9).should == "1985-04-12T23:20:50.521245000Z" + end + + it "generates ISO-8601 string with timezone offset for non-UTC times" do + t = Time.new(1985, 4, 12, 23, 20, 50, "+02:00") + t.iso8601.should == "1985-04-12T23:20:50+02:00" + t.iso8601(2).should == "1985-04-12T23:20:50.00+02:00" + end + + it "year is always at least 4 digits" do + t = Time.utc(12, 4, 12) + t.iso8601.should == "0012-04-12T00:00:00Z" + end + + it "year can be more than 4 digits" do + t = Time.utc(40_000, 4, 12) + t.iso8601.should == "40000-04-12T00:00:00Z" + end + + it "year can be negative" do + t = Time.utc(-2000, 4, 12) + t.iso8601.should == "-2000-04-12T00:00:00Z" end end end diff --git a/core/time/xmlschema_spec.rb b/core/time/xmlschema_spec.rb index 026b795f5..6a26861a4 100644 --- a/core/time/xmlschema_spec.rb +++ b/core/time/xmlschema_spec.rb @@ -2,32 +2,8 @@ describe "Time#xmlschema" do ruby_version_is "3.4" do - it "generates ISO-8601 strings in Z for UTC times" do - t = Time.utc(1985, 4, 12, 23, 20, 50, 521245) - t.xmlschema.should == "1985-04-12T23:20:50Z" - t.xmlschema(2).should == "1985-04-12T23:20:50.52Z" - t.xmlschema(9).should == "1985-04-12T23:20:50.521245000Z" - end - - it "generates ISO-8601 string with timezone offset for non-UTC times" do - t = Time.new(1985, 4, 12, 23, 20, 50, "+02:00") - t.xmlschema.should == "1985-04-12T23:20:50+02:00" - t.xmlschema(2).should == "1985-04-12T23:20:50.00+02:00" - end - - it "year is always at least 4 digits" do - t = Time.utc(12, 4, 12) - t.xmlschema.should == "0012-04-12T00:00:00Z" - end - - it "year can be more than 4 digits" do - t = Time.utc(40_000, 4, 12) - t.xmlschema.should == "40000-04-12T00:00:00Z" - end - - it "year can be negative" do - t = Time.utc(-2000, 4, 12) - t.xmlschema.should == "-2000-04-12T00:00:00Z" + it "is an alias of Time#iso8601" do + Time.instance_method(:xmlschema).should == Time.instance_method(:iso8601) end end end diff --git a/library/date/asctime_spec.rb b/library/date/asctime_spec.rb index 67d158cc0..e268ffe09 100644 --- a/library/date/asctime_spec.rb +++ b/library/date/asctime_spec.rb @@ -2,5 +2,8 @@ require 'date' describe "Date#asctime" do - it "needs to be reviewed for spec completeness" + it "returns a canonical string representation of date" do + d = Date.today + d.asctime.should == d.strftime("%a %b %e %H:%M:%S %Y") + end end diff --git a/library/datetime/iso8601_spec.rb b/library/datetime/iso8601_spec.rb index 457881277..4368300fd 100644 --- a/library/datetime/iso8601_spec.rb +++ b/library/datetime/iso8601_spec.rb @@ -6,5 +6,7 @@ end describe "DateTime#iso8601" do - it "needs to be reviewed for spec completeness" + it "is an alias of DateTime#isoxmlschema8601" do + DateTime.instance_method(:iso8601).should == DateTime.instance_method(:xmlschema) + end end diff --git a/library/datetime/xmlschema_spec.rb b/library/datetime/xmlschema_spec.rb index fd0026cac..42832631e 100644 --- a/library/datetime/xmlschema_spec.rb +++ b/library/datetime/xmlschema_spec.rb @@ -6,7 +6,5 @@ end describe "DateTime#xmlschema" do - it "is an alias of DateTime#iso8601" do - DateTime.instance_method(:xmlschema).should == DateTime.instance_method(:iso8601) - end + it "needs to be reviewed for spec completeness" end diff --git a/library/getoptlong/each_option_spec.rb b/library/getoptlong/each_option_spec.rb index 53987e487..3349554aa 100644 --- a/library/getoptlong/each_option_spec.rb +++ b/library/getoptlong/each_option_spec.rb @@ -2,7 +2,20 @@ require 'getoptlong' describe "GetoptLong#each_option" do - it "is an alias of GetoptLong#each" do - GetoptLong.instance_method(:each_option).should == GetoptLong.instance_method(:each) + before :each do + @opts = GetoptLong.new( + [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], + [ '--query', '-q', GetoptLong::NO_ARGUMENT ], + [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] + ) + end + + it "passes each_option argument/value pair to the block" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + pairs = [] + @opts.each_option { |arg, val| pairs << [ arg, val ] } + pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ] + end end end diff --git a/library/getoptlong/each_spec.rb b/library/getoptlong/each_spec.rb index d342db47c..646c3297b 100644 --- a/library/getoptlong/each_spec.rb +++ b/library/getoptlong/each_spec.rb @@ -2,20 +2,7 @@ require 'getoptlong' describe "GetoptLong#each" do - before :each do - @opts = GetoptLong.new( - [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], - [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], - [ '--query', '-q', GetoptLong::NO_ARGUMENT ], - [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] - ) - end - - it "passes each argument/value pair to the block" do - argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do - pairs = [] - @opts.each { |arg, val| pairs << [ arg, val ] } - pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ] - end + it "is an alias of GetoptLong#each_option" do + GetoptLong.instance_method(:each).should == GetoptLong.instance_method(:each_option) end end