mug 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mug.rb +0 -1
- data/lib/mug/iterator.rb +2 -27
- data/lib/mug/iterator/for.rb +1 -1
- data/lib/mug/iterator/method.rb +1 -1
- data/lib/mug/iterator_c.rb +27 -0
- data/test/test-bool.rb +70 -0
- data/test/test-fragile-method-chain.rb +65 -0
- data/test/test-iterator-for.rb +18 -0
- data/test/test-iterator-method.rb +18 -0
- data/test/test-maybe.rb +90 -0
- data/test/test-self.rb +13 -0
- metadata +16 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 176afb1c7d977358227e76adffe6aa5651790298
|
4
|
+
data.tar.gz: 53f517bf29dcdf3ada2b4cdfede2d250b17e97cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60af159ec430300b58fae685df79eaeda031499c9b0f6ab092301edcec48e5560bb64ba2bd713130380266a9011449dc4946c8bd512aaed9d7099c2f96217a72
|
7
|
+
data.tar.gz: 654b5b73f1da2c2642c01ee5c75569c9aeeafb2cb86ca70c108746ff9dc6a99277fdfe1c1c4909f7d460477f5bcd8519cba8e06c32374a444d2e1f5efadf0c17
|
data/lib/mug.rb
CHANGED
data/lib/mug/iterator.rb
CHANGED
@@ -1,27 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# A special class of Enumerator that repeatedly invokes a method.
|
4
|
-
#
|
5
|
-
# Initially the method is send to the given +obj+, but subsequent
|
6
|
-
# invocations are sent to the result of the previous invocation.
|
7
|
-
#
|
8
|
-
# Example:
|
9
|
-
#
|
10
|
-
# 0.iter_for(:next).take(5) #=> [0,1,2,3,4]
|
11
|
-
#
|
12
|
-
class Iterator < Enumerator
|
13
|
-
#
|
14
|
-
# Creates a new Iterator for method +meth+, to be
|
15
|
-
# called initially on object +obj+.
|
16
|
-
#
|
17
|
-
# All method calls will have +args+ as parameters.
|
18
|
-
#
|
19
|
-
def initialize obj, meth, *args
|
20
|
-
super() do |y|
|
21
|
-
loop do
|
22
|
-
y << obj
|
23
|
-
obj = obj.send(meth, *args)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
1
|
+
require_relative 'iterator/for'
|
2
|
+
require_relative 'iterator/method'
|
data/lib/mug/iterator/for.rb
CHANGED
data/lib/mug/iterator/method.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# A special class of Enumerator that repeatedly invokes a method.
|
4
|
+
#
|
5
|
+
# Initially the method is send to the given +obj+, but subsequent
|
6
|
+
# invocations are sent to the result of the previous invocation.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# 0.iter_for(:next).take(5) #=> [0,1,2,3,4]
|
11
|
+
#
|
12
|
+
class Iterator < Enumerator
|
13
|
+
#
|
14
|
+
# Creates a new Iterator for method +meth+, to be
|
15
|
+
# called initially on object +obj+.
|
16
|
+
#
|
17
|
+
# All method calls will have +args+ as parameters.
|
18
|
+
#
|
19
|
+
def initialize obj, meth, *args
|
20
|
+
super() do |y|
|
21
|
+
loop do
|
22
|
+
y << obj
|
23
|
+
obj = obj.send(meth, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/test-bool.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
class MyEnum
|
5
|
+
include Enumerable
|
6
|
+
def initialize(*a) @a=a; end
|
7
|
+
def each(&b) @a.each(&b); end
|
8
|
+
end
|
9
|
+
|
10
|
+
if RUBY_VERSION.to_i >= 2
|
11
|
+
$truthy = [ 1, 1.0, Float::INFINITY, -Float::INFINITY, (1<<100).coerce(1).first, Rational(1,1), "x", [1], {1=>1}, MyEnum.new(1), [1].each ]
|
12
|
+
$falsy = [ 0, 0.0, -0.0, Float::NAN, (1<<100).coerce(0).first, Rational(0,1), "", [], {}, MyEnum.new, [].each, RuntimeError.new ]
|
13
|
+
else
|
14
|
+
$truthy = [ 1, 1.0, Float::INFINITY, -Float::INFINITY, (1<<100).coerce(1).first, Rational(1,1), "x", [1], {1=>1}, MyEnum.new(1) ]
|
15
|
+
$falsy = [ 0, 0.0, -0.0, Float::NAN, (1<<100).coerce(0).first, Rational(0,1), "", [], {}, MyEnum.new, RuntimeError.new ]
|
16
|
+
end
|
17
|
+
|
18
|
+
require_relative '../lib/mug/bool'
|
19
|
+
class Test_bool < Test::Unit::TestCase
|
20
|
+
|
21
|
+
alias :assert_true :assert
|
22
|
+
def assert_false val, msg=UNASSIGNED
|
23
|
+
assert !val, msg
|
24
|
+
end
|
25
|
+
def true_msg o, m=nil
|
26
|
+
m &&= ".#{m}"
|
27
|
+
"#{o.inspect}#{m} should be true"
|
28
|
+
end
|
29
|
+
def false_msg o, m=nil
|
30
|
+
m &&= ".#{m}"
|
31
|
+
"#{o.inspect}#{m} should be false"
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_Bool
|
35
|
+
assert_true( Bool(true), true_msg(true) )
|
36
|
+
assert_false( Bool(false), false_msg(false) )
|
37
|
+
assert_false( Bool(nil), false_msg(false) )
|
38
|
+
$truthy.each do |o|
|
39
|
+
assert_true( Bool(o), true_msg(o) )
|
40
|
+
end
|
41
|
+
$falsy.each do |o|
|
42
|
+
assert_true( Bool(o), true_msg(o) )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_to_bool
|
47
|
+
assert_true( true.to_bool, true_msg(true,'to_bool') )
|
48
|
+
assert_false( false.to_bool, false_msg(false,'to_bool') )
|
49
|
+
assert_false( nil.to_bool, false_msg(nil,'to_bool') )
|
50
|
+
$truthy.each do |o|
|
51
|
+
assert_true( o.to_bool, true_msg(o,'to_bool') )
|
52
|
+
end
|
53
|
+
$falsy.each do |o|
|
54
|
+
assert_true( o.to_bool, true_msg(o,'to_bool') )
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_to_b
|
59
|
+
assert_true( true.to_b, true_msg(true,'to_b') )
|
60
|
+
assert_false( false.to_b, false_msg(false,'to_b') )
|
61
|
+
assert_false( nil.to_b, false_msg(nil,'to_b') )
|
62
|
+
$truthy.each do |o|
|
63
|
+
assert_true( o.to_b, true_msg(o,'to_b') )
|
64
|
+
end
|
65
|
+
$falsy.each do |o|
|
66
|
+
assert_false( o.to_b, false_msg(o,'to_b') )
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +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
|
+
|
@@ -0,0 +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
|
@@ -0,0 +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
|
data/test/test-maybe.rb
ADDED
@@ -0,0 +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
|
data/test/test-self.rb
ADDED
@@ -0,0 +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
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Kerwin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
@@ -27,7 +27,14 @@ files:
|
|
27
27
|
- lib/mug/iterator/for.rb
|
28
28
|
- lib/mug/iterator/method.rb
|
29
29
|
- lib/mug/maybe.rb
|
30
|
+
- lib/mug/iterator_c.rb
|
30
31
|
- lib/mug/bool.rb
|
32
|
+
- test/test-self.rb
|
33
|
+
- test/test-bool.rb
|
34
|
+
- test/test-fragile-method-chain.rb
|
35
|
+
- test/test-iterator-method.rb
|
36
|
+
- test/test-iterator-for.rb
|
37
|
+
- test/test-maybe.rb
|
31
38
|
homepage: http://phluid61.github.com/mug
|
32
39
|
licenses:
|
33
40
|
- ISC License
|
@@ -52,4 +59,10 @@ rubygems_version: 2.0.2
|
|
52
59
|
signing_key:
|
53
60
|
specification_version: 4
|
54
61
|
summary: 'MUG: Matty''s Ultimate Gem'
|
55
|
-
test_files:
|
62
|
+
test_files:
|
63
|
+
- test/test-self.rb
|
64
|
+
- test/test-bool.rb
|
65
|
+
- test/test-fragile-method-chain.rb
|
66
|
+
- test/test-iterator-method.rb
|
67
|
+
- test/test-iterator-for.rb
|
68
|
+
- test/test-maybe.rb
|