mug 1.2.2 → 1.5.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.
- checksums.yaml +4 -4
- data/lib/mug.rb +2 -1
- data/lib/mug/diggable.rb +34 -0
- data/lib/mug/enumerable/hash-like.rb +7 -7
- data/lib/mug/fragile-method-chain.rb +11 -1
- data/lib/mug/hash.rb +1 -0
- data/lib/mug/hash/fetch-assign.rb +33 -0
- data/lib/mug/hash/map.rb +4 -2
- data/lib/mug/iterator.rb +1 -1
- data/lib/mug/maybe.rb +10 -1
- data/lib/mug/with.rb +2 -2
- data/test/2-6-test-clamp.rb +17 -0
- data/test/2-7-test-clamp.rb +17 -0
- data/test/test-alias.rb +21 -21
- data/test/test-apply.rb +7 -6
- data/test/test-array-samples.rb +37 -36
- data/test/test-bool.rb +20 -20
- data/test/test-clamp.rb +7 -0
- data/test/test-diggable.rb +42 -0
- data/test/test-enumerable-hash-like.rb +12 -11
- data/test/test-fragile-method-chain.rb +27 -17
- data/test/test-hashfetchassign.rb +23 -0
- data/test/test-maybe.rb +27 -17
- data/test/test-with.rb +1 -10
- metadata +43 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07d9d3f3a20cd20fcc6215086d2de4453052caacce79d03dfca21c779d362f30
|
4
|
+
data.tar.gz: 19b0ab7a8e6e64ada49eaebcc0412e3f6d50dc750a56b9393cb197a8610417cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c87042f97f0e62885e891d47a28d2eef4453eda56fef0f9414b541536dfc0275fce0d69792cbb0cdc99dd35dd0fdcfa73246036f4b76dbe2c4483df1238b6fb1
|
7
|
+
data.tar.gz: 920b93e52a85d10ded07d19d88950b2a7c608379ef599f344d1291dc837acd13c5fb6f324bf77092fb3e9e19d4d60bcb6c5eab9b57e00c9f843ace7c4f14c4de
|
data/lib/mug.rb
CHANGED
@@ -11,12 +11,13 @@ require_relative 'mug/array/to_proc'
|
|
11
11
|
require_relative 'mug/bool'
|
12
12
|
require_relative 'mug/bittest'
|
13
13
|
require_relative 'mug/clamp'
|
14
|
-
require_relative 'mug/
|
14
|
+
require_relative 'mug/diggable'
|
15
15
|
require_relative 'mug/enumerable/any-and-all'
|
16
16
|
require_relative 'mug/enumerable/chain'
|
17
17
|
require_relative 'mug/enumerable/counts'
|
18
18
|
require_relative 'mug/enumerable/hash-like'
|
19
19
|
require_relative 'mug/fragile-method-chain'
|
20
|
+
require_relative 'mug/hash/fetch-assign'
|
20
21
|
require_relative 'mug/hash/map'
|
21
22
|
require_relative 'mug/hash/merge'
|
22
23
|
require_relative 'mug/hash/operations'
|
data/lib/mug/diggable.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Extend any class or object that implements a #[] method, to
|
4
|
+
# also have #dig
|
5
|
+
#
|
6
|
+
module Diggable
|
7
|
+
#
|
8
|
+
# Extracts the nested value specified by the sequence of +idx+ objects by
|
9
|
+
# calling +dig+ at each step, returning +nil+ if any intermediate step is
|
10
|
+
# +nil+.
|
11
|
+
#
|
12
|
+
def dig *idx
|
13
|
+
inner = self[idx.shift]
|
14
|
+
return inner if idx.empty? || inner.nil?
|
15
|
+
inner.dig(*idx)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
=begin
|
20
|
+
Copyright (c) 2020, Matthew Kerwin <matthew@kerwin.net.au>
|
21
|
+
|
22
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
23
|
+
purpose with or without fee is hereby granted, provided that the above
|
24
|
+
copyright notice and this permission notice appear in all copies.
|
25
|
+
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
27
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
28
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
29
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
30
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
31
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
32
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
33
|
+
=end
|
34
|
+
|
@@ -47,7 +47,7 @@ module Enumerable
|
|
47
47
|
return v if k == key
|
48
48
|
end
|
49
49
|
if args.length > 1
|
50
|
-
|
50
|
+
default
|
51
51
|
elsif block_given?
|
52
52
|
yield key
|
53
53
|
else
|
@@ -78,12 +78,12 @@ module Enumerable
|
|
78
78
|
break if remain < 1
|
79
79
|
|
80
80
|
key_indices = key_map[k]
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
81
|
+
next unless key_indices
|
82
|
+
|
83
|
+
key_indices.each do |key_index|
|
84
|
+
result[key_index] = v
|
85
|
+
define[key_index] = true
|
86
|
+
remain -= 1
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
@@ -34,6 +34,16 @@ class FragileMethodChain
|
|
34
34
|
self
|
35
35
|
end
|
36
36
|
|
37
|
+
# If the object is resolved, defer. Otherwise, sure, I
|
38
|
+
# respond to anything, I guess.
|
39
|
+
def respond_to_missing? meth, priv
|
40
|
+
if __defer?
|
41
|
+
@o.respond_to_missing? meth, priv
|
42
|
+
else
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
37
47
|
# Explicitly invoke :_? as a method in the chain.
|
38
48
|
def _? #:nodoc:
|
39
49
|
# Unconditionally invoke it, so the eventual _! doesn't fail
|
@@ -60,7 +70,7 @@ class Object
|
|
60
70
|
end
|
61
71
|
|
62
72
|
=begin
|
63
|
-
Copyright (c) 2013,2016, Matthew Kerwin <matthew@kerwin.net.au>
|
73
|
+
Copyright (c) 2013,2016,2020, Matthew Kerwin <matthew@kerwin.net.au>
|
64
74
|
|
65
75
|
Permission to use, copy, modify, and/or distribute this software for any
|
66
76
|
purpose with or without fee is hereby granted, provided that the above
|
data/lib/mug/hash.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
class Hash
|
3
|
+
|
4
|
+
#
|
5
|
+
# Returns a value from the hash for the given key. If the key can’t be
|
6
|
+
# found, there are several options: if default is given, then that will
|
7
|
+
# be stored and returned; if the optional code block is specified, then
|
8
|
+
# that will be run and its result stored and returned.
|
9
|
+
#
|
10
|
+
def fetch_assign key, *default
|
11
|
+
raise ArgumentError, "wrong number of arguments (given #{default.length + 1}, expected 1..2)" if default.length > 1
|
12
|
+
store key, (default.length == 1 ? default[0] : yield(key)) unless key? key
|
13
|
+
fetch key
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
=begin
|
19
|
+
Copyright (c) 2020, Matthew Kerwin <matthew@kerwin.net.au>
|
20
|
+
|
21
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
22
|
+
purpose with or without fee is hereby granted, provided that the above
|
23
|
+
copyright notice and this permission notice appear in all copies.
|
24
|
+
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
26
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
27
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
28
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
29
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
30
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
31
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
32
|
+
=end
|
33
|
+
|
data/lib/mug/hash/map.rb
CHANGED
@@ -78,9 +78,11 @@ class Hash
|
|
78
78
|
end
|
79
79
|
|
80
80
|
#
|
81
|
-
# Replaces the values in +hsh+ by running them each through +block+.
|
81
|
+
# Replaces the keys and values in +hsh+ by running them each through +block+.
|
82
82
|
#
|
83
|
-
#
|
83
|
+
# If +block+ returns duplicate keys, they will be overwritten.
|
84
|
+
#
|
85
|
+
# See: #map_pairs
|
84
86
|
#
|
85
87
|
def map_pairs! &block # :yields: key, value
|
86
88
|
return enum_for(:map_pairs!) unless block_given?
|
data/lib/mug/iterator.rb
CHANGED
@@ -23,7 +23,7 @@ class Iterator < Enumerator
|
|
23
23
|
# given method with any +args+ to the iterand.
|
24
24
|
#
|
25
25
|
# Use of this form is discouraged. Use Object#iter_for or
|
26
|
-
#
|
26
|
+
# Method#to_iter instead.
|
27
27
|
#
|
28
28
|
# @call-seq new(initial, *args) { |obj, *args| ... }
|
29
29
|
# @call-seq new(initial, method=:each, *args)
|
data/lib/mug/maybe.rb
CHANGED
@@ -21,6 +21,15 @@ class MaybeDelegator
|
|
21
21
|
def method_missing *a, &b #:nodoc:
|
22
22
|
@o && @o.send(*a, &b)
|
23
23
|
end
|
24
|
+
|
25
|
+
# This is a bit flakey, but I think it's meaningful.
|
26
|
+
def respond_to_missing? meth, priv
|
27
|
+
if @o
|
28
|
+
@o.repond_to_missing? meth, priv
|
29
|
+
else
|
30
|
+
true
|
31
|
+
end
|
32
|
+
end
|
24
33
|
end
|
25
34
|
|
26
35
|
class Object
|
@@ -42,7 +51,7 @@ class Object
|
|
42
51
|
end
|
43
52
|
|
44
53
|
=begin
|
45
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
54
|
+
Copyright (c) 2013,2020, Matthew Kerwin <matthew@kerwin.net.au>
|
46
55
|
|
47
56
|
Permission to use, copy, modify, and/or distribute this software for any
|
48
57
|
purpose with or without fee is hereby granted, provided that the above
|
data/lib/mug/with.rb
CHANGED
@@ -5,13 +5,13 @@ module Kernel
|
|
5
5
|
#
|
6
6
|
def with *args
|
7
7
|
return enum_for(:with, *args) unless block_given?
|
8
|
-
yield
|
8
|
+
yield(*args)
|
9
9
|
end
|
10
10
|
private :with
|
11
11
|
end
|
12
12
|
|
13
13
|
=begin
|
14
|
-
Copyright (c)
|
14
|
+
Copyright (c) 2019, Matthew Kerwin <matthew@kerwin.net.au>
|
15
15
|
|
16
16
|
Permission to use, copy, modify, and/or distribute this software for any
|
17
17
|
purpose with or without fee is hereby granted, provided that the above
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Test_clamp < Test::Unit::TestCase
|
2
|
+
|
3
|
+
def test_clamp__endless_range
|
4
|
+
rng = (1..)
|
5
|
+
assert_raise(RangeError) { 2.clamp(rng) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_bound__endless
|
9
|
+
rng = (1..)
|
10
|
+
assert_raise(RangeError) { rng.bound(2) }
|
11
|
+
|
12
|
+
rng = (1...)
|
13
|
+
assert_raise(RangeError) { rng.bound(2) }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Test_clamp < Test::Unit::TestCase
|
2
|
+
|
3
|
+
def test_clamp__beginless_range
|
4
|
+
rng = (..3)
|
5
|
+
assert_raise(RangeError) { 2.clamp(rng) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_bound__beginless
|
9
|
+
rng = (..3)
|
10
|
+
assert_raise(RangeError) { rng.bound(2) }
|
11
|
+
|
12
|
+
rng = (...3)
|
13
|
+
assert_raise(RangeError) { rng.bound(2) }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
data/test/test-alias.rb
CHANGED
@@ -1,54 +1,54 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
class ::DummyClass
|
5
|
-
def self.dummy_method
|
6
|
-
:ok
|
7
|
-
end
|
8
|
-
def self.clobber_method
|
9
|
-
:original
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
4
|
require_relative '../lib/mug/alias'
|
14
5
|
class Test_alias < Test::Unit::TestCase
|
15
6
|
|
7
|
+
class DummyClass
|
8
|
+
def self.dummy_method
|
9
|
+
:ok
|
10
|
+
end
|
11
|
+
def self.clobber_method
|
12
|
+
:original
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
16
|
def test_alias
|
17
|
-
assert_equal( :ok,
|
18
|
-
assert_raise(NoMethodError) {
|
17
|
+
assert_equal( :ok, DummyClass.dummy_method )
|
18
|
+
assert_raise(NoMethodError) { DummyClass.aliased_method }
|
19
19
|
|
20
|
-
|
20
|
+
DummyClass.instance_eval do
|
21
21
|
alias_singleton_method :aliased_method, :dummy_method
|
22
22
|
end
|
23
23
|
|
24
|
-
assert_equal( :ok,
|
25
|
-
assert_equal( :ok,
|
24
|
+
assert_equal( :ok, DummyClass.dummy_method )
|
25
|
+
assert_equal( :ok, DummyClass.aliased_method )
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_alias_safe
|
29
|
-
assert_equal( :original,
|
29
|
+
assert_equal( :original, DummyClass.clobber_method )
|
30
30
|
|
31
|
-
|
31
|
+
DummyClass.instance_eval do
|
32
32
|
alias_singleton_method :unclobbered_method, :clobber_method
|
33
33
|
def self.clobber_method
|
34
34
|
:clobbered
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
assert_equal( :clobbered,
|
39
|
-
assert_equal( :original,
|
38
|
+
assert_equal( :clobbered, DummyClass.clobber_method )
|
39
|
+
assert_equal( :original, DummyClass.unclobbered_method )
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_alias_return
|
43
43
|
result = nil
|
44
|
-
|
44
|
+
DummyClass.instance_eval do
|
45
45
|
result = alias_singleton_method :aliased_method2, :dummy_method
|
46
46
|
end
|
47
|
-
assert_equal(
|
47
|
+
assert_equal( DummyClass, result )
|
48
48
|
end
|
49
49
|
|
50
50
|
def test_alias_private
|
51
|
-
assert_raise(NoMethodError) {
|
51
|
+
assert_raise(NoMethodError) { DummyClass.alias_singleton_method :aliased_method3, :dummy_method }
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|
data/test/test-apply.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
class Foo
|
5
|
-
def bar a, b
|
6
|
-
(a||0) + (b||0)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
4
|
require_relative '../lib/mug/apply'
|
11
5
|
class Test_apply < Test::Unit::TestCase
|
6
|
+
|
7
|
+
class Foo
|
8
|
+
def bar a, b
|
9
|
+
(a||0) + (b||0)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
12
13
|
def test_apply_proc
|
13
14
|
prc = proc{|a,b,*c| (a||0) + (b||0) + c.inject(0, &:+) }
|
14
15
|
p1 = prc.apply(1)
|
data/test/test-array-samples.rb
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
$MIN = 4
|
7
|
-
$MAX = 6
|
4
|
+
require_relative '../lib/mug/array/samples'
|
5
|
+
class Test_array_extend < Test::Unit::TestCase
|
8
6
|
|
9
|
-
class MyPRNG
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
class MyPRNG
|
8
|
+
def rand n=1.0
|
9
|
+
case n
|
10
|
+
when Integer
|
11
|
+
n - 1
|
12
|
+
when Range
|
13
|
+
n.max
|
14
|
+
when Float
|
15
|
+
n.prev_float
|
16
|
+
end
|
18
17
|
end
|
19
18
|
end
|
20
|
-
end
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
ARRAY = proc { 8.times.to_a }
|
21
|
+
TRIES = 40320 # 8 factorial
|
22
|
+
MIN = 4
|
23
|
+
MAX = 6
|
24
|
+
|
24
25
|
def test_samples
|
25
|
-
a =
|
26
|
-
|
26
|
+
a = ARRAY.call
|
27
|
+
TRIES.times do
|
27
28
|
s = a.samples
|
28
29
|
assert( s.length >= 1 )
|
29
30
|
assert( s.length <= a.length )
|
@@ -32,11 +33,11 @@ class Test_array_extend < Test::Unit::TestCase
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
def test_samples_min
|
35
|
-
a =
|
36
|
+
a = ARRAY.call
|
36
37
|
# successes
|
37
|
-
|
38
|
-
s = a.samples( :min =>
|
39
|
-
assert( s.length >=
|
38
|
+
TRIES.times do
|
39
|
+
s = a.samples( :min => MIN )
|
40
|
+
assert( s.length >= MIN )
|
40
41
|
assert( s.length <= a.length )
|
41
42
|
assert( s.uniq == s )
|
42
43
|
assert( s.all? {|n| a.include? n } )
|
@@ -45,12 +46,12 @@ class Test_array_extend < Test::Unit::TestCase
|
|
45
46
|
assert_raise( ArgumentError ) { a.samples( :min => -1 ) }
|
46
47
|
end
|
47
48
|
def test_samples_max
|
48
|
-
a =
|
49
|
+
a = ARRAY.call
|
49
50
|
# successes
|
50
|
-
|
51
|
-
s = a.samples( :max =>
|
51
|
+
TRIES.times do
|
52
|
+
s = a.samples( :max => MAX )
|
52
53
|
assert( s.length >= 1 )
|
53
|
-
assert( s.length <=
|
54
|
+
assert( s.length <= MAX )
|
54
55
|
assert( s.uniq == s )
|
55
56
|
assert( s.all? {|n| a.include? n } )
|
56
57
|
end
|
@@ -58,21 +59,21 @@ class Test_array_extend < Test::Unit::TestCase
|
|
58
59
|
assert_raise( ArgumentError ) { a.samples( :max => -1 ) }
|
59
60
|
end
|
60
61
|
def test_samples_minmax
|
61
|
-
a =
|
62
|
+
a = ARRAY.call
|
62
63
|
# successes
|
63
|
-
|
64
|
-
s = a.samples( :min =>
|
65
|
-
assert( s.length >=
|
66
|
-
assert( s.length <=
|
64
|
+
TRIES.times do
|
65
|
+
s = a.samples( :min => MIN, :max => MAX )
|
66
|
+
assert( s.length >= MIN )
|
67
|
+
assert( s.length <= MAX )
|
67
68
|
assert( s.uniq == s )
|
68
69
|
assert( s.all? {|n| a.include? n } )
|
69
70
|
end
|
70
71
|
# failures
|
71
|
-
assert_raise( ArgumentError ) { a.samples( :min =>
|
72
|
+
assert_raise( ArgumentError ) { a.samples( :min => MAX, :max => MIN ) }
|
72
73
|
end
|
73
74
|
def test_samples_random
|
74
|
-
a =
|
75
|
-
|
75
|
+
a = ARRAY.call
|
76
|
+
TRIES.times do
|
76
77
|
s = a.samples( :random => Random.new )
|
77
78
|
assert( s.length >= 1 )
|
78
79
|
assert( s.length <= a.length )
|
@@ -81,8 +82,8 @@ class Test_array_extend < Test::Unit::TestCase
|
|
81
82
|
end
|
82
83
|
end
|
83
84
|
def test_samples_random2
|
84
|
-
a =
|
85
|
-
|
85
|
+
a = ARRAY.call
|
86
|
+
TRIES.times do
|
86
87
|
s = a.samples( :random => MyPRNG.new )
|
87
88
|
assert( s.length >= 1 )
|
88
89
|
assert( s.length <= a.length )
|
data/test/test-bool.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
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
4
|
require_relative '../lib/mug/bool'
|
19
5
|
class Test_bool < Test::Unit::TestCase
|
20
6
|
|
7
|
+
class MyEnum
|
8
|
+
include Enumerable
|
9
|
+
def initialize(*a) @a=a; end
|
10
|
+
def each(&b) @a.each(&b); end
|
11
|
+
end
|
12
|
+
|
13
|
+
if RUBY_VERSION.to_i >= 2
|
14
|
+
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 ]
|
15
|
+
FALSY = [ 0, 0.0, -0.0, Float::NAN, (1<<100).coerce(0).first, Rational(0,1), "", [], {}, MyEnum.new, [].each, RuntimeError.new ]
|
16
|
+
else
|
17
|
+
TRUTHY = [ 1, 1.0, Float::INFINITY, -Float::INFINITY, (1<<100).coerce(1).first, Rational(1,1), "x", [1], {1=>1}, MyEnum.new(1) ]
|
18
|
+
FALSY = [ 0, 0.0, -0.0, Float::NAN, (1<<100).coerce(0).first, Rational(0,1), "", [], {}, MyEnum.new, RuntimeError.new ]
|
19
|
+
end
|
20
|
+
|
21
21
|
alias assert_true assert
|
22
22
|
def assert_false val, msg=UNASSIGNED
|
23
23
|
assert !val, msg
|
@@ -35,10 +35,10 @@ class Test_bool < Test::Unit::TestCase
|
|
35
35
|
assert_true( Bool(true), true_msg(true) )
|
36
36
|
assert_false( Bool(false), false_msg(false) )
|
37
37
|
assert_false( Bool(nil), false_msg(false) )
|
38
|
-
|
38
|
+
TRUTHY.each do |o|
|
39
39
|
assert_true( Bool(o), true_msg(o) )
|
40
40
|
end
|
41
|
-
|
41
|
+
FALSY.each do |o|
|
42
42
|
assert_true( Bool(o), true_msg(o) )
|
43
43
|
end
|
44
44
|
end
|
@@ -47,10 +47,10 @@ class Test_bool < Test::Unit::TestCase
|
|
47
47
|
assert_true( true.to_bool, true_msg(true,'to_bool') )
|
48
48
|
assert_false( false.to_bool, false_msg(false,'to_bool') )
|
49
49
|
assert_false( nil.to_bool, false_msg(nil,'to_bool') )
|
50
|
-
|
50
|
+
TRUTHY.each do |o|
|
51
51
|
assert_true( o.to_bool, true_msg(o,'to_bool') )
|
52
52
|
end
|
53
|
-
|
53
|
+
FALSY.each do |o|
|
54
54
|
assert_true( o.to_bool, true_msg(o,'to_bool') )
|
55
55
|
end
|
56
56
|
end
|
@@ -59,10 +59,10 @@ class Test_bool < Test::Unit::TestCase
|
|
59
59
|
assert_true( true.to_b, true_msg(true,'to_b') )
|
60
60
|
assert_false( false.to_b, false_msg(false,'to_b') )
|
61
61
|
assert_false( nil.to_b, false_msg(nil,'to_b') )
|
62
|
-
|
62
|
+
TRUTHY.each do |o|
|
63
63
|
assert_true( o.to_b, true_msg(o,'to_b') )
|
64
64
|
end
|
65
|
-
|
65
|
+
FALSY.each do |o|
|
66
66
|
assert_false( o.to_b, false_msg(o,'to_b') )
|
67
67
|
end
|
68
68
|
end
|
data/test/test-clamp.rb
CHANGED
@@ -46,6 +46,13 @@ class Test_clamp < Test::Unit::TestCase
|
|
46
46
|
assert_raise(ArgumentError) { rng.bound(5) }
|
47
47
|
assert_raise(ArgumentError) { rng.bound(6) }
|
48
48
|
end
|
49
|
+
end
|
50
|
+
|
51
|
+
if RUBY_VERSION.to_f >= 2.6
|
52
|
+
require_relative '2-6-test-clamp'
|
53
|
+
end
|
49
54
|
|
55
|
+
if RUBY_VERSION.to_f >= 2.7
|
56
|
+
require_relative '2-7-test-clamp'
|
50
57
|
end
|
51
58
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/diggable'
|
5
|
+
class Test_digabble < Test::Unit::TestCase
|
6
|
+
|
7
|
+
class DiggyClass
|
8
|
+
include Diggable
|
9
|
+
|
10
|
+
def initialize *args
|
11
|
+
@array = args
|
12
|
+
end
|
13
|
+
|
14
|
+
def [] idx
|
15
|
+
@array[idx]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_diggable_mid
|
20
|
+
z = [99]
|
21
|
+
y = DiggyClass.new 0, z
|
22
|
+
x = [nil, nil, y]
|
23
|
+
|
24
|
+
path = [2, 1, 0]
|
25
|
+
assert_equal( 99, x.dig(*path) )
|
26
|
+
end
|
27
|
+
def test_diggable_end
|
28
|
+
z = [99]
|
29
|
+
y = DiggyClass.new 0, z
|
30
|
+
x = [nil, nil, y]
|
31
|
+
|
32
|
+
path = [2, 1]
|
33
|
+
assert_equal( z, x.dig(*path) )
|
34
|
+
end
|
35
|
+
def test_diggable_nil
|
36
|
+
y = DiggyClass.new 0, nil
|
37
|
+
x = [nil, nil, y]
|
38
|
+
|
39
|
+
path = [2, 1, 0]
|
40
|
+
assert_equal( nil, x.dig(*path) )
|
41
|
+
end
|
42
|
+
end
|
@@ -1,19 +1,20 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
class HashLikeEnum
|
5
|
-
def each
|
6
|
-
return enum_for(:each) unless block_given?
|
7
|
-
yield 'a'
|
8
|
-
yield 'b'
|
9
|
-
yield 'c'
|
10
|
-
yield 'd'
|
11
|
-
end
|
12
|
-
include Enumerable
|
13
|
-
end
|
14
|
-
|
15
4
|
require_relative '../lib/mug/enumerable/hash-like'
|
16
5
|
class Test_hashlike < Test::Unit::TestCase
|
6
|
+
|
7
|
+
class HashLikeEnum
|
8
|
+
def each
|
9
|
+
return enum_for(:each) unless block_given?
|
10
|
+
yield 'a'
|
11
|
+
yield 'b'
|
12
|
+
yield 'c'
|
13
|
+
yield 'd'
|
14
|
+
end
|
15
|
+
include Enumerable
|
16
|
+
end
|
17
|
+
|
17
18
|
def test__each_pair__block
|
18
19
|
my_enum = HashLikeEnum.new
|
19
20
|
expect = [[0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']]
|
@@ -1,28 +1,38 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
def initialize; @b = B.new; end
|
7
|
-
attr_accessor :b
|
8
|
-
end
|
4
|
+
require_relative '../lib/mug/fragile-method-chain'
|
5
|
+
class Test_fmc < Test::Unit::TestCase
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
module FMCTest
|
8
|
+
class A
|
9
|
+
def initialize; @b = B.new; end
|
10
|
+
attr_accessor :b
|
11
|
+
end
|
12
|
+
|
13
|
+
class B
|
14
|
+
def initialize; @c = C.new; end
|
15
|
+
attr_accessor :c
|
16
|
+
end
|
14
17
|
|
15
|
-
|
16
|
-
|
18
|
+
class C
|
19
|
+
def to_i; 1; end
|
20
|
+
end
|
17
21
|
end
|
18
|
-
end
|
19
22
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
+
def self.startup
|
24
|
+
class << false
|
25
|
+
def c
|
26
|
+
2
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def self.shutdown
|
31
|
+
class << false
|
32
|
+
undef c
|
33
|
+
end
|
34
|
+
end
|
23
35
|
|
24
|
-
require_relative '../lib/mug/fragile-method-chain'
|
25
|
-
class Test_fmc < Test::Unit::TestCase
|
26
36
|
def test_fmc_nil
|
27
37
|
a = FMCTest::A.new
|
28
38
|
assert_equal( 1, a._?.b.c.to_i._! )
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/hash/fetch-assign'
|
5
|
+
class Test_hashfetchassign < Test::Unit::TestCase
|
6
|
+
def test_hashfetchassign
|
7
|
+
hsh = {}
|
8
|
+
assert_equal( 1, hsh.fetch_assign(:a, 1) )
|
9
|
+
assert_equal( {:a => 1}, hsh )
|
10
|
+
assert_equal( 1, hsh.fetch_assign(:a, 2) )
|
11
|
+
assert_equal( {:a => 1}, hsh )
|
12
|
+
end
|
13
|
+
def test_hashfetchassign_proc
|
14
|
+
hsh = {}
|
15
|
+
assert_equal( 1, hsh.fetch_assign(:a) { 1 } )
|
16
|
+
assert_equal( {:a => 1}, hsh )
|
17
|
+
assert_equal( 1, hsh.fetch_assign(:a) { 2 } )
|
18
|
+
assert_equal( {:a => 1}, hsh )
|
19
|
+
assert_equal( 'b', hsh.fetch_assign(:b) {|key| key.to_s } )
|
20
|
+
assert_equal( {:a => 1, :b => 'b'}, hsh )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
data/test/test-maybe.rb
CHANGED
@@ -1,28 +1,38 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
def initialize; @b = B.new; end
|
7
|
-
attr_accessor :b
|
8
|
-
end
|
4
|
+
require_relative '../lib/mug/maybe'
|
5
|
+
class Test_maybe < Test::Unit::TestCase
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
module MaybeTest
|
8
|
+
class A
|
9
|
+
def initialize; @b = B.new; end
|
10
|
+
attr_accessor :b
|
11
|
+
end
|
12
|
+
|
13
|
+
class B
|
14
|
+
def initialize; @c = C.new; end
|
15
|
+
attr_accessor :c
|
16
|
+
end
|
14
17
|
|
15
|
-
|
16
|
-
|
18
|
+
class C
|
19
|
+
def to_i; 1; end
|
20
|
+
end
|
17
21
|
end
|
18
|
-
end
|
19
22
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
+
def self.startup
|
24
|
+
class << false
|
25
|
+
def c
|
26
|
+
2
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def self.shutdown
|
31
|
+
class << false
|
32
|
+
undef c
|
33
|
+
end
|
34
|
+
end
|
23
35
|
|
24
|
-
require_relative '../lib/mug/maybe'
|
25
|
-
class Test_maybe < Test::Unit::TestCase
|
26
36
|
def test_maybe_block_nil
|
27
37
|
a = MaybeTest::A.new
|
28
38
|
assert_equal( 1, a.maybe{ b.maybe{ c } }.to_i )
|
data/test/test-with.rb
CHANGED
@@ -1,15 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
|
-
class ::DummyClass
|
5
|
-
def self.dummy_method
|
6
|
-
:ok
|
7
|
-
end
|
8
|
-
def self.clobber_method
|
9
|
-
:original
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
4
|
require_relative '../lib/mug/with'
|
14
5
|
class Test_with < Test::Unit::TestCase
|
15
6
|
|
@@ -39,7 +30,7 @@ class Test_with < Test::Unit::TestCase
|
|
39
30
|
end
|
40
31
|
|
41
32
|
def test_with__private
|
42
|
-
assert_raise(NoMethodError) {
|
33
|
+
assert_raise(NoMethodError) { Object.new.with }
|
43
34
|
end
|
44
35
|
|
45
36
|
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: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Kerwin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/mug/bool.rb
|
39
39
|
- lib/mug/clamp.rb
|
40
40
|
- lib/mug/counts.rb
|
41
|
+
- lib/mug/diggable.rb
|
41
42
|
- lib/mug/enumerable.rb
|
42
43
|
- lib/mug/enumerable/any-and-all.rb
|
43
44
|
- lib/mug/enumerable/chain.rb
|
@@ -45,6 +46,7 @@ files:
|
|
45
46
|
- lib/mug/enumerable/hash-like.rb
|
46
47
|
- lib/mug/fragile-method-chain.rb
|
47
48
|
- lib/mug/hash.rb
|
49
|
+
- lib/mug/hash/fetch-assign.rb
|
48
50
|
- lib/mug/hash/map.rb
|
49
51
|
- lib/mug/hash/merge.rb
|
50
52
|
- lib/mug/hash/operations.rb
|
@@ -70,6 +72,8 @@ files:
|
|
70
72
|
- lib/mug/to_h.rb
|
71
73
|
- lib/mug/top.rb
|
72
74
|
- lib/mug/with.rb
|
75
|
+
- test/2-6-test-clamp.rb
|
76
|
+
- test/2-7-test-clamp.rb
|
73
77
|
- test/test-affix.rb
|
74
78
|
- test/test-alias.rb
|
75
79
|
- test/test-and-or.rb
|
@@ -84,9 +88,11 @@ files:
|
|
84
88
|
- test/test-bool.rb
|
85
89
|
- test/test-clamp.rb
|
86
90
|
- test/test-counts.rb
|
91
|
+
- test/test-diggable.rb
|
87
92
|
- test/test-enumerable-chain.rb
|
88
93
|
- test/test-enumerable-hash-like.rb
|
89
94
|
- test/test-fragile-method-chain.rb
|
95
|
+
- test/test-hashfetchassign.rb
|
90
96
|
- test/test-hashmap.rb
|
91
97
|
- test/test-hashmerge.rb
|
92
98
|
- test/test-hashop.rb
|
@@ -126,45 +132,49 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
132
|
- !ruby/object:Gem::Version
|
127
133
|
version: '0'
|
128
134
|
requirements: []
|
129
|
-
rubygems_version: 3.
|
135
|
+
rubygems_version: 3.1.4
|
130
136
|
signing_key:
|
131
137
|
specification_version: 4
|
132
138
|
summary: 'MUG: Matty''s Ultimate Gem'
|
133
139
|
test_files:
|
134
|
-
- test/
|
135
|
-
- test/test-clamp.rb
|
136
|
-
- test/test-
|
137
|
-
- test/test-rexproc.rb
|
138
|
-
- test/test-tau.rb
|
139
|
-
- test/test-matchdata_each.rb
|
140
|
+
- test/2-6-test-clamp.rb
|
141
|
+
- test/2-7-test-clamp.rb
|
142
|
+
- test/test-affix.rb
|
140
143
|
- test/test-alias.rb
|
141
|
-
- test/test-bittest.rb
|
142
|
-
- test/test-iterator.rb
|
143
|
-
- test/test-hashwhen.rb
|
144
|
-
- test/test-matchdata_hash.rb
|
145
|
-
- test/test-not.rb
|
146
|
-
- test/test-array-extend.rb
|
147
|
-
- test/test-iterator-for.rb
|
148
|
-
- test/test-top.rb
|
149
144
|
- test/test-and-or.rb
|
150
|
-
- test/test-array-to_proc.rb
|
151
|
-
- test/test-affix.rb
|
152
|
-
- test/test-hashmerge.rb
|
153
|
-
- test/test-with.rb
|
154
145
|
- test/test-any-and-all.rb
|
155
|
-
- test/test-
|
156
|
-
- test/test-
|
157
|
-
- test/test-
|
158
|
-
- test/test-
|
159
|
-
- test/test-
|
146
|
+
- test/test-apply.rb
|
147
|
+
- test/test-array-delete_all.rb
|
148
|
+
- test/test-array-extend.rb
|
149
|
+
- test/test-array-minus.rb
|
150
|
+
- test/test-array-samples.rb
|
151
|
+
- test/test-array-to_proc.rb
|
152
|
+
- test/test-bittest.rb
|
160
153
|
- test/test-bool.rb
|
154
|
+
- test/test-clamp.rb
|
155
|
+
- test/test-counts.rb
|
156
|
+
- test/test-diggable.rb
|
157
|
+
- test/test-enumerable-chain.rb
|
158
|
+
- test/test-enumerable-hash-like.rb
|
159
|
+
- test/test-fragile-method-chain.rb
|
160
|
+
- test/test-hashfetchassign.rb
|
161
161
|
- test/test-hashmap.rb
|
162
|
-
- test/test-
|
163
|
-
- test/test-maybe.rb
|
164
|
-
- test/test-iff.rb
|
165
|
-
- test/test-array-samples.rb
|
162
|
+
- test/test-hashmerge.rb
|
166
163
|
- test/test-hashop.rb
|
164
|
+
- test/test-hashwhen.rb
|
165
|
+
- test/test-iff.rb
|
166
|
+
- test/test-iterator-for.rb
|
167
167
|
- test/test-iterator-method.rb
|
168
|
-
- test/test-
|
169
|
-
- test/test-
|
170
|
-
- test/test-
|
168
|
+
- test/test-iterator.rb
|
169
|
+
- test/test-loop-with.rb
|
170
|
+
- test/test-matchdata_each.rb
|
171
|
+
- test/test-matchdata_hash.rb
|
172
|
+
- test/test-maybe.rb
|
173
|
+
- test/test-negativity.rb
|
174
|
+
- test/test-not.rb
|
175
|
+
- test/test-rexproc.rb
|
176
|
+
- test/test-self.rb
|
177
|
+
- test/test-tau.rb
|
178
|
+
- test/test-time.rb
|
179
|
+
- test/test-top.rb
|
180
|
+
- test/test-with.rb
|