mug 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,22 +1,22 @@
1
- require 'test/unit'
2
- require 'bigdecimal'
3
- require 'bigdecimal/math'
4
-
5
- $VERBOSE = true
6
- require_relative '../lib/mug/tau'
7
- class Test_tau < Test::Unit::TestCase
8
- def test_math_tau
9
- # 6.283185307179586..
10
- assert_equal(Math::TAU, Math::PI*2.0)
11
- end
12
-
13
- include BigMath
14
- def test_TAU
15
- assert_equal(TAU(1).round(1).to_s('F'), '6.3')
16
- assert_equal(TAU(100).truncate(100).to_s('F'), '6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696506842341359')
17
- assert_raise(ArgumentError) { TAU(0) }
18
- assert_raise(ArgumentError) { TAU(-3) }
19
- assert_raise(ArgumentError) { TAU('barf') }
20
- end
21
- end
22
-
1
+ require 'test/unit'
2
+ require 'bigdecimal'
3
+ require 'bigdecimal/math'
4
+
5
+ $VERBOSE = true
6
+ require_relative '../lib/mug/tau'
7
+ class Test_tau < Test::Unit::TestCase
8
+ def test_math_tau
9
+ # 6.283185307179586..
10
+ assert_equal(Math::TAU, Math::PI*2.0)
11
+ end
12
+
13
+ include BigMath
14
+ def test_TAU
15
+ assert_equal(TAU(1).round(1).to_s('F'), '6.3')
16
+ assert_equal(TAU(100).truncate(100).to_s('F'), '6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696506842341359')
17
+ assert_raise(ArgumentError) { TAU(0) }
18
+ assert_raise(ArgumentError) { TAU(-3) }
19
+ assert_raise(ArgumentError) { TAU('barf') }
20
+ end
21
+ end
22
+
@@ -1,16 +1,16 @@
1
- require 'test/unit'
2
-
3
- $VERBOSE = true
4
- $ruby_2_1 = RUBY_VERSION.to_f >= 2.1
5
- require_relative '../lib/mug/to_h'
6
- class Test_enum_to_h < Test::Unit::TestCase
7
- def test_enum_to_h
8
- assert_equal( {}, [].to_h )
9
- assert_equal( {1=>nil,2=>nil}, [1,2].to_h ) unless $ruby_2_1
10
- assert_equal( {1=>nil,2=>nil}, (1..2).to_h ) unless $ruby_2_1
11
- assert_equal( {1=>2,3=>4}, [[1,2],[3,4]].to_h )
12
- assert_equal( {1=>4}, [[1,2],[1,4]].to_h )
13
- assert_raise(ArgumentError) { [[1,2,3]].to_h }
14
- end
15
- end
16
-
1
+ require 'test/unit'
2
+
3
+ $VERBOSE = true
4
+ $ruby_2_1 = RUBY_VERSION.to_f >= 2.1
5
+ require_relative '../lib/mug/to_h'
6
+ class Test_enum_to_h < Test::Unit::TestCase
7
+ def test_enum_to_h
8
+ assert_equal( {}, [].to_h )
9
+ assert_equal( {1=>nil,2=>nil}, [1,2].to_h ) unless $ruby_2_1
10
+ assert_equal( {1=>nil,2=>nil}, (1..2).to_h ) unless $ruby_2_1
11
+ assert_equal( {1=>2,3=>4}, [[1,2],[3,4]].to_h )
12
+ assert_equal( {1=>4}, [[1,2],[1,4]].to_h )
13
+ assert_raise(ArgumentError) { [[1,2,3]].to_h }
14
+ end
15
+ end
16
+
@@ -0,0 +1,116 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/top'
5
+ class Test_and_or < Test::Unit::TestCase
6
+
7
+ def test_top
8
+ a = [1,3,5,2,4,3]
9
+ assert_equal( [5], a.top )
10
+ assert_equal( [5,4,3], a.top(3) )
11
+ assert_equal( [5,4,3,3,2,1], a.top(100) )
12
+ end
13
+ def test_bottom
14
+ a = [3,1,4,3,2,5]
15
+ assert_equal( [1], a.bottom )
16
+ assert_equal( [1,2,3], a.bottom(3) )
17
+ assert_equal( [1,2,3,3,4,5], a.bottom(100) )
18
+ end
19
+
20
+ def test_top__block
21
+ a = [1,3,5,2,4,3]
22
+ b = proc{|x,y| y<=>x }
23
+ assert_equal( [1], a.top(&b) )
24
+ assert_equal( [1,2,3], a.top(3,&b) )
25
+ assert_equal( [1,2,3,3,4,5], a.top(100,&b) )
26
+ end
27
+ def test_bottom__block
28
+ a = [3,1,4,3,2,5]
29
+ b = proc{|x,y| y<=>x }
30
+ assert_equal( [5], a.bottom(&b) )
31
+ assert_equal( [5,4,3], a.bottom(3,&b) )
32
+ assert_equal( [5,4,3,3,2,1], a.bottom(100,&b) )
33
+ end
34
+
35
+ def test_top_by
36
+ a = [1,3,5,2,4,3]
37
+ b = proc{|x| x % 3 }
38
+ assert_equal( [5], a.top_by(&b) )
39
+ assert_equal( [5,2,1], a.top_by(3, &b) )
40
+ assert_equal( [5,2,1,4,3,3], a.top_by(100, &b) )
41
+ end
42
+ def test_bottom_by
43
+ a = [3,1,4,3,2,5]
44
+ b = proc{|x| x % 3 }
45
+ assert_equal( [3], a.bottom_by(&b) )
46
+ assert_equal( [3,3,1], a.bottom_by(3, &b) )
47
+ assert_equal( [3,3,1,4,2,5], a.bottom_by(100, &b) )
48
+ end
49
+
50
+ def test_top_by__noblock
51
+ a = [1,3,5,2,4,3]
52
+ b = proc{|x| x % 3 }
53
+ assert_equal( [5], a.top_by.each(&b) )
54
+ assert_equal( [5,2,1], a.top_by(3).each(&b) )
55
+ assert_equal( [5,2,1,4,3,3], a.top_by(100).each(&b) )
56
+ end
57
+ def test_bottom_by__noblock
58
+ a = [3,1,4,3,2,5]
59
+ b = proc{|x| x % 3 }
60
+ assert_equal( [3], a.bottom_by.each(&b) )
61
+ assert_equal( [3,3,1], a.bottom_by(3).each(&b) )
62
+ assert_equal( [3,3,1,4,2,5], a.bottom_by(100).each(&b) )
63
+ end
64
+
65
+ def test_topBANG
66
+ a = [1,3,5,2,4,3]
67
+ a.top!(3)
68
+ assert_equal( [5,4,3], a )
69
+ end
70
+ def test_bottomBANG
71
+ a = [3,1,4,3,2,5]
72
+ a.bottom!(3)
73
+ assert_equal( [1,2,3], a )
74
+ end
75
+
76
+ def test_topBANG__block
77
+ a = [1,3,5,2,4,3]
78
+ b = proc{|x,y| y<=>x }
79
+ a.top!(3, &b)
80
+ assert_equal( [1,2,3], a )
81
+ end
82
+ def test_bottomBANG__block
83
+ a = [3,1,4,3,2,5]
84
+ b = proc{|x,y| y<=>x }
85
+ a.bottom!(3, &b)
86
+ assert_equal( [5,4,3], a )
87
+ end
88
+
89
+ def test_top_byBANG
90
+ a = [1,3,5,2,4,3]
91
+ b = proc{|x| x % 3 }
92
+ a.top_by!(3, &b)
93
+ assert_equal( [5,2,1], a )
94
+ end
95
+ def test_bottom_byBANG
96
+ a = [3,1,4,3,2,5]
97
+ b = proc{|x| x % 3 }
98
+ a.bottom_by!(3, &b)
99
+ assert_equal( [3,3,1], a )
100
+ end
101
+
102
+ def test_top_byBANG__noblock
103
+ a = [1,3,5,2,4,3]
104
+ b = proc{|x| x % 3 }
105
+ a.top_by!(3).each(&b)
106
+ assert_equal( [5,2,1], a )
107
+ end
108
+ def test_bottom_byBANG__noblock
109
+ a = [3,1,4,3,2,5]
110
+ b = proc{|x| x % 3 }
111
+ a.bottom_by!(3).each(&b)
112
+ assert_equal( [3,3,1], a )
113
+ end
114
+
115
+ end
116
+
@@ -1,56 +1,56 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- class Foo
5
- def bar a, b
6
- (a||0) + (b||0)
7
- end
8
- end
9
-
10
- require_relative '../lib/mug/apply'
11
- class Test_self < Test::Unit::TestCase
12
- def test_apply_proc
13
- prc = proc{|a,b,*c| (a||0) + (b||0) + c.inject(0, &:+) }
14
- p1 = prc.apply(1)
15
- assert( p1.is_a? Proc )
16
- assert_equal( 3, p1.apply(2) )
17
- assert_equal( 3, prc.apply(1,2) )
18
- assert_equal( 15, prc.apply(1,2,3,4,5) )
19
- end
20
- def test_apply_lambda
21
- lmb = lambda{|a,b,c| (a||0) + (b||0) + (c||0) }
22
-
23
- l1 = lmb.apply(1)
24
- assert( l1.is_a? Proc )
25
-
26
- l2 = l1.apply(2)
27
- assert( l2.is_a? Proc )
28
-
29
- assert_equal( 6, l2.apply(3) )
30
- assert_equal( 6, l1.apply(2,3) )
31
-
32
- assert_raise( ArgumentError ){ l2.apply(3, 4) }
33
- assert_raise( ArgumentError ){ lmb.apply(1, 2, 3, 4) }
34
- end
35
- def test_apply_method
36
- m1 = 1.method :+
37
- assert_equal( 3, m1.apply(2) )
38
- assert_raise( ArgumentError ){ m1.apply(2,3) }
39
-
40
- m2 = Foo.new.method :bar
41
- l1 = m2.apply(1)
42
- assert( l1.is_a? Proc )
43
-
44
- assert_equal( 3, l1.apply(2) )
45
- assert_equal( 3, m2.apply(1,2) )
46
-
47
- assert_raise( ArgumentError ){ l1.apply(2, 3) }
48
- assert_raise( ArgumentError ){ m2.apply(1, 2, 3) }
49
- end
50
- def test_curry_method
51
- m1 = 1.method :+
52
- assert( m1.curry.is_a? Proc )
53
- assert( m1.curry(1).is_a? Proc )
54
- assert_raise( ArgumentError ){ m1.curry(2) }
55
- end
56
- end
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ class Foo
5
+ def bar a, b
6
+ (a||0) + (b||0)
7
+ end
8
+ end
9
+
10
+ require_relative '../lib/mug/apply'
11
+ class Test_self < Test::Unit::TestCase
12
+ def test_apply_proc
13
+ prc = proc{|a,b,*c| (a||0) + (b||0) + c.inject(0, &:+) }
14
+ p1 = prc.apply(1)
15
+ assert( p1.is_a? Proc )
16
+ assert_equal( 3, p1.apply(2) )
17
+ assert_equal( 3, prc.apply(1,2) )
18
+ assert_equal( 15, prc.apply(1,2,3,4,5) )
19
+ end
20
+ def test_apply_lambda
21
+ lmb = lambda{|a,b,c| (a||0) + (b||0) + (c||0) }
22
+
23
+ l1 = lmb.apply(1)
24
+ assert( l1.is_a? Proc )
25
+
26
+ l2 = l1.apply(2)
27
+ assert( l2.is_a? Proc )
28
+
29
+ assert_equal( 6, l2.apply(3) )
30
+ assert_equal( 6, l1.apply(2,3) )
31
+
32
+ assert_raise( ArgumentError ){ l2.apply(3, 4) }
33
+ assert_raise( ArgumentError ){ lmb.apply(1, 2, 3, 4) }
34
+ end
35
+ def test_apply_method
36
+ m1 = 1.method :+
37
+ assert_equal( 3, m1.apply(2) )
38
+ assert_raise( ArgumentError ){ m1.apply(2,3) }
39
+
40
+ m2 = Foo.new.method :bar
41
+ l1 = m2.apply(1)
42
+ assert( l1.is_a? Proc )
43
+
44
+ assert_equal( 3, l1.apply(2) )
45
+ assert_equal( 3, m2.apply(1,2) )
46
+
47
+ assert_raise( ArgumentError ){ l1.apply(2, 3) }
48
+ assert_raise( ArgumentError ){ m2.apply(1, 2, 3) }
49
+ end
50
+ def test_curry_method
51
+ m1 = 1.method :+
52
+ assert( m1.curry.is_a? Proc )
53
+ assert( m1.curry(1).is_a? Proc )
54
+ assert_raise( ArgumentError ){ m1.curry(2) }
55
+ end
56
+ end