mug 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mug/to_h.rb CHANGED
@@ -1,44 +1,44 @@
1
-
2
- unless {}.respond_to? :to_h
3
- warn %|Warning: "mug/to_h" does not make much sense without "to_h" (https://rubygems.org/gems/to_h)|
4
- end
5
-
6
- module Enumerable
7
- #
8
- # Converts +enum+ to a Hash.
9
- #
10
- # Each element of +enum+ must be a single item, or an array of two items.
11
- # Duplicate keys are overwritten in order.
12
- #
13
- # [].to_h #=> {}
14
- # [1,2].to_h #=> {1=>nil, 2=>nil}
15
- # (1..2).to_h #=> {1=>nil, 2=>nil}
16
- # [[1,2],[3,4]].to_h #=> {1=>2, 3=>4}
17
- # [[1,2],[1,4]].to_h #=> {1=>4}
18
- #
19
- def to_h
20
- hsh = {}
21
- each do |k,v,*x|
22
- raise ArgumentError, "invalid number of elements (#{x.length+1} for 1..2)" if x.any?
23
- hsh[k] = v
24
- end
25
- hsh
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
+ unless {}.respond_to? :to_h
3
+ warn %|Warning: "mug/to_h" does not make much sense without "to_h" (https://rubygems.org/gems/to_h)|
4
+ end
5
+
6
+ module Enumerable
7
+ #
8
+ # Converts +enum+ to a Hash.
9
+ #
10
+ # Each element of +enum+ must be a single item, or an array of two items.
11
+ # Duplicate keys are overwritten in order.
12
+ #
13
+ # [].to_h #=> {}
14
+ # [1,2].to_h #=> {1=>nil, 2=>nil}
15
+ # (1..2).to_h #=> {1=>nil, 2=>nil}
16
+ # [[1,2],[3,4]].to_h #=> {1=>2, 3=>4}
17
+ # [[1,2],[1,4]].to_h #=> {1=>4}
18
+ #
19
+ def to_h
20
+ hsh = {}
21
+ each do |k,v,*x|
22
+ raise ArgumentError, "invalid number of elements (#{x.length+1} for 1..2)" if x.any?
23
+ hsh[k] = v
24
+ end
25
+ hsh
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
+
data/test/test-bool.rb CHANGED
@@ -1,70 +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
-
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
+
@@ -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-hashmap.rb CHANGED
@@ -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/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
+