mug 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mug/and-or.rb +26 -26
- data/lib/mug/any-and-all.rb +16 -16
- data/lib/mug/apply.rb +37 -37
- data/lib/mug/array/extend.rb +43 -43
- data/lib/mug/array/minus.rb +32 -32
- data/lib/mug/bool.rb +71 -71
- data/lib/mug/clamp.rb +35 -35
- data/lib/mug/counts.rb +27 -27
- data/lib/mug/fragile-method-chain.rb +42 -42
- data/lib/mug/hash/map.rb +75 -75
- data/lib/mug/hash/operations.rb +47 -47
- data/lib/mug/iterator/for.rb +7 -7
- data/lib/mug/iterator/method.rb +7 -7
- data/lib/mug/iterator_c.rb +14 -14
- data/lib/mug/loop-with.rb +30 -29
- data/lib/mug/matchdata/each.rb +40 -40
- data/lib/mug/matchdata/hash.rb +29 -29
- data/lib/mug/maybe.rb +33 -33
- data/lib/mug/negativity.rb +20 -20
- data/lib/mug/not.rb +6 -6
- data/lib/mug/rexproc.rb +6 -6
- data/lib/mug/self.rb +31 -31
- data/lib/mug/to_h.rb +2 -2
- data/lib/mug/top.rb +100 -100
- data/test/test-and-or.rb +32 -32
- data/test/test-any-and-all.rb +19 -19
- data/test/test-apply.rb +47 -47
- data/test/test-array-extend.rb +54 -54
- data/test/test-array-minus.rb +11 -11
- data/test/test-bool.rb +48 -48
- data/test/test-clamp.rb +42 -42
- data/test/test-counts.rb +21 -21
- data/test/test-fragile-method-chain.rb +56 -56
- data/test/test-hashmap.rb +14 -14
- data/test/test-hashop.rb +47 -47
- data/test/test-iterator-for.rb +12 -12
- data/test/test-loop-with.rb +117 -66
- data/test/test-matchdata_each.rb +50 -50
- data/test/test-matchdata_hash.rb +40 -40
- data/test/test-maybe.rb +76 -76
- data/test/test-negativity.rb +40 -40
- data/test/test-not.rb +53 -53
- data/test/test-rexproc.rb +6 -6
- data/test/test-self.rb +21 -21
- data/test/test-tau.rb +12 -12
- data/test/test-top.rb +100 -100
- metadata +4 -4
data/test/test-hashmap.rb
CHANGED
@@ -2,23 +2,23 @@ require 'test/unit'
|
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
4
|
if RUBY_VERSION.to_f < 1.9
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
class Symbol
|
6
|
+
def next
|
7
|
+
to_s.next.to_sym
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|
11
11
|
|
12
12
|
require_relative '../lib/mug/hash/map'
|
13
13
|
class Test_hashmap < Test::Unit::TestCase
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
def test_hashmap
|
15
|
+
h = {'s'=>1, :x=>2, 3=>'a'}
|
16
|
+
h_k_next = {'t'=>1, :y=>2, 4=>'a'}
|
17
|
+
h_v_next = {'s'=>2, :x=>3, 3=>'b'}
|
18
|
+
h_p_next = {'t'=>2, :y=>3, 4=>'b'}
|
19
|
+
assert_equal( h_k_next, h.map_keys {|k| k.next } )
|
20
|
+
assert_equal( h_v_next, h.map_values{|v| v.next } )
|
21
|
+
assert_equal( h_p_next, h.map_pairs {|k,v| [k.next,v.next] } )
|
22
|
+
end
|
23
23
|
end
|
24
24
|
|
data/test/test-hashop.rb
CHANGED
@@ -3,60 +3,60 @@ $VERBOSE = true
|
|
3
3
|
|
4
4
|
$coercable = Object.new
|
5
5
|
def $coercable.to_h
|
6
|
-
|
6
|
+
{:a=>0, :z=>99}
|
7
7
|
end
|
8
8
|
|
9
9
|
require_relative '../lib/mug/hash/operations'
|
10
10
|
class Test_hashop < Test::Unit::TestCase
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
11
|
+
def test_hash_or
|
12
|
+
h = { :a => 1, :b => 2 }
|
13
|
+
[
|
14
|
+
[{}, h, h],
|
15
|
+
[{:c=>3}, h, {:a=>1, :b=>2, :c=>3}],
|
16
|
+
[{:a=>0, :b=>0}, h, {:a=>0, :b=>0}],
|
17
|
+
[{:a=>0, :c=>3}, h, {:a=>0, :b=>2, :c=>3}],
|
18
|
+
[h, {}, h],
|
19
|
+
].each do |a, b, x|
|
20
|
+
assert_equal( a|b, x )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def test_hash_plus
|
24
|
+
h = { :a => 1, :b => 2 }
|
25
|
+
[
|
26
|
+
[{}, h, h],
|
27
|
+
[{:c=>3}, h, {:a=>1, :b=>2, :c=>3}],
|
28
|
+
[{:a=>0, :b=>0}, h, h],
|
29
|
+
[{:a=>0, :c=>3}, h, {:a=>1, :b=>2, :c=>3}],
|
30
|
+
[h, {}, h],
|
31
|
+
].each do |a, b, x|
|
32
|
+
assert_equal( a+b, x )
|
33
|
+
end
|
34
|
+
end
|
35
|
+
def test_hash_poke
|
36
|
+
h = {}
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
38
|
+
# Regular Hash poking
|
39
|
+
assert_nothing_raised { h << {:a=>1} }
|
40
|
+
assert_equal h, {:a=>1}
|
41
|
+
assert_nothing_raised { h << {:b=>2} }
|
42
|
+
assert_equal h, {:a=>1,:b=>2}
|
43
|
+
assert_nothing_raised { h << {:a=>3} }
|
44
|
+
assert_equal h, {:a=>3,:b=>2}
|
45
|
+
assert_nothing_raised { h << {:a=>1,:c=>3} }
|
46
|
+
assert_equal h, {:a=>1,:b=>2,:c=>3}
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
48
|
+
# Two-element Array poking
|
49
|
+
assert_nothing_raised { h << [:d,1] }
|
50
|
+
assert_equal h, {:a=>1,:b=>2,:c=>3,:d=>1}
|
51
|
+
assert_nothing_raised { h << [:d,4] }
|
52
|
+
assert_equal h, {:a=>1,:b=>2,:c=>3,:d=>4}
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
# Objects with .to_h
|
55
|
+
assert_nothing_raised { h << $coercable }
|
56
|
+
assert_equal h, {:a=>0,:b=>2,:c=>3,:d=>4,:z=>99}
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
# Failure
|
59
|
+
assert_raise(ArgumentError) { h << Object.new }
|
60
|
+
end
|
61
61
|
end
|
62
62
|
|
data/test/test-iterator-for.rb
CHANGED
@@ -3,16 +3,16 @@ $VERBOSE = true
|
|
3
3
|
|
4
4
|
require_relative '../lib/mug/iterator/for'
|
5
5
|
class IterForTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
6
|
+
def test_iter_for
|
7
|
+
# Test on Integer#next => Integer
|
8
|
+
assert_equal([0, 1, 2, 3, 4], 0.iter_for(:next).take(5))
|
9
|
+
# Test on String#succ => String
|
10
|
+
assert_equal(%w[a b c d e f g h i j], 'a'.iter_for(:succ).take(10))
|
11
|
+
# Test on Integer#inspect => String#inspect => String
|
12
|
+
assert_equal([1, "1", "\"1\""], 1.iter_for(:inspect).take(3))
|
13
|
+
end
|
14
|
+
def test_iter_for_args
|
15
|
+
# Test with a parameter
|
16
|
+
assert_equal([0, 2, 4, 6, 8], 0.iter_for(:+, 2).take(5))
|
17
|
+
end
|
18
18
|
end
|
data/test/test-loop-with.rb
CHANGED
@@ -4,72 +4,123 @@ $VERBOSE = true
|
|
4
4
|
require_relative '../lib/mug/loop-with'
|
5
5
|
class Test_loop_with < Test::Unit::TestCase
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
7
|
+
def test_loop_with_index__block
|
8
|
+
a = []
|
9
|
+
b = []
|
10
|
+
x = loop_with_index do |i|
|
11
|
+
a << i
|
12
|
+
break if a.length >= 3
|
13
|
+
end
|
14
|
+
y = loop_with_index do |i|
|
15
|
+
b << i
|
16
|
+
raise StopIteration if b.length >= 3
|
17
|
+
end
|
18
|
+
assert_equal( [0,1,2], a )
|
19
|
+
assert_equal( [0,1,2], b )
|
20
|
+
assert_equal( 2, y )
|
21
|
+
end
|
22
|
+
def test_loop_with_index__block_offset
|
23
|
+
a = []
|
24
|
+
b = []
|
25
|
+
x = loop_with_index(10) do |i|
|
26
|
+
a << i
|
27
|
+
break if a.length >= 3
|
28
|
+
end
|
29
|
+
y = loop_with_index(10) do |i|
|
30
|
+
b << i
|
31
|
+
raise StopIteration if b.length >= 3
|
32
|
+
end
|
33
|
+
assert_equal( [10,11,12], a )
|
34
|
+
assert_equal( [10,11,12], b )
|
35
|
+
assert_equal( 12, y )
|
36
|
+
end
|
37
|
+
def test_loop_with_index__enum
|
38
|
+
enum = loop_with_index
|
39
|
+
assert_respond_to( enum, :each )
|
40
|
+
|
41
|
+
a = []
|
42
|
+
b = []
|
43
|
+
x = enum.each do |i|
|
44
|
+
a << i
|
45
|
+
break if a.length >= 3
|
46
|
+
end
|
47
|
+
y = enum.each do |i|
|
48
|
+
b << i
|
49
|
+
raise StopIteration if b.length >= 3
|
50
|
+
end
|
51
|
+
assert_equal( [0,1,2], a )
|
52
|
+
assert_equal( [0,1,2], b )
|
53
|
+
assert_equal( 2, y )
|
54
|
+
end
|
55
|
+
def test_loop_with_index__enum_offset
|
56
|
+
enum = loop_with_index(10)
|
57
|
+
assert_respond_to( enum, :each )
|
58
|
+
|
59
|
+
a = []
|
60
|
+
b = []
|
61
|
+
x = enum.each do |i|
|
62
|
+
a << i
|
63
|
+
break if a.length >= 3
|
64
|
+
end
|
65
|
+
y = enum.each do |i|
|
66
|
+
b << i
|
67
|
+
raise StopIteration if b.length >= 3
|
68
|
+
end
|
69
|
+
assert_equal( [10,11,12], a )
|
70
|
+
assert_equal( [10,11,12], b )
|
71
|
+
assert_equal( 12, y )
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_loop_with_object__block
|
75
|
+
o = "x"
|
76
|
+
a = []
|
77
|
+
b = []
|
78
|
+
x = loop_with_object(o) do |e|
|
79
|
+
a << e
|
80
|
+
break if a.length >= 3
|
81
|
+
end
|
82
|
+
y = loop_with_object(o) do |e|
|
83
|
+
b << e
|
84
|
+
raise StopIteration if b.length >= 3
|
85
|
+
end
|
86
|
+
assert_equal( [o,o,o], a )
|
87
|
+
assert_equal( [o,o,o], b )
|
88
|
+
assert( y.equal?(o), "should return `obj`" )
|
89
|
+
end
|
90
|
+
def test_loop_with_object__enum
|
91
|
+
o = "x"
|
92
|
+
enum = loop_with_object(o)
|
93
|
+
assert_respond_to( enum, :each )
|
94
|
+
|
95
|
+
a = []
|
96
|
+
b = []
|
97
|
+
x = enum.each do |e|
|
98
|
+
a << e
|
99
|
+
break if a.length >= 3
|
100
|
+
end
|
101
|
+
y = enum.each do |e|
|
102
|
+
b << e
|
103
|
+
raise StopIteration if b.length >= 3
|
104
|
+
end
|
105
|
+
assert_equal( [o,o,o], a )
|
106
|
+
assert_equal( [o,o,o], b )
|
107
|
+
assert( y.equal?(o), "should return `obj`" )
|
108
|
+
|
109
|
+
c = []
|
110
|
+
loop_with_object(c).with_index do |a, i|
|
111
|
+
a << i
|
112
|
+
break if i >= 2
|
113
|
+
end
|
114
|
+
assert_equal( [0,1,2], c )
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_loop_with_index__error
|
118
|
+
assert_raise(RuntimeError) { loop_with_index {|i| raise 'die' } }
|
119
|
+
end
|
120
|
+
def test_loop_with_object__error
|
121
|
+
a = []
|
122
|
+
assert_raise(RuntimeError) { loop_with_object(a) {|e| raise 'die' } }
|
123
|
+
end
|
73
124
|
|
74
125
|
end
|
75
126
|
|
data/test/test-matchdata_each.rb
CHANGED
@@ -3,55 +3,55 @@ $VERBOSE = true
|
|
3
3
|
|
4
4
|
require_relative '../lib/mug/matchdata/each'
|
5
5
|
class Test_matchdata_each < Test::Unit::TestCase
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
6
|
+
def test__matchdata__each
|
7
|
+
str = 'a12b'
|
8
|
+
[
|
9
|
+
[/( \d)( \d)/x.match(str), %w[ 12 1 2 ]],
|
10
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), %w[ 12 1 2 ]],
|
11
|
+
[/( \d)(?<y>\d)/x.match(str), %w[ 12 2 ]], # ):
|
12
|
+
].each do |m, x|
|
13
|
+
assert_equal( x, m.each.to_a )
|
14
|
+
end
|
15
|
+
end
|
16
|
+
def test__matchdata__each_capture
|
17
|
+
str = 'a12b'
|
18
|
+
[
|
19
|
+
[/( \d)( \d)/x.match(str), [[ 1 ,'1'],[ 2 ,'2']]],
|
20
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [['x','1'],['y','2']]],
|
21
|
+
[/( \d)(?<y>\d)/x.match(str), [ ['y','2']]], # ):
|
22
|
+
].each do |m, x|
|
23
|
+
assert_equal( x, m.each_capture.to_a )
|
24
|
+
end
|
25
|
+
end
|
26
|
+
def test__matchdata__each_named_capture
|
27
|
+
str = 'a12b'
|
28
|
+
[
|
29
|
+
[/( \d)( \d)/x.match(str), [ ]],
|
30
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [['x','1'],['y','2']]],
|
31
|
+
[/( \d)(?<y>\d)/x.match(str), [ ['y','2']]],
|
32
|
+
].each do |m, x|
|
33
|
+
assert_equal( x, m.each_named_capture.to_a )
|
34
|
+
end
|
35
|
+
end
|
36
|
+
def test__matchdata__each_positional_capture
|
37
|
+
str = 'a12b'
|
38
|
+
[
|
39
|
+
[/( \d)( \d)/x.match(str), [[ 1, '1'],[ 2 ,'2']]],
|
40
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [ ]],
|
41
|
+
[/( \d)(?<y>\d)/x.match(str), [ ]], # D:
|
42
|
+
].each do |m, x|
|
43
|
+
assert_equal( x, m.each_positional_capture.to_a )
|
44
|
+
end
|
45
|
+
end
|
46
|
+
def test__matchdata__each_positional_capture2
|
47
|
+
str = 'a12b'
|
48
|
+
[
|
49
|
+
[/( \d)( \d)/x.match(str), [[ 1, '1'],[ 2 ,'2']]],
|
50
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [[ 1, '1'],[ 2 ,'2']]],
|
51
|
+
[/( \d)(?<y>\d)/x.match(str), [[ 1, '2']]], # ):
|
52
|
+
].each do |m, x|
|
53
|
+
assert_equal( x, m.each_positional_capture(include_names: true).to_a )
|
54
|
+
end
|
55
|
+
end
|
56
56
|
end
|
57
57
|
|