mug 0.0.8 → 0.1.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.
data/test/test-hashop.rb CHANGED
@@ -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/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,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
data/test/test-maybe.rb CHANGED
@@ -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
data/test/test-self.rb CHANGED
@@ -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
data/test/test-tau.rb CHANGED
@@ -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
+