Sha256: 021d3d8475f3842aa96444867d679546cebc4a21c8ec97df07e9642eb78a7f82

Contents?: true

Size: 1.08 KB

Versions: 58

Compression:

Stored size: 1.08 KB

Contents

require 'test_helper'
require 'tins'

module Tins
  class SubhashTest < Test::Unit::TestCase
    require 'tins/xt/subhash'

    def test_subhash
      h = { 'foo1' => 1, 'foo2' => 2, 'bar666' => 666 }
      assert_equal [ [ 'bar666', 666 ] ], h.subhash(/\Abar/).to_a
      assert h.subhash(/\Abaz/).empty?
      assert_equal [ [ 'foo1', 1 ], [ 'foo2', 2 ] ], h.subhash(/\Afoo\d/).sort
      assert_equal [ [ 'foo2', 2 ] ], h.subhash('foo2').to_a
    end

    def test_subhash_bang
      h = { 'foo1' => 1, 'foo2' => 2, 'bar666' => 666 }
      h.subhash!('foo2')
      assert_equal [ [ 'foo2', 2 ] ], h.to_a
    end

    def test_subhash_with_block
      h = { 'foo1' => 1, 'foo2' => 2, 'bar666' => 666 }
      assert h.subhash(/\Abaz/) { :foo }.empty?
      assert_equal [ [ 'foo1', 1 ], [ 'foo2', 2 ] ],
        h.subhash(/\Afoo(\d)/) { |_,_,m| Integer(m[1]) }.to_a.sort
    end

    def test_subhash_with_default_proc
      h = Hash.new { |h, k| h[k] = :foo }
      h.update('foo' => 1, 'bar' => 2)
      s = h.subhash('foo')
      assert_equal 1, s['foo']
      assert_equal :foo, s['bar']
    end
  end
end

Version data entries

58 entries across 55 versions & 8 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/tests/subhash_test.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/tests/subhash_test.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/tests/subhash_test.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/tests/subhash_test.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/subhash_test.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/subhash_test.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/subhash_test.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/subhash_test.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/tins-1.13.2/tests/subhash_test.rb
tdiary-5.0.5 vendor/bundle/gems/tins-1.13.2/tests/subhash_test.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/tins-1.13.2/tests/subhash_test.rb
tdiary-5.0.4 vendor/bundle/gems/tins-1.13.2/tests/subhash_test.rb
tins-1.13.2 tests/subhash_test.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/tins-1.13.0/tests/subhash_test.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/tins-1.13.0/tests/subhash_test.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/tins-1.13.0/tests/subhash_test.rb
tins-1.13.0 tests/subhash_test.rb
tdiary-5.0.2 vendor/bundle/gems/tins-1.12.0/tests/subhash_test.rb
tins-1.12.0 tests/subhash_test.rb
tins-1.11.0 tests/subhash_test.rb