mug 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,24 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- if RUBY_VERSION.to_f < 1.9
5
- class Symbol
6
- def next
7
- to_s.next.to_sym
8
- end
9
- end
10
- end
11
-
12
- require_relative '../lib/mug/hashmap'
13
- class Test_hashmap < Test::Unit::TestCase
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
- end
24
-
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ if RUBY_VERSION.to_f < 1.9
5
+ class Symbol
6
+ def next
7
+ to_s.next.to_sym
8
+ end
9
+ end
10
+ end
11
+
12
+ require_relative '../lib/mug/hash/map'
13
+ class Test_hashmap < Test::Unit::TestCase
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
+ end
24
+
@@ -1,62 +1,62 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- $coercable = Object.new
5
- def $coercable.to_h
6
- {:a=>0, :z=>99}
7
- end
8
-
9
- require_relative '../lib/mug/hashop'
10
- class Test_hashop < Test::Unit::TestCase
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
-
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
-
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
-
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
-
58
- # Failure
59
- assert_raise(ArgumentError) { h << Object.new }
60
- end
61
- end
62
-
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ $coercable = Object.new
5
+ def $coercable.to_h
6
+ {:a=>0, :z=>99}
7
+ end
8
+
9
+ require_relative '../lib/mug/hash/operations'
10
+ class Test_hashop < Test::Unit::TestCase
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
+
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
+
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
+
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
+
58
+ # Failure
59
+ assert_raise(ArgumentError) { h << Object.new }
60
+ end
61
+ end
62
+
@@ -1,18 +1,18 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- require_relative '../lib/mug/iterator/for'
5
- class IterForTest < Test::Unit::TestCase
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
- end
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/iterator/for'
5
+ class IterForTest < Test::Unit::TestCase
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
+ end
@@ -1,18 +1,18 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- require_relative '../lib/mug/iterator/method'
5
- class MethodToIterTest < Test::Unit::TestCase
6
- def test_to_iter
7
- # Test on Integer#next => Integer
8
- assert_equal([0, 1, 2, 3, 4], 0.method(:next).to_iter.take(5))
9
- # Test on String#succ => String
10
- assert_equal(%w[a b c d e f g h i j], 'a'.method(:succ).to_iter.take(10))
11
- # Test on Integer#inspect => String#inspect => String
12
- assert_equal([1, "1", "\"1\""], 1.method(:inspect).to_iter.take(3))
13
- end
14
- def test_iter_for_args
15
- # Test with a parameter
16
- assert_equal([0, 2, 4, 6, 8], 0.method(:+).to_iter(2).take(5))
17
- end
18
- end
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/iterator/method'
5
+ class MethodToIterTest < Test::Unit::TestCase
6
+ def test_to_iter
7
+ # Test on Integer#next => Integer
8
+ assert_equal([0, 1, 2, 3, 4], 0.method(:next).to_iter.take(5))
9
+ # Test on String#succ => String
10
+ assert_equal(%w[a b c d e f g h i j], 'a'.method(:succ).to_iter.take(10))
11
+ # Test on Integer#inspect => String#inspect => String
12
+ assert_equal([1, "1", "\"1\""], 1.method(:inspect).to_iter.take(3))
13
+ end
14
+ def test_iter_for_args
15
+ # Test with a parameter
16
+ assert_equal([0, 2, 4, 6, 8], 0.method(:+).to_iter(2).take(5))
17
+ end
18
+ end
@@ -1,90 +1,90 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- module MaybeTest
5
- class A
6
- def initialize; @b = B.new; end
7
- attr_accessor :b
8
- end
9
-
10
- class B
11
- def initialize; @c = C.new; end
12
- attr_accessor :c
13
- end
14
-
15
- class C
16
- def to_i; 1; end
17
- end
18
- end
19
-
20
- def false.c
21
- 2
22
- end
23
-
24
- require_relative '../lib/mug/maybe'
25
- class Test_maybe < Test::Unit::TestCase
26
- def test_maybe_block_nil
27
- a = MaybeTest::A.new
28
- assert_equal( 1, a.maybe{ b.maybe{ c } }.to_i )
29
- assert_equal( 1, a.maybe{ b.c.to_i } )
30
- assert_equal( 1, a.maybe{ b }.c.to_i )
31
- a.b.c = nil
32
- assert_equal( 0, a.maybe{ b.maybe{ c } }.to_i )
33
- assert_equal( 0, a.maybe{ b.c.to_i } )
34
- assert_equal( 0, a.maybe{ b }.c.to_i )
35
- a.b = nil
36
- assert_nil( a.maybe{ b.maybe{ c } } )
37
- assert_raise(NoMethodError) { a.maybe{ b.c } }
38
- assert_raise(NoMethodError) { a.maybe{ b }.c }
39
- a = nil
40
- assert_nil( a.maybe{ b.maybe{ c } } )
41
- assert_nil( a.maybe{ b.c } )
42
- assert_raise(NoMethodError) { a.maybe{ b }.c }
43
- end
44
- def test_maybe_block_false
45
- a = MaybeTest::A.new
46
- assert_equal( 1, a.maybe{ b.maybe{ c } }.to_i )
47
- assert_equal( 1, a.maybe{ b.c.to_i } )
48
- assert_equal( 1, a.maybe{ b }.c.to_i )
49
- a.b.c = false
50
- assert_raise(NoMethodError) { a.maybe{ b.maybe{ c } }.to_i }
51
- assert_raise(NoMethodError) { a.maybe{ b.c.to_i } }
52
- assert_raise(NoMethodError) { a.maybe{ b }.c.to_i }
53
- a.b = false
54
- assert_equal( false, a.maybe{ b.maybe{ c } } )
55
- assert_equal( 2, a.maybe{ b.c } )
56
- assert_equal( 2, a.maybe{ b }.c )
57
- a = false
58
- assert_equal( false, a.maybe{ b.maybe{ c } } )
59
- assert_equal( false, a.maybe{ b.c } )
60
- assert_equal( 2, a.maybe{ b }.c )
61
- end
62
- def test_maybe_delegator_nil
63
- a = MaybeTest::A.new
64
- assert_equal( 1, a.maybe.b.maybe.c.to_i )
65
- assert_equal( 1, a.maybe.b.c.to_i )
66
- a.b.c = nil
67
- assert_nil( a.maybe.b.maybe.c )
68
- assert_nil( a.maybe.b.c )
69
- a.b = nil
70
- assert_nil( a.maybe.b.maybe.c )
71
- assert_raise(NoMethodError) { a.maybe.b.c }
72
- a = nil
73
- assert_nil( a.maybe.b.maybe.c )
74
- assert_raise(NoMethodError) { a.maybe.b.c }
75
- end
76
- def test_maybe_delegator_false
77
- a = MaybeTest::A.new
78
- assert_equal( 1, a.maybe.b.maybe.c.to_i )
79
- assert_equal( 1, a.maybe.b.c.to_i )
80
- a.b.c = false
81
- assert_equal( false, a.maybe.b.maybe.c )
82
- assert_equal( false, a.maybe.b.c )
83
- a.b = false
84
- assert_equal( false, a.maybe.b.maybe.c )
85
- assert_equal( 2, a.maybe.b.c )
86
- a = false
87
- assert_equal( false, a.maybe.b.maybe.c )
88
- assert_equal( 2, a.maybe.b.c )
89
- end
90
- end
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ module MaybeTest
5
+ class A
6
+ def initialize; @b = B.new; end
7
+ attr_accessor :b
8
+ end
9
+
10
+ class B
11
+ def initialize; @c = C.new; end
12
+ attr_accessor :c
13
+ end
14
+
15
+ class C
16
+ def to_i; 1; end
17
+ end
18
+ end
19
+
20
+ def false.c
21
+ 2
22
+ end
23
+
24
+ require_relative '../lib/mug/maybe'
25
+ class Test_maybe < Test::Unit::TestCase
26
+ def test_maybe_block_nil
27
+ a = MaybeTest::A.new
28
+ assert_equal( 1, a.maybe{ b.maybe{ c } }.to_i )
29
+ assert_equal( 1, a.maybe{ b.c.to_i } )
30
+ assert_equal( 1, a.maybe{ b }.c.to_i )
31
+ a.b.c = nil
32
+ assert_equal( 0, a.maybe{ b.maybe{ c } }.to_i )
33
+ assert_equal( 0, a.maybe{ b.c.to_i } )
34
+ assert_equal( 0, a.maybe{ b }.c.to_i )
35
+ a.b = nil
36
+ assert_nil( a.maybe{ b.maybe{ c } } )
37
+ assert_raise(NoMethodError) { a.maybe{ b.c } }
38
+ assert_raise(NoMethodError) { a.maybe{ b }.c }
39
+ a = nil
40
+ assert_nil( a.maybe{ b.maybe{ c } } )
41
+ assert_nil( a.maybe{ b.c } )
42
+ assert_raise(NoMethodError) { a.maybe{ b }.c }
43
+ end
44
+ def test_maybe_block_false
45
+ a = MaybeTest::A.new
46
+ assert_equal( 1, a.maybe{ b.maybe{ c } }.to_i )
47
+ assert_equal( 1, a.maybe{ b.c.to_i } )
48
+ assert_equal( 1, a.maybe{ b }.c.to_i )
49
+ a.b.c = false
50
+ assert_raise(NoMethodError) { a.maybe{ b.maybe{ c } }.to_i }
51
+ assert_raise(NoMethodError) { a.maybe{ b.c.to_i } }
52
+ assert_raise(NoMethodError) { a.maybe{ b }.c.to_i }
53
+ a.b = false
54
+ assert_equal( false, a.maybe{ b.maybe{ c } } )
55
+ assert_equal( 2, a.maybe{ b.c } )
56
+ assert_equal( 2, a.maybe{ b }.c )
57
+ a = false
58
+ assert_equal( false, a.maybe{ b.maybe{ c } } )
59
+ assert_equal( false, a.maybe{ b.c } )
60
+ assert_equal( 2, a.maybe{ b }.c )
61
+ end
62
+ def test_maybe_delegator_nil
63
+ a = MaybeTest::A.new
64
+ assert_equal( 1, a.maybe.b.maybe.c.to_i )
65
+ assert_equal( 1, a.maybe.b.c.to_i )
66
+ a.b.c = nil
67
+ assert_nil( a.maybe.b.maybe.c )
68
+ assert_nil( a.maybe.b.c )
69
+ a.b = nil
70
+ assert_nil( a.maybe.b.maybe.c )
71
+ assert_raise(NoMethodError) { a.maybe.b.c }
72
+ a = nil
73
+ assert_nil( a.maybe.b.maybe.c )
74
+ assert_raise(NoMethodError) { a.maybe.b.c }
75
+ end
76
+ def test_maybe_delegator_false
77
+ a = MaybeTest::A.new
78
+ assert_equal( 1, a.maybe.b.maybe.c.to_i )
79
+ assert_equal( 1, a.maybe.b.c.to_i )
80
+ a.b.c = false
81
+ assert_equal( false, a.maybe.b.maybe.c )
82
+ assert_equal( false, a.maybe.b.c )
83
+ a.b = false
84
+ assert_equal( false, a.maybe.b.maybe.c )
85
+ assert_equal( 2, a.maybe.b.c )
86
+ a = false
87
+ assert_equal( false, a.maybe.b.maybe.c )
88
+ assert_equal( 2, a.maybe.b.c )
89
+ end
90
+ end
@@ -1,13 +1,13 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- require_relative '../lib/mug/self'
5
- class Test_self < Test::Unit::TestCase
6
- def test_self
7
- obj = Object.new
8
- assert_equal( 1, 1.self )
9
- assert_equal( obj, obj.self )
10
- assert_equal( 6, 2.self{|i| i*3 } )
11
- assert_equal( {1=>[1,1], 2=>[2,2], 3=>[3]}, [1,1,2,2,3].group_by(&:self) )
12
- end
13
- end
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/self'
5
+ class Test_self < Test::Unit::TestCase
6
+ def test_self
7
+ obj = Object.new
8
+ assert_equal( 1, 1.self )
9
+ assert_equal( obj, obj.self )
10
+ assert_equal( 6, 2.self{|i| i*3 } )
11
+ assert_equal( {1=>[1,1], 2=>[2,2], 3=>[3]}, [1,1,2,2,3].group_by(&:self) )
12
+ end
13
+ end