augmented 0.2.2 → 0.2.7

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.
@@ -1,29 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/modules/refined'
3
-
4
- describe Augmented::Modules::Refined do
5
- using Augmented::Modules::Refined
6
-
7
- describe '#refined' do
8
-
9
- before do
10
- class TesterClass
11
- using refined String,
12
- as_phrase: -> { self.capitalize.gsub(/\.?\z/, '.') },
13
- fill: -> filler { (filler * self.length)[0..length] }
14
-
15
- def do_test
16
- [
17
- 'hello world'.as_phrase == 'Hello world.',
18
- 'hello world'.fill('!') == '!!!!!!!!!!!',
19
- ]
20
- end
21
- end
22
- end
23
-
24
- it 'creates a refinement module on the fly for the given class, with the procs supplied' do
25
- assert_equal TesterClass.new.do_test, [true, true]
26
- end
27
-
28
- end
29
- end
@@ -1,69 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/objects/iffy'
3
-
4
- describe Augmented::Objects::Iffy do
5
- using Augmented::Objects::Iffy
6
-
7
- describe '#if' do
8
-
9
- it 'returns the object if the condition evaluates to truish' do
10
- subject = 'abc'
11
- condition = -> subj { subj.length == 3 }
12
-
13
- assert_same subject.if(true), subject
14
- assert_same subject.if(Object.new), subject
15
- assert_same subject.if(&condition), subject
16
- end
17
-
18
- it 'returns nil if the condition evaluates to falsy' do
19
- subject = 'abc'
20
- condition = -> subj { subj.length == 0 }
21
-
22
- assert_nil subject.if(false)
23
- assert_nil subject.if(nil)
24
- assert_nil subject.if(&condition)
25
- end
26
-
27
- end
28
-
29
- describe '#unless' do
30
-
31
- it 'returns the object if the condition evaluates to falsy' do
32
- subject = 'abc'
33
- condition = -> subj { subj.length == 0 }
34
-
35
- assert_same subject.unless(false), subject
36
- assert_same subject.unless(nil), subject
37
- assert_same subject.unless(&condition), subject
38
- end
39
-
40
- it 'returns nil if the condition evaluates to truish' do
41
- subject = 'abc'
42
- condition = -> subj { subj.length == 3 }
43
-
44
- assert_nil subject.unless(true)
45
- assert_nil subject.unless(Object.new)
46
- assert_nil subject.unless(&condition)
47
- end
48
-
49
- end
50
-
51
- describe '#else' do
52
-
53
- it 'returns the alternative if the object is falsy' do
54
- alternative = Object.new
55
-
56
- assert_same false.else(alternative), alternative
57
- assert_same nil.else(alternative), alternative
58
- end
59
-
60
- it 'returns the object if the object is truish' do
61
- subject = Object.new
62
-
63
- assert_same true.else(123), true
64
- assert_same subject.else(123), subject
65
- end
66
-
67
- end
68
-
69
- end
@@ -1,39 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/objects/pickable'
3
- require 'ostruct'
4
-
5
- describe Augmented::Objects::Pickable do
6
- using Augmented::Objects::Pickable
7
-
8
- describe '#pick' do
9
-
10
- it 'returns a hash with the results of invoking the list of picks in the target' do
11
- target = OpenStruct.new aaa: 111, bbb: 222, ccc: 333
12
-
13
- assert_equal target.pick(:aaa, :ccc), { aaa: 111, ccc: 333 }
14
- end
15
-
16
- it 'returns the result of invoking `pick` on every element of an enumerable target' do
17
- target = [ OpenStruct.new(val: 11), OpenStruct.new(val: 22), OpenStruct.new(val: 33) ]
18
-
19
- assert_equal target.pick(:val), [{val: 11}, {val: 22}, {val: 33}]
20
- end
21
-
22
- it 'applies picks recursively when provided with hashes' do
23
- target = OpenStruct.new(aa: (OpenStruct.new bb: (OpenStruct.new cc: 33)))
24
-
25
- assert_equal target.pick(aa: { bb: :cc }), { aa: { bb: { cc: 33 } } }
26
-
27
- target = OpenStruct.new(dd: OpenStruct.new(ee: 55), ff: OpenStruct.new(gg: 77))
28
-
29
- assert_equal target.pick(dd: :ee, ff: :gg), { dd: { ee: 55 }, ff: { gg: 77 } }
30
- end
31
-
32
- it 'allows you to specify pick lists with arrays when picking recursively' do
33
- target = OpenStruct.new aa: (OpenStruct.new bb: 22, cc: (OpenStruct.new dd: 44, ee: 55))
34
-
35
- assert_equal target.pick(aa: [:bb, cc: [:dd, :ee]]), { aa: { bb: 22, cc: { dd: 44, ee: 55 } } }
36
- end
37
-
38
- end
39
- end
@@ -1,25 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/objects/tackable'
3
-
4
- describe Augmented::Objects::Tackable do
5
- using Augmented::Objects::Tackable
6
-
7
- describe '#tack' do
8
-
9
- it 'attaches new methods to an object' do
10
- obj = Object.new
11
-
12
- obj.tack lorem: 123, ipsum: -> { self }
13
-
14
- assert_equal obj.lorem, 123
15
- assert_equal obj.ipsum.object_id, obj.object_id
16
- end
17
-
18
- it 'returns self' do
19
- obj = Object.new
20
- assert_equal obj.tack.object_id, obj.object_id
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,141 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/objects/tappable'
3
-
4
- describe Augmented::Objects::Tappable do
5
- using Augmented::Objects::Tappable
6
-
7
- describe '#tap_if' do
8
-
9
- it 'executes block if condition is truish' do
10
- subject = 'abc'
11
- test = nil
12
-
13
- subject.tap_if(true) { |subj| test = subj.upcase }
14
-
15
- assert_equal test, 'ABC'
16
-
17
- subject.tap_if(Object.new) { |subj| test = subj.reverse }
18
-
19
- assert_equal test, 'cba'
20
- end
21
-
22
- it 'does not execute block if condition is falsy' do
23
- subject = 'abc'
24
- test = nil
25
-
26
- subject.tap_if(false) { |subj| test = subj.upcase }
27
-
28
- assert_nil test
29
-
30
- subject.tap_if(nil) { |subj| test = subj.upcase }
31
-
32
- assert_nil test
33
- end
34
-
35
- it 'executes block if condition evaluates to truish' do
36
- subject = 'abc'
37
- test = nil
38
- condition_1 = -> subj { subj.length == 3 }
39
- condition_2 = -> subj { subj.length }
40
-
41
- subject.tap_if(condition_1) { |subj| test = subj.upcase }
42
-
43
- assert_equal test, 'ABC'
44
-
45
- subject.tap_if(condition_2) { |subj| test = subj.reverse }
46
-
47
- assert_equal test, 'cba'
48
- end
49
-
50
- it 'does not execute block if condition evaluates to falsy' do
51
- subject = 'abc'
52
- test = nil
53
- condition_1 = -> subj { subj.length == 0 }
54
- condition_2 = -> subj { nil }
55
-
56
- subject.tap_if(condition_1) { |subj| test = subj.upcase }
57
-
58
- assert_nil test
59
-
60
- subject.tap_if(condition_2) { |subj| test = subj.upcase }
61
-
62
- assert_nil test
63
- end
64
-
65
- it 'always returns the object' do
66
- subject = 'abc'
67
-
68
- assert_same subject.tap_if(true){}, subject
69
- assert_same subject.tap_if(false){}, subject
70
- end
71
-
72
- end
73
-
74
- describe '#tap_unless' do
75
-
76
- it 'executes block if condition is falsy' do
77
- subject = 'abc'
78
- test = nil
79
-
80
- subject.tap_unless(false) { |subj| test = subj.upcase }
81
-
82
- assert_equal test, 'ABC'
83
-
84
- subject.tap_unless(nil) { |subj| test = subj.reverse }
85
-
86
- assert_equal test, 'cba'
87
- end
88
-
89
- it 'does not execute block if condition is truish' do
90
- subject = 'abc'
91
- test = nil
92
-
93
- subject.tap_unless(true) { |subj| test = subj.upcase }
94
-
95
- assert_nil test
96
-
97
- subject.tap_unless(Object.new) { |subj| test = subj.upcase }
98
-
99
- assert_nil test
100
- end
101
-
102
- it 'executes block if condition evaluates to falsy' do
103
- subject = 'abc'
104
- test = nil
105
- condition_1 = -> subj { subj.length == 0 }
106
- condition_2 = -> subj { nil }
107
-
108
- subject.tap_unless(condition_1) { |subj| test = subj.upcase }
109
-
110
- assert_equal test, 'ABC'
111
-
112
- subject.tap_unless(condition_2) { |subj| test = subj.reverse }
113
-
114
- assert_equal test, 'cba'
115
- end
116
-
117
- it 'does not execute block if condition evaluates to truish' do
118
- subject = 'abc'
119
- test = nil
120
- condition_1 = -> subj { subj.length == 3 }
121
- condition_2 = -> subj { subj.length }
122
-
123
- subject.tap_unless(condition_1) { |subj| test = subj.upcase }
124
-
125
- assert_nil test
126
-
127
- subject.tap_unless(condition_2) { |subj| test = subj.upcase }
128
-
129
- assert_nil test
130
- end
131
-
132
- it 'always returns the object' do
133
- subject = 'abc'
134
-
135
- assert_same subject.tap_unless(true){}, subject
136
- assert_same subject.tap_unless(false){}, subject
137
- end
138
-
139
- end
140
-
141
- end
@@ -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