limit_detectors 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/limit_detectors/version.rb +1 -1
- data/lib/limit_detectors.rb +2 -2
- data/spec/limit_detectors_spec.rb +53 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31a734b1d732fbc68adedf43ac4cb5ea4b19d7e9
|
4
|
+
data.tar.gz: 24a9827e6429aaa1d60ae8e507b481974aedf25e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fafa8b5a6d6886587fc4f329893d5eebb75552691d138e8ea0a5509daac3e6643cb959020a24797a662920b20b926e1cf930cf9a5ac3622dbdb4262cabb229a
|
7
|
+
data.tar.gz: 5f05f660b06669f3ae01eedb2a6a53f0780a3f1728d43a95ab6d77ed084b85440d49906b8cf21244bddea49eca0fced0c8ab3567efa0b41ea6bf9da30ebc40f1
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
In your code you can `require 'limit_detectors'` then define you classes (or use built-in classes like Array, Hash
|
29
29
|
or other enumerable objects), extend these objects with LimitDetectors (or include the module in your class) and
|
30
|
-
then call `at_most
|
30
|
+
then call `at_most?` (or àt_least?') on your object.
|
31
31
|
|
32
32
|
For example:
|
33
33
|
|
@@ -36,9 +36,9 @@ For example:
|
|
36
36
|
=> [1, 2, 3, 4, 5]
|
37
37
|
[2] pry(main)> a.extend LimitDetectors
|
38
38
|
=> [1, 2, 3, 4, 5]
|
39
|
-
[3] pry(main)> a.at_most(4){|e| e.odd?}
|
39
|
+
[3] pry(main)> a.at_most?(4){|e| e.odd?}
|
40
40
|
=> true # There are indeed no more than 4 odd numbers in the array
|
41
|
-
[4] pry(main)> a.at_most(1){|e| e.even?}
|
41
|
+
[4] pry(main)> a.at_most?(1){|e| e.even?}
|
42
42
|
=> false # In fact there are two even numbers in the array
|
43
43
|
|
44
44
|
## Compatibility
|
@@ -54,7 +54,7 @@ This gem is tested with these (MRI) Ruby versions:
|
|
54
54
|
* 2.1,
|
55
55
|
* 2.1.1,
|
56
56
|
|
57
|
-
as well as current version of
|
57
|
+
as well as current version of JRuby and Rubinius.
|
58
58
|
|
59
59
|
## Contributing
|
60
60
|
|
data/lib/limit_detectors.rb
CHANGED
@@ -2,13 +2,13 @@ require 'limit_detectors/version'
|
|
2
2
|
|
3
3
|
module LimitDetectors
|
4
4
|
|
5
|
-
# Deprecated, use
|
5
|
+
# Deprecated, use at_most? instead
|
6
6
|
def at_most(limit, &block)
|
7
7
|
Kernel.warn "'at_most' is deprecated, use 'at_most?' instead"
|
8
8
|
at_most? limit, &block
|
9
9
|
end
|
10
10
|
|
11
|
-
# Deprecated, use
|
11
|
+
# Deprecated, use at_most? instead
|
12
12
|
def at_least(limit, &block)
|
13
13
|
Kernel.warn "'at_least' is deprecated, use 'at_least?' instead"
|
14
14
|
at_least? limit, &block
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'set'
|
2
3
|
|
3
4
|
Array.send :include, LimitDetectors
|
4
5
|
|
@@ -7,6 +8,9 @@ describe '#at_most' do
|
|
7
8
|
it 'is true for an empty Array' do
|
8
9
|
expect(Kernel).to_not receive(:warn)
|
9
10
|
expect([].at_most?(5){ true }).to be_true
|
11
|
+
expect([].at_most?(0){ true }).to be_true
|
12
|
+
expect([].at_most?(1){ true }).to be_true
|
13
|
+
expect([].at_most?(5){ :foo }).to be_true
|
10
14
|
end
|
11
15
|
|
12
16
|
it 'is true if the criterion is met once' do
|
@@ -37,15 +41,15 @@ describe '#at_most' do
|
|
37
41
|
end
|
38
42
|
|
39
43
|
describe '#at_least' do
|
40
|
-
|
44
|
+
|
45
|
+
it 'is false for an empty Array, if at least one is expected' do
|
41
46
|
expect(Kernel).to_not receive(:warn)
|
42
47
|
expect([].at_least?(1){ true }).to be_false
|
43
|
-
expect([].at_least?(1){ false }).to be_false
|
44
48
|
end
|
45
49
|
|
46
50
|
it 'is true if the expected number is 0 and Array is empty' do
|
47
51
|
expect([].at_least?(0){ true }).to be_true
|
48
|
-
expect(
|
52
|
+
expect({}.at_least?(0){ false }).to be_true
|
49
53
|
end
|
50
54
|
|
51
55
|
it 'is false if the container ist smaller than the expected number' do
|
@@ -57,6 +61,18 @@ describe '#at_least' do
|
|
57
61
|
expect(["it's there"].at_least?(1){ |el| el == "it's there"}).to be_true
|
58
62
|
end
|
59
63
|
|
64
|
+
it 'is false for an empty Array if you expect at leat 1' do
|
65
|
+
expect([].at_least?(1){ true }).to be_false
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'is true for an empty Array if you expect at leat 0' do
|
69
|
+
expect([].at_least?(0){ }).to be_true
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'is true if the criterion is met once' do
|
73
|
+
expect(["it's there"].at_least?(1){ |el| el == "it's there"}).to be_true
|
74
|
+
end
|
75
|
+
|
60
76
|
it 'is true if all elements meet the criterion and the size is the given minimum number' do
|
61
77
|
expect([1,1,1].at_least?(3){|e| e == 1}).to be_true
|
62
78
|
end
|
@@ -73,6 +89,29 @@ describe '#at_least' do
|
|
73
89
|
|
74
90
|
end
|
75
91
|
|
92
|
+
describe '#ocurrences_of' do
|
93
|
+
context 'collection with content' do
|
94
|
+
Set.send :include, LimitDetectors
|
95
|
+
subject{ Set.new( [1, 2, 3, 4, 5, 6, 7]) }
|
96
|
+
|
97
|
+
it('counts 3 even numbers') { expect( subject.ocurrences_of &:even?).to be 3 }
|
98
|
+
it('counts 4 odd numbers') { expect( subject.ocurrences_of &:odd?).to be 4 }
|
99
|
+
it('counts no number < 0') { expect( subject.ocurrences_of{ |e| e < 0}).to be 0 }
|
100
|
+
it('counts 7 positive numbers') { expect( subject.ocurrences_of{ |e| e > 0}).to be 7 }
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'empty collection' do
|
104
|
+
it 'counts 0 for any empty collection' do
|
105
|
+
[[], Set.new, {}].each do | obj |
|
106
|
+
expect(obj.ocurrences_of {true}).to be(0), "Expected to count 0, for an empty #{obj.class}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
it('doen\'t return nil') { expect([1].ocurrences_of {}).not_to be_nil }
|
112
|
+
end
|
113
|
+
|
114
|
+
|
76
115
|
describe 'Using an object that doesn\'t respond to #inject will raise an exception' do
|
77
116
|
object = Object.new
|
78
117
|
object.extend LimitDetectors
|
@@ -82,13 +121,21 @@ describe 'Using an object that doesn\'t respond to #inject will raise an excepti
|
|
82
121
|
end
|
83
122
|
|
84
123
|
describe 'Give a warning, if non-predicate versions are used' do
|
85
|
-
it '
|
124
|
+
it 'yields a warning for old-style at_most' do
|
86
125
|
expect(Kernel).to receive(:warn).with(/'at_most'.+deprecated.+'at_most\?'/)
|
87
126
|
[1,2,4,8].at_most(2) {|e| e.even?}
|
88
127
|
end
|
89
128
|
|
90
|
-
it '
|
129
|
+
it 'yields a warning for old-style at_least' do
|
91
130
|
expect(Kernel).to receive(:warn).with(/'at_least'.+deprecated.+'at_least\?'/)
|
92
131
|
[1,2,4,8].at_least(2) {|e| e.even?}
|
93
132
|
end
|
94
|
-
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'When the provided block raises an exception' do
|
136
|
+
subject{ [1] }
|
137
|
+
it 'passes up the stack unchanged' do
|
138
|
+
expect{ subject.at_most?(1) { raise ArgumentError, 'BoomError' } }.to raise_error(ArgumentError, 'BoomError')
|
139
|
+
expect{ subject.ocurrences_of { raise ArgumentError, 'BoomError'} }.to raise_error(ArgumentError, 'BoomError')
|
140
|
+
end
|
141
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limit_detectors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephan Kämper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|