This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
require 'helper'
|
||||
|
||||
# must be required independently
|
||||
require 'hashery/association'
|
||||
|
||||
testcase Association do
|
||||
|
||||
class_method :new do
|
||||
test do
|
||||
Association.new(:A, :B)
|
||||
end
|
||||
end
|
||||
|
||||
class_method :[] do
|
||||
test do
|
||||
a = Association[:a, 1]
|
||||
a.assert.index == :a
|
||||
a.assert.value == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :index do
|
||||
test do
|
||||
a = Association.new(:a,1)
|
||||
a.index.assert == :a
|
||||
end
|
||||
end
|
||||
|
||||
method :value do
|
||||
test do
|
||||
a = Association.new(:a,1)
|
||||
a.value.assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :to_ary do
|
||||
test do
|
||||
k,v = [],[]
|
||||
ohash = [ 'A' >> '3', 'B' >> '2', 'C' >> '1' ]
|
||||
ohash.each { |e1,e2| k << e1 ; v << e2 }
|
||||
k.assert == ['A','B','C']
|
||||
v.assert == ['3','2','1']
|
||||
end
|
||||
end
|
||||
|
||||
method :index do
|
||||
test do
|
||||
complex = [ 'Drop Menu' >> [ 'Button 1', 'Button 2', 'Button 3' ], 'Help' ]
|
||||
complex[0].index.assert == 'Drop Menu'
|
||||
end
|
||||
end
|
||||
|
||||
method :<=> do
|
||||
test 'when differnt in value' do
|
||||
a = Association.new(:a,1)
|
||||
b = Association.new(:b,2)
|
||||
(a <=> b).assert == -1
|
||||
(b <=> a).assert == 1
|
||||
end
|
||||
|
||||
test 'when equal value' do
|
||||
a = Association.new(:a,1)
|
||||
b = Association.new(:b,1)
|
||||
(a <=> b).assert == 0
|
||||
end
|
||||
end
|
||||
|
||||
method :invert! do
|
||||
test do
|
||||
a = Association.new(:a,1)
|
||||
a.invert!
|
||||
a.index.assert == 1
|
||||
a.value.assert == :a
|
||||
end
|
||||
end
|
||||
|
||||
method :inspect do
|
||||
test do
|
||||
a = Association.new(:a,1)
|
||||
a.inspect.assert == ":a >> 1"
|
||||
end
|
||||
end
|
||||
|
||||
method :to_s do
|
||||
test do
|
||||
a = Association.new(:a,1)
|
||||
a.to_s.assert == "a >> 1"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
testcase Object do
|
||||
method :associations do
|
||||
test do
|
||||
s = 'a'
|
||||
complex = [ s >> :b, s >> :c ]
|
||||
s.associations.assert == [:b, :c]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
require 'helper'
|
||||
|
||||
test_case CastingHash do
|
||||
|
||||
class_method :[] do
|
||||
test do
|
||||
h = CastingHash[:a=>1, :b=>2]
|
||||
end
|
||||
end
|
||||
|
||||
class_method :new do
|
||||
test do
|
||||
h = CastingHash.new
|
||||
end
|
||||
|
||||
test 'with default' do
|
||||
h = CastingHash.new(0)
|
||||
h['a'].assert == 0
|
||||
end
|
||||
|
||||
test 'with casting procedure' do
|
||||
h = CastingHash.new{ |k,v| [k.to_sym, v] }
|
||||
h['a'] = 1
|
||||
h.assert == {:a=>1}
|
||||
end
|
||||
|
||||
test 'with default and casting procedure' do
|
||||
h = CastingHash.new(0){ |k,v| [k.to_sym, v] }
|
||||
h['a'].assert == 0
|
||||
h['b'] = 2
|
||||
h.assert == {:b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :recast! do
|
||||
test do
|
||||
h = CastingHash[:a=>1, :b=>2]
|
||||
h.cast_proc{ |k,v| [k.to_s, v] }
|
||||
h.recast!
|
||||
h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :cast_proc= do
|
||||
test do
|
||||
h = CastingHash[:a=>1, :b=>2]
|
||||
h.cast_proc = Proc.new{ |k,v| [k.to_s, v] }
|
||||
h.recast!
|
||||
h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :to_hash do
|
||||
test do
|
||||
h = CastingHash[:a=>1, :b=>2]
|
||||
h.to_hash
|
||||
::Hash.assert === h
|
||||
h.assert == {:a=>1, :b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :to_h do
|
||||
test do
|
||||
h = CastingHash[:a=>1, :b=>2]
|
||||
h.to_h
|
||||
::Hash.assert === h
|
||||
h.assert == {:a=>1, :b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,79 @@
|
||||
require 'helper'
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :rekey do
|
||||
|
||||
test "default" do
|
||||
foo = { "a"=>1, "b"=>2 }
|
||||
foo.rekey.assert == { :a=>1, :b=>2 }
|
||||
foo.assert == { "a"=>1, "b"=>2 }
|
||||
end
|
||||
|
||||
test "specific key" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.rekey(:a=>:c)
|
||||
foo[:c].assert == 1
|
||||
foo[:b].assert == 2
|
||||
foo[:a].assert == nil
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
bar = { :a=>1, :b=>2 }
|
||||
foo = bar.rekey{ |k| k.to_s }
|
||||
foo['a'].assert == 1
|
||||
foo['b'].assert == 2
|
||||
foo[:a].assert == nil
|
||||
foo[:b].assert == nil
|
||||
foo.assert == { 'a'=>1, 'b'=>2 }
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey(&:to_s).assert == { "a"=>1, "b"=>2 }
|
||||
foo.assert == { :a =>1, :b=>2 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :rekey! do
|
||||
|
||||
test "default" do
|
||||
foo = { "a"=>1, "b"=>2 }
|
||||
foo.rekey!.assert == { :a=>1, :b=>2 }
|
||||
foo.assert == { :a=>1, :b=>2 }
|
||||
end
|
||||
|
||||
test "specific key" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey!(:a=>:c)
|
||||
foo[:c].assert == 1
|
||||
foo[:b].assert == 2
|
||||
foo[:a].assert == nil
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey!{ |k| k.to_s }
|
||||
foo['a'].assert == 1
|
||||
foo['b'].assert == 2
|
||||
foo[:a].assert == nil
|
||||
foo[:b].assert == nil
|
||||
foo.assert == { 'a'=>1, 'b'=>2 }
|
||||
end
|
||||
|
||||
test "symbol proc" do
|
||||
foo = { :a=>1, :b=>2 }
|
||||
foo.rekey!(&:to_s).assert == { "a"=>1, "b"=>2 }
|
||||
foo.assert == { "a"=>1, "b"=>2 }
|
||||
end
|
||||
|
||||
test "no conflict between keys" do
|
||||
r = {1 => :a, 2 => :b}.rekey!{ |k| k + 1 }
|
||||
r.refute = {3 => :a}
|
||||
r.assert = {2 => :a, 3 => :b}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
require 'helper'
|
||||
|
||||
test_case CRUDHash do
|
||||
|
||||
class_method :create do
|
||||
test do
|
||||
h = CRUDHash.create(:a=>1,:b=>2)
|
||||
h.assert == {:a=>1,:b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
class_method :auto do
|
||||
test 'without a block' do
|
||||
h = CRUDHash.auto
|
||||
h[:a].assert == {}
|
||||
end
|
||||
|
||||
test 'with a block' do
|
||||
h = CRUDHash.auto{ [] }
|
||||
h[:a].assert == []
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# OT: Why not make `:a=>1` a Pair object?
|
||||
#
|
||||
@@ -0,0 +1,328 @@
|
||||
require 'helper'
|
||||
|
||||
testcase Dictionary do
|
||||
include AE::Legacy::Assertions
|
||||
|
||||
class_method :[] do
|
||||
test do
|
||||
d = Dictionary['z', 1, 'a', 2, 'c', 3]
|
||||
assert_equal( ['z','a','c'], d.keys )
|
||||
end
|
||||
end
|
||||
|
||||
class_method :new do
|
||||
test "with default" do
|
||||
d = Dictionary.new{ |hash,key| hash[key] = 0 }
|
||||
d[:a] = 0
|
||||
d[:b] += 1
|
||||
assert_equal [0, 1], d.values
|
||||
assert_equal [:a,:b], d.keys
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test do
|
||||
d = Dictionary['a', 1]
|
||||
d['a'].assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
d = Dictionary.new
|
||||
d['z'] = 1
|
||||
d['a'] = 2
|
||||
d['c'] = 3
|
||||
assert_equal( ['z','a','c'], d.keys )
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
d = Dictionary[]
|
||||
d[:a] = 1
|
||||
d[:c] = 3
|
||||
assert_equal( [1,3], d.values )
|
||||
d[:b,1] = 2
|
||||
assert_equal( [1,2,3], d.values )
|
||||
assert_equal( [:a,:b,:c], d.keys )
|
||||
end
|
||||
end
|
||||
|
||||
method :push do
|
||||
test do
|
||||
d = Dictionary['a', 1, 'c', 2, 'z', 3]
|
||||
assert( d.push('end', 15) )
|
||||
assert_equal( 15, d['end'] )
|
||||
assert( ! d.push('end', 30) )
|
||||
assert( d.unshift('begin', 50) )
|
||||
assert_equal( 50, d['begin'] )
|
||||
assert( ! d.unshift('begin', 60) )
|
||||
assert_equal( ["begin", "a", "c", "z", "end"], d.keys )
|
||||
assert_equal( ["end", 15], d.pop )
|
||||
assert_equal( ["begin", "a", "c", "z"], d.keys )
|
||||
assert_equal( ["begin", 50], d.shift )
|
||||
end
|
||||
end
|
||||
|
||||
method :insert do
|
||||
test "front" do
|
||||
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
||||
r = Dictionary['d', 4, 'a', 1, 'b', 2, 'c', 3]
|
||||
assert_equal( 4, d.insert(0,'d',4) )
|
||||
assert_equal( r, d )
|
||||
end
|
||||
test "back" do
|
||||
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
||||
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
||||
assert_equal( 4, d.insert(-1,'d',4) )
|
||||
assert_equal( r, d )
|
||||
end
|
||||
end
|
||||
|
||||
method :update do
|
||||
test "with other orderred hash" do
|
||||
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
||||
c = Dictionary['d', 4]
|
||||
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
||||
assert_equal( r, d.update(c) )
|
||||
assert_equal( r, d )
|
||||
end
|
||||
test "with other hash" do
|
||||
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
||||
c = { 'd' => 4 }
|
||||
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
||||
assert_equal( r, d.update(c) )
|
||||
assert_equal( r, d )
|
||||
end
|
||||
end
|
||||
|
||||
method :merge do
|
||||
test "with other orderred hash" do
|
||||
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
||||
c = Dictionary['d', 4]
|
||||
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
||||
assert_equal( r, d.merge(c) )
|
||||
end
|
||||
test "with other hash" do
|
||||
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
||||
c = { 'd' => 4 }
|
||||
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
||||
assert_equal( r, d.merge(c) )
|
||||
end
|
||||
end
|
||||
|
||||
method :order_by do
|
||||
test do
|
||||
d = Dictionary['a', 3, 'b', 2, 'c', 1]
|
||||
d.order_by{ |k,v| v }
|
||||
assert_equal( [1,2,3], d.values )
|
||||
assert_equal( ['c','b','a'], d.keys )
|
||||
end
|
||||
end
|
||||
|
||||
method :reverse! do
|
||||
test do
|
||||
d = Dictionary['z', 1, 'a', 2, 'c', 3]
|
||||
d.reverse!
|
||||
assert_equal( ['c','a','z'], d.keys )
|
||||
end
|
||||
end
|
||||
|
||||
method :collect do
|
||||
test "enumerable method" do
|
||||
d = Dictionary[]
|
||||
d[:a] = "a"
|
||||
d[:c] = "b"
|
||||
r = d.collect{|k,v| v.capitalize}
|
||||
r.assert == ["A","B"]
|
||||
end
|
||||
end
|
||||
|
||||
method :dup do
|
||||
test "with array values" do
|
||||
d = Dictionary.new
|
||||
d.dup
|
||||
d[:a]=['t',5]
|
||||
assert_equal(d, d.dup)
|
||||
end
|
||||
end
|
||||
|
||||
method :first do
|
||||
test do
|
||||
d = Dictionary[]
|
||||
d[:a] = "a"
|
||||
d[:b] = "b"
|
||||
d[:c] = "c"
|
||||
d.first.assert == "a"
|
||||
d.first(0).assert == []
|
||||
assert_equal ["a"] , d.first(1)
|
||||
assert_equal ["a", "b"] , d.first(2)
|
||||
end
|
||||
end
|
||||
|
||||
method :last do
|
||||
test do
|
||||
d = Dictionary[]
|
||||
d[:a] = "a"
|
||||
d[:b] = "b"
|
||||
d[:c] = "c"
|
||||
d.last.assert == "c"
|
||||
d.last(0).assert == []
|
||||
d.last(1).assert == ["c"]
|
||||
d.last(2).assert == ["b", "c"]
|
||||
end
|
||||
end
|
||||
|
||||
method :select do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
r = d.select{ |k,v| v % 2 == 1 }
|
||||
r.assert == [[:a, 1], [:c, 3]]
|
||||
end
|
||||
end
|
||||
|
||||
method :to_h do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2]
|
||||
h = d.to_h
|
||||
h.assert == {:a=>1, :b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :replace do
|
||||
test do
|
||||
d1 = Dictionary[:a=>1, :b=>2]
|
||||
d2 = Dictionary[:c=>3, :d=>4]
|
||||
d1.replace(d2)
|
||||
d1.to_h.assert == {:c=>3, :d=>4}
|
||||
end
|
||||
end
|
||||
|
||||
method :reverse do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
r = d.reverse
|
||||
r.first.assert == 3
|
||||
end
|
||||
end
|
||||
|
||||
method :invert do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
r = d.invert
|
||||
Dictionary.assert === r
|
||||
r.to_h.assert == {1=>:a, 2=>:b, 3=>:c}
|
||||
end
|
||||
end
|
||||
|
||||
method :each_key do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.order_by_key
|
||||
a = []
|
||||
d.each_key{ |k| a << k }
|
||||
a.assert == [:a, :b, :c]
|
||||
end
|
||||
|
||||
method :each_value do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.order_by_value
|
||||
a = []
|
||||
d.each_value{ |v| a << v }
|
||||
a.assert == [1, 2, 3]
|
||||
end
|
||||
|
||||
method :clear do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.clear
|
||||
d.to_a.assert == []
|
||||
end
|
||||
|
||||
method :fetch do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.fetch(:a).assert == 1
|
||||
end
|
||||
|
||||
method :key? do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.assert.key?(:a)
|
||||
d.refute.key?(:d)
|
||||
end
|
||||
end
|
||||
|
||||
method :has_key? do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.assert.has_key?(:a)
|
||||
d.refute.has_key?(:d)
|
||||
end
|
||||
end
|
||||
|
||||
method :length do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.length.assert == 3
|
||||
end
|
||||
end
|
||||
|
||||
method :to_a do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.to_a.assert == [[:a,1], [:b,2], [:c,3]]
|
||||
end
|
||||
end
|
||||
|
||||
method :to_hash do
|
||||
test do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.to_hash.assert == {:a=>1, :b=>2, :c=>3}
|
||||
end
|
||||
end
|
||||
|
||||
method :empty? do
|
||||
test "is emtpy" do
|
||||
d = Dictionary[]
|
||||
d.assert.empty?
|
||||
end
|
||||
|
||||
test 'is not emtpy' do
|
||||
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
||||
d.refute.empty?
|
||||
end
|
||||
end
|
||||
|
||||
method :order_by_key do
|
||||
test do
|
||||
d = Dictionary[:b=>1, :c=>2, :a=>4]
|
||||
d.order_by_key
|
||||
d.order.assert == [:a, :b, :c]
|
||||
end
|
||||
end
|
||||
|
||||
method :order_by_value do
|
||||
test do
|
||||
d = Dictionary[:b=>1, :c=>2, :a=>4]
|
||||
d.order_by_value
|
||||
d.order.assert == [:b, :c, :a]
|
||||
end
|
||||
end
|
||||
|
||||
class_method :alpha do
|
||||
test do
|
||||
d = Dictionary.alpha
|
||||
d.update(:b=>1, :c=>2, :a=>4)
|
||||
d.order.assert == [:a, :b, :c]
|
||||
end
|
||||
end
|
||||
|
||||
class_method :auto do
|
||||
test do
|
||||
d = Dictionary.auto
|
||||
s = d[:foo]
|
||||
s.class.assert == Dictionary
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
require 'helper'
|
||||
|
||||
test_case IniHash do
|
||||
|
||||
class_method :new do
|
||||
test do
|
||||
h = IniHash.new('foo.ini', false)
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
h = IniHash.new('foo.ini', false)
|
||||
h['a'] = '1'
|
||||
h['a'].assert = '1'
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test do
|
||||
h = IniHash.new('foo.ini', false)
|
||||
h['a'] = '1'
|
||||
h['a'].assert = '1'
|
||||
end
|
||||
end
|
||||
|
||||
method :to_h do
|
||||
test do
|
||||
h = IniHash.new('foo.ini', false)
|
||||
h['a'] = '1'
|
||||
h.to_h.assert = {'a'=>'1'}
|
||||
end
|
||||
end
|
||||
|
||||
method :to_s do
|
||||
test do
|
||||
h = IniHash.new('foo.ini', false)
|
||||
h['a'] = '1'
|
||||
h.to_s.assert == "a=1\n"
|
||||
end
|
||||
|
||||
test do
|
||||
h = IniHash.new('foo.ini', false)
|
||||
h['a'] = '1'
|
||||
h['b'] = {'c'=>3}
|
||||
h.to_s.assert == "a=1\n[b]\nc=3\n"
|
||||
end
|
||||
end
|
||||
|
||||
class_method :load do
|
||||
h = IniHash.load('test/fixture/example.ini')
|
||||
h['a'].assert == '1'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
require 'helper'
|
||||
|
||||
testcase KeyHash do
|
||||
|
||||
class_method :[] do
|
||||
test 'creates new KeyHash' do
|
||||
s = KeyHash[]
|
||||
KeyHash.assert === s
|
||||
end
|
||||
|
||||
test 'pre-assigns values' do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s[:a].assert == 1
|
||||
s[:b].assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test 'instance level fetch' do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s[:a].assert == 1
|
||||
s[:b].assert == 2
|
||||
end
|
||||
|
||||
test 'by default keys are converted to strings' do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s['a'].assert == 1
|
||||
s['b'].assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
s = KeyHash.new
|
||||
s[:a] = 1
|
||||
s[:b] = 2
|
||||
s[:a].assert == 1
|
||||
s[:b].assert == 2
|
||||
s['a'].assert == 1
|
||||
s['b'].assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :initialize do
|
||||
test do
|
||||
StandardError.refute.raised? do
|
||||
s = KeyHash.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :to_hash do
|
||||
test do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s.to_hash.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :to_h do
|
||||
test do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s.to_h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :replace do
|
||||
test do
|
||||
s = KeyHash.new
|
||||
s.replace(:a=>1, :b=>2)
|
||||
s.to_h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :delete do
|
||||
test do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s.delete(:a)
|
||||
s.to_h.assert == {'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :each do
|
||||
test do
|
||||
s = KeyHash[:a=>1, :b=>2]
|
||||
s.each do |k,v|
|
||||
String.assert === k
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :store do
|
||||
test do
|
||||
s = KeyHash.new
|
||||
s.store(:a, 1)
|
||||
s.to_h.assert == {'a'=>1}
|
||||
end
|
||||
end
|
||||
|
||||
method :update do
|
||||
test do
|
||||
s1 = KeyHash[:a=>1,:b=>2]
|
||||
s2 = KeyHash[:c=>3,:d=>4]
|
||||
s1.update(s2)
|
||||
s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
|
||||
end
|
||||
end
|
||||
|
||||
method :rekey do
|
||||
test do
|
||||
s = KeyHash[:a=>1,:b=>2,:c=>3]
|
||||
x = s.rekey{ |k| k.upcase }
|
||||
x.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
|
||||
end
|
||||
end
|
||||
|
||||
method :rekey! do
|
||||
test do
|
||||
s = KeyHash[:a=>1,:b=>2,:c=>3]
|
||||
s.rekey!{ |k| k.upcase }
|
||||
s.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
|
||||
end
|
||||
end
|
||||
|
||||
method :key? do
|
||||
test do
|
||||
s = KeyHash[:a=>1]
|
||||
s.assert.key?(:a)
|
||||
s.assert.key?('a')
|
||||
end
|
||||
end
|
||||
|
||||
method :has_key? do
|
||||
test do
|
||||
s = KeyHash[:a=>1]
|
||||
s.assert.has_key?(:a)
|
||||
s.assert.has_key?('a')
|
||||
end
|
||||
end
|
||||
|
||||
method :<< do
|
||||
test do
|
||||
s = KeyHash.new
|
||||
s << [:a, 1]
|
||||
s << [:b, 2]
|
||||
s.to_h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :merge! do
|
||||
test do
|
||||
s1 = KeyHash[:a=>1,:b=>2]
|
||||
s2 = KeyHash[:c=>3,:d=>4]
|
||||
s1.merge!(s2)
|
||||
s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
|
||||
end
|
||||
end
|
||||
|
||||
method :values_at do
|
||||
test do
|
||||
s = KeyHash[:a=>1,:b=>2,:c=>3]
|
||||
s.values_at(:a, :b).assert == [1,2]
|
||||
s.values_at('a','b').assert == [1,2]
|
||||
end
|
||||
end
|
||||
|
||||
method :fetch do
|
||||
test do
|
||||
s = KeyHash[:a=>1,:b=>2,:c=>3]
|
||||
s.fetch(:a).assert == 1
|
||||
s.fetch('a').assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
#method :cast_key do
|
||||
# test do
|
||||
# s = KeyHash.new
|
||||
# s.send(:cast_key, :a).assert == 'a'
|
||||
# end
|
||||
#end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
require 'helper'
|
||||
|
||||
test_case LinkedList do
|
||||
|
||||
class_method :new do
|
||||
ll = LinkedList.new
|
||||
LinkedList.assert === ll
|
||||
end
|
||||
|
||||
method :to_a do
|
||||
test 'empty' do
|
||||
ll = LinkedList.new
|
||||
ll.to_a.assert == []
|
||||
end
|
||||
test 'not empty' do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.to_a.assert == [:a]
|
||||
end
|
||||
end
|
||||
|
||||
method :empty? do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.assert.empty?
|
||||
end
|
||||
end
|
||||
|
||||
method :delete do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.to_a.assert == [:a]
|
||||
ll.delete(:a)
|
||||
ll.to_a.assert == []
|
||||
end
|
||||
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll.push :c
|
||||
ll.to_a.assert == [:a, :b, :c]
|
||||
ll.delete(:b)
|
||||
ll.to_a.assert == [:a, :c]
|
||||
end
|
||||
end
|
||||
|
||||
method :each do
|
||||
test do
|
||||
a = []
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.each do |e|
|
||||
a << e
|
||||
end
|
||||
a.assert == [:a]
|
||||
end
|
||||
end
|
||||
|
||||
method :length do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.length.assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :push do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.to_a.assert == [:a]
|
||||
end
|
||||
end
|
||||
|
||||
method :unshift do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.unshift :a
|
||||
ll.to_a.assert == [:a]
|
||||
end
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.unshift :b
|
||||
ll.to_a.assert == [:b, :a]
|
||||
end
|
||||
end
|
||||
|
||||
method :pop do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll.to_a.assert == [:a, :b]
|
||||
ll.pop
|
||||
ll.to_a.assert == [:a]
|
||||
end
|
||||
end
|
||||
|
||||
method :shift do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll.to_a.assert == [:a, :b]
|
||||
ll.shift
|
||||
ll.to_a.assert == [:b]
|
||||
end
|
||||
end
|
||||
|
||||
method :first do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll.to_a.assert == [:a, :b]
|
||||
ll.first.assert == :a
|
||||
end
|
||||
end
|
||||
|
||||
method :last do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll.to_a.assert == [:a, :b]
|
||||
ll.last.assert == :b
|
||||
end
|
||||
end
|
||||
|
||||
method :queue do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll.queue.assert == [:a, :b]
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll[:a] = :b
|
||||
ll.to_a.assert == [:b]
|
||||
ll[:a].assert == :b
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll[:a].assert == :a
|
||||
end
|
||||
|
||||
test do
|
||||
ll = LinkedList.new
|
||||
ll.push :a
|
||||
ll.push :b
|
||||
ll[:a].assert == :a
|
||||
ll[:b].assert == :b
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,162 @@
|
||||
require 'helper'
|
||||
|
||||
test_case LRUHash do
|
||||
|
||||
class_method :new do
|
||||
h = LRUHash.new(10)
|
||||
LRUHash.assert === h
|
||||
end
|
||||
|
||||
method :max_size= do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h.max_size = 100
|
||||
end
|
||||
end
|
||||
|
||||
method :store do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h.store(:a, 1)
|
||||
h[:a].assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h.store(:a, 1)
|
||||
h[:a].assert == 1
|
||||
end
|
||||
|
||||
test do
|
||||
h = LRUHash.new(10){ |h,k| h[k] = 0 }
|
||||
h[:a].assert == 0
|
||||
end
|
||||
end
|
||||
|
||||
method :empty? do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h.assert.empty?
|
||||
end
|
||||
end
|
||||
|
||||
method :key do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.key(1).assert == :a
|
||||
end
|
||||
end
|
||||
|
||||
method :keys do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.keys.assert == [:a]
|
||||
end
|
||||
end
|
||||
|
||||
method :values do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.values.assert == [1]
|
||||
end
|
||||
end
|
||||
|
||||
method :values_at do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h[:b] = 2
|
||||
h.values_at(:a).assert == [1]
|
||||
end
|
||||
end
|
||||
|
||||
method :has_key? do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.assert.has_key?(:a)
|
||||
end
|
||||
end
|
||||
|
||||
method :has_value? do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.assert.has_value?(1)
|
||||
end
|
||||
end
|
||||
|
||||
method :assoc do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h[:b] = 2
|
||||
h.assoc(:a).assert == [:a,1]
|
||||
end
|
||||
end
|
||||
|
||||
method :rassoc do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h[:b] = 2
|
||||
h.rassoc(1).assert == [:a,1]
|
||||
end
|
||||
end
|
||||
|
||||
method :each_key do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h[:b] = 2
|
||||
h.each_key do |k|
|
||||
[:a,:b].assert.include?(k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :each_value do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h[:b] = 2
|
||||
h.each_value do |v|
|
||||
[1,2].assert.include?(v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :clear do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h[:b] = 2
|
||||
h.clear
|
||||
h.assert.empty?
|
||||
end
|
||||
end
|
||||
|
||||
method :delete do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.delete(:a)
|
||||
h.assert.empty?
|
||||
end
|
||||
end
|
||||
|
||||
method :delete_if do
|
||||
test do
|
||||
h = LRUHash.new(10)
|
||||
h[:a] = 1
|
||||
h.delete_if{ |k,v| k == :a }
|
||||
h.assert.empty?
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,122 @@
|
||||
require 'helper'
|
||||
|
||||
testcase OpenCascade do
|
||||
include AE::Legacy::Assertions
|
||||
|
||||
class_method :new do
|
||||
OpenCascade[:a=>1]
|
||||
end
|
||||
|
||||
class_method :[] do
|
||||
test "hash" do
|
||||
o = OpenCascade[:a=>1,:b=>2]
|
||||
assert_equal(1, o.a)
|
||||
assert_equal(2, o.b)
|
||||
end
|
||||
test "hash in hash" do
|
||||
o = OpenCascade[:a=>1,:b=>2,:c=>{:x=>9}]
|
||||
assert_equal(9, o.c.x)
|
||||
end
|
||||
test "hash in hash in hash" do
|
||||
h = {:a=>1,:x=>{:y=>{:z=>1}}}
|
||||
c = OpenCascade[h]
|
||||
assert_equal(1, c.x.y.z)
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test "basic assignment" do
|
||||
o = OpenCascade.new
|
||||
o[:a] = 1
|
||||
assert_equal(1, o[:a])
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test "basic assignment with primed OpenCascade" do
|
||||
o = OpenCascade[:a=>1]
|
||||
o[:b] = 2
|
||||
o.to_h.assert == {:a=>1,:b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :to_a do
|
||||
test do
|
||||
o = OpenCascade[:a=>1,:b=>2]
|
||||
a = o.to_a
|
||||
a.assert.include?([:a,1])
|
||||
a.assert.include?([:b,2])
|
||||
a.size.assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :to_h do
|
||||
test do
|
||||
o = OpenCascade[:a=>1]
|
||||
assert_equal({:a=>1}, o.to_h)
|
||||
end
|
||||
end
|
||||
|
||||
method :merge! do
|
||||
test do
|
||||
o = OpenCascade[:f0=>"f0"]
|
||||
r = OpenCascade[:f0=>"f0", :h0=>"h0"]
|
||||
h = { :h0=>"h0" }
|
||||
assert_equal(r, o.merge!(h))
|
||||
assert_equal({:f0=>"f0", :h0=>"h0"}, h.merge(o))
|
||||
end
|
||||
end
|
||||
|
||||
method :update do
|
||||
test do
|
||||
o = OpenCascade[:f1=>"f1"]
|
||||
o.update(:h1=>"h1")
|
||||
o.assert == OpenCascade[:f1=>"f1", :h1=>"h1"]
|
||||
end
|
||||
end
|
||||
|
||||
method :method_missing do
|
||||
test "writer and reader" do
|
||||
o = OpenCascade.new
|
||||
10.times{ |i| o.__send__("n#{i}=", 1 ) }
|
||||
10.times{ |i| assert_equal(1, o.__send__("n#{i}")) }
|
||||
end
|
||||
end
|
||||
|
||||
method :<< do
|
||||
test do
|
||||
c = OpenCascade.new
|
||||
c << [:x,8]
|
||||
c << [:y,9]
|
||||
|
||||
assert_equal(8, c.x)
|
||||
assert_equal(9, c.y)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
testcase Hash do
|
||||
|
||||
method :update do
|
||||
test "hash can be updated by OpenCascade" do
|
||||
o = OpenCascade[:f1=>"f1"]
|
||||
h = {:h1=>"h1"}
|
||||
h.update(o)
|
||||
h.assert == {:f1=>"f1", :h1=>"h1"}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
testcase Array do
|
||||
|
||||
method :flatten do
|
||||
test "array can be flattened if contains OpenCascade" do
|
||||
cascade = OpenCascade[:foo=>"bar"]
|
||||
array = [cascade]
|
||||
array.flatten # should not raise error
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,87 @@
|
||||
require 'helper'
|
||||
|
||||
testcase OpenHash do
|
||||
|
||||
class_method :[] do
|
||||
test do
|
||||
o = OpenHash[:a=>1, :b=>2]
|
||||
o.a.assert == 1
|
||||
o.b.assert == 2
|
||||
end
|
||||
|
||||
test do
|
||||
o = OpenHash[:a=>1, :b=>2]
|
||||
o.a.assert == 1
|
||||
o.b.assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :open? do
|
||||
test do
|
||||
o = OpenHash[:a=>1, :b=>2]
|
||||
o.assert.open?(:foo)
|
||||
o.refute.open?(:each)
|
||||
end
|
||||
end
|
||||
|
||||
method :open! do
|
||||
test do
|
||||
o = OpenHash[:a=>1, :b=>2]
|
||||
o.open!(:each)
|
||||
o.assert.open?(:each)
|
||||
o.each = 10
|
||||
o.each.assert == 10
|
||||
end
|
||||
end
|
||||
|
||||
method :close! do
|
||||
test do
|
||||
o = OpenHash[:a=>1, :b=>2]
|
||||
o.open!(:each)
|
||||
o.assert.open?(:each)
|
||||
o.each = 10
|
||||
o.each.assert == 10
|
||||
o.close!(:each)
|
||||
o.each.refute == 10
|
||||
end
|
||||
end
|
||||
|
||||
method :method_missing do
|
||||
test 'bang method' do
|
||||
o = OpenHash[]
|
||||
o.open!(:each)
|
||||
o.each = 10
|
||||
o.each.assert == 10
|
||||
|
||||
a = []
|
||||
o.each! do |k,v|
|
||||
a << [k,v]
|
||||
end
|
||||
a.assert == [[:each,10]]
|
||||
end
|
||||
|
||||
test 'query method' do
|
||||
o = OpenHash[]
|
||||
o.a = 1
|
||||
o.assert.a?
|
||||
o.refute.b?
|
||||
end
|
||||
end
|
||||
|
||||
method :send do
|
||||
test do
|
||||
o = OpenHash[]
|
||||
o.open!(:each)
|
||||
o.each = 10
|
||||
o.each.assert == 10
|
||||
|
||||
a = []
|
||||
o.send(:each) do |k,v|
|
||||
a << [k,v]
|
||||
end
|
||||
a.assert == [[:each,10]]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
require 'helper'
|
||||
|
||||
test_case PropertyHash do
|
||||
|
||||
class_method :new do
|
||||
test do
|
||||
PropertyHash.new
|
||||
end
|
||||
end
|
||||
|
||||
method :update do
|
||||
test do
|
||||
h = PropertyHash.new
|
||||
h.property :a
|
||||
h.property :b
|
||||
h.update(:a=>1, :b=>2)
|
||||
h.assert == {:a=>1, :b=>2}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,226 @@
|
||||
require 'helper'
|
||||
|
||||
testcase QueryHash do
|
||||
|
||||
class_method :new do
|
||||
test 'new QueryHash with default proc' do
|
||||
q = QueryHash.new{ |h,k| h[k] = 1 }
|
||||
q[:a].assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
class_method :[] do
|
||||
test 'creates new QueryHash' do
|
||||
s = QueryHash[]
|
||||
QueryHash.assert === s
|
||||
end
|
||||
|
||||
test 'pre-assigns values' do
|
||||
s = QueryHash[:a=>1, :b=>2]
|
||||
s[:a].assert == 1
|
||||
s[:b].assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :[] do
|
||||
test 'instance level fetch' do
|
||||
s = QueryHash[:a=>1, :b=>2]
|
||||
s[:a].assert == 1
|
||||
s[:b].assert == 2
|
||||
#s['a'].assert == 1
|
||||
#s['b'].assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :[]= do
|
||||
test do
|
||||
s = QueryHash.new
|
||||
s[:a] = 1
|
||||
s[:b] = 2
|
||||
s[:a].assert == 1
|
||||
s[:b].assert == 2
|
||||
#s['a'].assert == 1
|
||||
#s['b'].assert == 2
|
||||
end
|
||||
end
|
||||
|
||||
method :respond_to? do
|
||||
test 'responds to all query methods' do
|
||||
q = QueryHash.new
|
||||
q.assert.respond_to?(:anything?)
|
||||
end
|
||||
|
||||
test 'responds to all bang methods' do
|
||||
q = QueryHash.new
|
||||
q.assert.respond_to?(:anything!)
|
||||
end
|
||||
|
||||
test 'responds to all setter methods' do
|
||||
q = QueryHash.new
|
||||
q.assert.respond_to?(:anything=)
|
||||
end
|
||||
|
||||
test 'responds to usual methods' do
|
||||
q = QueryHash.new
|
||||
q.assert.respond_to?(:each)
|
||||
end
|
||||
end
|
||||
|
||||
method :to_hash do
|
||||
test do
|
||||
s = QueryHash[:a=>1, :b=>2]
|
||||
s.to_hash.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :to_h do
|
||||
test do
|
||||
s = QueryHash[:a=>1, :b=>2]
|
||||
s.to_h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :replace do
|
||||
test do
|
||||
s = QueryHash.new
|
||||
s.replace(:a=>1, :b=>2)
|
||||
s.to_h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :delete do
|
||||
test do
|
||||
s = QueryHash[:a=>1, :b=>2]
|
||||
s.delete(:a)
|
||||
s.to_h.assert == {'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :each do
|
||||
test do
|
||||
s = QueryHash[:a=>1, :b=>2]
|
||||
s.each do |k,v|
|
||||
String.assert === k
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
method :store do
|
||||
test do
|
||||
s = QueryHash.new
|
||||
s.store(:a, 1)
|
||||
s.to_h.assert == {'a'=>1}
|
||||
end
|
||||
end
|
||||
|
||||
method :update do
|
||||
test do
|
||||
s1 = QueryHash[:a=>1,:b=>2]
|
||||
s2 = QueryHash[:c=>3,:d=>4]
|
||||
s1.update(s2)
|
||||
s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
|
||||
end
|
||||
end
|
||||
|
||||
method :rekey do
|
||||
test do
|
||||
s = QueryHash[:a=>1,:b=>2,:c=>3]
|
||||
x = s.rekey{ |k| k.upcase }
|
||||
x.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
|
||||
end
|
||||
end
|
||||
|
||||
method :rekey! do
|
||||
test do
|
||||
s = QueryHash[:a=>1,:b=>2,:c=>3]
|
||||
s.rekey!{ |k| k.upcase }
|
||||
s.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
|
||||
end
|
||||
end
|
||||
|
||||
method :key? do
|
||||
test do
|
||||
s = QueryHash[:a=>1]
|
||||
s.assert.key?(:a)
|
||||
s.assert.key?('a')
|
||||
end
|
||||
end
|
||||
|
||||
method :has_key? do
|
||||
test do
|
||||
s = QueryHash[:a=>1]
|
||||
s.assert.has_key?(:a)
|
||||
s.assert.has_key?('a')
|
||||
end
|
||||
end
|
||||
|
||||
method :<< do
|
||||
test do
|
||||
s = QueryHash.new
|
||||
s << [:a, 1]
|
||||
s << [:b, 2]
|
||||
s.to_h.assert == {'a'=>1, 'b'=>2}
|
||||
end
|
||||
end
|
||||
|
||||
method :merge! do
|
||||
test do
|
||||
s1 = QueryHash[:a=>1,:b=>2]
|
||||
s2 = QueryHash[:c=>3,:d=>4]
|
||||
s1.merge!(s2)
|
||||
s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
|
||||
end
|
||||
end
|
||||
|
||||
method :values_at do
|
||||
test do
|
||||
s = QueryHash[:a=>1,:b=>2,:c=>3]
|
||||
s.values_at(:a, :b).assert == [1,2]
|
||||
s.values_at('a','b').assert == [1,2]
|
||||
end
|
||||
end
|
||||
|
||||
method :fetch do
|
||||
test do
|
||||
s = QueryHash[:a=>1,:b=>2,:c=>3]
|
||||
s.fetch(:a).assert == 1
|
||||
s.fetch('a').assert == 1
|
||||
end
|
||||
end
|
||||
|
||||
method :cast_key do
|
||||
test do
|
||||
s = QueryHash.new
|
||||
s.pry.cast_key(:a).assert == 'a'
|
||||
end
|
||||
end
|
||||
|
||||
method :method_missing do
|
||||
test 'dynamic query methods can look-up values' do
|
||||
q = QueryHash[:a=>1,:b=>2,:c=>3]
|
||||
q.a?.assert == 1
|
||||
q.b?.assert == 2
|
||||
q.c?.assert == 3
|
||||
end
|
||||
|
||||
test 'dynamic bang methods can looks up values too' do
|
||||
q = QueryHash[:a=>1,:b=>2,:c=>3]
|
||||
q.a!.assert == 1
|
||||
q.b!.assert == 2
|
||||
q.c!.assert == 3
|
||||
end
|
||||
|
||||
test 'dynamic bang methods will auto-instantiate' do
|
||||
q = QueryHash.new
|
||||
q.default_proc{ |h,k| h[k] = 'default' }
|
||||
q.foo!.assert == 'default'
|
||||
end
|
||||
|
||||
test 'dynamic query methods will NOT auto-instantiate' do
|
||||
q = QueryHash.new
|
||||
q.default_proc{ |h,k| h[k] = 'default' }
|
||||
q.foo?.assert == nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
# top comment
|
||||
|
||||
a = 1
|
||||
|
||||
[section]
|
||||
x = 2
|
||||
y = 3
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
require 'lemon'
|
||||
require 'ae'
|
||||
require 'ae/legacy' # bacause imitation BasicObject sucks
|
||||
require 'ae/pry'
|
||||
|
||||
require 'hashery'
|
||||
|
||||
include Hashery
|
||||
Reference in New Issue
Block a user