sarah 0.0.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sarah"
3
- s.version = "0.0.4"
4
- s.date = "2013-07-28"
3
+ s.version = "2.0.0"
4
+ s.date = "2014-03-31"
5
5
  s.authors = ["Brian Katzung"]
6
6
  s.email = ["briank@kappacs.com"]
7
7
  s.homepage = "http://rubygems.org/gems/sarah"
@@ -0,0 +1,49 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ # Class methods since 2.0.0
5
+
6
+ class TestSarah_05 < MiniTest::Unit::TestCase
7
+
8
+ def test_cmethod
9
+ [ :[], :new, :try_convert
10
+ ].each { |method| assert_respond_to Sarah, method }
11
+ end
12
+
13
+ def test_copy
14
+ s1 = Sarah[ 1, 2, 5 => 'five', :ten => 10 ]
15
+ s1.default = false
16
+ s1.negative_mode = :actual
17
+ s2 = Sarah.new s1
18
+
19
+ assert_equal s1.to_a, s2.to_a, 's1.to_a == s2.to_a'
20
+ assert_equal s1.to_h, s2.to_h, 's1.to_h == s2.to_h'
21
+ assert_equal :actual, s2.negative_mode, ':actual mode copied'
22
+ assert_equal false, s2.default, 'default copied'
23
+ end
24
+
25
+ def test_new
26
+ assert_equal [0, 'one', 5 => 'five', :ten => 10 ],
27
+ Sarah[0, 'one', 5 => 'five', :ten => 10 ].to_a,
28
+ 'Failed to create Sarah literal'
29
+
30
+ assert_equal [0, 'one', {}],
31
+ Sarah.new(:from => [0, 'one']).to_a,
32
+ 'Failed to create Sarah from array'
33
+
34
+ assert_equal [0, 'one', 5 => 'five', :ten => 10 ],
35
+ Sarah.new(:from => { 0 => 0, 1 => 'one', 5 => 'five', :ten => 10 }).
36
+ to_a, 'Failed to create Sarah from hash'
37
+
38
+ assert_equal [0, 'one', {}],
39
+ Sarah.try_convert([0, 'one']).to_a,
40
+ 'Failed to try_convert array to Sarah'
41
+
42
+ assert_equal [0, 'one', 5 => 'five', :ten => 10 ],
43
+ Sarah.try_convert({ 0 => 0, 1 => 'one', 5 => 'five', :ten => 10 }).
44
+ to_a, 'Failed to try_convert hash to Sarah'
45
+ end
46
+
47
+ end
48
+
49
+ # END
@@ -0,0 +1,41 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ # Instance methods since 2.0,0
5
+
6
+ class TestSarah_06 < MiniTest::Unit::TestCase
7
+
8
+ def setup
9
+ @s = Sarah.new
10
+ end
11
+
12
+ def test_imethods_accessors
13
+ [
14
+ :default, :default=, :default_proc, :default_proc=,
15
+ :negative_mode, :negative_mode=
16
+ ].each { |method| assert_respond_to @s, method }
17
+ end
18
+
19
+ def test_imethods_user_api
20
+ [
21
+ :&, :|, :+, :concat, :-, :<<, :push, :==, :eql?,
22
+ :[], :at, :[]=, :store, :append!, :assoc,
23
+ :clear, :collect!, :map!, :compact!, :count,
24
+ :delete_at, :delete_key, :delete_if, :delete_value,
25
+ :each, :each_index, :each_key, :each_pair, :empty?,
26
+ :fetch, :find_index, :index, :first, :flatten!,
27
+ :has_key?, :key?, :member?, :has_value?, :include?, :value?,
28
+ :insert!, :inspect, :to_s, :join, :key, :keys,
29
+ :last, :length, :size, :merge!, :update, :new_similar,
30
+ :pairs_at, :pop, :rehash, :replace, :reverse!, :reverse_each,
31
+ :rindex, :rotate!, :sample, :select, :set, :set_kv, :set_pairs,
32
+ :shift, :shuffle, :shuffle!, :slice, :slice!, :sort, :sort!,
33
+ :to_a, :to_h, :uniq, :uniq!, :unset_at, :unset_key,
34
+ :unset_if, :unset_value, :unshift,
35
+ :values, :values_at, :zip
36
+ ].each { |method| assert_respond_to @s, method }
37
+ end
38
+
39
+ end
40
+
41
+ # END
@@ -0,0 +1,84 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ class TestSarah_07 < MiniTest::Unit::TestCase
5
+
6
+ def setup; @s = Sarah.new; end
7
+
8
+ def test_defaults
9
+ @s.default = true
10
+ assert_equal true, @s.default, 'Set default to true'
11
+ @s.default = false
12
+ assert_equal false, @s.default, 'Change default to false'
13
+
14
+ begin
15
+ @s.default_proc = Proc.new { |s, k| true }
16
+ rescue
17
+ assert false, 'Failed to set valid default_proc'
18
+ else
19
+ assert true, 'Succeeded in setting valid default_proc'
20
+ assert_kind_of Proc, @s.default_proc, 'Default_proc is a Proc'
21
+ end
22
+
23
+ begin
24
+ @s.default_proc = nil
25
+ rescue
26
+ assert false, 'Failed to set nil default_proc'
27
+ else
28
+ assert_equal nil, @s.default_proc,
29
+ 'Succeeded in setting nil default_proc'
30
+ end
31
+
32
+ begin
33
+ @s.default_proc = true
34
+ rescue TypeError
35
+ assert true, 'Got TypeError for invalid default_proc'
36
+ rescue
37
+ assert false, 'Got incorrect exception for invalid default_proc'
38
+ else
39
+ assert false, 'No exception raised for invalid default_proc'
40
+ end
41
+ end
42
+
43
+ def test_neg_mode
44
+ @s.negative_mode = :ignore
45
+ @s.set 1, 2
46
+
47
+ assert_equal [1, 2], @s.values, 'Two values are correct'
48
+ assert_equal [0, 1], @s.keys, 'Two keys are correct'
49
+ assert_equal nil, @s[2], 'Key too big returns nil'
50
+ assert_equal nil, @s[-3], 'Ignore key too small returns nil'
51
+
52
+ @s.negative_mode = :error
53
+ begin
54
+ value = @s[-3]
55
+ rescue IndexError
56
+ assert true, 'Error key too small raises IndexError'
57
+ rescue
58
+ assert false, 'Error key too small raises incorrect exception'
59
+ else
60
+ assert false, 'Error key too small does not raise an exception'
61
+ end
62
+
63
+ @s.negative_mode = :actual
64
+ assert_equal nil, @s[-3], 'Negative key before set returns nil'
65
+ @s[-3] = '-3'
66
+ assert_equal '-3', @s[-3], 'Negative key after set is OK'
67
+
68
+ @s.negative_mode = :ignore
69
+ assert_equal :actual, @s.negative_mode,
70
+ 'Neg mode :ignore with negative key is ignored'
71
+
72
+ @s.negative_mode = :error
73
+ assert_equal :actual, @s.negative_mode,
74
+ 'Neg mode :error with negative key is ignored'
75
+
76
+ @s.shift
77
+ @s.negative_mode = :ignore
78
+ assert_equal :ignore, @s.negative_mode,
79
+ 'Neg mode :ignore after neg key removed is accepted'
80
+ end
81
+
82
+ end
83
+
84
+ # END
@@ -0,0 +1,36 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ class TestSarah_08 < MiniTest::Unit::TestCase
5
+
6
+ def setup
7
+ @s1 = Sarah[1, 3, 4, 5, 'a', 'c', 'd', 'e', 20 => 'x', 21 => 'y']
8
+ @a1 = [1, 3, 4, 5, 'a', 'c', 'd', 'e', 'x', 'y']
9
+ @s2 = Sarah[1, 2, 3, 5, 'a', 'b', 'c', 'e', 20 => 'x', 22 => 'z']
10
+ @a2 = [1, 2, 3, 5, 'a', 'b', 'c', 'e', 'x', 'z']
11
+
12
+ @s3 = Sarah[ :a => ?a, :b => ?b, :c => ?c, :d => ?d, :x => 1 ]
13
+ @s4 = Sarah[ :b => ?b, :d => ?d, :e => ?e, :x => 2 ]
14
+ end
15
+
16
+ def test_inter
17
+ assert_equal((@a1 & @a2), (@s1 & @s2).values, 'Sarah ary intersection')
18
+ assert_equal({ :b => ?b, :d => ?d }, (@s3 & @s4).to_h,
19
+ 'Sarah rnd intersection')
20
+ end
21
+
22
+ def test_union
23
+ assert_equal((@a1 | @a2), (@s1 | @s2).values, 'Sarah ary union')
24
+ assert_equal({ :a => ?a, :b => ?b, :c => ?c, :d => ?d,
25
+ :e => ?e, :x => 2}, (@s3 | @s4).to_h, 'Sarah rnd union')
26
+ end
27
+
28
+ def test_minus
29
+ assert_equal((@a1 - @a2), (@s1 - @s2).values, 'Sarah ary difference')
30
+ assert_equal({ :a => ?a, :c => ?c, :x => 1 }, (@s3 - @s4).to_h,
31
+ 'Sarah rnd difference')
32
+ end
33
+
34
+ end
35
+
36
+ # END
@@ -0,0 +1,135 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ class TestSarah_09 < MiniTest::Unit::TestCase
5
+
6
+ def test_clear
7
+ assert_equal({}, Sarah[ ?a, :b => ?c ].clear.to_h, 'clear :all')
8
+ assert_equal({ :b => ?c }, Sarah[ ?a, :b => ?c ].clear(:ary).to_h,
9
+ 'clear :ary')
10
+ assert_equal({ 0 => ?a }, Sarah[ ?a, :b => ?c ].clear(:rnd).to_h,
11
+ 'clear :rnd')
12
+ end
13
+
14
+ def test_delete_at
15
+ s = Sarah[ 0, 1, 2, 5 => 5, 7 => 7, 9 => 9,
16
+ :a => ?a, :b => ?b, :c => ?c ]
17
+
18
+ assert_equal ?b, s.delete_at(:b), 'delete_at :b'
19
+ assert_equal 7, s.delete_at(7), 'delete_at 7'
20
+ assert_equal 1, s.delete_at(1), 'delete_at 1'
21
+ assert_equal [ 0, 2, 4 => 5, 7 => 9, :a => ?a, :c => ?c ], s.to_a,
22
+ 'after delete_at x3'
23
+ end
24
+
25
+ def test_delete_at_nma
26
+ s = Sarah.new([ 0, 1, 2, 5 => 5, 7 => 7, 9 => 9,
27
+ :a => ?a, :b => ?b, :c => ?c ], :negative_mode => :actual)
28
+
29
+ assert_equal ?b, s.delete_at(:b), 'NMA delete_at :b'
30
+ assert_equal 7, s.delete_at(7), 'NMA delete_at 7'
31
+ assert_equal 1, s.delete_at(1), 'NMA delete_at 1'
32
+ assert_equal [ 0, 2 => 2, 5 => 5, 9 => 9, :a => ?a, :c => ?c ], s.to_a,
33
+ 'after NMA delete_at x3'
34
+ end
35
+
36
+ def test_delete_if
37
+ s = Sarah[1, 2, 3, 5 => 1, 6 => 2, 7 => 3, :a => 1, :b => 2, :c => 3]
38
+
39
+ s.delete_if(:ary) { |v, k| v == 1 }
40
+ assert_equal [2, 3, 4 => 2, 5 => 3, :a => 1, :b => 2, :c => 3 ],
41
+ s.to_a, 'delete_if :ary == 1'
42
+
43
+ s.delete_if(:rnd) { |v, k| v == 2 }
44
+ assert_equal [2, 3, 4 => 2, 5 => 3, :a => 1, :c => 3 ],
45
+ s.to_a, 'delete_if :rnd == 2'
46
+
47
+ s.delete_if { |v, k| v == 3 }
48
+ assert_equal [2, 3 => 2, :a => 1 ], s.to_a, 'delete_if == 3'
49
+ end
50
+
51
+ def test_delete_val
52
+ s = Sarah[1, 2, 3, 4, 6=>1, 7=>2, 8=>3, 9=>4, :a=>2, :b=>3 ]
53
+ s.delete_value 2, :rnd
54
+ assert_equal [1,2,3,4,1,2,3,4,3], s.values, 'delete_value 2, :rnd'
55
+ s.delete_value 1, :ary
56
+ assert_equal [2,3,4,2,3,4,3], s.values, 'delete_value 1, :ary'
57
+ s.delete_value 3
58
+ assert_equal [2,4,2,4], s.values, 'delete_value 3'
59
+ end
60
+
61
+ def test_pop
62
+ s = Sarah[ 0, 5 => 1, 7 => 2, 9 => 3, :a => ?a]
63
+ a = s.pop 2
64
+ assert_equal [ 2, 3 ], a, 'spr pop 2 ressult'
65
+ a = s.pop 2
66
+ assert_equal [ 0, 1 ], a, 'seq/spr pop 2 result'
67
+ assert_equal [ :a => ?a ], s.to_a, 'sarah after popping'
68
+ end
69
+
70
+ def test_shift
71
+ s = Sarah[ 0, 1, 2, 5 => 5, 7 => 7, 9 => 9,
72
+ :a => ?a, :b => ?b, :c => ?c ]
73
+
74
+ v = s.shift
75
+ assert_equal 0, v, 'seq shift result'
76
+ assert_equal [ 1, 2, 4 => 5, 6 => 7, 8 => 9, :a => ?a, :b => ?b,
77
+ :c => ?c ], s.to_a, 'sarah after shift'
78
+
79
+ a = s.shift 2
80
+ assert_equal [ 1, 2 ], a, 'seq shift 2 result'
81
+ assert_equal [ 2 => 5, 4 => 7, 6 => 9, :a => ?a, :b => ?b,
82
+ :c => ?c ], s.to_a, 'sarah after shift 2'
83
+
84
+ s = Sarah[ 0, 5 => 1, 7 => 2, 9 => 3, :a => ?a]
85
+ a = s.shift 2
86
+ assert_equal [ 0, 1 ], a, 'seq/spr shift 2 result'
87
+ a = s.shift 2
88
+ assert_equal [ 2, 3 ], a, 'spr shift 2 ressult'
89
+ assert_equal [ :a => ?a ], s.to_a, 'sarah after shifting'
90
+ end
91
+
92
+ def test_unset_at
93
+ s = Sarah[ 0, 1, 2, 5 => 5, 7 => 7, 9 => 9,
94
+ :a => ?a, :b => ?b, :c => ?c ]
95
+
96
+ assert_equal ?b, s.unset_at(:b), 'unset_at :b'
97
+ assert_equal 7, s.unset_at(7), 'unset_at 7'
98
+ assert_equal 1, s.unset_at(1), 'unset_at 1'
99
+ assert_equal [ 0, 2 => 2, 5 => 5, 9 => 9, :a => ?a, :c => ?c ], s.to_a,
100
+ 'after unset_at x3'
101
+ end
102
+
103
+ def test_unset_if
104
+ s = Sarah[1, 2, 3, 5 => 1, 6 => 2, 7 => 3, :a => 1, :b => 2, :c => 3]
105
+
106
+ s.unset_if(:ary) { |v, k| v == 1 }
107
+ assert_equal [1 => 2, 2 => 3, 6 => 2, 7 => 3,
108
+ :a => 1, :b => 2, :c => 3 ], s.to_a, 'unset_if :ary == 1'
109
+
110
+ s.unset_if(:rnd) { |v, k| v == 2 }
111
+ assert_equal [1 => 2, 2 => 3, 6 => 2, 7 => 3, :a => 1, :c => 3 ],
112
+ s.to_a, 'unset_if :rnd == 2'
113
+
114
+ s.unset_if { |v, k| v == 3 }
115
+ assert_equal [1 => 2, 6 => 2, :a => 1 ], s.to_a, 'unset_if == 3'
116
+ end
117
+
118
+ def test_unset_value
119
+ s = Sarah[1, 2, 3, 4, 6=>1, 7=>2, 8=>3, 9=>4, :a=>2, :b=>3 ]
120
+
121
+ s.unset_value 2, :rnd
122
+ assert_equal [1, 2, 3, 4, 6 => 1, 7 => 2, 8 => 3, 9 => 4, :b => 3],
123
+ s.to_a, 'unset_value 2, :rnd'
124
+
125
+ s.unset_value 1, :ary
126
+ assert_equal [1 => 2, 2 => 3, 3 => 4, 7 => 2, 8 => 3, 9 => 4,
127
+ :b => 3], s.to_a, 'unset_value 1, :ary'
128
+
129
+ s.unset_value 3
130
+ assert_equal [1 => 2, 3 => 4, 7 => 2, 9 => 4], s.to_a, 'unset_value 3'
131
+ end
132
+
133
+ end
134
+
135
+ # END
@@ -0,0 +1,36 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ class TestSarah_10 < MiniTest::Unit::TestCase
5
+
6
+ def test_count
7
+ s0 = Sarah[ 1, 3, 5 => 1, 6 => 3, :a => 3, :b => 5 ]
8
+
9
+ assert_equal 2, s0.count(:ary, 1), 'count :ary, 1'
10
+ assert_equal 4, s0.count(:ary) { |i| i.odd? }, 'count :ary, odd'
11
+ assert_equal 1, s0.count(:rnd, 3), 'count :rnd, 3'
12
+ assert_equal 2, s0.count(:rnd) { |i| i.odd? }, 'count :rnd, odd'
13
+ assert_equal 3, s0.count(:all, 3), 'count :all, 3'
14
+ assert_equal 6, s0.count(:all) { |i| i.odd? }, 'count :all, odd'
15
+ assert_equal 6, s0.count { |i| i.odd? }, 'count odd'
16
+ end
17
+
18
+ def test_size
19
+ s = Sarah[1, 2, 3, 4, 7 => 5, 8 => 6, :ix => 7]
20
+
21
+ assert_equal 4, s.size(:seq), 'size :seq'
22
+ assert_equal 2, s.size(:spr), 'size :spr'
23
+ assert_equal 6, s.size(:ary), 'size :ary'
24
+ assert_equal 1, s.size(:rnd), 'size :rnd'
25
+ assert_equal 7, s.size(:all), 'size :all'
26
+ assert_equal 7, s.size, 'size'
27
+
28
+ s.unset_value 3
29
+ assert_equal 2, s.size(:seq), 'size :seq after unset'
30
+ assert_equal 3, s.size(:spr), 'size :spr after unset'
31
+ assert_equal 5, s.size(:ary), 'size :ary after unset'
32
+ end
33
+
34
+ end
35
+
36
+ # END
@@ -0,0 +1,49 @@
1
+ require 'minitest/autorun'
2
+ require 'sarah'
3
+
4
+ class TestSarah_11 < MiniTest::Unit::TestCase
5
+
6
+ def test_each
7
+ s = Sarah[1, 2, 5 => 3, 6 => 4, :a => 5, :b => 6]
8
+
9
+ h = {}
10
+ s.each(:seq) { |k, v| h[k] = v }
11
+ assert_equal({ 0 => 1, 1 => 2 }, h, 'each :seq')
12
+
13
+ h.clear
14
+ s.each(:spr) { |k, v| h[k] = v }
15
+ assert_equal({ 5 => 3, 6 => 4 }, h, 'each :spr')
16
+
17
+ h.clear
18
+ s.each(:ary) { |k, v| h[k] = v }
19
+ assert_equal({ 0 => 1, 1 => 2, 5 => 3, 6 => 4 }, h, 'each :ary')
20
+
21
+ h.clear
22
+ s.each(:rnd) { |k, v| h[k] = v }
23
+ assert_equal({ :a => 5, :b => 6 }, h, 'each :rnd')
24
+
25
+ h.clear
26
+ s.each(:all) { |k, v| h[k] = v }
27
+ assert_equal s.to_h, h, 'each :all'
28
+
29
+ a = []
30
+ s.reverse_each { |k, v| a << [k, v] }
31
+ assert_equal [[6, 4], [5, 3], [1, 2], [0, 1]], a, 'reverse_each'
32
+ end
33
+
34
+ def test_pairs_at
35
+ s = Sarah[1, 2, 5 => 3, 6 => 4, :a => 5, :b => 6]
36
+
37
+ assert_equal s.to_h, s.pairs_at(*s.keys), 'to_h matches pairs_at(*keys)'
38
+ end
39
+
40
+ def test_values_at
41
+ s = Sarah[1, 2, 5 => 3, 6 => 4, :a => 5, :b => 6]
42
+
43
+ assert_equal s.values, s.values_at(*s.keys),
44
+ 'values matches values_at(*keys)'
45
+ end
46
+
47
+ end
48
+
49
+ # END