augmented 0.2.2 → 0.2.3
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/README.md +55 -27
- data/augmented.gemspec +16 -8
- data/lib/augmented.rb +2 -0
- data/lib/augmented/enumerators/indexing.rb +3 -1
- data/lib/augmented/strings.rb +9 -0
- data/lib/augmented/strings/blank.rb +15 -0
- data/lib/augmented/strings/truncatable.rb +20 -0
- data/lib/augmented/version.rb +1 -1
- metadata +20 -45
- data/test/augmented/arrays/tieable_test.rb +0 -66
- data/test/augmented/enumerators/indexing_test.rb +0 -15
- data/test/augmented/hashes/mappable_test.rb +0 -37
- data/test/augmented/hashes/polymorphable_test.rb +0 -45
- data/test/augmented/hashes/transformable_test.rb +0 -87
- data/test/augmented/modules/refined_test.rb +0 -29
- data/test/augmented/objects/iffy_test.rb +0 -69
- data/test/augmented/objects/pickable_test.rb +0 -39
- data/test/augmented/objects/tackable_test.rb +0 -25
- data/test/augmented/objects/tappable_test.rb +0 -141
- data/test/augmented/objects/thru_test.rb +0 -98
- data/test/augmented/procs/chainable_test.rb +0 -22
- data/test/augmented/procs/rescuable_test.rb +0 -38
- data/test/augmented/symbols/arguable_test.rb +0 -51
- data/test/augmented/symbols/comparing_test.rb +0 -131
@@ -1,98 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/objects/thru'
|
3
|
-
|
4
|
-
describe Augmented::Objects::Thru do
|
5
|
-
using Augmented::Objects::Thru
|
6
|
-
|
7
|
-
describe '#thru' do
|
8
|
-
|
9
|
-
it 'returns the result of applying the given function to the object' do
|
10
|
-
plus_10 = -> i { i + 10 }
|
11
|
-
|
12
|
-
assert_equal 5.thru(&plus_10), 15
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'returns the object untouched if called without arguments' do
|
16
|
-
obj = Object.new
|
17
|
-
assert_equal obj.thru.object_id, obj.object_id
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
describe '#thru_if' do
|
23
|
-
|
24
|
-
it 'applies the given function to the object if the condition is truish' do
|
25
|
-
plus_10 = -> i { i + 10 }
|
26
|
-
|
27
|
-
assert_equal 5.thru_if(true, &plus_10), 15
|
28
|
-
assert_equal 5.thru_if(Object.new, &plus_10), 15
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'applies the given function to the object if the condition evaluates to truish' do
|
32
|
-
plus_10 = -> i { i + 10 }
|
33
|
-
|
34
|
-
condition_1 = -> i { i == 5 }
|
35
|
-
condition_2 = -> i { i.to_s }
|
36
|
-
|
37
|
-
assert_equal 5.thru_if(condition_1, &plus_10), 15
|
38
|
-
assert_equal 5.thru_if(condition_2, &plus_10), 15
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'returns the object without applying the function if the condition is falsy' do
|
42
|
-
plus_10 = -> i { i + 10 }
|
43
|
-
|
44
|
-
assert_equal 5.thru_if(false, &plus_10), 5
|
45
|
-
assert_equal 5.thru_if(nil, &plus_10), 5
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'returns the object without applying the function if the condition evaluates to falsy' do
|
49
|
-
plus_10 = -> i { i + 10 }
|
50
|
-
|
51
|
-
condition_1 = -> i { i == 10 }
|
52
|
-
condition_2 = -> i { nil }
|
53
|
-
|
54
|
-
assert_equal 5.thru_if(condition_1, &plus_10), 5
|
55
|
-
assert_equal 5.thru_if(condition_2, &plus_10), 5
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
describe '#thru_unless' do
|
61
|
-
|
62
|
-
it 'applies the given function to the object if the condition is falsy' do
|
63
|
-
plus_10 = -> i { i + 10 }
|
64
|
-
|
65
|
-
assert_equal 5.thru_unless(false, &plus_10), 15
|
66
|
-
assert_equal 5.thru_unless(nil, &plus_10), 15
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'applies the given function to the object if the condition evaluates to falsy' do
|
70
|
-
plus_10 = -> i { i + 10 }
|
71
|
-
|
72
|
-
condition_1 = -> i { i == 10 }
|
73
|
-
condition_2 = -> i { nil }
|
74
|
-
|
75
|
-
assert_equal 5.thru_unless(condition_1, &plus_10), 15
|
76
|
-
assert_equal 5.thru_unless(condition_2, &plus_10), 15
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'returns the object without applying the function if the condition is truish' do
|
80
|
-
plus_10 = -> i { i + 10 }
|
81
|
-
|
82
|
-
assert_equal 5.thru_unless(true, &plus_10), 5
|
83
|
-
assert_equal 5.thru_unless(Object.new, &plus_10), 5
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'returns the object without applying the function if the condition evaluates to truish' do
|
87
|
-
plus_10 = -> i { i + 10 }
|
88
|
-
|
89
|
-
condition_1 = -> i { i == 5 }
|
90
|
-
condition_2 = -> i { i.to_s }
|
91
|
-
|
92
|
-
assert_equal 5.thru_unless(condition_1, &plus_10), 5
|
93
|
-
assert_equal 5.thru_unless(condition_2, &plus_10), 5
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/procs/chainable'
|
3
|
-
|
4
|
-
describe Augmented::Procs::Chainable do
|
5
|
-
using Augmented::Procs::Chainable
|
6
|
-
|
7
|
-
describe '#|' do
|
8
|
-
|
9
|
-
it 'returns a function that invokes from left to right' do
|
10
|
-
|
11
|
-
add_one = -> (i) { i + 1 }
|
12
|
-
triple = -> (i) { i * 3 }
|
13
|
-
sub_two = -> (i) { i - 2 }
|
14
|
-
add_twenty = -> (i) { i + 20 }
|
15
|
-
|
16
|
-
chain = add_one | triple | sub_two | add_twenty
|
17
|
-
|
18
|
-
assert_equal chain.call(1), 24
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/procs/rescuable'
|
3
|
-
|
4
|
-
describe Augmented::Procs::Rescuable do
|
5
|
-
using Augmented::Procs::Rescuable
|
6
|
-
|
7
|
-
describe '#rescues' do
|
8
|
-
|
9
|
-
it 'returns a proc which returns a provided value if the expected exception is raised' do
|
10
|
-
specific_exception_class = Class.new RuntimeError
|
11
|
-
|
12
|
-
unsafe_proc = -> { raise specific_exception_class }
|
13
|
-
rescued_proc = unsafe_proc.rescues specific_exception_class, 42
|
14
|
-
|
15
|
-
assert_equal rescued_proc.call, 42
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns a proc which returns the result of the provided block if the expected exception is raised' do
|
19
|
-
specific_exception_class = Class.new RuntimeError
|
20
|
-
|
21
|
-
unsafe_proc = -> { raise specific_exception_class }
|
22
|
-
rescued_proc = unsafe_proc.rescues(specific_exception_class){ |exception| exception }
|
23
|
-
|
24
|
-
assert_instance_of specific_exception_class, rescued_proc.call
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'returns a proc which lets exceptions other than the expected one to be raised' do
|
28
|
-
specific_exception_class = Class.new RuntimeError
|
29
|
-
|
30
|
-
unsafe_proc = -> { raise RuntimeError }
|
31
|
-
rescued_proc = unsafe_proc.rescues specific_exception_class, 42
|
32
|
-
|
33
|
-
assert_raises(RuntimeError){ rescued_proc.call }
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/symbols/arguable'
|
3
|
-
|
4
|
-
describe Augmented::Symbols::Arguable do
|
5
|
-
using Augmented::Symbols::Arguable
|
6
|
-
|
7
|
-
describe '#with' do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@eleven = Class.new do
|
11
|
-
def add other_number
|
12
|
-
11 + other_number
|
13
|
-
end
|
14
|
-
|
15
|
-
def add_many one_number, another_number, *many_numbers
|
16
|
-
11 + one_number + another_number + many_numbers.reduce(0, :+)
|
17
|
-
end
|
18
|
-
|
19
|
-
def do_whatever
|
20
|
-
yield 11
|
21
|
-
end
|
22
|
-
end.new
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'returns a function that calls the method named <symbol> while also passing the arguments supplied' do
|
26
|
-
assert_equal :add.with(9).call(@eleven), 20
|
27
|
-
assert_equal :add_many.with(3, 6, 10, 20, 50).call(@eleven), 100
|
28
|
-
end
|
29
|
-
|
30
|
-
describe 'the returned function' do
|
31
|
-
|
32
|
-
it "it preserves Symbol's `to_proc` behavior of passing extra arguments, if supplied" do
|
33
|
-
assert_equal :add.to_proc.call(@eleven, 4), 15
|
34
|
-
assert_equal :add.with().call(@eleven, 4), 15
|
35
|
-
|
36
|
-
assert_equal :add_many.with(10, 20).call(@eleven, 4, 5), 50
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'passes along the block supplied to `with`, if any' do
|
40
|
-
result = nil
|
41
|
-
set_result = -> i { result = i }
|
42
|
-
|
43
|
-
:do_whatever.with(&set_result).call(@eleven)
|
44
|
-
|
45
|
-
assert_equal result, 11
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
@@ -1,131 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'augmented/symbols/comparing'
|
3
|
-
|
4
|
-
describe Augmented::Symbols::Comparing do
|
5
|
-
using Augmented::Symbols::Comparing
|
6
|
-
|
7
|
-
let(:dummy) { Struct.new :lorem_ipsum }
|
8
|
-
|
9
|
-
let(:thing_A) { dummy.new 123 }
|
10
|
-
let(:thing_B) { dummy.new 123 }
|
11
|
-
let(:thing_C) { dummy.new 987 }
|
12
|
-
let(:thing_D) { dummy.new 0 }
|
13
|
-
|
14
|
-
describe '#eq' do
|
15
|
-
|
16
|
-
it 'returns a function that sends <symbol> to two objects and compares the results with `==`' do
|
17
|
-
comparator = :lorem_ipsum.eq
|
18
|
-
|
19
|
-
assert_equal comparator.call(thing_A, thing_B), true
|
20
|
-
assert_equal comparator.call(thing_A, thing_C), false
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'if you give it a value, it returns a function that sends <symbol> to an object and compares the result to a given value using `==`' do
|
24
|
-
comparator = :lorem_ipsum.eq(123)
|
25
|
-
|
26
|
-
assert_equal comparator.call(thing_A), true
|
27
|
-
assert_equal comparator.call(thing_C), false
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
describe '#neq' do
|
33
|
-
|
34
|
-
it 'returns a function that sends <symbol> to two objects and compares the results with `!=`' do
|
35
|
-
comparator = :lorem_ipsum.neq
|
36
|
-
|
37
|
-
assert_equal comparator.call(thing_A, thing_B), false
|
38
|
-
assert_equal comparator.call(thing_A, thing_C), true
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'if you give it a value, it returns a function that sends <symbol> to an object and compares the result to a given value using `!=`' do
|
42
|
-
comparator = :lorem_ipsum.neq(123)
|
43
|
-
|
44
|
-
assert_equal comparator.call(thing_A), false
|
45
|
-
assert_equal comparator.call(thing_C), true
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
describe '#lt' do
|
51
|
-
|
52
|
-
it 'returns a function that sends <symbol> to two objects and compares the results with `<`' do
|
53
|
-
comparator = :lorem_ipsum.lt
|
54
|
-
|
55
|
-
assert_equal comparator.call(thing_A, thing_B), false
|
56
|
-
assert_equal comparator.call(thing_A, thing_C), true
|
57
|
-
assert_equal comparator.call(thing_C, thing_B), false
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'if you give it a value, it returns a function that sends <symbol> to an object and compares the result to a given value using `<`' do
|
61
|
-
comparator = :lorem_ipsum.lt(123)
|
62
|
-
|
63
|
-
assert_equal comparator.call(thing_A), false
|
64
|
-
assert_equal comparator.call(thing_C), false
|
65
|
-
assert_equal comparator.call(thing_D), true
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
describe '#lte' do
|
71
|
-
|
72
|
-
it 'returns a function that sends <symbol> to two objects and compares the results with `<=`' do
|
73
|
-
comparator = :lorem_ipsum.lte
|
74
|
-
|
75
|
-
assert_equal comparator.call(thing_A, thing_B), true
|
76
|
-
assert_equal comparator.call(thing_A, thing_C), true
|
77
|
-
assert_equal comparator.call(thing_C, thing_B), false
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'if you give it a value, it returns a function that sends <symbol> to an object and compares the result to a given value using `<=`' do
|
81
|
-
comparator = :lorem_ipsum.lte(123)
|
82
|
-
|
83
|
-
assert_equal comparator.call(thing_A), true
|
84
|
-
assert_equal comparator.call(thing_C), false
|
85
|
-
assert_equal comparator.call(thing_D), true
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
describe '#gt' do
|
91
|
-
|
92
|
-
it 'returns a function that sends <symbol> to two objects and compares the results with `>`' do
|
93
|
-
comparator = :lorem_ipsum.gt
|
94
|
-
|
95
|
-
assert_equal comparator.call(thing_A, thing_B), false
|
96
|
-
assert_equal comparator.call(thing_A, thing_C), false
|
97
|
-
assert_equal comparator.call(thing_C, thing_B), true
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'if you give it a value, it returns a function that sends <symbol> to an object and compares the result to a given value using `>`' do
|
101
|
-
comparator = :lorem_ipsum.gt(123)
|
102
|
-
|
103
|
-
assert_equal comparator.call(thing_A), false
|
104
|
-
assert_equal comparator.call(thing_C), true
|
105
|
-
assert_equal comparator.call(thing_D), false
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
describe '#gte' do
|
111
|
-
|
112
|
-
it 'returns a function that sends <symbol> to two objects and compares the results with `>=`' do
|
113
|
-
comparator = :lorem_ipsum.gte
|
114
|
-
|
115
|
-
assert_equal comparator.call(thing_A, thing_B), true
|
116
|
-
assert_equal comparator.call(thing_A, thing_C), false
|
117
|
-
assert_equal comparator.call(thing_C, thing_B), true
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'if you give it a value, it returns a function that sends <symbol> to an object and compares the result to a given value using `>=`' do
|
121
|
-
comparator = :lorem_ipsum.gte(123)
|
122
|
-
|
123
|
-
assert_equal comparator.call(thing_A), true
|
124
|
-
assert_equal comparator.call(thing_C), true
|
125
|
-
assert_equal comparator.call(thing_D), false
|
126
|
-
end
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
|
131
|
-
end
|