Sha256: 329ffd9822e0d3058a76d176ed6c0fe27805bcc947910121b020ba18a938796b

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require 'minitest/autorun'
require 'xkeys'

class TestXK < MiniTest::Unit::TestCase

    def test_hash_get
	h = { :a => 'a', :b => { :c => 'bc' },
	  :d => { :e => { :f => 'def' }}}.extend XKeys::Get

	assert_respond_to(h, :xfetch)
	assert_respond_to(h, :[])

	assert_equal('a', h.xfetch(:a), 'h.xfetch :a')
	assert_equal({ :c => 'bc' }, h.xfetch(:b), 'h.xfetch :b')
	assert_equal('bc', h.xfetch(:b, :c), 'h.xfetch :b, :c')

	assert_equal(false, h.xfetch(:b, :d, :else => false),
	  'h.xfetch :b, :d, :else => false')
	assert_raises(KeyError, 'h.xfetch :b, :d') { h.xfetch :b, :d }

	assert_equal('a', h[:a], 'h[:a]')
	assert_equal({ :c => 'bc' }, h[:b], 'h[:b]')
	assert_equal('bc', h[:b, :c], 'h[:b, :c]')

	assert_equal(false, h[:b, :d, :else=>false], 'h[:b, :d, :else=>false]')
	assert_equal(nil, h[:b, :d], 'h[:b, :d]')
	assert_raises(KeyError, 'h[:b, :d, {}]') { h[:b, :d, {}] }
    end

    def test_array_get
	a = [ '0', [ '1.0' ], [ '2.0', [ '2.1.0', '2.1.1' ]]].extend XKeys::Get

	assert_respond_to(a, :xfetch)
	assert_respond_to(a, :[])

	assert_equal('0', a.xfetch(0), 'a.xfetch 0')
	assert_equal('1.0', a.xfetch(1, 0), 'a.xfetch 1, 0')
	assert_equal('1.0', a.xfetch(nil, 1, 0), 'a.xfetch nil, 1, 0')
	assert_equal('1.0', a.xfetch(1, 0, {}), 'a.xfetch 1, 0, {}')
	assert_equal('2.1.1', a.xfetch(2, 1, 1), 'a.xfetch 2, 1, 1')

	assert_equal(false, a.xfetch(1, 1, :else => false),
	  'a.xfetch 1, 1, :else => false')
	assert_raises(IndexError, 'a.xfetch 1, 1') { a.xfetch 1, 1 }

	assert_equal('0', a[0], 'a[0]')
	assert_equal([['1.0']], a[1, 1], 'a[1, 1]')
	assert_equal('1.0', a[nil, 1, 0], 'a[nil, 1, 0]')
	assert_equal('1.0', a[1, 0, {}], 'a[1, 0, {}]')
	assert_equal('1.0', a[nil, 1, 0, {}], 'a[nil, 1, 0, {}]')
	assert_equal('2.1.1', a[2, 1, 1], 'a[2, 1, 1]')

	assert_equal(false, a[1, 1, :else=>false], 'a[1, 1, :else=>false]')
	assert_equal(nil, a[nil, 1, 1], 'a[nil, 1, 1]')
	assert_raises(IndexError, 'a[1, 1, {}]') { a[1, 1, {}] }
    end

end

# END

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xkeys-0.0.2 test/01get.rb
xkeys-0.0.1 test/01get.rb