mug 0.2.3 → 0.2.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 +48 -48
- data/lib/mug/apply.rb +58 -58
- data/lib/mug/bool.rb +128 -128
- data/lib/mug/fragile-method-chain.rb +70 -70
- data/lib/mug/iterator.rb +2 -2
- data/lib/mug/iterator/for.rb +29 -29
- data/lib/mug/iterator/method.rb +29 -29
- data/lib/mug/iterator_c.rb +44 -44
- data/lib/mug/maybe.rb +59 -59
- data/lib/mug/self.rb +13 -0
- data/lib/mug/tau.rb +44 -44
- data/test/test-and-or.rb +41 -41
- data/test/test-fragile-method-chain.rb +65 -65
- data/test/test-iterator-for.rb +18 -18
- data/test/test-iterator-method.rb +18 -18
- data/test/test-maybe.rb +90 -90
- data/test/test-self.rb +10 -0
- data/test/test-tau.rb +22 -22
- data/test/test-to_h.rb +16 -16
- data/test/test_apply.rb +56 -56
- metadata +19 -18
data/lib/mug/self.rb
CHANGED
@@ -20,6 +20,19 @@ class Object
|
|
20
20
|
warn 'Object#yield is deprecated; use Object#self'
|
21
21
|
self.self(&block)
|
22
22
|
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Yields this object (and any other arguments)
|
26
|
+
# to a block. If no block is given, returns an
|
27
|
+
# Enumerator.
|
28
|
+
#
|
29
|
+
def revapply(*args, &block)
|
30
|
+
if block_given?
|
31
|
+
yield self, *args
|
32
|
+
else
|
33
|
+
enum_for(:revapply, *args) { args.length + 1 }
|
34
|
+
end
|
35
|
+
end
|
23
36
|
end
|
24
37
|
|
25
38
|
=begin
|
data/lib/mug/tau.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
|
-
|
2
|
-
module Math
|
3
|
-
# The true circle constant.
|
4
|
-
# The ratio of a circle's circumference to its radius.
|
5
|
-
TAU = PI * 2.0
|
6
|
-
end
|
7
|
-
|
8
|
-
module BigMath
|
9
|
-
##
|
10
|
-
# Computes the value of tau to the specific number of digits of precision.
|
11
|
-
#
|
12
|
-
# @param [Integer] prec the number of decimal digits of precision in the computed value.
|
13
|
-
# @return [BigDecimal] the computed value
|
14
|
-
# @raise [ArgumentError] if +prec+ is not positive
|
15
|
-
#
|
16
|
-
# @example
|
17
|
-
# require 'bigdecimal'
|
18
|
-
# require 'bigdecimal/math'
|
19
|
-
# include BigMath
|
20
|
-
#
|
21
|
-
# puts TAU(150)
|
22
|
-
#
|
23
|
-
def TAU(prec)
|
24
|
-
raise ArgumentError, 'Zero or negative argument for TAU' if prec <= 0
|
25
|
-
PI(prec) * BigDecimal('2')
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
=begin
|
30
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
31
|
-
|
32
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
33
|
-
purpose with or without fee is hereby granted, provided that the above
|
34
|
-
copyright notice and this permission notice appear in all copies.
|
35
|
-
|
36
|
-
THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
37
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
38
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
39
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
40
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
41
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
42
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
43
|
-
=end
|
44
|
-
|
1
|
+
|
2
|
+
module Math
|
3
|
+
# The true circle constant.
|
4
|
+
# The ratio of a circle's circumference to its radius.
|
5
|
+
TAU = PI * 2.0
|
6
|
+
end
|
7
|
+
|
8
|
+
module BigMath
|
9
|
+
##
|
10
|
+
# Computes the value of tau to the specific number of digits of precision.
|
11
|
+
#
|
12
|
+
# @param [Integer] prec the number of decimal digits of precision in the computed value.
|
13
|
+
# @return [BigDecimal] the computed value
|
14
|
+
# @raise [ArgumentError] if +prec+ is not positive
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# require 'bigdecimal'
|
18
|
+
# require 'bigdecimal/math'
|
19
|
+
# include BigMath
|
20
|
+
#
|
21
|
+
# puts TAU(150)
|
22
|
+
#
|
23
|
+
def TAU(prec)
|
24
|
+
raise ArgumentError, 'Zero or negative argument for TAU' if prec <= 0
|
25
|
+
PI(prec) * BigDecimal('2')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
=begin
|
30
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
31
|
+
|
32
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
33
|
+
purpose with or without fee is hereby granted, provided that the above
|
34
|
+
copyright notice and this permission notice appear in all copies.
|
35
|
+
|
36
|
+
THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
37
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
38
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
39
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
40
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
41
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
42
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
43
|
+
=end
|
44
|
+
|
data/test/test-and-or.rb
CHANGED
@@ -1,41 +1,41 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
$VERBOSE = true
|
3
|
-
|
4
|
-
require_relative '../lib/mug/and-or'
|
5
|
-
class Test_and_or < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def test_and
|
8
|
-
assert_equal( 2, 1.and(2) )
|
9
|
-
assert_equal( 2, 0.and(2) )
|
10
|
-
assert_equal( 2, 'x'.and(2) )
|
11
|
-
assert_equal( 2, true.and(2) )
|
12
|
-
assert_equal( false, false.and(2) )
|
13
|
-
assert_equal( nil, nil.and(2) )
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_and_2
|
17
|
-
assert_equal( [1], [1].and([2], &:empty?) )
|
18
|
-
assert_equal( [2], [].and([2], &:empty?) )
|
19
|
-
assert_equal( 'abc', 'abc'.and(''){|x|x =~ /\d/} )
|
20
|
-
assert_equal( '', 'a1c'.and(''){|x|x =~ /\d/} )
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
def test_or
|
25
|
-
assert_equal( 1, 1.or(2) )
|
26
|
-
assert_equal( 0, 0.or(2) )
|
27
|
-
assert_equal( 'x', 'x'.or(2) )
|
28
|
-
assert_equal( true, true.or(2) )
|
29
|
-
assert_equal( 2, false.or(2) )
|
30
|
-
assert_equal( 2, nil.or(2) )
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_or_2
|
34
|
-
assert_equal( [2], [1].or([2], &:empty?) )
|
35
|
-
assert_equal( [], [].or([2], &:empty?) )
|
36
|
-
assert_equal( '', 'abc'.or(''){|x|x =~ /\d/} )
|
37
|
-
assert_equal( 'a1c', 'a1c'.or(''){|x|x =~ /\d/} )
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/and-or'
|
5
|
+
class Test_and_or < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_and
|
8
|
+
assert_equal( 2, 1.and(2) )
|
9
|
+
assert_equal( 2, 0.and(2) )
|
10
|
+
assert_equal( 2, 'x'.and(2) )
|
11
|
+
assert_equal( 2, true.and(2) )
|
12
|
+
assert_equal( false, false.and(2) )
|
13
|
+
assert_equal( nil, nil.and(2) )
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_and_2
|
17
|
+
assert_equal( [1], [1].and([2], &:empty?) )
|
18
|
+
assert_equal( [2], [].and([2], &:empty?) )
|
19
|
+
assert_equal( 'abc', 'abc'.and(''){|x|x =~ /\d/} )
|
20
|
+
assert_equal( '', 'a1c'.and(''){|x|x =~ /\d/} )
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def test_or
|
25
|
+
assert_equal( 1, 1.or(2) )
|
26
|
+
assert_equal( 0, 0.or(2) )
|
27
|
+
assert_equal( 'x', 'x'.or(2) )
|
28
|
+
assert_equal( true, true.or(2) )
|
29
|
+
assert_equal( 2, false.or(2) )
|
30
|
+
assert_equal( 2, nil.or(2) )
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_or_2
|
34
|
+
assert_equal( [2], [1].or([2], &:empty?) )
|
35
|
+
assert_equal( [], [].or([2], &:empty?) )
|
36
|
+
assert_equal( '', 'abc'.or(''){|x|x =~ /\d/} )
|
37
|
+
assert_equal( 'a1c', 'a1c'.or(''){|x|x =~ /\d/} )
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
@@ -1,65 +1,65 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
$VERBOSE = true
|
3
|
-
|
4
|
-
module FMCTest
|
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/fragile-method-chain'
|
25
|
-
class Test_fmc < Test::Unit::TestCase
|
26
|
-
def test_fmc_nil
|
27
|
-
a = FMCTest::A.new
|
28
|
-
assert_equal( 1, a._?.b.c.to_i._! )
|
29
|
-
assert_equal( 1, a._?.b.c._!.to_i )
|
30
|
-
assert_equal( 1, a._?.b._!.c.to_i )
|
31
|
-
a.b.c = nil
|
32
|
-
assert_nil( a._?.b.c.to_i._! )
|
33
|
-
assert_equal( 0, a._?.b.c._!.to_i )
|
34
|
-
assert_equal( 0, a._?.b._!.c.to_i )
|
35
|
-
a.b = nil
|
36
|
-
assert_nil( a._?.b.c.to_i._! )
|
37
|
-
assert_equal( 0, a._?.b.c._!.to_i )
|
38
|
-
assert_raise(NoMethodError) { a._?.b._!.c.to_i }
|
39
|
-
a = nil
|
40
|
-
assert_nil( a._?.b.c.to_i._! )
|
41
|
-
end
|
42
|
-
def test_fmc_false
|
43
|
-
a = FMCTest::A.new
|
44
|
-
assert_equal( 1, a._?.b.c.to_i._! )
|
45
|
-
assert_equal( 1, a._?.b.c._!.to_i )
|
46
|
-
assert_equal( 1, a._?.b._!.c.to_i )
|
47
|
-
a.b.c = false
|
48
|
-
assert_equal( false, a._?.b.c.to_i._! )
|
49
|
-
assert_raise(NoMethodError) { a._?.b.c._!.to_i }
|
50
|
-
assert_raise(NoMethodError) { a._?.b._!.c.to_i }
|
51
|
-
a.b = false
|
52
|
-
assert_equal( false, a._?.b.c.to_i._! )
|
53
|
-
assert_raise(NoMethodError) { a._?.b.c._!.to_i }
|
54
|
-
assert_equal( 2, a._?.b._!.c.to_i )
|
55
|
-
a = false
|
56
|
-
assert_equal( false, a._?.b.c.to_i._! )
|
57
|
-
assert_raise(NoMethodError) { a._?.b.c._!.to_i }
|
58
|
-
assert_equal( 2, a._?.b._!.c.to_i )
|
59
|
-
end
|
60
|
-
def test_fmc_nested
|
61
|
-
a = FMCTest::A.new
|
62
|
-
assert_equal( 1, a._?.b._?.c._!.to_i._! )
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
module FMCTest
|
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/fragile-method-chain'
|
25
|
+
class Test_fmc < Test::Unit::TestCase
|
26
|
+
def test_fmc_nil
|
27
|
+
a = FMCTest::A.new
|
28
|
+
assert_equal( 1, a._?.b.c.to_i._! )
|
29
|
+
assert_equal( 1, a._?.b.c._!.to_i )
|
30
|
+
assert_equal( 1, a._?.b._!.c.to_i )
|
31
|
+
a.b.c = nil
|
32
|
+
assert_nil( a._?.b.c.to_i._! )
|
33
|
+
assert_equal( 0, a._?.b.c._!.to_i )
|
34
|
+
assert_equal( 0, a._?.b._!.c.to_i )
|
35
|
+
a.b = nil
|
36
|
+
assert_nil( a._?.b.c.to_i._! )
|
37
|
+
assert_equal( 0, a._?.b.c._!.to_i )
|
38
|
+
assert_raise(NoMethodError) { a._?.b._!.c.to_i }
|
39
|
+
a = nil
|
40
|
+
assert_nil( a._?.b.c.to_i._! )
|
41
|
+
end
|
42
|
+
def test_fmc_false
|
43
|
+
a = FMCTest::A.new
|
44
|
+
assert_equal( 1, a._?.b.c.to_i._! )
|
45
|
+
assert_equal( 1, a._?.b.c._!.to_i )
|
46
|
+
assert_equal( 1, a._?.b._!.c.to_i )
|
47
|
+
a.b.c = false
|
48
|
+
assert_equal( false, a._?.b.c.to_i._! )
|
49
|
+
assert_raise(NoMethodError) { a._?.b.c._!.to_i }
|
50
|
+
assert_raise(NoMethodError) { a._?.b._!.c.to_i }
|
51
|
+
a.b = false
|
52
|
+
assert_equal( false, a._?.b.c.to_i._! )
|
53
|
+
assert_raise(NoMethodError) { a._?.b.c._!.to_i }
|
54
|
+
assert_equal( 2, a._?.b._!.c.to_i )
|
55
|
+
a = false
|
56
|
+
assert_equal( false, a._?.b.c.to_i._! )
|
57
|
+
assert_raise(NoMethodError) { a._?.b.c._!.to_i }
|
58
|
+
assert_equal( 2, a._?.b._!.c.to_i )
|
59
|
+
end
|
60
|
+
def test_fmc_nested
|
61
|
+
a = FMCTest::A.new
|
62
|
+
assert_equal( 1, a._?.b._?.c._!.to_i._! )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
data/test/test-iterator-for.rb
CHANGED
@@ -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
|