Sha256: 7cbf793b0c978d24d4c86ba273c9fe3050a74d725cbf83fe43ce4f862aeae65e

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

require File.dirname(__FILE__)+'/../abstract_unit'
require 'gorillib/hash/slice'

class HashSliceTest < Test::Unit::TestCase

  def test_slice
    original = { :a => 'x', :b => 'y', :c => 10 }
    expected = { :a => 'x', :b => 'y' }

    # Should return a new hash with only the given keys.
    assert_equal expected, original.slice(:a, :b)
    assert_not_equal expected, original
  end

  def test_slice_inplace
    original = { :a => 'x', :b => 'y', :c => 10 }
    expected = { :c => 10 }

    # Should replace the hash with only the given keys.
    assert_equal expected, original.slice!(:a, :b)
  end

  def test_slice_with_an_array_key
    original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
    expected = { [:a, :b] => "an array key", :c => 10 }

    # Should return a new hash with only the given keys when given an array key.
    assert_equal expected, original.slice([:a, :b], :c)
    assert_not_equal expected, original
  end

  def test_slice_inplace_with_an_array_key
    original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
    expected = { :a => 'x', :b => 'y' }

    # Should replace the hash with only the given keys when given an array key.
    assert_equal expected, original.slice!([:a, :b], :c)
  end

  def test_slice_with_splatted_keys
    original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
    expected = { :a => 'x', :b => "y" }

    # Should grab each of the splatted keys.
    assert_equal expected, original.slice(*[:a, :b])
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gorillib-0.0.7 test/hash/slice_test.rb
gorillib-0.0.6 test/hash/slice_test.rb
gorillib-0.0.5 test/hash/slice_test.rb
gorillib-0.0.4 test/hash/slice_test.rb
gorillib-0.0.3 test/hash/slice_test.rb
gorillib-0.0.2 test/hash/slice_test.rb